Skip to content

Commit

Permalink
fix: Proxy of setSelectionRange (#70)
Browse files Browse the repository at this point in the history
* fix: rc-input change event

* test: add test case
  • Loading branch information
zombieJ authored May 22, 2024
1 parent fe30455 commit 490c37e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
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

0 comments on commit 490c37e

Please sign in to comment.