-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdropdown.component.tsx
More file actions
129 lines (123 loc) · 3.72 KB
/
Copy pathdropdown.component.tsx
File metadata and controls
129 lines (123 loc) · 3.72 KB
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
import React from 'react';
import classes from './dropdown.component.module.css';
import { ExpandDown } from '../icons/expand-down-icon.component';
import { DropdownOptionVm } from './dropdown.model';
import { SELECT_AN_OPTION } from './dropdown.const';
import { useA11ySelect } from '@/common/a11y';
import { Tick } from '../icons/tick-icon.component';
interface Props {
name: string;
options: DropdownOptionVm[];
value?: string[]; // Change to an array for multi-selection
onChange: (fields: string[]) => void; // Update to handle multiple selections
selectTitle?: string;
isError?: boolean;
multiSelect?: boolean; // Add multiSelect prop
}
export const Dropdown: React.FC<Props> = props => {
const {
name,
options,
selectTitle,
value = [],
onChange,
isError,
multiSelect,
} = props;
const findSelectedOptions = (value: string[] | undefined) => {
return options.filter(option => value?.includes(option.id));
};
const handleChange = (option: DropdownOptionVm | undefined) => {
if (!option) return; // Handle undefined case
if (multiSelect) {
// Handle multi-selection logic
const newValue = value.includes(option.id)
? value.filter(v => v !== option.id) // Remove if already selected
: [...value, option.id]; // Add if not selected
onChange(newValue);
} else {
// Handle single selection
onChange([option.id]);
}
};
const {
optionListRef,
buttonRef,
veilRef,
isOpen,
setIsOpen,
options: a11yOptions,
selectedOption,
onFocusOption,
} = useA11ySelect(
options,
option => option.id,
multiSelect ? undefined : findSelectedOptions(value)[0], // Handle single or multi-select
handleChange
);
return (
<>
<button
className={`${classes.selectSelect} ${isError && classes.selectError} ${isOpen && classes.selectActive}`}
ref={buttonRef}
id={`combobox-${name}`}
type="button"
role="combobox"
aria-haspopup="listbox"
aria-expanded={isOpen}
aria-controls={`listbox-${name}`}
aria-activedescendant={selectedOption?.id}
onClick={() => {
setIsOpen(!isOpen);
}}
aria-labelledby={name}
tabIndex={0}
>
<p className={classes.selectText}>
{multiSelect
? value
.map(id => options.find(option => option.id === id)?.label)
.join(', ') ||
selectTitle ||
SELECT_AN_OPTION
: selectedOption?.label || selectTitle || SELECT_AN_OPTION}
</p>
<ExpandDown />
{isOpen && (
<>
<div
ref={veilRef}
className={classes.veil}
aria-hidden="true"
></div>
<ul
id={`listbox-${name}`}
role="listbox"
aria-labelledby={name}
tabIndex={-1}
ref={optionListRef}
className={classes.options}
onClick={e => e.stopPropagation()}
>
{a11yOptions.map(option => (
<li
key={option.id}
role="option"
tabIndex={option.tabIndex}
aria-selected={value.includes(option.id)} // Check if the option is selected
onClick={() => handleChange(option)}
ref={onFocusOption(option)}
>
<div className={classes.svg} aria-hidden="true">
{value.includes(option.id) ? <Tick /> : ''}
</div>
{option.label}
</li>
))}
</ul>
</>
)}
</button>
</>
);
};