-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommentTextArea.tsx
More file actions
230 lines (220 loc) · 8.25 KB
/
CommentTextArea.tsx
File metadata and controls
230 lines (220 loc) · 8.25 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/** @jsxImportSource preact */
import React from "preact/compat";
import { useEffect } from "preact/hooks";
import { useCommentTextArea } from "../../../hooks/useCommentTextArea";
import { collabStyles, flexAlignCenter } from "../../../collab.style";
import {
ICommentTextAreaProps,
IMentionList,
} from "../../../types/collab.types";
import classNames from "classnames";
import Tooltip from "../Tooltip/Tooltip";
const ErrorIndicator: React.FC<{ errorMessage: string }> = ({
errorMessage,
}) => (
<div
className={classNames(
"collab-thread-input-indicator--error",
collabStyles()["collab-thread-input-indicator--error"]
)}
>
{errorMessage}
</div>
);
const CharacterCounter: React.FC<{
currentLength: number;
maxLength: number;
}> = ({ currentLength, maxLength }) => (
<div
className={classNames(
"collab-thread-input-indicator--count",
collabStyles()["collab-thread-input-indicator--count"]
)}
>
{currentLength}/{maxLength}
</div>
);
const MentionSuggestionsList: React.FC<{
filteredUsers: IMentionList[];
selectedIndex: number;
cursorPosition: { top: number; showAbove: boolean };
inputRef: React.RefObject<HTMLTextAreaElement>;
listRef: React.RefObject<HTMLUListElement>;
itemRefs: React.MutableRefObject<(HTMLLIElement | null)[]>;
insertMention: (user: IMentionList) => void;
handleKeyDown: (event: KeyboardEvent) => void;
}> = ({
filteredUsers,
selectedIndex,
cursorPosition,
inputRef,
listRef,
itemRefs,
insertMention,
handleKeyDown,
}) => {
if (filteredUsers.length === 0) return null;
return (
<ul
className={classNames(
"collab-thread-body--input--textarea--suggestionsList",
collabStyles()[
"collab-thread-body--input--textarea--suggestionsList"
]
)}
style={{
...(cursorPosition.showAbove
? {
bottom: `${window.innerHeight - (inputRef.current?.getBoundingClientRect().top || 0) - cursorPosition.top}px`,
top: "auto",
}
: {
top: `${(inputRef.current?.getBoundingClientRect().top || 0) + cursorPosition.top}px`,
}),
}}
ref={listRef}
>
{filteredUsers.map((user, index) => (
<li
key={user.uid}
onClick={() => insertMention(user)}
className={classNames(
"collab-thread-body--input--textarea--suggestionsList--item",
collabStyles()[
"collab-thread-body--input--textarea--suggestionsList--item"
],
index === selectedIndex
? collabStyles()[
"collab-thread-body--input--textarea--suggestionsList--item-selected"
]
: ""
)}
ref={(el) => (itemRefs.current[index] = el)}
onKeyDown={(e) =>
e.key === "Enter"
? insertMention(user)
: handleKeyDown(e as KeyboardEvent)
}
tabIndex={-1}
aria-selected={index === selectedIndex}
>
{user.display === user.email ? (
user.display.length > 20 ? (
<Tooltip content={user.display || ""}>
{(user.display || "").substring(0, 18) + "..."}
</Tooltip>
) : (
user.display
)
) : (
<Tooltip
content={user.display + " - " + user.email || ""}
>
{user.display.length > 20
? (user.display || "").substring(0, 18) + "..."
: user.display}
</Tooltip>
)}
</li>
))}
</ul>
);
};
const CommentTextArea: React.FC<ICommentTextAreaProps> = React.memo(
({ userState, handleOnSaveRef, comment, onClose }) => {
const {
state,
error,
showSuggestions,
cursorPosition,
selectedIndex,
filteredUsers,
inputRef,
listRef,
itemRefs,
handleInputChange,
handleKeyDown,
handleSubmit,
insertMention,
maxMessageLength,
} = useCommentTextArea(userState, comment, onClose);
const onChangeHandler = (event: Event) =>
handleInputChange(event as any);
const onKeyDownHandler = (event: KeyboardEvent) =>
handleKeyDown(event as any);
useEffect(() => {
handleOnSaveRef.current = handleSubmit;
}, [handleSubmit, handleOnSaveRef]);
return (
<div
className={classNames(
"collab-thread-body--input--wrapper",
collabStyles()["collab-thread-body--input--wrapper"]
)}
>
<div
className={classNames(
"collab-thread-body--input",
collabStyles()["collab-thread-body--input"]
)}
>
<div
className={classNames(
"collab-thread-body--input--textarea--wrapper",
collabStyles()[
"collab-thread-body--input--textarea--wrapper"
]
)}
>
<textarea
name="collab-thread-body--input--textarea"
id="collab-thread-body--input--textarea"
rows={1}
className={classNames(
"collab-thread-body--input--textarea",
collabStyles()[
"collab-thread-body--input--textarea"
]
)}
value={state.message}
onChange={onChangeHandler}
onKeyDown={onKeyDownHandler}
maxLength={maxMessageLength}
placeholder="Enter a comment or tag others using “@”"
ref={inputRef}
/>
{showSuggestions && (
<MentionSuggestionsList
filteredUsers={filteredUsers}
selectedIndex={selectedIndex}
cursorPosition={cursorPosition}
inputRef={inputRef}
listRef={listRef}
itemRefs={itemRefs}
insertMention={insertMention}
handleKeyDown={handleKeyDown}
/>
)}
</div>
</div>
<div
className={classNames(
"collab-thread-input-indicator--wrapper",
"flex-v-center",
collabStyles()[
"collab-thread-input-indicator--wrapper"
],
flexAlignCenter
)}
>
<ErrorIndicator errorMessage={error.message} />
<CharacterCounter
currentLength={state.message.length}
maxLength={maxMessageLength}
/>
</div>
</div>
);
}
);
export default CommentTextArea;