|
1 | 1 | import { hooks } from 'botframework-webchat-api'; |
2 | 2 | import cx from 'classnames'; |
3 | | -import React, { forwardRef, Fragment, useCallback, type FormEventHandler, type KeyboardEventHandler } from 'react'; |
| 3 | +import React, { |
| 4 | + forwardRef, |
| 5 | + Fragment, |
| 6 | + useCallback, |
| 7 | + useRef, |
| 8 | + type FormEventHandler, |
| 9 | + type KeyboardEventHandler |
| 10 | +} from 'react'; |
4 | 11 | import { useStyles } from '../../styles'; |
5 | 12 | import styles from './TextArea.module.css'; |
6 | 13 |
|
@@ -30,13 +37,22 @@ const TextArea = forwardRef< |
30 | 37 | >((props, ref) => { |
31 | 38 | const [uiState] = useUIState(); |
32 | 39 | const classNames = useStyles(styles); |
| 40 | + const isInCompositionRef = useRef<boolean>(false); |
33 | 41 |
|
34 | 42 | const disabled = uiState === 'disabled'; |
35 | 43 |
|
| 44 | + const handleCompositionEnd = useCallback(() => { |
| 45 | + isInCompositionRef.current = false; |
| 46 | + }, [isInCompositionRef]); |
| 47 | + |
| 48 | + const handleCompositionStart = useCallback(() => { |
| 49 | + isInCompositionRef.current = true; |
| 50 | + }, [isInCompositionRef]); |
| 51 | + |
36 | 52 | const handleKeyDown = useCallback<KeyboardEventHandler<HTMLTextAreaElement>>(event => { |
37 | 53 | // Shift+Enter adds a new line |
38 | 54 | // Enter requests related form submission |
39 | | - if (!event.shiftKey && event.key === 'Enter') { |
| 55 | + if (!event.shiftKey && event.key === 'Enter' && !isInCompositionRef.current) { |
40 | 56 | event.preventDefault(); |
41 | 57 |
|
42 | 58 | if ('form' in event.target && event.target.form instanceof HTMLFormElement) { |
@@ -84,6 +100,8 @@ const TextArea = forwardRef< |
84 | 100 | classNames['sendbox__text-area-shared'] |
85 | 101 | )} |
86 | 102 | data-testid={props['data-testid']} |
| 103 | + onCompositionEnd={handleCompositionEnd} |
| 104 | + onCompositionStart={handleCompositionStart} |
87 | 105 | onInput={props.onInput} |
88 | 106 | onKeyDown={handleKeyDown} |
89 | 107 | placeholder={props.placeholder} |
|
0 commit comments