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 | 1x 75x 75x 75x 75x 75x 128x 75x 75x 75x 75x 75x 16x 16x 16x 16x 16x 9x 9x 16x 9x 9x 9x 9x 9x 16x 16x 75x 16x 16x 16x 16x 16x 16x 9x 9x 16x 10x 10x 10x 10x 10x 16x 16x 75x 1x 1x | import {forwardRef, useRef} from 'react' import cx from 'classnames' import PropTypes from 'prop-types' import useControlledState from '@s-ui/react-hooks/lib/useControlledState' import useMergeRefs from '@s-ui/react-hooks/lib/useMergeRefs' import CheckboxIcon from './CheckboxIcon.js' import { BASE_CLASS, CHECKBOX_SIZES, CHECKBOX_STATUS, getIcon, getIsNative, getReadOnly, isFunction, pressedValue, updateStatus } from './config.js' const AtomCheckbox = forwardRef( ( { defaultChecked: defaultCheckedProp = false, checked: checkedProp, checkedIcon: CheckedIcon, uncheckedIcon: UncheckedIcon, disabled, readOnly: readOnlyProp, id, defaultIndeterminate: defaultIndeterminateProp = false, indeterminate: indeterminateProp, indeterminateIcon: IndeterminateIcon, icon: IconProp, iconSize, name: nameProp, onChange: onChangeFromProps, status, size = CHECKBOX_SIZES.MEDIUM, value, className, ...props }, forwardedRef ) => { const inputRef = useRef() const [checked, setChecked, isCheckedControlled] = useControlledState(checkedProp, defaultCheckedProp) const name = nameProp || id const [indeterminate, setIndeterminate, isIndeterminateControlled] = useControlledState( indeterminateProp, defaultIndeterminateProp ) const ref = useMergeRefs( node => updateStatus(node, { isChecked: checked, isIndeterminate: indeterminate }), inputRef, forwardedRef ) const isControlled = isCheckedControlled || isIndeterminateControlled const isNative = getIsNative( {checked, indeterminate}, {CheckedIcon, UncheckedIcon, IndeterminateIcon, Icon: IconProp} ) const readOnlyAttributes = getReadOnly({ readOnly: readOnlyProp, disabled, isNative }) const Icon = getIcon( {isNative, checked, indeterminate}, {CheckedIcon, UncheckedIcon, IndeterminateIcon, Icon: IconProp} ) const handleChange = ref => event => { Eif (!disabled) { const {name, value} = event.target let newChecked = isControlled ? indeterminate || !checked : event.target.checked let newIndeterminate = false if (!isControlled) { setChecked(newChecked) setIndeterminate(newIndeterminate) } if (readOnlyProp) { newChecked = checked newIndeterminate = indeterminate setChecked(checked) setIndeterminate(indeterminate) event.target.indeterminate = indeterminate } isFunction(onChangeFromProps) && onChangeFromProps(event, { name, value, checked: newChecked, indeterminate: newIndeterminate }) ref.current.focus() } } const handleClick = ref => event => { event.preventDefault() event.stopPropagation() Eif (!disabled) { let newChecked = indeterminate || !checked let newIndeterminate = false if (!isControlled) { setChecked(newChecked) setIndeterminate(newIndeterminate) } if (readOnlyProp) { newChecked = checked newIndeterminate = indeterminate setChecked(checked) setIndeterminate(indeterminate) event.target.indeterminate = indeterminate } isFunction(onChangeFromProps) && onChangeFromProps(event, { name, value, checked: newChecked, indeterminate: newIndeterminate }) ref.current.focus() } } return ( <span className={cx( BASE_CLASS, `${BASE_CLASS}--native-${isNative ? 'enabled' : 'disabled'}`, `${BASE_CLASS}--size-${size}`, className )} > <input ref={ref} type="checkbox" id={id} name={name || id} value={value} disabled={disabled} {...readOnlyAttributes} checked={checked} aria-checked={pressedValue({checked, indeterminate})} data-indeterminate={indeterminate ? 'true' : undefined} onChange={handleChange(inputRef)} {...{ ...(!isNative && {'aria-hidden': 'true'}), ...(Object.values(CHECKBOX_STATUS).includes(status) && { 'data-status': status }), ...props }} /> <CheckboxIcon disabled={disabled} size={iconSize || size} status={status} checked={checked} data-indeterminate={indeterminate} onClick={handleClick} isNative={isNative} icon={Icon} {...props} /> </span> ) } ) AtomCheckbox.displayName = 'AtomCheckbox' AtomCheckbox.propTypes = { /* The DOM id global attribute. */ id: PropTypes.string, /* Determine the size of the checkbox. (default: CHECKBOX_SIZES.MEDIUM) */ size: PropTypes.string, /** * Defines the value associated with the button's name when it's submitted with the form data. * This value is passed to the server in params when the form is submitted using this button. */ value: PropTypes.oneOfType([PropTypes.number, PropTypes.string, PropTypes.bool]), /* Name attribute for the input */ name: PropTypes.string, /* This Boolean attribute prevents the user from interacting with the input */ disabled: PropTypes.bool, /* A read-only field cannot be modified. However, a user can tab to it, highlight it, and copy the text from it. */ readOnly: PropTypes.bool, /* Mark the input as default initial selected */ defaultChecked: PropTypes.bool, /* Mark the input as selected */ checked: PropTypes.bool, /* AtomIcon when checkbox is checked */ checkedIcon: PropTypes.elementType, /* AtomIcon when checkbox is unchecked */ uncheckedIcon: PropTypes.elementType, /* Mark the input as default initial selected */ defaultIndeterminate: PropTypes.bool, /* Mark the input as indeterminate */ indeterminate: PropTypes.bool, /* AtomIcon when checkbox is intermediate */ indeterminateIcon: PropTypes.elementType, /** mandatory icon shown not depending on its state. Change it depending on the checkbox state to emulate the behavior. **/ icon: PropTypes.elementType, /* Determine the size of the icon. */ iconSize: PropTypes.string, /* onChange callback */ onChange: PropTypes.func, /* Will set a red/green/orange border if set to 'error' / 'success' / 'alert' */ status: PropTypes.oneOf(Object.values(CHECKBOX_STATUS)), /* Additional classes */ className: PropTypes.string } export default AtomCheckbox export {CHECKBOX_STATUS as atomCheckboxStatus} export {CHECKBOX_SIZES as atomCheckboxSizes} |