Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 39x 39x 39x 39x 39x 39x 1x 1x 1x 22x 228x 5x 5x 1x 228x 228x 228x 228x 228x 228x 2x 2x 2x 2x 1x 2x 3x 3x 3x 3x 19x 15x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 4x 4x 4x 2x 4x 2x 4x 3x 3x 2x 3x 3x 2x 3x 1x 19x 10x 10x 10x 10x 190x 189x 189x 190x 1x 7x 1x 1x 228x | import {debounce} from '@s-ui/js/lib/function' import {getValueType, MASK, valueChecker} from '../config.js' import PIN_INPUT_ACTION_TYPES from './actionTypes.js' export const getInitialPinInputReducerState = ({ defaultValue = '', disabled = false, mask = MASK.NUMBER, value } = {}) => { const valueType = getValueType({value, defaultValue}) const arrayValue = typeof value === 'string' ? value.split('') : value const arrayDefaultValue = typeof defaultValue === 'string' ? defaultValue.split('') : defaultValue let innerValue = value !== undefined ? arrayValue : arrayDefaultValue innerValue = valueChecker({mask, length: innerValue.length})(innerValue.filter(Boolean).join('')) ? innerValue : [] return { valueType, focusPosition: 0, mask, innerValue, checker: valueChecker({mask}), disabled, elements: [] } } const focus = element => setTimeout(() => element.focus(), 0) const debouncedFocus = debounce(focus, 20) const focusElement = element => { debouncedFocus(element) } const onChangeHandler = onChange => (event, state) => { const {innerValue, focusPosition, valueType} = state typeof onChange === 'function' && onChange(event, { value: valueType === 'string' ? innerValue.filter(Boolean).join('') : innerValue, key: innerValue[focusPosition], index: focusPosition, innerValue }) } export const pinInputReducer = (state, {actionType, payload}) => { let nextState = Object.assign({}, state) const {checker, innerValue, focusPosition, elements} = state const {disabled, mask, node, event = {}} = payload const {key, shiftKey} = event const onChange = onChangeHandler(payload.onChange) let isValidPayloadInnerValue let newChecker let isInvalid let newIndex let position switch (actionType) { case PIN_INPUT_ACTION_TYPES.SET_PIN_INPUT_DISABLED: nextState = {...state, disabled} break case PIN_INPUT_ACTION_TYPES.SET_PIN_INPUT_VALUE: isValidPayloadInnerValue = valueChecker({ mask: state.mask, length: payload.innerValue.length })(payload.innerValue.filter(Boolean).join('')) if ( isValidPayloadInnerValue && !( innerValue?.length === payload?.innerValue?.length && innerValue.every((value, index) => value === payload.innerValue[index]) ) ) { nextState = { ...state, ...{ innerValue: [...payload.innerValue] } } } break case PIN_INPUT_ACTION_TYPES.SET_PIN_INPUT_MASK: newChecker = valueChecker({mask}) isInvalid = innerValue.find(value => !newChecker(value)) nextState = { ...state, checker: newChecker, mask, innerValue: isInvalid ? [] : innerValue } break case PIN_INPUT_ACTION_TYPES.SET_PIN_INPUT_KEY: if (key.length > 1) { switch (key) { case 'ArrowUp': case 'ArrowDown': focusElement(elements[focusPosition]) break case 'ArrowLeft': nextState = { ...state, focusPosition: elements[focusPosition - 1] ? focusPosition - 1 : focusPosition } focusElement(elements[nextState.focusPosition]) break case 'ArrowRight': nextState = { ...state, focusPosition: elements[focusPosition + 1] ? focusPosition + 1 : focusPosition } focusElement(elements[nextState.focusPosition]) break case 'Backspace': innerValue[focusPosition] = undefined nextState = { ...state, innerValue: [...innerValue], focusPosition: elements[focusPosition - 1] ? focusPosition - 1 : focusPosition } onChange(event, nextState) break case 'Delete': innerValue[focusPosition] = undefined nextState = {...state, innerValue: [...innerValue]} onChange(event, nextState) break case 'Tab': newIndex = shiftKey ? focusPosition - 1 : focusPosition + 1 nextState = { ...state, focusPosition: elements[newIndex] ? newIndex : focusPosition } if (newIndex < elements.length && newIndex > 0) { focusElement(elements[nextState.focusPosition]) } break case 'Meta': default: break } } else if (checker(key)) { const isKeyChange = innerValue[focusPosition] !== key if (isKeyChange) { innerValue[focusPosition] = key } nextState = { ...state, innerValue: isKeyChange ? [...innerValue] : innerValue, focusPosition: elements[focusPosition + 1] ? focusPosition + 1 : focusPosition } if (isKeyChange) { onChange(event, nextState) } focusElement(elements[nextState.focusPosition]) } else { focusElement(elements[focusPosition]) } break case PIN_INPUT_ACTION_TYPES.SET_PIN_INPUT_FOCUS: position = elements[payload.focusPosition] ? payload.focusPosition : focusPosition nextState = {...state, focusPosition: position} focusElement(elements[position]) break case PIN_INPUT_ACTION_TYPES.SET_PIN_INPUT_ELEMENT: if (!elements.includes(node)) { elements[elements.length] = node nextState = { ...state, elements: [...elements] } } break case PIN_INPUT_ACTION_TYPES.REMOVE_PIN_INPUT_ELEMENT: nextState = { ...state, elements: elements.filter(element => node !== element) } break default: break } return nextState } |