|
| 1 | +import { IcCaretDown, IcCaretUp } from '@/assets/IconList' |
| 2 | +import clsx from 'clsx' |
| 3 | + |
| 4 | +import { Box } from '@/components/common/containers' |
| 5 | +import { Dropdown, useDropdownContext } from '@/components/common/dropdown' |
| 6 | +import { CheckboxInput } from '@/components/common/input' |
| 7 | + |
| 8 | +type Option = { |
| 9 | + label: string |
| 10 | + value: string |
| 11 | +} |
| 12 | + |
| 13 | +interface SelectProps { |
| 14 | + options: Option[] |
| 15 | + values: string[] |
| 16 | + onChange: React.Dispatch<React.SetStateAction<string[]>> |
| 17 | + placeholder?: string |
| 18 | + disabled?: boolean |
| 19 | +} |
| 20 | +export const MultiSelect = ({ |
| 21 | + options, |
| 22 | + values, |
| 23 | + onChange, |
| 24 | + placeholder = 'Select an option', |
| 25 | + disabled = false, |
| 26 | +}: SelectProps): JSX.Element => { |
| 27 | + const selectedOptions = options.filter(option => |
| 28 | + values.includes(option.value) |
| 29 | + ) |
| 30 | + const selectedLabel = selectedOptions[0]?.value |
| 31 | + ? `${selectedOptions[0]?.value}` + |
| 32 | + (selectedOptions.length - 1 |
| 33 | + ? ` 외 ${selectedOptions.length - 1}개 선택` |
| 34 | + : '') |
| 35 | + : '' |
| 36 | + |
| 37 | + const handleSelect = (value: string) => { |
| 38 | + onChange(prev => |
| 39 | + prev.includes(value) ? prev.filter(v => v !== value) : [...prev, value] |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + const triggerStyle = clsx({ |
| 44 | + 'pointer-events-none cursor-not-allowed': disabled, |
| 45 | + }) |
| 46 | + |
| 47 | + return ( |
| 48 | + <Dropdown> |
| 49 | + <Dropdown.Trigger className={triggerStyle} aria-disabled={disabled}> |
| 50 | + <DropdownTriggerBox |
| 51 | + label={selectedLabel || placeholder} |
| 52 | + isSelected={!!selectedLabel} |
| 53 | + isDisabled={disabled} |
| 54 | + /> |
| 55 | + </Dropdown.Trigger> |
| 56 | + <Dropdown.Menu> |
| 57 | + {options.map(option => ( |
| 58 | + <Dropdown.Item |
| 59 | + key={option.value} |
| 60 | + closeOnSelect={false} |
| 61 | + onClick={() => handleSelect(option.value)} |
| 62 | + > |
| 63 | + <CheckboxInput |
| 64 | + label={option.label} |
| 65 | + onClick={() => handleSelect(option.value)} |
| 66 | + variant='checkbox' |
| 67 | + checked={selectedOptions.some( |
| 68 | + selectedOption => selectedOption.value === option.value |
| 69 | + )} |
| 70 | + className='ml-4 cursor-pointer' |
| 71 | + /> |
| 72 | + </Dropdown.Item> |
| 73 | + ))} |
| 74 | + </Dropdown.Menu> |
| 75 | + </Dropdown> |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +interface DropdownTriggerBoxProps { |
| 80 | + label: string |
| 81 | + isSelected: boolean |
| 82 | + isDisabled: boolean |
| 83 | +} |
| 84 | + |
| 85 | +const DropdownTriggerBox = ({ |
| 86 | + label, |
| 87 | + isSelected, |
| 88 | + isDisabled, |
| 89 | +}: DropdownTriggerBoxProps) => { |
| 90 | + const { isOpen } = useDropdownContext() |
| 91 | + const triggerBoxClass = clsx( |
| 92 | + 'h-48 w-210 justify-between p-12 text-body1 font-medium text-gray-500 focus:border-primary-normal', |
| 93 | + { 'text-gray-800': isSelected }, |
| 94 | + { 'bg-gray-200 text-gray-400': isDisabled } |
| 95 | + ) |
| 96 | + |
| 97 | + return ( |
| 98 | + <Box className={triggerBoxClass} rounded={8}> |
| 99 | + {label} |
| 100 | + {isOpen ? <IcCaretUp /> : <IcCaretDown />} |
| 101 | + </Box> |
| 102 | + ) |
| 103 | +} |
0 commit comments