/
use-pin-input.ts
368 lines (328 loc) · 10.2 KB
/
use-pin-input.ts
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
import { createDescendantContext } from "@chakra-ui/descendant"
import { useControllableState } from "@chakra-ui/react-use-controllable-state"
import { ariaAttr, callAllHandlers } from "@chakra-ui/shared-utils"
import { createContext } from "@chakra-ui/react-context"
import { mergeRefs } from "@chakra-ui/react-use-merge-refs"
import { useCallback, useEffect, useState, useId } from "react"
/* -------------------------------------------------------------------------------------------------
* Create context to track descendants and their indices
* -----------------------------------------------------------------------------------------------*/
export const [
PinInputDescendantsProvider,
usePinInputDescendantsContext,
usePinInputDescendants,
usePinInputDescendant,
] = createDescendantContext<HTMLInputElement>()
/* -------------------------------------------------------------------------------------------------
* Create context that stores pin-input logic
* -----------------------------------------------------------------------------------------------*/
export type PinInputContext = Omit<UsePinInputReturn, "descendants"> & {
/**
* Sets the pin input component to the disabled state
*/
isDisabled?: boolean
/**
* Sets the pin input component to the invalid state
*/
isInvalid?: boolean
}
export const [PinInputProvider, usePinInputContext] =
createContext<PinInputContext>({
name: "PinInputContext",
errorMessage:
"usePinInputContext: `context` is undefined. Seems you forgot to all pin input fields within `<PinInput />`",
})
/* -------------------------------------------------------------------------------------------------
* usePinInput hook
* -----------------------------------------------------------------------------------------------*/
export interface UsePinInputProps {
/**
* If `true`, the pin input receives focus on mount
*/
autoFocus?: boolean
/**
* The value of the pin input. This is the value
* that will be returned when the pin input is filled
*/
value?: string
/**
* The default value of the pin input
*/
defaultValue?: string
/**
* Function called on input change
*/
onChange?: (value: string) => void
/**
* Function called when all inputs have valid values
*/
onComplete?: (value: string) => void
/**
* The placeholder for the pin input
*/
placeholder?: string
/**
* If `true`, focus will move automatically to the next input once filled
* @default true
*/
manageFocus?: boolean
/**
* If `true`, the pin input component signals to its fields that they should
* use `autocomplete="one-time-code"`.
*/
otp?: boolean
/**
* The top-level id string that will be applied to the input fields.
* The index of the input will be appended to this top-level id.
*
* @example
* if id="foo", the first input will have `foo-0`
*/
id?: string
/**
* If `true`, the pin input component is put in the disabled state
*/
isDisabled?: boolean
/**
* If `true`, the pin input component is put in the invalid state
*/
isInvalid?: boolean
/**
* The type of values the pin-input should allow
*/
type?: "alphanumeric" | "number"
/**
* If `true`, the input's value will be masked just like `type=password`
*/
mask?: boolean
}
const toArray = (value?: string) => value?.split("")
function validate(value: string, type: UsePinInputProps["type"]) {
const NUMERIC_REGEX = /^[0-9]+$/
const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i
const regex = type === "alphanumeric" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX
return regex.test(value)
}
/* -------------------------------------------------------------------------------------------------
* usePinInput - handles the general pin input logic
* -----------------------------------------------------------------------------------------------*/
/**
* @internal
*/
export function usePinInput(props: UsePinInputProps = {}) {
const {
autoFocus,
value,
defaultValue,
onChange,
onComplete,
placeholder = "○",
manageFocus = true,
otp = false,
id: idProp,
isDisabled,
isInvalid,
type = "number",
mask,
} = props
const uuid = useId()
const id = idProp ?? `pin-input-${uuid}`
const descendants = usePinInputDescendants()
const [moveFocus, setMoveFocus] = useState(true)
const [focusedIndex, setFocusedIndex] = useState(-1)
const [values, setValues] = useControllableState<string[]>({
defaultValue: toArray(defaultValue) || [],
value: toArray(value),
onChange: (values) => onChange?.(values.join("")),
})
useEffect(() => {
if (autoFocus) {
const first = descendants.first()
if (first) {
requestAnimationFrame(() => {
first.node.focus()
})
}
}
// We don't want to listen for updates to `autoFocus` since it only runs initially
// eslint-disable-next-line
}, [descendants])
const focusNext = useCallback(
(index: number) => {
if (!moveFocus || !manageFocus) return
const next = descendants.next(index, false)
if (next) {
requestAnimationFrame(() => {
next.node.focus()
})
}
},
[descendants, moveFocus, manageFocus],
)
const setValue = useCallback(
(value: string, index: number, handleFocus: boolean = true) => {
const nextValues = [...values]
nextValues[index] = value
setValues(nextValues)
const isComplete =
value !== "" &&
nextValues.length === descendants.count() &&
nextValues.every(
(inputValue) => inputValue != null && inputValue !== "",
)
if (isComplete) {
onComplete?.(nextValues.join(""))
} else {
if (handleFocus) focusNext(index)
}
},
[values, setValues, focusNext, onComplete, descendants],
)
const clear = useCallback(() => {
const values: string[] = Array(descendants.count()).fill("")
setValues(values)
const first = descendants.first()
first?.node?.focus()
}, [descendants, setValues])
const getNextValue = useCallback((value: string, eventValue: string) => {
let nextValue = eventValue
if (value?.length > 0) {
if (value[0] === eventValue.charAt(0)) {
nextValue = eventValue.charAt(1)
} else if (value[0] === eventValue.charAt(1)) {
nextValue = eventValue.charAt(0)
}
}
return nextValue
}, [])
const getInputProps = useCallback(
(props: InputProps & { index: number }): InputProps => {
const { index, ...rest } = props
/**
* Improved from: https://github.com/uber/baseweb/blob/master/src/pin-code/pin-code.js
*/
const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const eventValue = event.target.value
const currentValue = values[index]
const nextValue = getNextValue(currentValue, eventValue)
// if the value was removed using backspace
if (nextValue === "") {
setValue("", index)
return
}
// in the case of an autocomplete or copy and paste
if (eventValue.length > 2) {
// see if we can use the string to fill out our values
if (validate(eventValue, type)) {
// Ensure the value matches the number of inputs
const nextValue = eventValue
.split("")
.filter((_, index) => index < descendants.count())
setValues(nextValue)
// if pasting fills the entire input fields, trigger `onComplete`
if (nextValue.length === descendants.count()) {
onComplete?.(nextValue.join(""))
}
}
} else {
// only set if the new value is a number
if (validate(nextValue, type)) {
setValue(nextValue, index)
}
setMoveFocus(true)
}
}
const onKeyDown = (event: React.KeyboardEvent) => {
if (event.key === "Backspace" && manageFocus) {
if ((event.target as HTMLInputElement).value === "") {
const prevInput = descendants.prev(index, false)
if (prevInput) {
setValue("", index - 1, false)
prevInput.node?.focus()
setMoveFocus(true)
}
} else {
setMoveFocus(false)
}
}
}
const onFocus = () => {
setFocusedIndex(index)
}
const onBlur = () => {
setFocusedIndex(-1)
}
const hasFocus = focusedIndex === index
const inputType = type === "number" ? "tel" : "text"
return {
"aria-label": "Please enter your pin code",
inputMode: type === "number" ? "numeric" : "text",
type: mask ? "password" : inputType,
...rest,
id: `${id}-${index}`,
disabled: isDisabled,
"aria-invalid": ariaAttr(isInvalid),
onChange: callAllHandlers(rest.onChange, onChange),
onKeyDown: callAllHandlers(rest.onKeyDown, onKeyDown),
onFocus: callAllHandlers(rest.onFocus, onFocus),
onBlur: callAllHandlers(rest.onBlur, onBlur),
value: values[index] || "",
autoComplete: otp ? "one-time-code" : "off",
placeholder: hasFocus ? "" : placeholder,
}
},
[
descendants,
focusedIndex,
getNextValue,
id,
isDisabled,
mask,
isInvalid,
manageFocus,
onComplete,
otp,
placeholder,
setValue,
setValues,
type,
values,
],
)
return {
// prop getter
getInputProps,
// state
id,
descendants,
values,
// actions
setValue,
setValues,
clear,
}
}
export type UsePinInputReturn = ReturnType<typeof usePinInput>
export interface UsePinInputFieldProps extends InputProps {
ref?: React.Ref<HTMLInputElement>
}
/**
* @internal
*/
export function usePinInputField(
props: UsePinInputFieldProps = {},
ref: React.Ref<any> = null,
) {
const { getInputProps } = usePinInputContext()
const { index, register } = usePinInputDescendant()
return getInputProps({
...props,
ref: mergeRefs(register, ref),
index,
})
}
interface InputProps
extends Omit<
React.ComponentPropsWithRef<"input">,
"color" | "height" | "width"
> {}