forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextInputWithTokens.tsx
383 lines (345 loc) · 12.5 KB
/
TextInputWithTokens.tsx
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import {FocusKeys} from '@primer/behaviors'
import {isFocusable} from '@primer/behaviors/utils'
import {omit} from '@styled-system/props'
import React, {FocusEventHandler, KeyboardEventHandler, MouseEventHandler, RefObject, useRef, useState} from 'react'
import Box from './Box'
import {useProvidedRefOrCreate} from './hooks'
import {useCombinedRefs} from './hooks/useCombinedRefs'
import {useFocusZone} from './hooks/useFocusZone'
import Text from './Text'
import {TextInputProps} from './TextInput'
import Token from './Token/Token'
import {TokenSizeKeys} from './Token/TokenBase'
import TextInputInnerVisualSlot from './_TextInputInnerVisualSlot'
import TextInputWrapper, {textInputHorizPadding, TextInputSizes} from './_TextInputWrapper'
import UnstyledTextInput from './_UnstyledTextInput'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyReactComponent = React.ComponentType<any>
// NOTE: if these props or their JSDoc comments are updated, be sure to also update
// the prop table in docs/content/TextInputTokens.mdx
export type TextInputWithTokensProps<TokenComponentType extends AnyReactComponent = typeof Token> = {
/**
* The array of tokens to render
*/
tokens: TokenComponentType extends React.ComponentType<infer TokenProps> ? TokenProps[] : never
/**
* The function that gets called when a token is removed
*/
onTokenRemove: (tokenId: string | number) => void
/**
* The component used to render each token
*/
tokenComponent?: TokenComponentType
/**
* The maximum height of the component. If the content in the input exceeds this height,
* it will scroll vertically
*/
maxHeight?: React.CSSProperties['maxHeight']
/**
* Whether tokens should render inline horizontally. By default, tokens wrap to new lines.
*/
preventTokenWrapping?: boolean
/**
* The size of the tokens and text input
*/
size?: TokenSizeKeys
/**
* Whether the remove buttons should be rendered in the tokens
*/
hideTokenRemoveButtons?: boolean
/**
* The number of tokens to display before truncating
*/
visibleTokenCount?: number
} & Omit<TextInputProps, 'size'>
const overflowCountFontSizeMap: Record<TokenSizeKeys, number> = {
small: 0,
medium: 1,
large: 1,
extralarge: 2,
xlarge: 2 // will eventually replace "extralarge" per this ADR: https://github.com/github/primer/blob/main/adrs/2022-02-09-size-naming-guidelines.md
}
// using forwardRef is important so that other components (ex. Autocomplete) can use the ref
function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactComponent>(
{
icon: IconComponent,
leadingVisual: LeadingVisual,
trailingVisual: TrailingVisual,
loading,
loaderPosition,
contrast,
className,
block,
disabled,
sx: sxProp,
tokens,
onTokenRemove,
tokenComponent: TokenComponent,
preventTokenWrapping,
size,
hideTokenRemoveButtons,
maxHeight,
width: widthProp,
minWidth: minWidthProp,
maxWidth: maxWidthProp,
validationStatus,
variant: variantProp, // deprecated. use `size` instead
visibleTokenCount,
...rest
}: TextInputWithTokensProps<TokenComponentType>,
externalRef: React.ForwardedRef<HTMLInputElement>
) {
const {onBlur, onFocus, onKeyDown, ...inputPropsRest} = omit(rest)
const ref = useProvidedRefOrCreate<HTMLInputElement>(externalRef as React.RefObject<HTMLInputElement>)
const localInputRef = useRef<HTMLInputElement>(null)
const combinedInputRef = useCombinedRefs(localInputRef, ref)
const [selectedTokenIndex, setSelectedTokenIndex] = useState<number | undefined>()
const [tokensAreTruncated, setTokensAreTruncated] = useState<boolean>(Boolean(visibleTokenCount))
const {containerRef} = useFocusZone(
{
focusOutBehavior: 'wrap',
bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd,
focusableElementFilter: element => {
return !element.getAttributeNames().includes('aria-hidden')
},
getNextFocusable: direction => {
if (!selectedTokenIndex && selectedTokenIndex !== 0) {
return undefined
}
let nextIndex = selectedTokenIndex + 1 // "+ 1" accounts for the first element: the text input
if (direction === 'next') {
nextIndex += 1
}
if (direction === 'previous') {
nextIndex -= 1
}
if (nextIndex > tokens.length || nextIndex < 1) {
return combinedInputRef.current || undefined
}
return containerRef.current?.children[nextIndex] as HTMLElement
}
},
[selectedTokenIndex]
)
const handleTokenRemove = (tokenId: string | number) => {
onTokenRemove(tokenId)
// HACK: wait a tick for the token node to be removed from the DOM
setTimeout(() => {
const nextElementToFocus = containerRef.current?.children[selectedTokenIndex || 0] as HTMLElement | undefined
// when removing the first token by keying "Backspace" or "Delete",
// `nextFocusableElement` is the div that wraps the input
const firstFocusable =
nextElementToFocus && isFocusable(nextElementToFocus)
? nextElementToFocus
: (Array.from(containerRef.current?.children || []) as HTMLElement[]).find(el => isFocusable(el))
if (firstFocusable) {
firstFocusable.focus()
} else {
// if there are no tokens left, focus the input
ref.current?.focus()
}
}, 0)
}
const handleTokenFocus: (tokenIndex: number) => FocusEventHandler = tokenIndex => () => {
setSelectedTokenIndex(tokenIndex)
}
const handleTokenBlur: FocusEventHandler = () => {
setSelectedTokenIndex(undefined)
// HACK: wait a tick and check the focused element before hiding truncated tokens
// this prevents the tokens from hiding when the user is moving focus between tokens,
// but still hides the tokens when the user blurs the token by tabbing out or clicking somewhere else on the page
setTimeout(() => {
if (!containerRef.current?.contains(document.activeElement) && visibleTokenCount) {
setTokensAreTruncated(true)
}
}, 0)
}
const handleTokenKeyUp: KeyboardEventHandler = event => {
if (event.key === 'Escape') {
ref.current?.focus()
}
}
const handleInputFocus: FocusEventHandler = event => {
onFocus && onFocus(event)
setSelectedTokenIndex(undefined)
visibleTokenCount && setTokensAreTruncated(false)
}
const handleInputBlur: FocusEventHandler = event => {
onBlur && onBlur(event)
// HACK: wait a tick and check the focused element before hiding truncated tokens
// this prevents the tokens from hiding when the user is moving focus from the input to a token,
// but still hides the tokens when the user blurs the input by tabbing out or clicking somewhere else on the page
setTimeout(() => {
if (!containerRef.current?.contains(document.activeElement) && visibleTokenCount) {
setTokensAreTruncated(true)
}
}, 0)
}
const handleInputKeyDown: KeyboardEventHandler = e => {
if (onKeyDown) {
onKeyDown(e)
}
if (ref.current?.value) {
return
}
const lastToken = tokens[tokens.length - 1]
if (e.key === 'Backspace' && lastToken) {
handleTokenRemove(lastToken.id)
if (ref.current) {
// TODO: eliminate the first hack by making changes to the Autocomplete component
//
// HACKS:
// 1. Directly setting `ref.current.value` instead of updating state because the autocomplete
// highlight behavior doesn't work correctly if we update the value with a setState action in onChange
// 2. Adding an extra space so that when I backspace, it doesn't delete the last letter
ref.current.value = `${lastToken.text} `
}
// HACK: for some reason we need to wait a tick for `.select()` to work
setTimeout(() => {
ref.current?.select()
}, 0)
}
}
const focusInput: MouseEventHandler = () => {
combinedInputRef.current?.focus()
}
const preventTokenClickPropagation: MouseEventHandler = event => {
event.stopPropagation()
}
const visibleTokens = tokensAreTruncated ? tokens.slice(0, visibleTokenCount) : tokens
const inputSizeMap: Record<TokenSizeKeys, TextInputSizes> = {
small: 'small',
medium: 'small',
large: 'medium',
extralarge: 'medium',
xlarge: 'medium' // will eventually replace "extralarge" per this ADR: https://github.com/github/primer/blob/main/adrs/2022-02-09-size-naming-guidelines.md
}
const showLeadingLoadingIndicator =
loading && (loaderPosition === 'leading' || Boolean(LeadingVisual && loaderPosition !== 'trailing'))
const showTrailingLoadingIndicator =
loading && (loaderPosition === 'trailing' || (loaderPosition === 'auto' && !LeadingVisual))
return (
<TextInputWrapper
block={block}
className={className}
contrast={contrast}
disabled={disabled}
hasLeadingVisual={Boolean(LeadingVisual || showLeadingLoadingIndicator)}
hasTrailingVisual={Boolean(TrailingVisual || showTrailingLoadingIndicator)}
width={widthProp}
minWidth={minWidthProp}
maxWidth={maxWidthProp}
size={size && inputSizeMap[size]}
validationStatus={validationStatus}
variant={variantProp} // deprecated. use `size` prop instead
onClick={focusInput}
sx={{
paddingLeft: textInputHorizPadding,
py: `calc(${textInputHorizPadding} / 2)`,
...(block
? {
display: 'flex',
width: '100%'
}
: {}),
...(maxHeight
? {
maxHeight,
overflow: 'auto'
}
: {}),
...(preventTokenWrapping
? {
overflow: 'auto'
}
: {}),
...sxProp
}}
>
{IconComponent && !LeadingVisual && <IconComponent className="TextInput-icon" />}
<TextInputInnerVisualSlot
hasLoadingIndicator={typeof loading === 'boolean'}
visualPosition="leading"
showLoadingIndicator={showLeadingLoadingIndicator}
>
{typeof LeadingVisual === 'function' ? <LeadingVisual /> : LeadingVisual}
</TextInputInnerVisualSlot>
<Box
ref={containerRef as RefObject<HTMLDivElement>}
display="flex"
sx={{
alignItems: 'center',
flexWrap: preventTokenWrapping ? 'nowrap' : 'wrap',
marginLeft: '-0.25rem',
marginBottom: '-0.25rem',
flexGrow: 1,
'> *': {
flexShrink: 0,
marginLeft: '0.25rem',
marginBottom: '0.25rem'
}
}}
>
<Box
sx={{
order: 1,
flexGrow: 1
}}
>
<UnstyledTextInput
ref={combinedInputRef}
disabled={disabled}
onFocus={handleInputFocus}
onBlur={handleInputBlur}
onKeyDown={handleInputKeyDown}
type="text"
sx={{height: '100%'}}
aria-invalid={validationStatus === 'error' ? 'true' : 'false'}
{...inputPropsRest}
/>
</Box>
{TokenComponent
? visibleTokens.map(({id, ...tokenRest}, i) => (
<TokenComponent
key={id}
onFocus={handleTokenFocus(i)}
onBlur={handleTokenBlur}
onKeyUp={handleTokenKeyUp}
onClick={preventTokenClickPropagation}
isSelected={selectedTokenIndex === i}
onRemove={() => {
handleTokenRemove(id)
}}
hideRemoveButton={hideTokenRemoveButtons}
size={size}
tabIndex={0}
{...tokenRest}
/>
))
: null}
{tokensAreTruncated ? (
<Text color="fg.muted" fontSize={size && overflowCountFontSizeMap[size]}>
+{tokens.length - visibleTokens.length}
</Text>
) : null}
</Box>
<TextInputInnerVisualSlot
hasLoadingIndicator={typeof loading === 'boolean'}
visualPosition="trailing"
showLoadingIndicator={showTrailingLoadingIndicator}
>
{typeof TrailingVisual === 'function' ? <TrailingVisual /> : TrailingVisual}
</TextInputInnerVisualSlot>
</TextInputWrapper>
)
}
const TextInputWithTokens = React.forwardRef(TextInputWithTokensInnerComponent)
TextInputWithTokens.defaultProps = {
tokenComponent: Token,
size: 'xlarge',
hideTokenRemoveButtons: false,
preventTokenWrapping: false,
loaderPosition: 'auto'
}
TextInputWithTokens.displayName = 'TextInputWithTokens'
export default TextInputWithTokens