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 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 | 1x 24x 24x 24x 3x 21x 12x 9x 24x 24x 24x 14x 2x 24x 24x 25x 25x 24x 24x 24x 5x 3x 3x 24x 11x 11x 11x 11x 11x 24x 1x 1x 1x 24x 1x 1x 1x 24x 9x 5x 5x 5x 4x 4x 4x 24x 1x 1x 1x 24x 24x 19x 14x 24x 24x 21x 21x 24x 1x 1x | import {forwardRef, useEffect, useState} from 'react' import cx from 'classnames' import PropTypes from 'prop-types' import AtomButton, {atomButtonDesigns} from '@s-ui/react-atom-button' import AtomInput, {inputSizes} from '@s-ui/react-atom-input' import MoleculeField from '@s-ui/react-molecule-field' import {ACTIONS, BASE_CLASS, CLASS_INPUT_CONTAINER, moleculeDataCounterSizes, sizeConversor} from './config.js' import useMouseHold from './useMouseHold.js' const MoleculeDataCounter = forwardRef( ( { addIcon = '+', charsSize, disabled, errorText: errorTextProps, id, inputDisabled = false, isLoading = false, label, max = 100, maxValueErrorText, maxValueHelpText, min = 0, minValueErrorText, minValueHelpText, onChange, size = inputSizes.MEDIUM, substractIcon = '-', initialValue, value, allowCustomValue = false }, ref ) => { let initialStateValue const numMax = Number(max) const numMin = Number(min) if (value !== undefined) { initialStateValue = Number(value) } else if (initialValue !== undefined) { initialStateValue = Number(initialValue) } else { initialStateValue = numMin + Math.trunc((numMax - numMin) / 2) } const [lastAction, setLastActions] = useState() const [internalValue, setInternalValue] = useState(initialStateValue) useEffect(() => { if (value !== undefined) { setInternalValue(Number(value)) } }, [value]) const isMaxValue = (number = internalValue) => number === numMax const isMinValue = (number = internalValue) => number === numMin const isLowerThanMinValue = (number = internalValue) => number < numMin const isHigherThanMaxValue = (number = internalValue) => number > numMax const decrementDisabled = disabled || internalValue <= numMin const incrementDisabled = disabled || internalValue >= numMax const sanitize = value => { if (!value) return 0 Iif (isNaN(value)) return 0 return value } const assignDiff = (event, {diff, lastAction}) => { setInternalValue(currentValue => { const finalValue = allowCustomValue && lastAction === 'change' ? sanitize(diff) : currentValue + diff typeof onChange === 'function' && onChange(event, { value: String(finalValue), action: lastAction }) return value === undefined ? finalValue : currentValue }) lastAction && setLastActions(lastAction) } const incrementValue = event => { const diff = isHigherThanMaxValue(internalValue + 1) ? 0 : 1 const lastAction = ACTIONS.MORE assignDiff(event, {diff, lastAction}) } const decrementValue = event => { const diff = isLowerThanMinValue(internalValue - 1) ? 0 : -1 const lastAction = ACTIONS.LESS assignDiff(event, {diff, lastAction}) } const handleChange = (event, {value}) => { if (allowCustomValue) { const parsedIntNewValue = parseInt(value, 10) assignDiff(event, { diff: parsedIntNewValue, lastAction: ACTIONS.CHANGE }) return } const parsedIntNewValue = parseInt(value, 10) const diffValue = isNaN(parsedIntNewValue) && 0 assignDiff(event, { diff: diffValue, lastAction: ACTIONS.CHANGE }) } const handleKeyDown = event => { const {key} = event Iif (key === 'ArrowUp') incrementValue(event) Iif (key === 'ArrowDown') decrementValue(event) } let helpText, errorText Eif (!disabled) { if (isMaxValue()) helpText = maxValueHelpText else if (isMinValue()) helpText = minValueHelpText else helpText = null Iif (isLowerThanMinValue()) errorText = minValueErrorText else if (isHigherThanMaxValue()) errorText = maxValueErrorText else Iif (errorTextProps) errorText = errorTextProps else errorText = null } return ( <div className={BASE_CLASS}> <MoleculeField name={id} label={label} helpText={helpText} errorText={errorText}> <div className={cx(CLASS_INPUT_CONTAINER, `${CLASS_INPUT_CONTAINER}--${size}`)}> <AtomButton design={atomButtonDesigns.OUTLINE} disabled={decrementDisabled} type="button" aria-label="substract" isLoading={isLoading && lastAction === ACTIONS.LESS} size={sizeConversor[size]} {...useMouseHold(decrementValue, { interval: 100, delay: 500, disabled: decrementDisabled })} > {substractIcon} </AtomButton> <AtomInput ref={ref} charsSize={charsSize || internalValue.toString().length <= 2 ? 2 : internalValue.toString().length} disabled={disabled || inputDisabled} id={id} isLoading={isLoading && lastAction === ACTIONS.CHANGE} onChange={handleChange} onKeyDown={handleKeyDown} size={size} value={internalValue} /> <AtomButton design={atomButtonDesigns.OUTLINE} disabled={incrementDisabled} type="button" aria-label="add" isLoading={isLoading && lastAction === ACTIONS.MORE} size={sizeConversor[size]} {...useMouseHold(incrementValue, { interval: 100, delay: 500, disabled: incrementDisabled })} > {addIcon} </AtomButton> </div> </MoleculeField> </div> ) } ) MoleculeDataCounter.displayName = 'MoleculeDataCounter' MoleculeDataCounter.propTypes = { /** Text to be displayed as label */ label: PropTypes.string, /** used as label for attribute and input element id */ id: PropTypes.string, /** width of input based in number of characters (native "size" attribute) */ charsSize: PropTypes.number.isRequired, /** text to display in case of error */ errorText: PropTypes.string, /** value of the control */ value: PropTypes.number, /** initial value of the control */ initialValue: PropTypes.number, /** max value allowed */ max: PropTypes.number, /** min value allowed */ min: PropTypes.number, /* callback to be called with every update of the input value */ onChange: PropTypes.func, /* HelpText to be displayed when value reaches minimun value */ minValueHelpText: PropTypes.string.isRequired, // /* ErrorText to be displayed when value is lower than minimun value */ minValueErrorText: PropTypes.string.isRequired, // /* HelpText to be displayed when value reaches maximum value */ maxValueHelpText: PropTypes.string.isRequired, // /* ErrorText to be displayed when value is lower than maximun value */ maxValueErrorText: PropTypes.string.isRequired, /* component disabled or not */ disabled: PropTypes.bool, /** 's', 'm' or 'l', default: 'm' */ size: PropTypes.oneOf(Object.values(inputSizes)), /** use to show loading icon on apply an action */ isLoading: PropTypes.bool, /** input disabled or not */ inputDisabled: PropTypes.bool, /** Icon to show on add button */ addIcon: PropTypes.node, /** Icon to show on substract button */ substractIcon: PropTypes.node, /** Flag to allow the input value to be editable */ allowCustomValue: PropTypes.bool } export default MoleculeDataCounter export {moleculeDataCounterSizes} |