@sonamu-kit/react-components 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/index.d.ts +65 -0
- package/dist/components/ui/accordion.d.ts +7 -0
- package/dist/components/ui/alert-dialog.d.ts +20 -0
- package/dist/components/ui/alert.d.ts +8 -0
- package/dist/components/ui/aspect-ratio.d.ts +3 -0
- package/dist/components/ui/async-select.d.ts +36 -0
- package/dist/components/ui/avatar.d.ts +6 -0
- package/dist/components/ui/badge.d.ts +9 -0
- package/dist/components/ui/breadcrumb.d.ts +19 -0
- package/dist/components/ui/button.d.ts +13 -0
- package/dist/components/ui/calendar.d.ts +5 -0
- package/dist/components/ui/card.d.ts +9 -0
- package/dist/components/ui/carousel.d.ts +18 -0
- package/dist/components/ui/checkbox.d.ts +8 -0
- package/dist/components/ui/collapsible.d.ts +5 -0
- package/dist/components/ui/combobox.d.ts +20 -0
- package/dist/components/ui/command.d.ts +80 -0
- package/dist/components/ui/common-modal.d.ts +28 -0
- package/dist/components/ui/context-menu.d.ts +27 -0
- package/dist/components/ui/date-input.d.ts +7 -0
- package/dist/components/ui/date-picker.d.ts +26 -0
- package/dist/components/ui/date-selector-multiple.d.ts +38 -0
- package/dist/components/ui/dialog.d.ts +19 -0
- package/dist/components/ui/drawer.d.ts +22 -0
- package/dist/components/ui/dropdown-menu.d.ts +27 -0
- package/dist/components/ui/form.d.ts +23 -0
- package/dist/components/ui/hover-card.d.ts +6 -0
- package/dist/components/ui/image-uploader.d.ts +14 -0
- package/dist/components/ui/input-otp.d.ts +34 -0
- package/dist/components/ui/input.d.ts +7 -0
- package/dist/components/ui/label.d.ts +5 -0
- package/dist/components/ui/menubar.d.ts +28 -0
- package/dist/components/ui/month-picker-multiple.d.ts +41 -0
- package/dist/components/ui/multi-image-uploader.d.ts +15 -0
- package/dist/components/ui/multi-select.d.ts +229 -0
- package/dist/components/ui/navigation-menu.d.ts +12 -0
- package/dist/components/ui/pagination.d.ts +10 -0
- package/dist/components/ui/popover.d.ts +7 -0
- package/dist/components/ui/progress.d.ts +4 -0
- package/dist/components/ui/radio-group.d.ts +5 -0
- package/dist/components/ui/resizable.d.ts +23 -0
- package/dist/components/ui/scroll-area.d.ts +5 -0
- package/dist/components/ui/select.d.ts +20 -0
- package/dist/components/ui/separator.d.ts +4 -0
- package/dist/components/ui/sheet.d.ts +25 -0
- package/dist/components/ui/sidebar.d.ts +69 -0
- package/dist/components/ui/skeleton.d.ts +2 -0
- package/dist/components/ui/slider.d.ts +8 -0
- package/dist/components/ui/sonner.d.ts +4 -0
- package/dist/components/ui/switch.d.ts +8 -0
- package/dist/components/ui/table.d.ts +24 -0
- package/dist/components/ui/tabs.d.ts +7 -0
- package/dist/components/ui/textarea.d.ts +7 -0
- package/dist/components/ui/toast.d.ts +15 -0
- package/dist/components/ui/toaster.d.ts +1 -0
- package/dist/components/ui/toggle-group.d.ts +12 -0
- package/dist/components/ui/toggle.d.ts +12 -0
- package/dist/components/ui/tooltip.d.ts +7 -0
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/use-toast.d.ts +44 -0
- package/dist/index.d.ts +3 -0
- package/dist/lib/caster.d.ts +3 -0
- package/dist/lib/helpers.d.ts +72 -0
- package/dist/lib/index.d.ts +6 -0
- package/{src/lib/lazy-upload.ts → dist/lib/lazy-upload.d.ts} +1 -12
- package/dist/lib/use-mobile.d.ts +1 -0
- package/dist/lib/utils.d.ts +2 -0
- package/dist/react-components.es.js +28375 -0
- package/package.json +105 -76
- package/COMPONENTS_LIST.md +0 -106
- package/COMPONENTS_STATUS.md +0 -114
- package/HELPERS_GUIDE.md +0 -489
- package/MIGRATION_PLAN.md +0 -404
- package/SETUP_GUIDE.md +0 -125
- package/components.json +0 -21
- package/postcss.config.js +0 -6
- package/src/components/index.ts +0 -315
- package/src/components/ui/accordion.tsx +0 -54
- package/src/components/ui/alert-dialog.tsx +0 -115
- package/src/components/ui/alert.tsx +0 -49
- package/src/components/ui/aspect-ratio.tsx +0 -5
- package/src/components/ui/async-select.tsx +0 -186
- package/src/components/ui/avatar.tsx +0 -45
- package/src/components/ui/badge.tsx +0 -38
- package/src/components/ui/breadcrumb.tsx +0 -102
- package/src/components/ui/button.tsx +0 -54
- package/src/components/ui/calendar.tsx +0 -193
- package/src/components/ui/card.tsx +0 -65
- package/src/components/ui/carousel.tsx +0 -243
- package/src/components/ui/checkbox.tsx +0 -67
- package/src/components/ui/collapsible.tsx +0 -9
- package/src/components/ui/combobox.tsx +0 -135
- package/src/components/ui/command.tsx +0 -143
- package/src/components/ui/common-modal.tsx +0 -95
- package/src/components/ui/context-menu.tsx +0 -189
- package/src/components/ui/date-picker.tsx +0 -112
- package/src/components/ui/date-selector-multiple.tsx +0 -197
- package/src/components/ui/dialog.tsx +0 -104
- package/src/components/ui/drawer.tsx +0 -100
- package/src/components/ui/dropdown-menu.tsx +0 -189
- package/src/components/ui/form.tsx +0 -171
- package/src/components/ui/hover-card.tsx +0 -27
- package/src/components/ui/image-uploader.tsx +0 -251
- package/src/components/ui/input-otp.tsx +0 -69
- package/src/components/ui/input.tsx +0 -38
- package/src/components/ui/label.tsx +0 -19
- package/src/components/ui/menubar.tsx +0 -231
- package/src/components/ui/month-picker-multiple.tsx +0 -351
- package/src/components/ui/multi-image-uploader.tsx +0 -283
- package/src/components/ui/multi-select.tsx +0 -1143
- package/src/components/ui/navigation-menu.tsx +0 -120
- package/src/components/ui/pagination.tsx +0 -72
- package/src/components/ui/popover.tsx +0 -42
- package/src/components/ui/progress.tsx +0 -25
- package/src/components/ui/radio-group.tsx +0 -38
- package/src/components/ui/resizable.tsx +0 -42
- package/src/components/ui/scroll-area.tsx +0 -46
- package/src/components/ui/select.tsx +0 -235
- package/src/components/ui/separator.tsx +0 -24
- package/src/components/ui/sheet.tsx +0 -119
- package/src/components/ui/sidebar.tsx +0 -683
- package/src/components/ui/skeleton.tsx +0 -7
- package/src/components/ui/slider.tsx +0 -57
- package/src/components/ui/sonner.tsx +0 -39
- package/src/components/ui/switch.tsx +0 -63
- package/src/components/ui/table.tsx +0 -94
- package/src/components/ui/tabs.tsx +0 -53
- package/src/components/ui/textarea.tsx +0 -34
- package/src/components/ui/toast.tsx +0 -122
- package/src/components/ui/toaster.tsx +0 -29
- package/src/components/ui/toggle-group.tsx +0 -55
- package/src/components/ui/toggle.tsx +0 -41
- package/src/components/ui/tooltip.tsx +0 -28
- package/src/hooks/index.ts +0 -2
- package/src/hooks/use-toast.ts +0 -189
- package/src/icons.d.ts +0 -1
- package/src/index.ts +0 -4
- package/src/lib/caster.ts +0 -66
- package/src/lib/helpers.ts +0 -394
- package/src/lib/index.ts +0 -31
- package/src/lib/use-mobile.ts +0 -19
- package/src/lib/utils.ts +0 -6
- package/src/styles/globals.css +0 -658
- package/tailwind.config.ts +0 -8
- package/tsconfig.json +0 -31
- package/tsconfig.node.json +0 -11
|
@@ -1,1143 +0,0 @@
|
|
|
1
|
-
import { cva, type VariantProps } from "class-variance-authority";
|
|
2
|
-
import * as React from "react";
|
|
3
|
-
import CheckIcon from "~icons/lucide/check";
|
|
4
|
-
import ChevronDownIcon from "~icons/lucide/chevron-down";
|
|
5
|
-
import WandSparklesIcon from "~icons/lucide/wand-sparkles";
|
|
6
|
-
import XIcon from "~icons/lucide/x";
|
|
7
|
-
import XCircleIcon from "~icons/lucide/x-circle";
|
|
8
|
-
|
|
9
|
-
import { cn } from "../../lib/utils";
|
|
10
|
-
import { Badge } from "./badge";
|
|
11
|
-
import { Button } from "./button";
|
|
12
|
-
import {
|
|
13
|
-
Command,
|
|
14
|
-
CommandEmpty,
|
|
15
|
-
CommandGroup,
|
|
16
|
-
CommandInput,
|
|
17
|
-
CommandItem,
|
|
18
|
-
CommandList,
|
|
19
|
-
CommandSeparator,
|
|
20
|
-
} from "./command";
|
|
21
|
-
import { Popover, PopoverContent, PopoverTrigger } from "./popover";
|
|
22
|
-
import { Separator } from "./separator";
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Animation types and configurations
|
|
26
|
-
*/
|
|
27
|
-
export interface AnimationConfig {
|
|
28
|
-
/** Badge animation type */
|
|
29
|
-
badgeAnimation?: "bounce" | "pulse" | "wiggle" | "fade" | "slide" | "none";
|
|
30
|
-
/** Popover animation type */
|
|
31
|
-
popoverAnimation?: "scale" | "slide" | "fade" | "flip" | "none";
|
|
32
|
-
/** Option hover animation type */
|
|
33
|
-
optionHoverAnimation?: "highlight" | "scale" | "glow" | "none";
|
|
34
|
-
/** Animation duration in seconds */
|
|
35
|
-
duration?: number;
|
|
36
|
-
/** Animation delay in seconds */
|
|
37
|
-
delay?: number;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Variants for the multi-select component to handle different styles.
|
|
42
|
-
* Uses class-variance-authority (cva) to define different styles based on "variant" prop.
|
|
43
|
-
*/
|
|
44
|
-
const multiSelectVariants = cva("m-1 transition-all duration-300 ease-in-out", {
|
|
45
|
-
variants: {
|
|
46
|
-
variant: {
|
|
47
|
-
default: "border-foreground/10 text-foreground bg-card hover:bg-card/80",
|
|
48
|
-
secondary:
|
|
49
|
-
"border-foreground/10 bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
|
50
|
-
destructive:
|
|
51
|
-
"border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80",
|
|
52
|
-
inverted: "inverted",
|
|
53
|
-
},
|
|
54
|
-
badgeAnimation: {
|
|
55
|
-
bounce: "hover:-translate-y-1 hover:scale-110",
|
|
56
|
-
pulse: "hover:animate-pulse",
|
|
57
|
-
wiggle: "hover:animate-wiggle",
|
|
58
|
-
fade: "hover:opacity-80",
|
|
59
|
-
slide: "hover:translate-x-1",
|
|
60
|
-
none: "",
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
defaultVariants: {
|
|
64
|
-
variant: "default",
|
|
65
|
-
badgeAnimation: "bounce",
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Option interface for MultiSelect component
|
|
71
|
-
*/
|
|
72
|
-
interface MultiSelectOption {
|
|
73
|
-
/** The text to display for the option. */
|
|
74
|
-
label: string;
|
|
75
|
-
/** The unique value associated with the option. */
|
|
76
|
-
value: string;
|
|
77
|
-
/** Optional icon component to display alongside the option. */
|
|
78
|
-
icon?: React.ComponentType<{ className?: string }>;
|
|
79
|
-
/** Whether this option is disabled */
|
|
80
|
-
disabled?: boolean;
|
|
81
|
-
/** Custom styling for the option */
|
|
82
|
-
style?: {
|
|
83
|
-
/** Custom badge color */
|
|
84
|
-
badgeColor?: string;
|
|
85
|
-
/** Custom icon color */
|
|
86
|
-
iconColor?: string;
|
|
87
|
-
/** Gradient background for badge */
|
|
88
|
-
gradient?: string;
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Group interface for organizing options
|
|
94
|
-
*/
|
|
95
|
-
interface MultiSelectGroup {
|
|
96
|
-
/** Group heading */
|
|
97
|
-
heading: string;
|
|
98
|
-
/** Options in this group */
|
|
99
|
-
options: MultiSelectOption[];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Props for MultiSelect component
|
|
104
|
-
*/
|
|
105
|
-
interface MultiSelectProps
|
|
106
|
-
extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "animationConfig">,
|
|
107
|
-
VariantProps<typeof multiSelectVariants> {
|
|
108
|
-
/**
|
|
109
|
-
* An array of option objects or groups to be displayed in the multi-select component.
|
|
110
|
-
*/
|
|
111
|
-
options: MultiSelectOption[] | MultiSelectGroup[];
|
|
112
|
-
/**
|
|
113
|
-
* Callback function triggered when the selected values change.
|
|
114
|
-
* Receives an array of the new selected values.
|
|
115
|
-
*/
|
|
116
|
-
onValueChange: (value: string[]) => void;
|
|
117
|
-
|
|
118
|
-
/** The default selected values when the component mounts. */
|
|
119
|
-
defaultValue?: string[];
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Placeholder text to be displayed when no values are selected.
|
|
123
|
-
* Optional, defaults to "Select options".
|
|
124
|
-
*/
|
|
125
|
-
placeholder?: string;
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Animation duration in seconds for the visual effects (e.g., bouncing badges).
|
|
129
|
-
* Optional, defaults to 0 (no animation).
|
|
130
|
-
*/
|
|
131
|
-
animation?: number;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Advanced animation configuration for different component parts.
|
|
135
|
-
* Optional, allows fine-tuning of various animation effects.
|
|
136
|
-
*/
|
|
137
|
-
animationConfig?: AnimationConfig;
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Maximum number of items to display. Extra selected items will be summarized.
|
|
141
|
-
* Optional, defaults to 3.
|
|
142
|
-
*/
|
|
143
|
-
maxCount?: number;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* The modality of the popover. When set to true, interaction with outside elements
|
|
147
|
-
* will be disabled and only popover content will be visible to screen readers.
|
|
148
|
-
* Optional, defaults to false.
|
|
149
|
-
*/
|
|
150
|
-
modalPopover?: boolean;
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* If true, renders the multi-select component as a child of another component.
|
|
154
|
-
* Optional, defaults to false.
|
|
155
|
-
*/
|
|
156
|
-
asChild?: boolean;
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Additional class names to apply custom styles to the multi-select component.
|
|
160
|
-
* Optional, can be used to add custom styles.
|
|
161
|
-
*/
|
|
162
|
-
className?: string;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* If true, disables the select all functionality.
|
|
166
|
-
* Optional, defaults to false.
|
|
167
|
-
*/
|
|
168
|
-
hideSelectAll?: boolean;
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* If true, shows search functionality in the popover.
|
|
172
|
-
* If false, hides the search input completely.
|
|
173
|
-
* Optional, defaults to true.
|
|
174
|
-
*/
|
|
175
|
-
searchable?: boolean;
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Custom empty state message when no options match search.
|
|
179
|
-
* Optional, defaults to "No results found."
|
|
180
|
-
*/
|
|
181
|
-
emptyIndicator?: React.ReactNode;
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* If true, allows the component to grow and shrink with its content.
|
|
185
|
-
* If false, uses fixed width behavior.
|
|
186
|
-
* Optional, defaults to false.
|
|
187
|
-
*/
|
|
188
|
-
autoSize?: boolean;
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* If true, shows badges in a single line with horizontal scroll.
|
|
192
|
-
* If false, badges wrap to multiple lines.
|
|
193
|
-
* Optional, defaults to false.
|
|
194
|
-
*/
|
|
195
|
-
singleLine?: boolean;
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Custom CSS class for the popover content.
|
|
199
|
-
* Optional, can be used to customize popover appearance.
|
|
200
|
-
*/
|
|
201
|
-
popoverClassName?: string;
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* If true, disables the component completely.
|
|
205
|
-
* Optional, defaults to false.
|
|
206
|
-
*/
|
|
207
|
-
disabled?: boolean;
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Responsive configuration for different screen sizes.
|
|
211
|
-
* Allows customizing maxCount and other properties based on viewport.
|
|
212
|
-
* Can be boolean true for default responsive behavior or an object for custom configuration.
|
|
213
|
-
*/
|
|
214
|
-
responsive?:
|
|
215
|
-
| boolean
|
|
216
|
-
| {
|
|
217
|
-
/** Configuration for mobile devices (< 640px) */
|
|
218
|
-
mobile?: {
|
|
219
|
-
maxCount?: number;
|
|
220
|
-
hideIcons?: boolean;
|
|
221
|
-
compactMode?: boolean;
|
|
222
|
-
};
|
|
223
|
-
/** Configuration for tablet devices (640px - 1024px) */
|
|
224
|
-
tablet?: {
|
|
225
|
-
maxCount?: number;
|
|
226
|
-
hideIcons?: boolean;
|
|
227
|
-
compactMode?: boolean;
|
|
228
|
-
};
|
|
229
|
-
/** Configuration for desktop devices (> 1024px) */
|
|
230
|
-
desktop?: {
|
|
231
|
-
maxCount?: number;
|
|
232
|
-
hideIcons?: boolean;
|
|
233
|
-
compactMode?: boolean;
|
|
234
|
-
};
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Minimum width for the component.
|
|
239
|
-
* Optional, defaults to auto-sizing based on content.
|
|
240
|
-
* When set, component will not shrink below this width.
|
|
241
|
-
*/
|
|
242
|
-
minWidth?: string;
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Maximum width for the component.
|
|
246
|
-
* Optional, defaults to 100% of container.
|
|
247
|
-
* Component will not exceed container boundaries.
|
|
248
|
-
*/
|
|
249
|
-
maxWidth?: string;
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* If true, automatically removes duplicate options based on their value.
|
|
253
|
-
* Optional, defaults to false (shows warning in dev mode instead).
|
|
254
|
-
*/
|
|
255
|
-
deduplicateOptions?: boolean;
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* If true, the component will reset its internal state when defaultValue changes.
|
|
259
|
-
* Useful for React Hook Form integration and form reset functionality.
|
|
260
|
-
* Optional, defaults to true.
|
|
261
|
-
*/
|
|
262
|
-
resetOnDefaultValueChange?: boolean;
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* If true, automatically closes the popover after selecting an option.
|
|
266
|
-
* Useful for single-selection-like behavior or mobile UX.
|
|
267
|
-
* Optional, defaults to false.
|
|
268
|
-
*/
|
|
269
|
-
closeOnSelect?: boolean;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Imperative methods exposed through ref
|
|
274
|
-
*/
|
|
275
|
-
export interface MultiSelectRef {
|
|
276
|
-
/**
|
|
277
|
-
* Programmatically reset the component to its default value
|
|
278
|
-
*/
|
|
279
|
-
reset: () => void;
|
|
280
|
-
/**
|
|
281
|
-
* Get current selected values
|
|
282
|
-
*/
|
|
283
|
-
getSelectedValues: () => string[];
|
|
284
|
-
/**
|
|
285
|
-
* Set selected values programmatically
|
|
286
|
-
*/
|
|
287
|
-
setSelectedValues: (values: string[]) => void;
|
|
288
|
-
/**
|
|
289
|
-
* Clear all selected values
|
|
290
|
-
*/
|
|
291
|
-
clear: () => void;
|
|
292
|
-
/**
|
|
293
|
-
* Focus the component
|
|
294
|
-
*/
|
|
295
|
-
focus: () => void;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
export const MultiSelect = React.forwardRef<MultiSelectRef, MultiSelectProps>(
|
|
299
|
-
(
|
|
300
|
-
{
|
|
301
|
-
options,
|
|
302
|
-
onValueChange,
|
|
303
|
-
variant,
|
|
304
|
-
defaultValue = [],
|
|
305
|
-
placeholder = "Select options",
|
|
306
|
-
animation = 0,
|
|
307
|
-
animationConfig,
|
|
308
|
-
maxCount = 3,
|
|
309
|
-
modalPopover = false,
|
|
310
|
-
asChild = false,
|
|
311
|
-
className,
|
|
312
|
-
hideSelectAll = false,
|
|
313
|
-
searchable = true,
|
|
314
|
-
emptyIndicator,
|
|
315
|
-
autoSize = false,
|
|
316
|
-
singleLine = false,
|
|
317
|
-
popoverClassName,
|
|
318
|
-
disabled = false,
|
|
319
|
-
responsive,
|
|
320
|
-
minWidth,
|
|
321
|
-
maxWidth,
|
|
322
|
-
deduplicateOptions = false,
|
|
323
|
-
resetOnDefaultValueChange = true,
|
|
324
|
-
closeOnSelect = false,
|
|
325
|
-
...props
|
|
326
|
-
},
|
|
327
|
-
ref,
|
|
328
|
-
) => {
|
|
329
|
-
const [selectedValues, setSelectedValues] = React.useState<string[]>(defaultValue);
|
|
330
|
-
const [isPopoverOpen, setIsPopoverOpen] = React.useState(false);
|
|
331
|
-
const [isAnimating, setIsAnimating] = React.useState(false);
|
|
332
|
-
const [searchValue, setSearchValue] = React.useState("");
|
|
333
|
-
|
|
334
|
-
const [politeMessage, setPoliteMessage] = React.useState("");
|
|
335
|
-
const [assertiveMessage, setAssertiveMessage] = React.useState("");
|
|
336
|
-
const prevSelectedCount = React.useRef(selectedValues.length);
|
|
337
|
-
const prevIsOpen = React.useRef(isPopoverOpen);
|
|
338
|
-
const prevSearchValue = React.useRef(searchValue);
|
|
339
|
-
|
|
340
|
-
const announce = React.useCallback(
|
|
341
|
-
(message: string, priority: "polite" | "assertive" = "polite") => {
|
|
342
|
-
if (priority === "assertive") {
|
|
343
|
-
setAssertiveMessage(message);
|
|
344
|
-
setTimeout(() => setAssertiveMessage(""), 100);
|
|
345
|
-
} else {
|
|
346
|
-
setPoliteMessage(message);
|
|
347
|
-
setTimeout(() => setPoliteMessage(""), 100);
|
|
348
|
-
}
|
|
349
|
-
},
|
|
350
|
-
[],
|
|
351
|
-
);
|
|
352
|
-
|
|
353
|
-
const multiSelectId = React.useId();
|
|
354
|
-
const listboxId = `${multiSelectId}-listbox`;
|
|
355
|
-
const triggerDescriptionId = `${multiSelectId}-description`;
|
|
356
|
-
const selectedCountId = `${multiSelectId}-count`;
|
|
357
|
-
|
|
358
|
-
const prevDefaultValueRef = React.useRef<string[]>(defaultValue);
|
|
359
|
-
|
|
360
|
-
const isGroupedOptions = React.useCallback(
|
|
361
|
-
(opts: MultiSelectOption[] | MultiSelectGroup[]): opts is MultiSelectGroup[] => {
|
|
362
|
-
return opts.length > 0 && "heading" in opts[0];
|
|
363
|
-
},
|
|
364
|
-
[],
|
|
365
|
-
);
|
|
366
|
-
|
|
367
|
-
const arraysEqual = React.useCallback((a: string[], b: string[]): boolean => {
|
|
368
|
-
if (a.length !== b.length) return false;
|
|
369
|
-
const sortedA = [...a].sort();
|
|
370
|
-
const sortedB = [...b].sort();
|
|
371
|
-
return sortedA.every((val, index) => val === sortedB[index]);
|
|
372
|
-
}, []);
|
|
373
|
-
|
|
374
|
-
const resetToDefault = React.useCallback(() => {
|
|
375
|
-
setSelectedValues(defaultValue);
|
|
376
|
-
setIsPopoverOpen(false);
|
|
377
|
-
setSearchValue("");
|
|
378
|
-
onValueChange(defaultValue);
|
|
379
|
-
}, [defaultValue, onValueChange]);
|
|
380
|
-
|
|
381
|
-
const buttonRef = React.useRef<HTMLButtonElement>(null);
|
|
382
|
-
|
|
383
|
-
React.useImperativeHandle(
|
|
384
|
-
ref,
|
|
385
|
-
() => ({
|
|
386
|
-
reset: resetToDefault,
|
|
387
|
-
getSelectedValues: () => selectedValues,
|
|
388
|
-
setSelectedValues: (values: string[]) => {
|
|
389
|
-
setSelectedValues(values);
|
|
390
|
-
onValueChange(values);
|
|
391
|
-
},
|
|
392
|
-
clear: () => {
|
|
393
|
-
setSelectedValues([]);
|
|
394
|
-
onValueChange([]);
|
|
395
|
-
},
|
|
396
|
-
focus: () => {
|
|
397
|
-
if (buttonRef.current) {
|
|
398
|
-
buttonRef.current.focus();
|
|
399
|
-
const originalOutline = buttonRef.current.style.outline;
|
|
400
|
-
const originalOutlineOffset = buttonRef.current.style.outlineOffset;
|
|
401
|
-
buttonRef.current.style.outline = "2px solid hsl(var(--ring))";
|
|
402
|
-
buttonRef.current.style.outlineOffset = "2px";
|
|
403
|
-
setTimeout(() => {
|
|
404
|
-
if (buttonRef.current) {
|
|
405
|
-
buttonRef.current.style.outline = originalOutline;
|
|
406
|
-
buttonRef.current.style.outlineOffset = originalOutlineOffset;
|
|
407
|
-
}
|
|
408
|
-
}, 1000);
|
|
409
|
-
}
|
|
410
|
-
},
|
|
411
|
-
}),
|
|
412
|
-
[resetToDefault, selectedValues, onValueChange],
|
|
413
|
-
);
|
|
414
|
-
|
|
415
|
-
const [screenSize, setScreenSize] = React.useState<"mobile" | "tablet" | "desktop">("desktop");
|
|
416
|
-
|
|
417
|
-
React.useEffect(() => {
|
|
418
|
-
if (typeof window === "undefined") return;
|
|
419
|
-
const handleResize = () => {
|
|
420
|
-
const width = window.innerWidth;
|
|
421
|
-
if (width < 640) {
|
|
422
|
-
setScreenSize("mobile");
|
|
423
|
-
} else if (width < 1024) {
|
|
424
|
-
setScreenSize("tablet");
|
|
425
|
-
} else {
|
|
426
|
-
setScreenSize("desktop");
|
|
427
|
-
}
|
|
428
|
-
};
|
|
429
|
-
handleResize();
|
|
430
|
-
window.addEventListener("resize", handleResize);
|
|
431
|
-
return () => {
|
|
432
|
-
if (typeof window !== "undefined") {
|
|
433
|
-
window.removeEventListener("resize", handleResize);
|
|
434
|
-
}
|
|
435
|
-
};
|
|
436
|
-
}, []);
|
|
437
|
-
|
|
438
|
-
const getResponsiveSettings = () => {
|
|
439
|
-
if (!responsive) {
|
|
440
|
-
return {
|
|
441
|
-
maxCount: maxCount,
|
|
442
|
-
hideIcons: false,
|
|
443
|
-
compactMode: false,
|
|
444
|
-
};
|
|
445
|
-
}
|
|
446
|
-
if (responsive === true) {
|
|
447
|
-
const defaultResponsive = {
|
|
448
|
-
mobile: { maxCount: 2, hideIcons: false, compactMode: true },
|
|
449
|
-
tablet: { maxCount: 4, hideIcons: false, compactMode: false },
|
|
450
|
-
desktop: { maxCount: 6, hideIcons: false, compactMode: false },
|
|
451
|
-
};
|
|
452
|
-
const currentSettings = defaultResponsive[screenSize];
|
|
453
|
-
return {
|
|
454
|
-
maxCount: currentSettings?.maxCount ?? maxCount,
|
|
455
|
-
hideIcons: currentSettings?.hideIcons ?? false,
|
|
456
|
-
compactMode: currentSettings?.compactMode ?? false,
|
|
457
|
-
};
|
|
458
|
-
}
|
|
459
|
-
const currentSettings = responsive[screenSize];
|
|
460
|
-
return {
|
|
461
|
-
maxCount: currentSettings?.maxCount ?? maxCount,
|
|
462
|
-
hideIcons: currentSettings?.hideIcons ?? false,
|
|
463
|
-
compactMode: currentSettings?.compactMode ?? false,
|
|
464
|
-
};
|
|
465
|
-
};
|
|
466
|
-
|
|
467
|
-
const responsiveSettings = getResponsiveSettings();
|
|
468
|
-
|
|
469
|
-
const getBadgeAnimationClass = () => {
|
|
470
|
-
if (animationConfig?.badgeAnimation) {
|
|
471
|
-
switch (animationConfig.badgeAnimation) {
|
|
472
|
-
case "bounce":
|
|
473
|
-
return isAnimating ? "animate-bounce" : "hover:-translate-y-1 hover:scale-110";
|
|
474
|
-
case "pulse":
|
|
475
|
-
return "hover:animate-pulse";
|
|
476
|
-
case "wiggle":
|
|
477
|
-
return "hover:animate-wiggle";
|
|
478
|
-
case "fade":
|
|
479
|
-
return "hover:opacity-80";
|
|
480
|
-
case "slide":
|
|
481
|
-
return "hover:translate-x-1";
|
|
482
|
-
case "none":
|
|
483
|
-
return "";
|
|
484
|
-
default:
|
|
485
|
-
return "";
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
return isAnimating ? "animate-bounce" : "";
|
|
489
|
-
};
|
|
490
|
-
|
|
491
|
-
const getPopoverAnimationClass = () => {
|
|
492
|
-
if (animationConfig?.popoverAnimation) {
|
|
493
|
-
switch (animationConfig.popoverAnimation) {
|
|
494
|
-
case "scale":
|
|
495
|
-
return "animate-scaleIn";
|
|
496
|
-
case "slide":
|
|
497
|
-
return "animate-slideInDown";
|
|
498
|
-
case "fade":
|
|
499
|
-
return "animate-fadeIn";
|
|
500
|
-
case "flip":
|
|
501
|
-
return "animate-flipIn";
|
|
502
|
-
case "none":
|
|
503
|
-
return "";
|
|
504
|
-
default:
|
|
505
|
-
return "";
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
return "";
|
|
509
|
-
};
|
|
510
|
-
|
|
511
|
-
const getAllOptions = React.useCallback((): MultiSelectOption[] => {
|
|
512
|
-
if (options.length === 0) return [];
|
|
513
|
-
let allOptions: MultiSelectOption[];
|
|
514
|
-
if (isGroupedOptions(options)) {
|
|
515
|
-
allOptions = options.flatMap((group) => group.options);
|
|
516
|
-
} else {
|
|
517
|
-
allOptions = options;
|
|
518
|
-
}
|
|
519
|
-
const valueSet = new Set<string>();
|
|
520
|
-
const duplicates: string[] = [];
|
|
521
|
-
const uniqueOptions: MultiSelectOption[] = [];
|
|
522
|
-
allOptions.forEach((option) => {
|
|
523
|
-
if (valueSet.has(option.value)) {
|
|
524
|
-
duplicates.push(option.value);
|
|
525
|
-
if (!deduplicateOptions) {
|
|
526
|
-
uniqueOptions.push(option);
|
|
527
|
-
}
|
|
528
|
-
} else {
|
|
529
|
-
valueSet.add(option.value);
|
|
530
|
-
uniqueOptions.push(option);
|
|
531
|
-
}
|
|
532
|
-
});
|
|
533
|
-
if (process.env.NODE_ENV === "development" && duplicates.length > 0) {
|
|
534
|
-
const action = deduplicateOptions ? "automatically removed" : "detected";
|
|
535
|
-
console.warn(
|
|
536
|
-
`MultiSelect: Duplicate option values ${action}: ${duplicates.join(", ")}. ` +
|
|
537
|
-
`${
|
|
538
|
-
deduplicateOptions
|
|
539
|
-
? "Duplicates have been removed automatically."
|
|
540
|
-
: "This may cause unexpected behavior. Consider setting 'deduplicateOptions={true}' or ensure all option values are unique."
|
|
541
|
-
}`,
|
|
542
|
-
);
|
|
543
|
-
}
|
|
544
|
-
return deduplicateOptions ? uniqueOptions : allOptions;
|
|
545
|
-
}, [options, deduplicateOptions, isGroupedOptions]);
|
|
546
|
-
|
|
547
|
-
const getOptionByValue = React.useCallback(
|
|
548
|
-
(value: string): MultiSelectOption | undefined => {
|
|
549
|
-
const option = getAllOptions().find((option) => option.value === value);
|
|
550
|
-
if (!option && process.env.NODE_ENV === "development") {
|
|
551
|
-
console.warn(`MultiSelect: Option with value "${value}" not found in options list`);
|
|
552
|
-
}
|
|
553
|
-
return option;
|
|
554
|
-
},
|
|
555
|
-
[getAllOptions],
|
|
556
|
-
);
|
|
557
|
-
|
|
558
|
-
const filteredOptions = React.useMemo(() => {
|
|
559
|
-
if (!searchable || !searchValue) return options;
|
|
560
|
-
if (options.length === 0) return [];
|
|
561
|
-
if (isGroupedOptions(options)) {
|
|
562
|
-
return options
|
|
563
|
-
.map((group) => ({
|
|
564
|
-
...group,
|
|
565
|
-
options: group.options.filter(
|
|
566
|
-
(option) =>
|
|
567
|
-
option.label.toLowerCase().includes(searchValue.toLowerCase()) ||
|
|
568
|
-
option.value.toLowerCase().includes(searchValue.toLowerCase()),
|
|
569
|
-
),
|
|
570
|
-
}))
|
|
571
|
-
.filter((group) => group.options.length > 0);
|
|
572
|
-
}
|
|
573
|
-
return options.filter(
|
|
574
|
-
(option) =>
|
|
575
|
-
option.label.toLowerCase().includes(searchValue.toLowerCase()) ||
|
|
576
|
-
option.value.toLowerCase().includes(searchValue.toLowerCase()),
|
|
577
|
-
);
|
|
578
|
-
}, [options, searchValue, searchable, isGroupedOptions]);
|
|
579
|
-
|
|
580
|
-
const handleInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
581
|
-
if (event.key === "Enter") {
|
|
582
|
-
setIsPopoverOpen(true);
|
|
583
|
-
} else if (event.key === "Backspace" && !event.currentTarget.value) {
|
|
584
|
-
const newSelectedValues = [...selectedValues];
|
|
585
|
-
newSelectedValues.pop();
|
|
586
|
-
setSelectedValues(newSelectedValues);
|
|
587
|
-
onValueChange(newSelectedValues);
|
|
588
|
-
}
|
|
589
|
-
};
|
|
590
|
-
|
|
591
|
-
const toggleOption = (optionValue: string) => {
|
|
592
|
-
if (disabled) return;
|
|
593
|
-
const option = getOptionByValue(optionValue);
|
|
594
|
-
if (option?.disabled) return;
|
|
595
|
-
const newSelectedValues = selectedValues.includes(optionValue)
|
|
596
|
-
? selectedValues.filter((value) => value !== optionValue)
|
|
597
|
-
: [...selectedValues, optionValue];
|
|
598
|
-
setSelectedValues(newSelectedValues);
|
|
599
|
-
onValueChange(newSelectedValues);
|
|
600
|
-
if (closeOnSelect) {
|
|
601
|
-
setIsPopoverOpen(false);
|
|
602
|
-
}
|
|
603
|
-
};
|
|
604
|
-
|
|
605
|
-
const handleClear = () => {
|
|
606
|
-
if (disabled) return;
|
|
607
|
-
setSelectedValues([]);
|
|
608
|
-
onValueChange([]);
|
|
609
|
-
};
|
|
610
|
-
|
|
611
|
-
const handleTogglePopover = () => {
|
|
612
|
-
if (disabled) return;
|
|
613
|
-
setIsPopoverOpen((prev) => !prev);
|
|
614
|
-
};
|
|
615
|
-
|
|
616
|
-
const clearExtraOptions = () => {
|
|
617
|
-
if (disabled) return;
|
|
618
|
-
const newSelectedValues = selectedValues.slice(0, responsiveSettings.maxCount);
|
|
619
|
-
setSelectedValues(newSelectedValues);
|
|
620
|
-
onValueChange(newSelectedValues);
|
|
621
|
-
};
|
|
622
|
-
|
|
623
|
-
const toggleAll = () => {
|
|
624
|
-
if (disabled) return;
|
|
625
|
-
const allOptions = getAllOptions().filter((option) => !option.disabled);
|
|
626
|
-
if (selectedValues.length === allOptions.length) {
|
|
627
|
-
handleClear();
|
|
628
|
-
} else {
|
|
629
|
-
const allValues = allOptions.map((option) => option.value);
|
|
630
|
-
setSelectedValues(allValues);
|
|
631
|
-
onValueChange(allValues);
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
if (closeOnSelect) {
|
|
635
|
-
setIsPopoverOpen(false);
|
|
636
|
-
}
|
|
637
|
-
};
|
|
638
|
-
|
|
639
|
-
React.useEffect(() => {
|
|
640
|
-
if (!resetOnDefaultValueChange) return;
|
|
641
|
-
const prevDefaultValue = prevDefaultValueRef.current;
|
|
642
|
-
if (!arraysEqual(prevDefaultValue, defaultValue)) {
|
|
643
|
-
if (!arraysEqual(selectedValues, defaultValue)) {
|
|
644
|
-
setSelectedValues(defaultValue);
|
|
645
|
-
}
|
|
646
|
-
prevDefaultValueRef.current = [...defaultValue];
|
|
647
|
-
}
|
|
648
|
-
}, [defaultValue, selectedValues, arraysEqual, resetOnDefaultValueChange]);
|
|
649
|
-
|
|
650
|
-
const getWidthConstraints = () => {
|
|
651
|
-
const defaultMinWidth = screenSize === "mobile" ? "0px" : "200px";
|
|
652
|
-
const effectiveMinWidth = minWidth || defaultMinWidth;
|
|
653
|
-
const effectiveMaxWidth = maxWidth || "100%";
|
|
654
|
-
return {
|
|
655
|
-
minWidth: effectiveMinWidth,
|
|
656
|
-
maxWidth: effectiveMaxWidth,
|
|
657
|
-
width: autoSize ? "auto" : "100%",
|
|
658
|
-
};
|
|
659
|
-
};
|
|
660
|
-
|
|
661
|
-
const widthConstraints = getWidthConstraints();
|
|
662
|
-
|
|
663
|
-
React.useEffect(() => {
|
|
664
|
-
if (!isPopoverOpen) {
|
|
665
|
-
setSearchValue("");
|
|
666
|
-
}
|
|
667
|
-
}, [isPopoverOpen]);
|
|
668
|
-
|
|
669
|
-
React.useEffect(() => {
|
|
670
|
-
const selectedCount = selectedValues.length;
|
|
671
|
-
const allOptions = getAllOptions();
|
|
672
|
-
const totalOptions = allOptions.filter((opt) => !opt.disabled).length;
|
|
673
|
-
if (selectedCount !== prevSelectedCount.current) {
|
|
674
|
-
const diff = selectedCount - prevSelectedCount.current;
|
|
675
|
-
if (diff > 0) {
|
|
676
|
-
const addedItems = selectedValues.slice(-diff);
|
|
677
|
-
const addedLabels = addedItems
|
|
678
|
-
.map((value) => allOptions.find((opt) => opt.value === value)?.label)
|
|
679
|
-
.filter(Boolean);
|
|
680
|
-
|
|
681
|
-
if (addedLabels.length === 1) {
|
|
682
|
-
announce(
|
|
683
|
-
`${addedLabels[0]} selected. ${selectedCount} of ${totalOptions} options selected.`,
|
|
684
|
-
);
|
|
685
|
-
} else {
|
|
686
|
-
announce(
|
|
687
|
-
`${addedLabels.length} options selected. ${selectedCount} of ${totalOptions} total selected.`,
|
|
688
|
-
);
|
|
689
|
-
}
|
|
690
|
-
} else if (diff < 0) {
|
|
691
|
-
announce(`Option removed. ${selectedCount} of ${totalOptions} options selected.`);
|
|
692
|
-
}
|
|
693
|
-
prevSelectedCount.current = selectedCount;
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
if (isPopoverOpen !== prevIsOpen.current) {
|
|
697
|
-
if (isPopoverOpen) {
|
|
698
|
-
announce(
|
|
699
|
-
`Dropdown opened. ${totalOptions} options available. Use arrow keys to navigate.`,
|
|
700
|
-
);
|
|
701
|
-
} else {
|
|
702
|
-
announce("Dropdown closed.");
|
|
703
|
-
}
|
|
704
|
-
prevIsOpen.current = isPopoverOpen;
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
if (searchValue !== prevSearchValue.current && searchValue !== undefined) {
|
|
708
|
-
if (searchValue && isPopoverOpen) {
|
|
709
|
-
const filteredCount = allOptions.filter(
|
|
710
|
-
(opt) =>
|
|
711
|
-
opt.label.toLowerCase().includes(searchValue.toLowerCase()) ||
|
|
712
|
-
opt.value.toLowerCase().includes(searchValue.toLowerCase()),
|
|
713
|
-
).length;
|
|
714
|
-
|
|
715
|
-
announce(
|
|
716
|
-
`${filteredCount} option${filteredCount === 1 ? "" : "s"} found for "${searchValue}"`,
|
|
717
|
-
);
|
|
718
|
-
}
|
|
719
|
-
prevSearchValue.current = searchValue;
|
|
720
|
-
}
|
|
721
|
-
}, [selectedValues, isPopoverOpen, searchValue, announce, getAllOptions]);
|
|
722
|
-
|
|
723
|
-
return (
|
|
724
|
-
<>
|
|
725
|
-
<div className="sr-only">
|
|
726
|
-
<div aria-live="polite" aria-atomic="true" role="status">
|
|
727
|
-
{politeMessage}
|
|
728
|
-
</div>
|
|
729
|
-
<div aria-live="assertive" aria-atomic="true" role="alert">
|
|
730
|
-
{assertiveMessage}
|
|
731
|
-
</div>
|
|
732
|
-
</div>
|
|
733
|
-
|
|
734
|
-
<Popover open={isPopoverOpen} onOpenChange={setIsPopoverOpen} modal={modalPopover}>
|
|
735
|
-
<div id={triggerDescriptionId} className="sr-only">
|
|
736
|
-
Multi-select dropdown. Use arrow keys to navigate, Enter to select, and Escape to close.
|
|
737
|
-
</div>
|
|
738
|
-
<div id={selectedCountId} className="sr-only" aria-live="polite">
|
|
739
|
-
{selectedValues.length === 0
|
|
740
|
-
? "No options selected"
|
|
741
|
-
: `${selectedValues.length} option${
|
|
742
|
-
selectedValues.length === 1 ? "" : "s"
|
|
743
|
-
} selected: ${selectedValues
|
|
744
|
-
.map((value) => getOptionByValue(value)?.label)
|
|
745
|
-
.filter(Boolean)
|
|
746
|
-
.join(", ")}`}
|
|
747
|
-
</div>
|
|
748
|
-
|
|
749
|
-
<PopoverTrigger asChild>
|
|
750
|
-
<Button
|
|
751
|
-
ref={buttonRef}
|
|
752
|
-
{...props}
|
|
753
|
-
onClick={handleTogglePopover}
|
|
754
|
-
disabled={disabled}
|
|
755
|
-
role="combobox"
|
|
756
|
-
aria-expanded={isPopoverOpen}
|
|
757
|
-
aria-haspopup="listbox"
|
|
758
|
-
aria-controls={isPopoverOpen ? listboxId : undefined}
|
|
759
|
-
aria-describedby={`${triggerDescriptionId} ${selectedCountId}`}
|
|
760
|
-
aria-label={`Multi-select: ${selectedValues.length} of ${
|
|
761
|
-
getAllOptions().length
|
|
762
|
-
} options selected. ${placeholder}`}
|
|
763
|
-
className={cn(
|
|
764
|
-
"flex p-1 rounded-md border min-h-10 h-auto items-center justify-between bg-inherit hover:bg-inherit [&_svg]:pointer-events-auto",
|
|
765
|
-
autoSize ? "w-auto" : "w-full",
|
|
766
|
-
responsiveSettings.compactMode && "min-h-8 text-sm",
|
|
767
|
-
screenSize === "mobile" && "min-h-12 text-base",
|
|
768
|
-
disabled && "opacity-50 cursor-not-allowed",
|
|
769
|
-
className,
|
|
770
|
-
)}
|
|
771
|
-
style={{
|
|
772
|
-
...widthConstraints,
|
|
773
|
-
maxWidth: `min(${widthConstraints.maxWidth}, 100%)`,
|
|
774
|
-
}}
|
|
775
|
-
>
|
|
776
|
-
{selectedValues.length > 0 ? (
|
|
777
|
-
<div className="flex justify-between items-center w-full">
|
|
778
|
-
<div
|
|
779
|
-
className={cn(
|
|
780
|
-
"flex items-center gap-1",
|
|
781
|
-
singleLine ? "overflow-x-auto multiselect-singleline-scroll" : "flex-wrap",
|
|
782
|
-
responsiveSettings.compactMode && "gap-0.5",
|
|
783
|
-
)}
|
|
784
|
-
style={
|
|
785
|
-
singleLine
|
|
786
|
-
? {
|
|
787
|
-
paddingBottom: "4px",
|
|
788
|
-
}
|
|
789
|
-
: {}
|
|
790
|
-
}
|
|
791
|
-
>
|
|
792
|
-
{selectedValues
|
|
793
|
-
.slice(0, responsiveSettings.maxCount)
|
|
794
|
-
.map((value) => {
|
|
795
|
-
const option = getOptionByValue(value);
|
|
796
|
-
const IconComponent = option?.icon;
|
|
797
|
-
const customStyle = option?.style;
|
|
798
|
-
if (!option) {
|
|
799
|
-
return null;
|
|
800
|
-
}
|
|
801
|
-
const badgeStyle: React.CSSProperties = {
|
|
802
|
-
animationDuration: `${animation}s`,
|
|
803
|
-
...(customStyle?.badgeColor && {
|
|
804
|
-
backgroundColor: customStyle.badgeColor,
|
|
805
|
-
}),
|
|
806
|
-
...(customStyle?.gradient && {
|
|
807
|
-
background: customStyle.gradient,
|
|
808
|
-
color: "white",
|
|
809
|
-
}),
|
|
810
|
-
};
|
|
811
|
-
return (
|
|
812
|
-
<Badge
|
|
813
|
-
key={value}
|
|
814
|
-
className={cn(
|
|
815
|
-
getBadgeAnimationClass(),
|
|
816
|
-
multiSelectVariants({ variant }),
|
|
817
|
-
customStyle?.gradient && "text-white border-transparent",
|
|
818
|
-
responsiveSettings.compactMode && "text-xs px-1.5 py-0.5",
|
|
819
|
-
screenSize === "mobile" && "max-w-[120px] truncate",
|
|
820
|
-
singleLine && "shrink-0 whitespace-nowrap",
|
|
821
|
-
"[&>svg]:pointer-events-auto",
|
|
822
|
-
)}
|
|
823
|
-
style={{
|
|
824
|
-
...badgeStyle,
|
|
825
|
-
animationDuration: `${animationConfig?.duration || animation}s`,
|
|
826
|
-
animationDelay: `${animationConfig?.delay || 0}s`,
|
|
827
|
-
}}
|
|
828
|
-
>
|
|
829
|
-
{IconComponent && !responsiveSettings.hideIcons && (
|
|
830
|
-
<IconComponent
|
|
831
|
-
className={cn(
|
|
832
|
-
"h-4 w-4 mr-2",
|
|
833
|
-
responsiveSettings.compactMode && "h-3 w-3 mr-1",
|
|
834
|
-
customStyle?.iconColor && "text-current",
|
|
835
|
-
)}
|
|
836
|
-
{...(customStyle?.iconColor && {
|
|
837
|
-
style: { color: customStyle.iconColor },
|
|
838
|
-
})}
|
|
839
|
-
/>
|
|
840
|
-
)}
|
|
841
|
-
<span className={cn(screenSize === "mobile" && "truncate")}>
|
|
842
|
-
{option.label}
|
|
843
|
-
</span>
|
|
844
|
-
<div
|
|
845
|
-
role="button"
|
|
846
|
-
tabIndex={0}
|
|
847
|
-
onClick={(event) => {
|
|
848
|
-
event.stopPropagation();
|
|
849
|
-
toggleOption(value);
|
|
850
|
-
}}
|
|
851
|
-
onKeyDown={(event) => {
|
|
852
|
-
if (event.key === "Enter" || event.key === " ") {
|
|
853
|
-
event.preventDefault();
|
|
854
|
-
event.stopPropagation();
|
|
855
|
-
toggleOption(value);
|
|
856
|
-
}
|
|
857
|
-
}}
|
|
858
|
-
aria-label={`Remove ${option.label} from selection`}
|
|
859
|
-
className="ml-2 h-4 w-4 cursor-pointer hover:bg-white/20 rounded-sm p-0.5 -m-0.5 focus:outline-none focus:ring-1 focus:ring-white/50"
|
|
860
|
-
>
|
|
861
|
-
<XCircleIcon
|
|
862
|
-
className={cn(
|
|
863
|
-
"h-3 w-3",
|
|
864
|
-
responsiveSettings.compactMode && "h-2.5 w-2.5",
|
|
865
|
-
)}
|
|
866
|
-
/>
|
|
867
|
-
</div>
|
|
868
|
-
</Badge>
|
|
869
|
-
);
|
|
870
|
-
})
|
|
871
|
-
.filter(Boolean)}
|
|
872
|
-
{selectedValues.length > responsiveSettings.maxCount && (
|
|
873
|
-
<Badge
|
|
874
|
-
className={cn(
|
|
875
|
-
"bg-transparent text-foreground border-foreground/1 hover:bg-transparent",
|
|
876
|
-
getBadgeAnimationClass(),
|
|
877
|
-
multiSelectVariants({ variant }),
|
|
878
|
-
responsiveSettings.compactMode && "text-xs px-1.5 py-0.5",
|
|
879
|
-
singleLine && "shrink-0 whitespace-nowrap",
|
|
880
|
-
"[&>svg]:pointer-events-auto",
|
|
881
|
-
)}
|
|
882
|
-
style={{
|
|
883
|
-
animationDuration: `${animationConfig?.duration || animation}s`,
|
|
884
|
-
animationDelay: `${animationConfig?.delay || 0}s`,
|
|
885
|
-
}}
|
|
886
|
-
>
|
|
887
|
-
{`+ ${selectedValues.length - responsiveSettings.maxCount} more`}
|
|
888
|
-
<XCircleIcon
|
|
889
|
-
className={cn(
|
|
890
|
-
"ml-2 h-4 w-4 cursor-pointer",
|
|
891
|
-
responsiveSettings.compactMode && "ml-1 h-3 w-3",
|
|
892
|
-
)}
|
|
893
|
-
onClick={(event) => {
|
|
894
|
-
event.stopPropagation();
|
|
895
|
-
clearExtraOptions();
|
|
896
|
-
}}
|
|
897
|
-
/>
|
|
898
|
-
</Badge>
|
|
899
|
-
)}
|
|
900
|
-
</div>
|
|
901
|
-
<div className="flex items-center justify-between">
|
|
902
|
-
<div
|
|
903
|
-
role="button"
|
|
904
|
-
tabIndex={0}
|
|
905
|
-
onClick={(event) => {
|
|
906
|
-
event.stopPropagation();
|
|
907
|
-
handleClear();
|
|
908
|
-
}}
|
|
909
|
-
onKeyDown={(event) => {
|
|
910
|
-
if (event.key === "Enter" || event.key === " ") {
|
|
911
|
-
event.preventDefault();
|
|
912
|
-
event.stopPropagation();
|
|
913
|
-
handleClear();
|
|
914
|
-
}
|
|
915
|
-
}}
|
|
916
|
-
aria-label={`Clear all ${selectedValues.length} selected options`}
|
|
917
|
-
className="flex items-center justify-center h-4 w-4 mx-2 cursor-pointer text-muted-foreground hover:text-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-1 rounded-sm"
|
|
918
|
-
>
|
|
919
|
-
<XIcon className="h-4 w-4" />
|
|
920
|
-
</div>
|
|
921
|
-
<Separator orientation="vertical" className="flex min-h-6 h-full" />
|
|
922
|
-
<ChevronDownIcon
|
|
923
|
-
className="h-4 mx-2 cursor-pointer text-muted-foreground"
|
|
924
|
-
aria-hidden="true"
|
|
925
|
-
/>
|
|
926
|
-
</div>
|
|
927
|
-
</div>
|
|
928
|
-
) : (
|
|
929
|
-
<div className="flex items-center justify-between w-full mx-auto">
|
|
930
|
-
<span className="text-sm text-muted-foreground mx-3">{placeholder}</span>
|
|
931
|
-
<ChevronDownIcon className="h-4 cursor-pointer text-muted-foreground mx-2" />
|
|
932
|
-
</div>
|
|
933
|
-
)}
|
|
934
|
-
</Button>
|
|
935
|
-
</PopoverTrigger>
|
|
936
|
-
<PopoverContent
|
|
937
|
-
id={listboxId}
|
|
938
|
-
role="listbox"
|
|
939
|
-
aria-multiselectable="true"
|
|
940
|
-
aria-label="Available options"
|
|
941
|
-
className={cn(
|
|
942
|
-
"w-auto p-0",
|
|
943
|
-
getPopoverAnimationClass(),
|
|
944
|
-
screenSize === "mobile" && "w-[85vw] max-w-[280px]",
|
|
945
|
-
screenSize === "tablet" && "w-[70vw] max-w-md",
|
|
946
|
-
screenSize === "desktop" && "min-w-[300px]",
|
|
947
|
-
popoverClassName,
|
|
948
|
-
)}
|
|
949
|
-
style={{
|
|
950
|
-
animationDuration: `${animationConfig?.duration || animation}s`,
|
|
951
|
-
animationDelay: `${animationConfig?.delay || 0}s`,
|
|
952
|
-
maxWidth: `min(${widthConstraints.maxWidth}, 85vw)`,
|
|
953
|
-
maxHeight: screenSize === "mobile" ? "70vh" : "60vh",
|
|
954
|
-
touchAction: "manipulation",
|
|
955
|
-
}}
|
|
956
|
-
align="start"
|
|
957
|
-
onEscapeKeyDown={() => setIsPopoverOpen(false)}
|
|
958
|
-
>
|
|
959
|
-
<Command>
|
|
960
|
-
{searchable && (
|
|
961
|
-
<CommandInput
|
|
962
|
-
placeholder="Search options..."
|
|
963
|
-
onKeyDown={handleInputKeyDown}
|
|
964
|
-
value={searchValue}
|
|
965
|
-
onValueChange={setSearchValue}
|
|
966
|
-
aria-label="Search through available options"
|
|
967
|
-
aria-describedby={`${multiSelectId}-search-help`}
|
|
968
|
-
/>
|
|
969
|
-
)}
|
|
970
|
-
{searchable && (
|
|
971
|
-
<div id={`${multiSelectId}-search-help`} className="sr-only">
|
|
972
|
-
Type to filter options. Use arrow keys to navigate results.
|
|
973
|
-
</div>
|
|
974
|
-
)}
|
|
975
|
-
<CommandList
|
|
976
|
-
className={cn(
|
|
977
|
-
"max-h-[40vh] overflow-y-auto multiselect-scrollbar",
|
|
978
|
-
screenSize === "mobile" && "max-h-[50vh]",
|
|
979
|
-
"overscroll-behavior-y-contain",
|
|
980
|
-
)}
|
|
981
|
-
>
|
|
982
|
-
<CommandEmpty>{emptyIndicator || "No results found."}</CommandEmpty>{" "}
|
|
983
|
-
{!hideSelectAll && !searchValue && (
|
|
984
|
-
<CommandGroup>
|
|
985
|
-
<CommandItem
|
|
986
|
-
key="all"
|
|
987
|
-
onSelect={toggleAll}
|
|
988
|
-
role="option"
|
|
989
|
-
aria-selected={
|
|
990
|
-
selectedValues.length ===
|
|
991
|
-
getAllOptions().filter((opt) => !opt.disabled).length
|
|
992
|
-
}
|
|
993
|
-
aria-label={`Select all ${getAllOptions().length} options`}
|
|
994
|
-
className="cursor-pointer"
|
|
995
|
-
>
|
|
996
|
-
<div
|
|
997
|
-
className={cn(
|
|
998
|
-
"mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-primary",
|
|
999
|
-
selectedValues.length ===
|
|
1000
|
-
getAllOptions().filter((opt) => !opt.disabled).length
|
|
1001
|
-
? "bg-primary text-primary-foreground"
|
|
1002
|
-
: "opacity-50 [&_svg]:invisible",
|
|
1003
|
-
)}
|
|
1004
|
-
aria-hidden="true"
|
|
1005
|
-
>
|
|
1006
|
-
<CheckIcon className="h-4 w-4" />
|
|
1007
|
-
</div>
|
|
1008
|
-
<span>
|
|
1009
|
-
(Select All
|
|
1010
|
-
{getAllOptions().length > 20 ? ` - ${getAllOptions().length} options` : ""})
|
|
1011
|
-
</span>
|
|
1012
|
-
</CommandItem>
|
|
1013
|
-
</CommandGroup>
|
|
1014
|
-
)}
|
|
1015
|
-
{isGroupedOptions(filteredOptions) ? (
|
|
1016
|
-
filteredOptions.map((group) => (
|
|
1017
|
-
<CommandGroup key={group.heading} heading={group.heading}>
|
|
1018
|
-
{group.options.map((option) => {
|
|
1019
|
-
const isSelected = selectedValues.includes(option.value);
|
|
1020
|
-
return (
|
|
1021
|
-
<CommandItem
|
|
1022
|
-
key={option.value}
|
|
1023
|
-
onSelect={() => toggleOption(option.value)}
|
|
1024
|
-
role="option"
|
|
1025
|
-
aria-selected={isSelected}
|
|
1026
|
-
aria-disabled={option.disabled}
|
|
1027
|
-
aria-label={`${option.label}${
|
|
1028
|
-
isSelected ? ", selected" : ", not selected"
|
|
1029
|
-
}${option.disabled ? ", disabled" : ""}`}
|
|
1030
|
-
className={cn(
|
|
1031
|
-
"cursor-pointer",
|
|
1032
|
-
option.disabled && "opacity-50 cursor-not-allowed",
|
|
1033
|
-
)}
|
|
1034
|
-
disabled={option.disabled}
|
|
1035
|
-
>
|
|
1036
|
-
<div
|
|
1037
|
-
className={cn(
|
|
1038
|
-
"mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-primary",
|
|
1039
|
-
isSelected
|
|
1040
|
-
? "bg-primary text-primary-foreground"
|
|
1041
|
-
: "opacity-50 [&_svg]:invisible",
|
|
1042
|
-
)}
|
|
1043
|
-
aria-hidden="true"
|
|
1044
|
-
>
|
|
1045
|
-
<CheckIcon className="h-4 w-4" />
|
|
1046
|
-
</div>
|
|
1047
|
-
{option.icon && (
|
|
1048
|
-
<option.icon
|
|
1049
|
-
className="mr-2 h-4 w-4 text-muted-foreground"
|
|
1050
|
-
aria-hidden="true"
|
|
1051
|
-
/>
|
|
1052
|
-
)}
|
|
1053
|
-
<span>{option.label}</span>
|
|
1054
|
-
</CommandItem>
|
|
1055
|
-
);
|
|
1056
|
-
})}
|
|
1057
|
-
</CommandGroup>
|
|
1058
|
-
))
|
|
1059
|
-
) : (
|
|
1060
|
-
<CommandGroup>
|
|
1061
|
-
{filteredOptions.map((option) => {
|
|
1062
|
-
const isSelected = selectedValues.includes(option.value);
|
|
1063
|
-
return (
|
|
1064
|
-
<CommandItem
|
|
1065
|
-
key={option.value}
|
|
1066
|
-
onSelect={() => toggleOption(option.value)}
|
|
1067
|
-
role="option"
|
|
1068
|
-
aria-selected={isSelected}
|
|
1069
|
-
aria-disabled={option.disabled}
|
|
1070
|
-
aria-label={`${option.label}${
|
|
1071
|
-
isSelected ? ", selected" : ", not selected"
|
|
1072
|
-
}${option.disabled ? ", disabled" : ""}`}
|
|
1073
|
-
className={cn(
|
|
1074
|
-
"cursor-pointer",
|
|
1075
|
-
option.disabled && "opacity-50 cursor-not-allowed",
|
|
1076
|
-
)}
|
|
1077
|
-
disabled={option.disabled}
|
|
1078
|
-
>
|
|
1079
|
-
<div
|
|
1080
|
-
className={cn(
|
|
1081
|
-
"mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-primary",
|
|
1082
|
-
isSelected
|
|
1083
|
-
? "bg-primary text-primary-foreground"
|
|
1084
|
-
: "opacity-50 [&_svg]:invisible",
|
|
1085
|
-
)}
|
|
1086
|
-
aria-hidden="true"
|
|
1087
|
-
>
|
|
1088
|
-
<CheckIcon className="h-4 w-4" />
|
|
1089
|
-
</div>
|
|
1090
|
-
{option.icon && (
|
|
1091
|
-
<option.icon
|
|
1092
|
-
className="mr-2 h-4 w-4 text-muted-foreground"
|
|
1093
|
-
aria-hidden="true"
|
|
1094
|
-
/>
|
|
1095
|
-
)}
|
|
1096
|
-
<span>{option.label}</span>
|
|
1097
|
-
</CommandItem>
|
|
1098
|
-
);
|
|
1099
|
-
})}
|
|
1100
|
-
</CommandGroup>
|
|
1101
|
-
)}
|
|
1102
|
-
<CommandSeparator />
|
|
1103
|
-
<CommandGroup>
|
|
1104
|
-
<div className="flex items-center justify-between">
|
|
1105
|
-
{selectedValues.length > 0 && (
|
|
1106
|
-
<>
|
|
1107
|
-
<CommandItem
|
|
1108
|
-
onSelect={handleClear}
|
|
1109
|
-
className="flex-1 justify-center cursor-pointer"
|
|
1110
|
-
>
|
|
1111
|
-
Clear
|
|
1112
|
-
</CommandItem>
|
|
1113
|
-
<Separator orientation="vertical" className="flex min-h-6 h-full" />
|
|
1114
|
-
</>
|
|
1115
|
-
)}
|
|
1116
|
-
<CommandItem
|
|
1117
|
-
onSelect={() => setIsPopoverOpen(false)}
|
|
1118
|
-
className="flex-1 justify-center cursor-pointer max-w-full"
|
|
1119
|
-
>
|
|
1120
|
-
Close
|
|
1121
|
-
</CommandItem>
|
|
1122
|
-
</div>
|
|
1123
|
-
</CommandGroup>
|
|
1124
|
-
</CommandList>
|
|
1125
|
-
</Command>
|
|
1126
|
-
</PopoverContent>
|
|
1127
|
-
{animation > 0 && selectedValues.length > 0 && (
|
|
1128
|
-
<WandSparklesIcon
|
|
1129
|
-
className={cn(
|
|
1130
|
-
"cursor-pointer my-2 text-foreground bg-background w-3 h-3",
|
|
1131
|
-
isAnimating ? "" : "text-muted-foreground",
|
|
1132
|
-
)}
|
|
1133
|
-
onClick={() => setIsAnimating(!isAnimating)}
|
|
1134
|
-
/>
|
|
1135
|
-
)}
|
|
1136
|
-
</Popover>
|
|
1137
|
-
</>
|
|
1138
|
-
);
|
|
1139
|
-
},
|
|
1140
|
-
);
|
|
1141
|
-
|
|
1142
|
-
MultiSelect.displayName = "MultiSelect";
|
|
1143
|
-
export type { MultiSelectOption, MultiSelectGroup, MultiSelectProps };
|