Skip to content
Open
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
20 changes: 20 additions & 0 deletions packages/x/components/sender/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ describe('Sender Component', () => {
});
expect(onSubmit).toHaveBeenCalledWith('bamboo', [], undefined);
});

it('should not submit when Enter is pressed within 100ms after composition end', () => {
const onSubmit = jest.fn();
const { container } = render(<Sender value="bamboo" onSubmit={onSubmit} />);
const textarea = container.querySelector('textarea')!;

act(() => {
fireEvent.compositionStart(textarea);
});

act(() => {
fireEvent.compositionEnd(textarea);
});

act(() => {
fireEvent.keyDown(textarea, { key: 'Enter', shiftKey: false });
});

expect(onSubmit).not.toHaveBeenCalled();
});
});

it('Sender.Header not can be focus', () => {
Expand Down
14 changes: 12 additions & 2 deletions packages/x/components/sender/components/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,30 @@ const TextArea = React.forwardRef<TextAreaRef>((_, ref) => {

// ============================ Submit ============================
const isCompositionRef = React.useRef(false);
const lastCompositionEndTick = React.useRef(0);

const onInternalCompositionStart = () => {
isCompositionRef.current = true;
};

const onInternalCompositionEnd = () => {
isCompositionRef.current = false;
lastCompositionEndTick.current = Date.now();
};

const onInternalKeyDown: React.KeyboardEventHandler<HTMLTextAreaElement> = (e) => {
const eventRes = onKeyDown?.(e);
const { key, shiftKey, ctrlKey, altKey, metaKey } = e;

if (isCompositionRef.current || key !== 'Enter' || eventRes === false) {
const COMPOSITION_END_DEBOUNCE_MS = 100;
const isJustFinishedComposition =
Date.now() - lastCompositionEndTick.current < COMPOSITION_END_DEBOUNCE_MS;

if (
isCompositionRef.current ||
isJustFinishedComposition ||
key !== 'Enter' ||
eventRes === false
) {
return;
}

Expand Down
Loading