Skip to content

Commit 160a99c

Browse files
authored
fix: email type (#67)
1 parent b85cb22 commit 160a99c

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

src/utils/commonUtils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ function cloneEvent<
2727

2828
// Fill data
2929
currentTarget.value = value;
30-
currentTarget.selectionStart = target.selectionStart;
31-
currentTarget.selectionEnd = target.selectionEnd;
30+
31+
// Fill selection. Some type like `email` not support selection
32+
// https://github.com/ant-design/ant-design/issues/47833
33+
if (
34+
typeof target.selectionStart === 'number' &&
35+
typeof target.selectionEnd === 'number'
36+
) {
37+
currentTarget.selectionStart = target.selectionStart;
38+
currentTarget.selectionEnd = target.selectionEnd;
39+
}
3240

3341
return newEvent;
3442
}

tests/index.test.tsx

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -332,34 +332,49 @@ describe('Input ref', () => {
332332
inputSpy.mockRestore();
333333
});
334334

335-
it('setSelectionRange should work', () => {
336-
const setSelectionRange = jest.fn();
337-
const inputSpy = spyElementPrototypes(HTMLInputElement, {
338-
setSelectionRange,
335+
describe('selection', () => {
336+
it('setSelectionRange should work', () => {
337+
const setSelectionRange = jest.fn();
338+
const inputSpy = spyElementPrototypes(HTMLInputElement, {
339+
setSelectionRange,
340+
});
341+
const ref = React.createRef<InputRef>();
342+
render(<Input ref={ref} defaultValue="light" prefixCls="rc-input" />);
343+
ref.current?.setSelectionRange(0, 0);
344+
expect(setSelectionRange).toHaveBeenCalledWith(
345+
expect.anything(),
346+
0,
347+
0,
348+
undefined,
349+
);
350+
inputSpy.mockRestore();
339351
});
340-
const ref = React.createRef<InputRef>();
341-
render(<Input ref={ref} defaultValue="light" prefixCls="rc-input" />);
342-
ref.current?.setSelectionRange(0, 0);
343-
expect(setSelectionRange).toHaveBeenCalledWith(
344-
expect.anything(),
345-
0,
346-
0,
347-
undefined,
348-
);
349-
inputSpy.mockRestore();
350-
});
351352

352-
it('selectionXXX should pass', () => {
353-
const onChange = jest.fn();
354-
const { container } = render(<Input onChange={onChange} />);
353+
it('selectionXXX should pass', () => {
354+
const onChange = jest.fn();
355+
const { container } = render(<Input onChange={onChange} />);
355356

356-
const inputEl = container.querySelector('input')!;
357-
fireEvent.change(inputEl, { target: { value: 'test' } });
357+
const inputEl = container.querySelector('input')!;
358+
fireEvent.change(inputEl, { target: { value: 'test' } });
358359

359-
expect(onChange).toHaveBeenCalled();
360-
const event = onChange.mock.calls[0][0];
361-
expect(event.target.selectionStart).toBe(4);
362-
expect(event.target.selectionEnd).toBe(4);
360+
expect(onChange).toHaveBeenCalled();
361+
const event = onChange.mock.calls[0][0];
362+
expect(event.target.selectionStart).toBe(4);
363+
expect(event.target.selectionEnd).toBe(4);
364+
});
365+
366+
it('email type not support selection', () => {
367+
const onChange = jest.fn();
368+
const { container } = render(<Input type="email" onChange={onChange} />);
369+
370+
fireEvent.change(container.querySelector('input')!, {
371+
target: { value: 'test' },
372+
});
373+
expect(onChange).toHaveBeenCalled();
374+
const event = onChange.mock.calls[0][0];
375+
expect(event.target.selectionStart).toBeNull();
376+
expect(event.target.selectionEnd).toBeNull();
377+
});
363378
});
364379

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

0 commit comments

Comments
 (0)