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: email type should not fill selectionXXX #67

Merged
merged 1 commit into from
Mar 12, 2024
Merged
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
fix: email type
zombieJ committed Mar 12, 2024
commit 5dc2b9d525a36856621b469c87b14556733b80ac
12 changes: 10 additions & 2 deletions src/utils/commonUtils.ts
Original file line number Diff line number Diff line change
@@ -27,8 +27,16 @@ function cloneEvent<

// Fill data
currentTarget.value = value;
currentTarget.selectionStart = target.selectionStart;
currentTarget.selectionEnd = target.selectionEnd;

// Fill selection. Some type like `email` not support selection
// https://github.com/ant-design/ant-design/issues/47833
if (
typeof target.selectionStart === 'number' &&
typeof target.selectionEnd === 'number'
) {
currentTarget.selectionStart = target.selectionStart;
currentTarget.selectionEnd = target.selectionEnd;
}

return newEvent;
}
63 changes: 39 additions & 24 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -332,34 +332,49 @@ describe('Input ref', () => {
inputSpy.mockRestore();
});

it('setSelectionRange should work', () => {
const setSelectionRange = jest.fn();
const inputSpy = spyElementPrototypes(HTMLInputElement, {
setSelectionRange,
describe('selection', () => {
it('setSelectionRange should work', () => {
const setSelectionRange = jest.fn();
const inputSpy = spyElementPrototypes(HTMLInputElement, {
setSelectionRange,
});
const ref = React.createRef<InputRef>();
render(<Input ref={ref} defaultValue="light" prefixCls="rc-input" />);
ref.current?.setSelectionRange(0, 0);
expect(setSelectionRange).toHaveBeenCalledWith(
expect.anything(),
0,
0,
undefined,
);
inputSpy.mockRestore();
});
const ref = React.createRef<InputRef>();
render(<Input ref={ref} defaultValue="light" prefixCls="rc-input" />);
ref.current?.setSelectionRange(0, 0);
expect(setSelectionRange).toHaveBeenCalledWith(
expect.anything(),
0,
0,
undefined,
);
inputSpy.mockRestore();
});

it('selectionXXX should pass', () => {
const onChange = jest.fn();
const { container } = render(<Input onChange={onChange} />);
it('selectionXXX should pass', () => {
const onChange = jest.fn();
const { container } = render(<Input onChange={onChange} />);

const inputEl = container.querySelector('input')!;
fireEvent.change(inputEl, { target: { value: 'test' } });
const inputEl = container.querySelector('input')!;
fireEvent.change(inputEl, { target: { value: 'test' } });

expect(onChange).toHaveBeenCalled();
const event = onChange.mock.calls[0][0];
expect(event.target.selectionStart).toBe(4);
expect(event.target.selectionEnd).toBe(4);
expect(onChange).toHaveBeenCalled();
const event = onChange.mock.calls[0][0];
expect(event.target.selectionStart).toBe(4);
expect(event.target.selectionEnd).toBe(4);
});

it('email type not support selection', () => {
const onChange = jest.fn();
const { container } = render(<Input type="email" onChange={onChange} />);

fireEvent.change(container.querySelector('input')!, {
target: { value: 'test' },
});
expect(onChange).toHaveBeenCalled();
const event = onChange.mock.calls[0][0];
expect(event.target.selectionStart).toBeNull();
expect(event.target.selectionEnd).toBeNull();
});
});

it('input should work', () => {