forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextArea.tsx
More file actions
123 lines (112 loc) · 3.75 KB
/
TextArea.tsx
File metadata and controls
123 lines (112 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { hooks } from 'botframework-webchat-api';
import cx from 'classnames';
import React, {
forwardRef,
Fragment,
useCallback,
useRef,
type FormEventHandler,
type KeyboardEventHandler
} from 'react';
import { useStyles } from '../../styles';
import styles from './TextArea.module.css';
const { useUIState } = hooks;
const TextArea = forwardRef<
HTMLTextAreaElement,
Readonly<{
'aria-label'?: string | undefined;
className?: string | undefined;
'data-testid'?: string | undefined;
/**
* `true`, if the text area should be hidden but stay in the DOM, otherwise, `false`.
*
* Keeping the element in the DOM while making it invisible to users and PWDs is useful in these scenarios:
*
* - When the DTMF keypad is going away, we need to send focus to the text area before we unmount DTMF keypad,
* This ensures the flow of focus did not sent to document body
*/
hidden?: boolean | undefined;
onInput?: FormEventHandler<HTMLTextAreaElement> | undefined;
placeholder?: string | undefined;
startRows?: number | undefined;
value?: string | undefined;
}>
>((props, ref) => {
const [uiState] = useUIState();
const classNames = useStyles(styles);
const isInCompositionRef = useRef<boolean>(false);
const disabled = uiState === 'disabled';
const handleCompositionEnd = useCallback(() => {
isInCompositionRef.current = false;
}, [isInCompositionRef]);
const handleCompositionStart = useCallback(() => {
isInCompositionRef.current = true;
}, [isInCompositionRef]);
const handleKeyDown = useCallback<KeyboardEventHandler<HTMLTextAreaElement>>(event => {
// Shift+Enter adds a new line
// Enter requests related form submission
if (!event.shiftKey && event.key === 'Enter' && !isInCompositionRef.current) {
event.preventDefault();
if ('form' in event.target && event.target.form instanceof HTMLFormElement) {
event.target?.form?.requestSubmit();
}
}
}, []);
return (
<div
className={cx(
classNames['sendbox__text-area'],
{ [classNames['sendbox__text-area--hidden']]: props.hidden },
props.className
)}
role={props.hidden ? 'hidden' : undefined}
>
{uiState === 'blueprint' ? (
<div
className={cx(
classNames['sendbox__text-area-doppelganger'],
classNames['sendbox__text-area-input--scroll'],
classNames['sendbox__text-area-shared']
)}
>
{' '}
</div>
) : (
<Fragment>
<div
className={cx(
classNames['sendbox__text-area-doppelganger'],
classNames['sendbox__text-area-input--scroll'],
classNames['sendbox__text-area-shared']
)}
>
{props.value || props.placeholder}{' '}
</div>
<textarea
aria-disabled={disabled}
aria-label={props['aria-label']}
className={cx(
classNames['sendbox__text-area-input'],
classNames['sendbox__text-area-input--scroll'],
classNames['sendbox__text-area-shared']
)}
data-testid={props['data-testid']}
onCompositionEnd={handleCompositionEnd}
onCompositionStart={handleCompositionStart}
onInput={props.onInput}
onKeyDown={handleKeyDown}
placeholder={props.placeholder}
readOnly={disabled}
ref={ref}
rows={props.startRows ?? 1}
// eslint-disable-next-line no-magic-numbers
tabIndex={props.hidden ? -1 : undefined}
value={props.value}
/>
</Fragment>
)}
</div>
);
});
TextArea.displayName = 'TextArea';
export default TextArea;