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 | 3x 16x 11x 5x 3x 16x 16x 16x 16x 16x 16x 16x 10x 6x 16x 3x 3x | import React, { useRef } from 'react' import * as PropTypes from 'prop-types' import { onChangeValue } from 'common/utils/general.utils' import './Input.scss' import { AiOutlineWarning } from 'react-icons/ai' import { BiX } from 'react-icons/bi' import { isMobileOrTablet } from 'common/utils/browser.utils' const ValidationMessage = ({ displayErrors, isValid, validation, value }) => { if (!validation) { return null } return ( displayErrors && !isValid && ( <aside role="alert" className="animate__animated animate__fadeIn input__warning"> <i className="input__warning--icon" role="presentation" aria-hidden="true"> <AiOutlineWarning size="16px" /> </i> <p className="input__warning--text">{validation.message(value)}</p> </aside> ) ) } const Input = ({ title, type, id, placeholder, value, displayErrors, onChange, isValid: isValidOuter, validation, list, disabled, showClearButton, onClear, translate, }) => { const inputRef = useRef() const isValid = !!validation ? validation.test(value) : !!isValidOuter const onBlur = () => onChange({ value, isValid, isTouched: value !== '' }) const emptyValue = () => { onChangeValue({ onChange, isValid, valueForced: '' })() onClear && onClear() } const onFocus = () => { if (isMobileOrTablet()) { inputRef.current.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start', }) } } const onKeyUp = () => { if (isMobileOrTablet()) { onBlur() } } const INPUT_ELEMENT = { text: () => ( <input id={id} onFocus={onFocus} ref={inputRef} disabled={disabled} onChange={onChangeValue({ onChange, isValid, isTouched: isMobileOrTablet() })} onKeyUp={onKeyUp} onBlur={onBlur} value={value} className="input__element--text" placeholder={placeholder} /> ), select: () => ( <> {!disabled && value !== '' && showClearButton && ( <div className="input__element--clear" role="button" aria-label={translate('Clear field value')} onClick={emptyValue} > <BiX /> </div> )} <select id={id} onFocus={onFocus} ref={inputRef} disabled={disabled} defaultValue={value} onChange={onChangeValue({ onChange, isValid })} className="input__element--select" > {!!placeholder && ( <option value="" selected={value === ''} disabled> {placeholder} </option> )} {list && list.map(({ value: innerValue, text }) => ( <option key={innerValue} value={innerValue}> {text} </option> ))} </select> </> ), } return ( <div className="input"> {!!title && ( <label className="input__title" htmlFor={id}> {title} </label> )} {INPUT_ELEMENT[type] && INPUT_ELEMENT[type]()} <ValidationMessage displayErrors={displayErrors} isValid={isValid} validation={validation} value={value} /> </div> ) } Input.defaultProps = { type: 'input', isValid: true, displayErrors: true, list: [], } Input.propTypes = { title: PropTypes.string, type: PropTypes.string.isRequired, id: PropTypes.string.isRequired, placeholder: PropTypes.string, value: PropTypes.string.isRequired, onChange: PropTypes.func.isRequired, isValid: PropTypes.bool, displayErrors: PropTypes.bool, disabled: PropTypes.bool, validation: PropTypes.object, list: PropTypes.array, } export default Input |