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 | 1x 28x 28x 28x 28x 28x 28x 28x 27x 2x 1x 1x 1x 28x 5x 5x 5x 3x 4x 3x 5x 5x 5x 28x 10x 10x 28x 27x 27x 27x 1x 1x | import {forwardRef} from 'react' import cx from 'classnames' import PropTypes from 'prop-types' import AtomInput, {inputSizes} from '@s-ui/react-atom-input' import AtomTag, {atomTagDesigns, atomTagSizes} from '@s-ui/react-atom-tag' import useControlledState from '@s-ui/react-hooks/lib/useControlledState' import useMergeRefs from '@s-ui/react-hooks/lib/useMergeRefs' import { BASE_CLASS_TAG_CONTAINER, CLASS_TAGS, CLASS_TAGS_DISABLED, CLASS_TAGS_ERROR, CLASS_TAGS_SUCCESS, handleOnFocusBlur, isDuplicate, isFunction } from './config.js' const MoleculeInputTags = forwardRef( ( { errorState, innerRefInput, // might be deprecated onChange: onInputChange, onChangeTags, optionsData, size = inputSizes.MEDIUM, tagSize = atomTagSizes.MEDIUM, defaultTags = [], tags: tagsFromProps, tagDesign, tagsCloseIcon, tagCloseLabel, defaultValue = '', value: valueFromProps, maxTags, placeholder = '', disabled = false, allowDuplicates = true, readOnly, name, isBorderless, responsive = true, onFocus, onBlur, ...restProps }, forwardedRef ) => { const ref = useMergeRefs(...[forwardedRef, innerRefInput].filter(Boolean)) const [tags, setTags] = useControlledState(tagsFromProps, defaultTags) const [value, setValue] = useControlledState(valueFromProps, defaultValue) const isFull = maxTags && tags?.length >= maxTags const isEmpty = tags.length === 0 const className = cx( CLASS_TAGS, { // [CLASS_TAGS_FOCUS]: focus === true, [CLASS_TAGS_ERROR]: errorState === true, [CLASS_TAGS_SUCCESS]: errorState === false, [CLASS_TAGS_DISABLED]: disabled === true || isFull === true, [`${CLASS_TAGS}-${size}`]: size }, isBorderless && `${CLASS_TAGS}--isBorderless` ) const removeTag = ({id: indexTag, label, value: removedValue}) => ev => { let nextTags = tags.filter((_, i) => i !== indexTag) Iif (optionsData) { const keys = Object.keys(optionsData) nextTags = keys.filter(key => nextTags.includes(optionsData[key])) } setTags(nextTags) isFunction(onChangeTags) && onChangeTags(ev, { name, tags: nextTags, value, tag: removedValue, label }) } const addTag = (ev, {value, ...args}) => { Eif (value) { const nextTags = [...tags] let options if (allowDuplicates || !isDuplicate(tags, value)) { if (optionsData) { options = optionsData.filter(optionData => optionData.label === value)[0] } nextTags.push(options || value) } setTags(nextTags) setValue('') isFunction(onChangeTags) && onChangeTags(ev, { name, tags: nextTags, value: '', tag: value, label: value }) } } const handleInputChange = (ev, {value, ...args}) => { setValue(value) isFunction(onInputChange) && onInputChange(ev, {...args, value, tags}) } return ( <div className={className} {...(disabled && {'aria-disabled': disabled})} {...(readOnly && !disabled && {'aria-readonly': readOnly})} > {tags.map((value, index) => { const label = typeof value === 'object' ? value.label : value const key = typeof value === 'object' ? value.key : index return ( <span key={key} className={cx(`${BASE_CLASS_TAG_CONTAINER}`, `${BASE_CLASS_TAG_CONTAINER}-size-${tagSize}`)} > <AtomTag id={index} closeIcon={tagsCloseIcon} value={value} design={tagDesign} onClose={removeTag({ id: key === undefined ? index : key, value, label })} label={label} size={tagSize} responsive={responsive} readOnly={readOnly} disabled={disabled} closeLabel={tagCloseLabel} /> </span> ) })} {!isFull && ( <AtomInput ref={ref} {...restProps} name={name} value={value} onChange={handleInputChange} onEnter={addTag} onEnterKey={['Enter']} onFocus={handleOnFocusBlur(onFocus, {tags})} onBlur={handleOnFocusBlur(onBlur, {tags})} noBorder size={size} readOnly={readOnly} disabled={disabled} placeholder={isEmpty ? placeholder : undefined} /> )} </div> ) } ) MoleculeInputTags.displayName = 'MoleculeInputTags' MoleculeInputTags.propTypes = { /* errorState */ errorState: PropTypes.bool, /** Tag size */ tagSize: PropTypes.oneOf(Object.values(atomTagSizes)), /** Input size */ size: PropTypes.oneOf(Object.values(inputSizes)), /* close icon to be displayed on tags */ tagsCloseIcon: PropTypes.node.isRequired, /* list of value/text tuple to be handled */ optionsData: PropTypes.object, /* list of values displayed as tags on first render */ defaultTags: PropTypes.array, /* list of values displayed as tags */ tags: PropTypes.array, /* value of the input on first render */ defaultValue: PropTypes.string, /* value of the input */ value: PropTypes.string, /* callback to be called with every update of the list of tags */ onChangeTags: PropTypes.func, /* callback to be called with every update of the input value */ onChange: PropTypes.func, /* object generated w/ React.createRef method to get a DOM reference of internal input */ innerRefInput: PropTypes.object, /* text to be displayed if there are no tags and the input is empty */ placeholder: PropTypes.string, /* number of maximum tags that can be added, after reaching this number the component will be disabled */ maxTags: PropTypes.number, /* prop to indicate that the field is disabled (will not render the input) */ disabled: PropTypes.bool, /* This Boolean attribute prevents the user from interacting with the input but without disabled styles */ readOnly: PropTypes.bool, /* prop to determinate if the field allows introducing duplicate values for the tags (case insensitive) */ allowDuplicates: PropTypes.bool, /* input name */ name: PropTypes.string, /* true for make responsive layout (default). Keeps large tag size in mobile */ responsive: PropTypes.bool, /* prop to determinate if the field is borderless */ isBorderless: PropTypes.bool, /** callback triggered when the user focuses on the inputTag */ onFocus: PropTypes.func, /** callback triggered when the user focuses out on the inputTag */ onBlur: PropTypes.func, /** the design to apply to the tags */ tagDesign: PropTypes.oneOf(Object.values(atomTagDesigns)), /** label for the clear icons of the tags */ tagCloseLabel: PropTypes.string } export default MoleculeInputTags export {inputSizes, inputSizes as moleculeInputTagsInputSizes, atomTagSizes as moleculeInputTagsSizes} |