|
| 1 | +import React, { useMemo, useCallback } from 'react'; |
| 2 | +import { |
| 3 | + RiCheckboxLine, |
| 4 | + RiCheckboxIndeterminateLine, |
| 5 | + RiCheckboxBlankLine, |
| 6 | +} from 'react-icons/ri'; |
| 7 | +import { |
| 8 | + randomString, |
| 9 | + _cs, |
| 10 | +} from '@togglecorp/fujs'; |
| 11 | + |
| 12 | +import styles from './styles.scss'; |
| 13 | + |
| 14 | +interface Props { |
| 15 | + className?: string; |
| 16 | + labelClassName?: string; |
| 17 | + checkIconClassName?: string; |
| 18 | + |
| 19 | + disabled?: boolean; |
| 20 | + value: boolean; |
| 21 | + onChange: (value: boolean) => void; |
| 22 | + label?: string | number; |
| 23 | + tooltip?: string; |
| 24 | + readOnly?: boolean; |
| 25 | + |
| 26 | + // NOTE: if value is false and indetermiate is true, show a filled checkbox |
| 27 | + indeterminate?: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +const Checkbox = (props: Props) => { |
| 31 | + const { |
| 32 | + label, |
| 33 | + tooltip, |
| 34 | + className: classNameFromProps, |
| 35 | + value, |
| 36 | + disabled, |
| 37 | + readOnly, |
| 38 | + onChange, |
| 39 | + checkIconClassName, |
| 40 | + labelClassName: labelClassNameFromProps, |
| 41 | + indeterminate, |
| 42 | + ...otherProps |
| 43 | + } = props; |
| 44 | + |
| 45 | + const inputId = useMemo( |
| 46 | + () => randomString(16), |
| 47 | + [], |
| 48 | + ); |
| 49 | + |
| 50 | + const handleChange = useCallback( |
| 51 | + (e) => { |
| 52 | + const v = e.target.checked; |
| 53 | + onChange(v); |
| 54 | + }, |
| 55 | + [onChange], |
| 56 | + ); |
| 57 | + |
| 58 | + const className = _cs( |
| 59 | + styles.checkbox, |
| 60 | + 'checkbox', |
| 61 | + classNameFromProps, |
| 62 | + (value || indeterminate) && styles.checked, |
| 63 | + (value || indeterminate) && 'checked', |
| 64 | + disabled && styles.disabled, |
| 65 | + disabled && 'disabled', |
| 66 | + readOnly && styles.readOnly, |
| 67 | + readOnly && 'read-only', |
| 68 | + ); |
| 69 | + |
| 70 | + const iconName = _cs( |
| 71 | + styles.checkmark, |
| 72 | + 'checkmark', |
| 73 | + checkIconClassName, |
| 74 | + ); |
| 75 | + |
| 76 | + const inputClassName = _cs( |
| 77 | + 'input', |
| 78 | + styles.input, |
| 79 | + ); |
| 80 | + |
| 81 | + const labelClassName = _cs( |
| 82 | + 'label', |
| 83 | + styles.label, |
| 84 | + labelClassNameFromProps, |
| 85 | + ); |
| 86 | + |
| 87 | + const Icon = (value && RiCheckboxLine) |
| 88 | + || (indeterminate && RiCheckboxIndeterminateLine) |
| 89 | + || RiCheckboxBlankLine; |
| 90 | + |
| 91 | + return ( |
| 92 | + <label |
| 93 | + htmlFor={inputId} |
| 94 | + className={className} |
| 95 | + title={tooltip} |
| 96 | + > |
| 97 | + <Icon |
| 98 | + className={iconName} |
| 99 | + /> |
| 100 | + <input |
| 101 | + id={inputId} |
| 102 | + onChange={handleChange} |
| 103 | + className={inputClassName} |
| 104 | + type="checkbox" |
| 105 | + checked={value} |
| 106 | + disabled={disabled || readOnly} |
| 107 | + {...otherProps} |
| 108 | + /> |
| 109 | + <div className={labelClassName}> |
| 110 | + { label } |
| 111 | + </div> |
| 112 | + </label> |
| 113 | + ); |
| 114 | +}; |
| 115 | + |
| 116 | +Checkbox.defaultProps = { |
| 117 | + disabled: false, |
| 118 | + readOnly: false, |
| 119 | + value: false, |
| 120 | +}; |
| 121 | + |
| 122 | +export default Checkbox; |
0 commit comments