|
| 1 | +import React, { useRef } from 'react'; |
| 2 | +import { styled, Box, BoxProps } from '@mui/material'; |
| 3 | + |
| 4 | +import CheckBoxSvgIcon from '../../../svg/CheckboxSvgIcon'; |
| 5 | + |
| 6 | +interface ContainerProps { |
| 7 | + disabled: boolean; |
| 8 | + isSelected: boolean; |
| 9 | + selectType: 'none' | 'one' | 'partial' | 'all'; |
| 10 | +} |
| 11 | + |
| 12 | +const Container = styled(Box, { |
| 13 | + shouldForwardProp: (prop) => { |
| 14 | + switch (prop) { |
| 15 | + case 'isSelected': |
| 16 | + case 'selectType': |
| 17 | + case 'sx': |
| 18 | + return false; |
| 19 | + default: |
| 20 | + return true; |
| 21 | + } |
| 22 | + }, |
| 23 | +})<ContainerProps>` |
| 24 | + cursor: pointer; |
| 25 | + display: inline-flex; |
| 26 | + align-items: center; |
| 27 | + user-select: none; |
| 28 | + pointer-events: ${({ disabled }) => (disabled ? 'none' : 'auto')}; |
| 29 | +
|
| 30 | + &:hover { |
| 31 | + & > .checkbox { |
| 32 | + border-color: ${({ theme }) => theme.color.primary.$60}; |
| 33 | + } |
| 34 | + } |
| 35 | +
|
| 36 | + & > .checkbox { |
| 37 | + display: block; |
| 38 | + width: 1.5rem; |
| 39 | + height: 1.5rem; |
| 40 | + border: 2px solid ${({ theme }) => theme.color.secondary.$40}; |
| 41 | + border-radius: 4px; |
| 42 | + margin: 8px; |
| 43 | + background: ${({ disabled, theme }) => |
| 44 | + disabled ? theme.color.secondary.$40 : '#fff'}; |
| 45 | + color: ${({ theme }) => theme.color.primary.$100}; |
| 46 | + opacity: ${({ disabled }) => (disabled ? 0.3 : 1)}; |
| 47 | +
|
| 48 | + ${({ isSelected, selectType }) => { |
| 49 | + if (isSelected && selectType !== 'partial') { |
| 50 | + return { |
| 51 | + border: 'none', |
| 52 | + }; |
| 53 | + } |
| 54 | + return undefined; |
| 55 | + }} |
| 56 | +
|
| 57 | + ${({ theme, isSelected, selectType }) => { |
| 58 | + if (isSelected) { |
| 59 | + if (selectType === 'partial') { |
| 60 | + return { |
| 61 | + padding: '4px', |
| 62 | + '&:before': { |
| 63 | + display: 'block', |
| 64 | + content: '""', |
| 65 | + width: '100%', |
| 66 | + height: '100%', |
| 67 | + background: theme.color.primary.$100, |
| 68 | + borderRadius: '1px', |
| 69 | + }, |
| 70 | + }; |
| 71 | + } |
| 72 | + } |
| 73 | + return undefined; |
| 74 | + }}; |
| 75 | + } |
| 76 | +
|
| 77 | + & > label { |
| 78 | + cursor: pointer; |
| 79 | + display: inline-block; |
| 80 | + margin-left: 8px; |
| 81 | + opacity: ${({ disabled }) => (disabled ? 0.6 : 1)}; |
| 82 | + } |
| 83 | +`; |
| 84 | + |
| 85 | +export interface CheckboxLightProps extends Omit<BoxProps, 'onChange'> { |
| 86 | + name?: string; |
| 87 | + label?: string; |
| 88 | + checked?: boolean; |
| 89 | + disabled?: boolean; |
| 90 | + selectType?: 'none' | 'one' | 'partial' | 'all'; |
| 91 | + onChange?: (checked: boolean) => void; |
| 92 | +} |
| 93 | + |
| 94 | +const CheckboxLight: React.FC<CheckboxLightProps> = ({ |
| 95 | + name, |
| 96 | + label, |
| 97 | + sx, |
| 98 | + checked = false, |
| 99 | + disabled = false, |
| 100 | + selectType = 'none', |
| 101 | + onChange, |
| 102 | +}) => { |
| 103 | + const checkboxRef = useRef<HTMLInputElement>(null); |
| 104 | + |
| 105 | + const handleOnClick = () => { |
| 106 | + onChange?.(!checked); |
| 107 | + }; |
| 108 | + |
| 109 | + return ( |
| 110 | + <Container |
| 111 | + ref={checkboxRef} |
| 112 | + sx={sx} |
| 113 | + isSelected={checked} |
| 114 | + disabled={disabled} |
| 115 | + selectType={selectType} |
| 116 | + onClick={handleOnClick} |
| 117 | + data-cy={`checkbox-list-device-item${disabled ? 'disabled' : ''}`} |
| 118 | + > |
| 119 | + <div className="checkbox"> |
| 120 | + {checked && selectType !== 'partial' && ( |
| 121 | + <CheckBoxSvgIcon sx={{ width: '1.5rem', height: '1.5rem' }} /> |
| 122 | + )} |
| 123 | + </div> |
| 124 | + {label && <label htmlFor={name}>{label}</label>} |
| 125 | + </Container> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +export default CheckboxLight; |
0 commit comments