|
| 1 | +import React, { |
| 2 | + Children, |
| 3 | + cloneElement, |
| 4 | + createContext, |
| 5 | + createRef, |
| 6 | + ReactElement, |
| 7 | + useCallback, |
| 8 | + useContext, |
| 9 | + useEffect, |
| 10 | + useMemo, |
| 11 | + useState, |
| 12 | +} from 'react'; |
| 13 | + |
| 14 | +const Context = createContext<{ |
| 15 | + focusInput: (index: number) => void; |
| 16 | + handleValuesChange?: (values: string) => void; |
| 17 | + selectInputContent: (index: number) => void; |
| 18 | + setInputValue: (value: string, index: number) => void; |
| 19 | + validate: (data: string) => boolean; |
| 20 | +}>({ |
| 21 | + focusInput: () => null, |
| 22 | + handleValuesChange: () => null, |
| 23 | + selectInputContent: () => null, |
| 24 | + setInputValue: () => null, |
| 25 | + validate: () => true, |
| 26 | +}); |
| 27 | + |
| 28 | +export interface ProviderProps { |
| 29 | + autoFocus?: boolean; |
| 30 | + children: ReactElement[]; |
| 31 | + defaultValue?: string; |
| 32 | + onChange?: (data: string) => void; |
| 33 | + onCompleted?: (data: string) => void; |
| 34 | + type?: 'alphanumeric' | 'number'; |
| 35 | +} |
| 36 | + |
| 37 | +export default function Provider({ |
| 38 | + children, |
| 39 | + autoFocus, |
| 40 | + defaultValue = '', |
| 41 | + onChange = () => null, |
| 42 | + onCompleted = () => null, |
| 43 | + type = 'number', |
| 44 | +}: ProviderProps) { |
| 45 | + /** |
| 46 | + * generate a new array, map through it |
| 47 | + * and replace with the value when possible |
| 48 | + */ |
| 49 | + const fillValues = useCallback( |
| 50 | + (value: string) => |
| 51 | + new Array(children.length).fill('').map((_, index) => value[index] ?? ''), |
| 52 | + [children.length] |
| 53 | + ); |
| 54 | + |
| 55 | + const [values, setValues] = useState(fillValues(defaultValue)); |
| 56 | + |
| 57 | + const refs = useMemo( |
| 58 | + () => |
| 59 | + new Array(children.length) |
| 60 | + .fill(null) |
| 61 | + .map(() => createRef<HTMLInputElement>()), |
| 62 | + [children.length] |
| 63 | + ); |
| 64 | + |
| 65 | + const focusInput = useCallback( |
| 66 | + (index: number) => { |
| 67 | + const input = refs[index]?.current; |
| 68 | + |
| 69 | + if (input) { |
| 70 | + requestAnimationFrame(() => { |
| 71 | + input.focus(); |
| 72 | + }); |
| 73 | + } |
| 74 | + }, |
| 75 | + [refs] |
| 76 | + ); |
| 77 | + |
| 78 | + const handleValuesChange = useCallback( |
| 79 | + (input: string) => { |
| 80 | + setValues(fillValues(input)); |
| 81 | + |
| 82 | + const isCompleted = input.length === length; |
| 83 | + |
| 84 | + if (isCompleted) { |
| 85 | + onCompleted(input); |
| 86 | + // blurInput(); |
| 87 | + } |
| 88 | + |
| 89 | + focusInput(input.length); |
| 90 | + }, |
| 91 | + [fillValues, focusInput, onCompleted] |
| 92 | + ); |
| 93 | + |
| 94 | + const selectInputContent = useCallback( |
| 95 | + (index: number) => { |
| 96 | + const input = refs[index]?.current; |
| 97 | + |
| 98 | + if (input) { |
| 99 | + requestAnimationFrame(() => { |
| 100 | + input.select(); |
| 101 | + }); |
| 102 | + } |
| 103 | + }, |
| 104 | + [refs] |
| 105 | + ); |
| 106 | + |
| 107 | + const setInputValue = useCallback( |
| 108 | + (value: string, index: number) => { |
| 109 | + const nextValues = [...values]; |
| 110 | + nextValues[index] = value; |
| 111 | + |
| 112 | + setValues(nextValues); |
| 113 | + |
| 114 | + const stringifiedValues = nextValues.join(''); |
| 115 | + const isLast = index === children.length - 1; |
| 116 | + console.log(index, children.length, index === children.length - 1); |
| 117 | + |
| 118 | + if (isLast) { |
| 119 | + onCompleted(stringifiedValues); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + onChange(stringifiedValues); |
| 124 | + }, |
| 125 | + [children.length, onChange, onCompleted, values] |
| 126 | + ); |
| 127 | + |
| 128 | + const validate = useCallback( |
| 129 | + (input: string) => { |
| 130 | + if (type === 'number') { |
| 131 | + return /^\d/.test(input); |
| 132 | + } |
| 133 | + |
| 134 | + if (type === 'alphanumeric') { |
| 135 | + return /^[a-zA-Z0-9]/.test(input); |
| 136 | + } |
| 137 | + |
| 138 | + return true; |
| 139 | + }, |
| 140 | + [type] |
| 141 | + ); |
| 142 | + |
| 143 | + const memoizedValue = useMemo( |
| 144 | + () => ({ |
| 145 | + focusInput, |
| 146 | + handleValuesChange, |
| 147 | + selectInputContent, |
| 148 | + setInputValue, |
| 149 | + validate, |
| 150 | + }), |
| 151 | + [ |
| 152 | + focusInput, |
| 153 | + handleValuesChange, |
| 154 | + selectInputContent, |
| 155 | + setInputValue, |
| 156 | + validate, |
| 157 | + ] |
| 158 | + ); |
| 159 | + |
| 160 | + /** |
| 161 | + * autoFocus |
| 162 | + */ |
| 163 | + useEffect(() => { |
| 164 | + if (autoFocus) { |
| 165 | + focusInput(0); |
| 166 | + } |
| 167 | + }, [autoFocus, focusInput]); |
| 168 | + |
| 169 | + return ( |
| 170 | + <Context.Provider value={memoizedValue}> |
| 171 | + <>{JSON.stringify(values, null, 2)}</> |
| 172 | + |
| 173 | + {Children.map(children, (child, index) => |
| 174 | + cloneElement(child, { |
| 175 | + index, |
| 176 | + isLast: index === children.length - 1, |
| 177 | + value: values[index], |
| 178 | + ref: refs[index], |
| 179 | + }) |
| 180 | + )} |
| 181 | + </Context.Provider> |
| 182 | + ); |
| 183 | +} |
| 184 | + |
| 185 | +export function useInputVerificationCode() { |
| 186 | + const context = useContext(Context); |
| 187 | + |
| 188 | + if (!context) { |
| 189 | + throw new Error( |
| 190 | + 'useInputVerificationCode can only be used inside of a Provider' |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + return context; |
| 195 | +} |
0 commit comments