|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import { nanoid } from 'nanoid'; |
| 3 | +import { |
| 4 | + StyledCheckbox, |
| 5 | + StyledCheckboxContainer, |
| 6 | + StyledColumn, |
| 7 | + StyledContainer, |
| 8 | + StyledLeftColumn, |
| 9 | + StyledRow, |
| 10 | +} from './CheckboxStyle'; |
| 11 | +import { noop } from 'lodash-es'; |
| 12 | +import { Icon } from '../Icon'; |
| 13 | +import { Typography } from '../Typography'; |
| 14 | +import { Neutral, Red, Blue } from '../utilities/colors'; |
| 15 | + |
| 16 | +export interface CheckboxProps |
| 17 | + extends Omit< |
| 18 | + React.HtmlHTMLAttributes<HTMLInputElement>, |
| 19 | + 'type' | 'onChange' |
| 20 | + > { |
| 21 | + id?: string; |
| 22 | + label?: string; |
| 23 | + hasError?: boolean; |
| 24 | + indeterminate?: boolean; |
| 25 | + checked?: boolean; |
| 26 | + disabled?: boolean; |
| 27 | + helpText?: string; |
| 28 | + onChange?(newChecked: boolean, id: string): void; |
| 29 | +} |
| 30 | + |
| 31 | +const randomId = nanoid(); |
| 32 | + |
| 33 | +export const Checkbox = ({ |
| 34 | + label, |
| 35 | + id, |
| 36 | + checked, |
| 37 | + onChange, |
| 38 | + disabled, |
| 39 | + indeterminate, |
| 40 | + hasError, |
| 41 | + helpText, |
| 42 | + ...otherProps |
| 43 | +}: CheckboxProps) => { |
| 44 | + const checkBoxId = id ? id : randomId; |
| 45 | + const labelId = `label-${checkBoxId}`; |
| 46 | + |
| 47 | + const inputNode = useRef<HTMLInputElement>(null); |
| 48 | + const [checkedState, setCheckedState] = useState<'mixed' | boolean>(false); |
| 49 | + const [isFocused, setIsFocused] = useState(false); |
| 50 | + const checkedIcon = indeterminate |
| 51 | + ? 'ri-checkbox-indeterminate-fill' |
| 52 | + : 'ri-checkbox-fill'; |
| 53 | + const iconFill = disabled ? Neutral.B95 : hasError ? Red.B93 : Blue.S99; |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + const value = indeterminate ? 'mixed' : checked; |
| 57 | + setCheckedState(value); |
| 58 | + }, [checked, indeterminate]); |
| 59 | + |
| 60 | + const handleClick = () => { |
| 61 | + if (!onChange || !inputNode.current || disabled) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const checkedValue = !checkedState; |
| 66 | + setCheckedState(checkedValue); |
| 67 | + onChange(checkedValue, checkBoxId); |
| 68 | + }; |
| 69 | + |
| 70 | + return ( |
| 71 | + <StyledContainer aria-disabled={disabled}> |
| 72 | + <StyledRow> |
| 73 | + <StyledLeftColumn> |
| 74 | + <StyledCheckboxContainer> |
| 75 | + <StyledCheckbox |
| 76 | + role="checkbox" |
| 77 | + aria-labelledby={labelId} |
| 78 | + aria-checked={checkedState} |
| 79 | + data-focus={isFocused} |
| 80 | + onClick={() => handleClick()} |
| 81 | + onMouseDown={() => setIsFocused(false)} |
| 82 | + onMouseUp={() => setIsFocused(false)} |
| 83 | + hasError={hasError} |
| 84 | + > |
| 85 | + <input |
| 86 | + ref={inputNode} |
| 87 | + disabled={disabled} |
| 88 | + id={checkBoxId} |
| 89 | + type="checkbox" |
| 90 | + onChange={noop} |
| 91 | + checked={checked} |
| 92 | + onFocus={() => setIsFocused(true)} |
| 93 | + onBlur={() => setIsFocused(false)} |
| 94 | + {...otherProps} |
| 95 | + /> |
| 96 | + </StyledCheckbox> |
| 97 | + <Icon |
| 98 | + name={checkedIcon} |
| 99 | + height="20px" |
| 100 | + width="20px" |
| 101 | + fill={iconFill} |
| 102 | + /> |
| 103 | + </StyledCheckboxContainer> |
| 104 | + </StyledLeftColumn> |
| 105 | + <StyledColumn> |
| 106 | + <label |
| 107 | + id={labelId} |
| 108 | + htmlFor={checkBoxId} |
| 109 | + onClick={e => { |
| 110 | + e.preventDefault(); |
| 111 | + handleClick(); |
| 112 | + }} |
| 113 | + onMouseDown={e => e.preventDefault()} |
| 114 | + onMouseUp={e => e.preventDefault()} |
| 115 | + > |
| 116 | + { |
| 117 | + <Typography as="div" variant="body1"> |
| 118 | + {label} |
| 119 | + </Typography> |
| 120 | + } |
| 121 | + </label> |
| 122 | + </StyledColumn> |
| 123 | + </StyledRow> |
| 124 | + <StyledRow> |
| 125 | + <StyledLeftColumn></StyledLeftColumn> |
| 126 | + <StyledColumn className="help-text"> |
| 127 | + <Typography as="span" variant="subtitle2"> |
| 128 | + {helpText} |
| 129 | + </Typography> |
| 130 | + </StyledColumn> |
| 131 | + </StyledRow> |
| 132 | + </StyledContainer> |
| 133 | + ); |
| 134 | +}; |
0 commit comments