Skip to content
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
16 changes: 15 additions & 1 deletion src/utils/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ function cloneEvent<
return newEvent;
}

function cloneEventWithTarget<
EventType extends React.SyntheticEvent<any, any>,
Element extends HTMLInputElement | HTMLTextAreaElement,
>(event: EventType, target: Element): EventType {
return Object.create(event, {
target: { value: target },
currentTarget: { value: target },
});
}

export function resolveOnChange<
E extends HTMLInputElement | HTMLTextAreaElement,
>(
Expand Down Expand Up @@ -84,7 +94,11 @@ export function resolveOnChange<
// https://github.com/ant-design/ant-design/issues/45737
// https://github.com/ant-design/ant-design/issues/46598
if (target.type !== 'file' && targetValue !== undefined) {
event = cloneEvent(e, target, targetValue);
if (target.value !== targetValue) {
event = cloneEvent(e, target, targetValue);
} else {
event = cloneEventWithTarget(e, target);
}
onChange(event as React.ChangeEvent<E>);
return;
Comment thread
afc163 marked this conversation as resolved.
}
Expand Down
13 changes: 13 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,19 @@ describe('Input ref', () => {
expect(spySetSelectionRange).toHaveBeenCalledWith(1, 2);
});

it('onChange target should be the mounted input when value is unchanged by formatter', () => {
const onChange = jest.fn();
const { container } = render(<Input onChange={onChange} />);
const inputEl = container.querySelector('input')!;

fireEvent.change(inputEl, { target: { value: 'test' } });

const event = onChange.mock.calls[0][0];
expect(event.target).toBe(inputEl);
expect(event.currentTarget).toBe(inputEl);
expect(document.contains(event.target)).toBe(true);
});

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