|
| 1 | +// PinCode.tsx |
| 2 | +'use client'; |
| 3 | + |
| 4 | +import React, { useRef, useState, useEffect, forwardRef } from 'react'; |
| 5 | +import useClasses from '../use-classes'; |
| 6 | +import useScale, { withScale } from '../use-scale'; |
| 7 | + |
| 8 | +interface Props { |
| 9 | + value?: string; |
| 10 | + length?: number; |
| 11 | + validChars?: string; |
| 12 | + placeholder?: string; |
| 13 | + autoFocus?: boolean; |
| 14 | + passwordMode?: boolean; |
| 15 | + className?: string; |
| 16 | + onChange?: (value: string) => void; |
| 17 | + onFocus?: () => void; |
| 18 | + onBlur?: () => void; |
| 19 | + onComplete?: (value: string) => void; |
| 20 | +} |
| 21 | + |
| 22 | +type NativeAttrs = Omit<React.HTMLAttributes<HTMLDivElement>, keyof Props>; |
| 23 | +export type PinCodeProps = Props & NativeAttrs; |
| 24 | + |
| 25 | +const PinCodeComponent = forwardRef<HTMLInputElement, PinCodeProps>( |
| 26 | + ( |
| 27 | + { |
| 28 | + value, |
| 29 | + length = 5, |
| 30 | + validChars = 'A-Za-z0-9', |
| 31 | + placeholder = '·', |
| 32 | + autoFocus = false, |
| 33 | + passwordMode = false, |
| 34 | + className, |
| 35 | + onChange, |
| 36 | + onFocus, |
| 37 | + onBlur, |
| 38 | + onComplete, |
| 39 | + ...props |
| 40 | + }, |
| 41 | + ref, |
| 42 | + ) => { |
| 43 | + const { SCALE, UNIT, CLASS_NAMES } = useScale(); |
| 44 | + |
| 45 | + const [localValue, setLocalValue] = useState(''); |
| 46 | + const [isActive, setActive] = useState(false); |
| 47 | + const inputRef = useRef<HTMLInputElement>(null); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (autoFocus && inputRef.current) { |
| 51 | + inputRef.current.focus(); |
| 52 | + } |
| 53 | + }, [autoFocus]); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + if (ref) { |
| 57 | + if (typeof ref === 'function') { |
| 58 | + ref(inputRef.current); |
| 59 | + } else { |
| 60 | + ref.current = inputRef.current; |
| 61 | + } |
| 62 | + } |
| 63 | + }, [ref]); |
| 64 | + |
| 65 | + const handleClick = () => { |
| 66 | + inputRef.current?.focus(); |
| 67 | + }; |
| 68 | + |
| 69 | + const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => { |
| 70 | + if (['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes(event.key)) { |
| 71 | + event.preventDefault(); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 76 | + const newInputVal = event.target.value.replace(/\s/g, ''); |
| 77 | + if (RegExp(`^[${validChars}]{0,${length}}$`).test(newInputVal)) { |
| 78 | + onChange?.(newInputVal); |
| 79 | + setLocalValue(newInputVal); |
| 80 | + if (newInputVal.length === length) { |
| 81 | + onComplete?.(newInputVal); |
| 82 | + } |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + const getValue = () => value ?? localValue; |
| 87 | + |
| 88 | + return ( |
| 89 | + <div className={useClasses('pincode-container', CLASS_NAMES)} onClick={() => inputRef.current?.focus()}> |
| 90 | + <input |
| 91 | + ref={inputRef} |
| 92 | + aria-label="pincode input" |
| 93 | + spellCheck={false} |
| 94 | + value={getValue()} |
| 95 | + onChange={handleInputChange} |
| 96 | + className={useClasses('pincode-input', className)} |
| 97 | + onKeyDown={handleKeyDown} |
| 98 | + onFocus={() => { |
| 99 | + setActive(true); |
| 100 | + onFocus?.(); |
| 101 | + }} |
| 102 | + onBlur={() => { |
| 103 | + setActive(false); |
| 104 | + onBlur?.(); |
| 105 | + }} |
| 106 | + type={passwordMode ? 'password' : 'text'} |
| 107 | + style={{ opacity: 0, position: 'absolute', zIndex: -1 }} |
| 108 | + {...props} |
| 109 | + /> |
| 110 | + {[...Array(length)].map((_, i) => ( |
| 111 | + <div |
| 112 | + className={useClasses('pincode-character', { |
| 113 | + selected: isActive && getValue().length === i, |
| 114 | + filled: getValue().length > i, |
| 115 | + inactive: getValue()[i] === placeholder, |
| 116 | + })} |
| 117 | + onClick={handleClick} |
| 118 | + key={i} |
| 119 | + > |
| 120 | + {passwordMode && getValue()[i] ? '*' : getValue()[i] || placeholder} |
| 121 | + </div> |
| 122 | + ))} |
| 123 | + |
| 124 | + <style jsx>{` |
| 125 | + .pincode-input { |
| 126 | + top: 0; |
| 127 | + right: 0; |
| 128 | + bottom: 0; |
| 129 | + left: 0; |
| 130 | + box-sizing: border-box; |
| 131 | + position: absolute; |
| 132 | + color: transparent; |
| 133 | + background: transparent; |
| 134 | + caret-color: transparent; |
| 135 | + outline: none; |
| 136 | + border: 0 none transparent; |
| 137 | + } |
| 138 | +
|
| 139 | + .pincode-input::-ms-reveal, |
| 140 | + .pincode-input::-ms-clear { |
| 141 | + display: none; |
| 142 | + } |
| 143 | +
|
| 144 | + .pincode-input::selection { |
| 145 | + background: transparent; |
| 146 | + } |
| 147 | +
|
| 148 | + :where(.pincode-container) { |
| 149 | + position: relative; |
| 150 | + display: flex; |
| 151 | + gap: 8px; |
| 152 | + } |
| 153 | +
|
| 154 | + :where(.pincode-character) { |
| 155 | + height: 100%; |
| 156 | + flex-grow: 1; |
| 157 | + flex-basis: 0; |
| 158 | + text-align: center; |
| 159 | + color: var(--color-foreground-1000); |
| 160 | + background-color: var(--color-background-1000); |
| 161 | + border: 1px solid var(--color-background-800); |
| 162 | + border-radius: var(--layout-radius); |
| 163 | + cursor: default; |
| 164 | + user-select: none; |
| 165 | + box-sizing: border-box; |
| 166 | + } |
| 167 | +
|
| 168 | + :where(.pincode-character.inactive) { |
| 169 | + color: var(--color-foreground-1000); |
| 170 | + background-color: var(--color-background-600); |
| 171 | + } |
| 172 | +
|
| 173 | + :where(.pincode-character.selected) { |
| 174 | + outline: 2px solid var(--color-foreground-600); |
| 175 | + color: var(--color-foreground-1000); |
| 176 | + } |
| 177 | +
|
| 178 | + ${SCALE.padding(0, value => `padding: ${value.top} ${value.right} ${value.bottom} ${value.left};`, undefined, 'pincode-container')} |
| 179 | + ${SCALE.margin(0, value => `margin: ${value.top} ${value.right} ${value.bottom} ${value.left};`, undefined, 'pincode-container')} |
| 180 | + ${SCALE.w(16, value => `width: ${value};`, undefined, 'pincode-container')} |
| 181 | + ${SCALE.h(2.4, value => `height: ${value};`, undefined, 'pincode-container')} |
| 182 | + ${SCALE.font(1, value => `font-size: ${value};`, 'inherit', 'pincode-character')} |
| 183 | + ${SCALE.lineHeight(2.4, value => `line-height: ${value};`, undefined, 'pincode-character')} |
| 184 | + ${UNIT('pincode-container')} |
| 185 | + `}</style> |
| 186 | + </div> |
| 187 | + ); |
| 188 | + }, |
| 189 | +); |
| 190 | +PinCodeComponent.displayName = 'HimalayaPinCode'; |
| 191 | +const PinCode = withScale(PinCodeComponent); |
| 192 | +export default PinCode; |
0 commit comments