Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Proxy of setSelectionRange #70

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/utils/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ function cloneEvent<
currentTarget.selectionEnd = target.selectionEnd;
}

currentTarget.setSelectionRange = (...args) => {
target.setSelectionRange(...args);
};

return newEvent;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ describe('Input ref', () => {
it('selectionXXX should pass', () => {
const onChange = jest.fn();
const { container } = render(<Input onChange={onChange} />);
const spySetSelectionRange = jest.spyOn(
container.querySelector('input')!,
'setSelectionRange',
);

const inputEl = container.querySelector('input')!;
fireEvent.change(inputEl, { target: { value: 'test' } });
Expand All @@ -361,6 +365,10 @@ describe('Input ref', () => {
const event = onChange.mock.calls[0][0];
expect(event.target.selectionStart).toBe(4);
expect(event.target.selectionEnd).toBe(4);

// Call `setSelectionRange`
event.target.setSelectionRange(1, 2);
expect(spySetSelectionRange).toHaveBeenCalledWith(1, 2);
});

it('email type not support selection', () => {
Expand Down
Loading