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