@@ -1,32 +1,40 @@
import { useCallback , useEffect , useLayoutEffect , useRef , useState } from 'react'
import { cx } from '../internal/cx.js'
import { Spinner } from './Spinner.jsx'
import { nextPin , pinAction } from './pin.js'
import { canSubmit , nextPin , pinAction } from './pin.js'
import { useLabels } from './labels.js'
const KEYS = [ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , null , '0' , 'Backspace' ]
const KEYS _VARIABLE = [ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'Enter' , '0' , 'Backspace' ]
/**
* Numeric PIN entry: dots + a 3× 4 keypad, also driven by the physical keyboard.
*
* <PinPad length={4} onComplete={async pin => { await unlock(pin) }} />
*
* onComplete(pin) runs when `length` digits are in. If it returns a promise the pad
* Fixed length (`length`, default 4): onComplete(pin) runs as soon as `length` digits are in.
* Variable length (`minLength` + `maxLength`, e.g. PINs of 4– 8 digits): an OK key (and the
* Enter key) submits once at least `minLength` digits are in.
*
* If onComplete returns a promise the pad
* shows a spinner until it settles. If it throws / rejects (or returns false), the pad
* shakes, shows `error` text if given, and clears for another try. On success it clears too.
*
* error: text under the dots (e.g. "Wrong PIN"); a change of it also shakes the pad.
* captureKeyboard (default true): digits/Backspace typed anywhere go to the pad,
* except while the user is typing in a field.
* backspaceLabel / progressLabel(n, length) override the UiLabelsProvider texts.
* backspaceLabel / submitLabel / progressLabel(n, length) override the UiLabelsProvider texts.
*/
export function PinPad ( {
length = 4 , onComplete , onChange , disabled = false , error , title ,
captureKeyboard = true , backspaceLabel , progressLabel , className ,
length = 4 , minLength , maxLength , onComplete , onChange , disabled = false , error , title ,
captureKeyboard = true , backspaceLabel , submitLabel , progressLabel , className ,
} ) {
const labels = useLabels ( )
const backspaceText = backspaceLabel ? ? labels . backspace
const submitText = submitLabel ? ? labels . submit
const progress = progressLabel ? ? labels . pinProgress
const variable = minLength != null && maxLength != null
const max = variable ? maxLength : length
const [ value , setValue ] = useState ( '' )
const [ busy , setBusy ] = useState ( false )
const [ shake , setShake ] = useState ( 0 )
@@ -61,12 +69,16 @@ export function PinPad({
const { value : current , busy : isBusy , disabled : isDisabled } = latest . current
const action = pinAction ( key )
if ( ! action || isBusy || isDisabled ) return
const next = nextPin ( current , action , length )
if ( action . type === 'submit' ) {
if ( variable && canSubmit ( current , minLength ) ) submit ( current )
return
}
const next = nextPin ( current , action , max )
if ( next === current ) return
setValue ( next )
onChange ? . ( next )
if ( next . length === length ) submit ( next )
} , [ length , onChange , submit ] )
if ( ! variable && next . length === max ) submit ( next )
} , [ variable , minLength , max , onChange , submit ] )
useEffect ( ( ) => {
if ( ! captureKeyboard ) return
@@ -74,38 +86,47 @@ export function PinPad({
if ( e . ctrlKey || e . metaKey || e . altKey ) return
const t = e . target
if ( t && ( t . tagName === 'INPUT' || t . tagName === 'TEXTAREA' || t . isContentEditable ) ) return
if ( pinAction ( e . key ) ) { e . preventDefault ( ) ; press ( e . key ) }
const action = pinAction ( e . key )
if ( ! action || ( action . type === 'submit' && ! variable ) ) return // Enter only means "OK" in variable mode
e . preventDefault ( )
press ( e . key )
}
document . addEventListener ( 'keydown' , onKey )
return ( ) => document . removeEventListener ( 'keydown' , onKey )
} , [ captureKeyboard , press ] )
} , [ captureKeyboard , press , variable ] )
return (
< div className = { cx ( 'x-pinpad' , disabled && 'x-pinpad--disabled' , className ) } >
{ title && < div className = "x-pinpad__title" > { title } < / div > }
{ /* Re-keyed on every failed attempt and on every new error text, which replays the shake */ }
< div key = { ` ${ shake } : ${ error || '' } ` } className = { cx ( 'x-pinpad__dots' , ( shake > 0 || error ) && 'x-pinpad__dots--shake' ) } aria - hidden = "true" >
{ Array . from ( { length } , ( _ , i ) => (
{ Array . from ( { length : variable ? Math . max ( minLength , value . length ) : max } , ( _ , i ) => (
< span key = { i } className = { cx ( 'x-pinpad__dot' , i < value . length && 'x-pinpad__dot--filled' , error && 'x-pinpad__dot--error' ) } / >
) ) }
< / div >
< div className = "x-visually-hidden" aria - live = "polite" > { progress ( value . length , length ) } < / div >
< div className = "x-visually-hidden" aria - live = "polite" > { progress ( value . length , variable ? minLength : max ) } < / div >
< div className = "x-pinpad__status" >
{ busy ? < Spinner size = "sm" / > : error ? < span className = "x-pinpad__error" role = "alert" > { error } < / span > : null }
< / div >
< div className = "x-pinpad__keys" >
{ KEYS . map ( k => k === null
{ ( variable ? KEYS _VARIABLE : KEYS ) . map ( k => k === null
? < span key = "blank" aria - hidden = "true" / >
: (
< button
key = { k }
type = "button"
className = { cx ( 'x-pinpad__key' , k === 'Backspace' && 'x-pinpad__key--backspace' ) }
className = { cx ( 'x-pinpad__key' , k === 'Backspace' && 'x-pinpad__key--backspace' , k === 'Enter' && 'x-pinpad__key--submit' ) }
onClick = { ( ) => press ( k ) }
disabled = { disabled || busy }
aria - label = { k === 'Backspace' ? backspaceText : undefined }
disabled = { disabled || busy || ( k === 'Enter' && ! canSubmit ( value , minLength ) ) }
aria - label = { k === 'Backspace' ? backspaceText : k === 'Enter' ? submitText : undefined }
>
{ k === 'Backspace '
{ k === 'Enter '
? (
< svg viewBox = "0 0 24 24" width = "22" height = "22" fill = "none" stroke = "currentColor" strokeWidth = "2.25" strokeLinecap = "round" strokeLinejoin = "round" aria - hidden = "true" >
< path d = "M5 12.5l4.5 4.5L19 7.5" / >
< / svg >
)
: k === 'Backspace'
? (
< svg viewBox = "0 0 24 24" width = "22" height = "22" fill = "none" stroke = "currentColor" strokeWidth = "2" strokeLinecap = "round" strokeLinejoin = "round" aria - hidden = "true" >
< path d = "M21 5H9l-6 7 6 7h12a1 1 0 0 0 1-1V6a1 1 0 0 0-1-1z" / > < path d = "M17 9l-6 6M11 9l6 6" / >