Skip to content

Commit

Permalink
feat: input supports onCompositionEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
yinkaihui committed Mar 15, 2024
1 parent 8631321 commit b9c3c64
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
31 changes: 31 additions & 0 deletions components/Input/__test__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,37 @@ describe('Test Search', () => {
expect(onSearch.mock.calls).toHaveLength(2);
expect(onPressEnter.mock.calls).toHaveLength(1);
});

it('composition event handlers', () => {
const rest = {
onCompositionStart: jest.fn(),
onCompositionUpdate: jest.fn(),
onCompositionEnd: jest.fn(),
};

const { getByTestId } = render(
<Input
data-testid="input"
onCompositionStart={rest.onCompositionStart}
onCompositionUpdate={rest.onCompositionUpdate}
onCompositionEnd={rest.onCompositionEnd}
/>
);

const input = getByTestId('input');

// Trigger composition start event
fireEvent.compositionStart(input);
expect(rest.onCompositionStart).toHaveBeenCalledTimes(1);

// Trigger composition update event
fireEvent.compositionUpdate(input);
expect(rest.onCompositionUpdate).toHaveBeenCalledTimes(1);

// Trigger composition end event
fireEvent.compositionEnd(input);
expect(rest.onCompositionEnd).toHaveBeenCalledTimes(1);
});
});

describe('Test Textarea', () => {
Expand Down
15 changes: 12 additions & 3 deletions components/Input/input-element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,18 @@ const InputComponent = React.forwardRef<RefInputType, InputComponentProps>(
className: inputClassNames,
onKeyDown: keyDownHandler,
onChange: valueChangeHandler,
onCompositionStart: compositionHandler,
onCompositionUpdate: compositionHandler,
onCompositionEnd: compositionHandler,
onCompositionStart: (e) => {
rest.onCompositionStart?.(e);
compositionHandler(e);
},
onCompositionUpdate: (e) => {
rest.onCompositionUpdate?.(e);
compositionHandler(e);
},
onCompositionEnd: (e) => {
rest.onCompositionEnd?.(e);
compositionHandler(e);
},
onBlur: (e) => {
props.onBlur?.(e);
const normalize = normalizeHandler('onBlur');
Expand Down

0 comments on commit b9c3c64

Please sign in to comment.