@mbao01/common 0.0.43 → 0.0.45
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/types/components/Chart/Chart.d.ts +2 -0
- package/dist/types/components/Chart/ChartContext.d.ts +2 -0
- package/dist/types/components/Chart/components/ChartLegend.d.ts +4 -0
- package/dist/types/components/Chart/components/ChartStyle.d.ts +2 -0
- package/dist/types/components/Chart/components/ChartTooltip.d.ts +4 -0
- package/dist/types/components/Chart/components/index.d.ts +3 -0
- package/dist/types/components/Chart/constants.d.ts +22 -0
- package/dist/types/components/Chart/helpers.d.ts +11 -0
- package/dist/types/components/Chart/hooks/index.d.ts +1 -0
- package/dist/types/components/Chart/hooks/useChart/index.d.ts +1 -0
- package/dist/types/components/Chart/hooks/useChart/useChart.d.ts +1 -0
- package/dist/types/components/Chart/index.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/activeShapeArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/animationArgs.d.ts +12 -0
- package/dist/types/components/Chart/stories/args/areaArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/areaChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/barArgs.d.ts +5 -0
- package/dist/types/components/Chart/stories/args/barChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/cartesianChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/cartesianSharedArgs.d.ts +11 -0
- package/dist/types/components/Chart/stories/args/chartArgs.d.ts +6 -0
- package/dist/types/components/Chart/stories/args/dotArgs.d.ts +10 -0
- package/dist/types/components/Chart/stories/args/events.d.ts +804 -0
- package/dist/types/components/Chart/stories/args/labelListArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/legendArgs.d.ts +3 -0
- package/dist/types/components/Chart/stories/args/lineArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/lineChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/pieArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/pieChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/polarChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/polarSharedArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radarArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radarChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radialBarArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radialBarChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/rectangleArgs.d.ts +9 -0
- package/dist/types/components/Chart/stories/args/referenceSharedArgs.d.ts +4 -0
- package/dist/types/components/Chart/stories/args/sectorArgs.d.ts +7 -0
- package/dist/types/components/Chart/stories/args/sharedAxisArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/stylesArgs.d.ts +6 -0
- package/dist/types/components/Chart/stories/args/textArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/tooltipArgs.d.ts +4 -0
- package/dist/types/components/Chart/stories/args/types.d.ts +13 -0
- package/dist/types/components/Chart/stories/args/xAxisArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/yAxisArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/examples/AreaChart.d.ts +16 -0
- package/dist/types/components/Chart/stories/examples/BarChart.d.ts +20 -0
- package/dist/types/components/Chart/stories/examples/LineChart.d.ts +16 -0
- package/dist/types/components/Chart/stories/examples/PieChart.d.ts +19 -0
- package/dist/types/components/Chart/stories/examples/RadarChart.d.ts +24 -0
- package/dist/types/components/Chart/stories/examples/RadialChart.d.ts +17 -0
- package/dist/types/components/Chart/stories/examples/Tooltip.d.ts +15 -0
- package/dist/types/components/Chart/stories/helpers/index.d.ts +9 -0
- package/dist/types/components/Chart/types.d.ts +37 -0
- package/dist/types/components/DatetimePicker/DatetimeGrid.d.ts +26 -0
- package/dist/types/components/DatetimePicker/DatetimePicker.d.ts +13 -0
- package/dist/types/components/DatetimePicker/constants.d.ts +15 -0
- package/dist/types/components/DatetimePicker/index.d.ts +1 -0
- package/dist/types/components/DatetimePicker/types.d.ts +25 -0
- package/dist/types/components/Form/DatetimeInput/DatetimeCalendar.d.ts +5 -0
- package/dist/types/components/Form/DatetimeInput/DatetimeInput.d.ts +83 -0
- package/dist/types/components/Form/DatetimeInput/DatetimeInputContext.d.ts +2 -0
- package/dist/types/components/Form/DatetimeInput/NaturalLanguageInput.d.ts +5 -0
- package/dist/types/components/Form/DatetimeInput/TimePicker.d.ts +1 -0
- package/dist/types/components/Form/DatetimeInput/constants.d.ts +24 -0
- package/dist/types/components/Form/DatetimeInput/helpers.d.ts +27 -0
- package/dist/types/components/Form/DatetimeInput/hooks/index.d.ts +1 -0
- package/dist/types/components/Form/DatetimeInput/hooks/useDateInput/index.d.ts +1 -0
- package/dist/types/components/Form/DatetimeInput/hooks/useDateInput/useDateInput.d.ts +1 -0
- package/dist/types/components/Form/DatetimeInput/index.d.ts +1 -0
- package/dist/types/components/Form/DatetimeInput/types.d.ts +31 -0
- package/dist/types/components/Form/MultiSelect/MultiSelect.d.ts +46 -0
- package/dist/types/components/Form/MultiSelect/MultiSelectContext.d.ts +2 -0
- package/dist/types/components/Form/MultiSelect/constants.d.ts +19 -0
- package/dist/types/components/Form/MultiSelect/hooks/index.d.ts +1 -0
- package/dist/types/components/Form/MultiSelect/hooks/useMultiSelect/index.d.ts +1 -0
- package/dist/types/components/Form/MultiSelect/hooks/useMultiSelect/useMultiSelect.d.ts +1 -0
- package/dist/types/components/Form/MultiSelect/index.d.ts +1 -0
- package/dist/types/components/Form/MultiSelect/types.d.ts +31 -0
- package/dist/types/components/Form/TagsInput/TagsInput.d.ts +13 -0
- package/dist/types/components/Form/TagsInput/constants.d.ts +16 -0
- package/dist/types/components/Form/TagsInput/index.d.ts +1 -0
- package/dist/types/components/Form/TagsInput/types.d.ts +9 -0
- package/dist/types/components/Form/index.d.ts +2 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +7 -3
- package/src/components/Chart/Chart.tsx +26 -0
- package/src/components/Chart/ChartContext.tsx +4 -0
- package/src/components/Chart/components/ChartLegend.tsx +52 -0
- package/src/components/Chart/components/ChartStyle.tsx +32 -0
- package/src/components/Chart/components/ChartTooltip.tsx +126 -0
- package/src/components/Chart/components/index.ts +3 -0
- package/src/components/Chart/constants.ts +78 -0
- package/src/components/Chart/helpers.ts +27 -0
- package/src/components/Chart/hooks/index.ts +1 -0
- package/src/components/Chart/hooks/useChart/index.ts +1 -0
- package/src/components/Chart/hooks/useChart/useChart.ts +12 -0
- package/src/components/Chart/index.ts +2 -0
- package/src/components/Chart/stories/args/activeShapeArgs.ts +34 -0
- package/src/components/Chart/stories/args/animationArgs.ts +67 -0
- package/src/components/Chart/stories/args/areaArgs.ts +16 -0
- package/src/components/Chart/stories/args/areaChartArgs.ts +19 -0
- package/src/components/Chart/stories/args/barArgs.ts +123 -0
- package/src/components/Chart/stories/args/barChartArgs.ts +65 -0
- package/src/components/Chart/stories/args/cartesianChartArgs.ts +42 -0
- package/src/components/Chart/stories/args/cartesianSharedArgs.ts +136 -0
- package/src/components/Chart/stories/args/chartArgs.ts +244 -0
- package/src/components/Chart/stories/args/dotArgs.ts +46 -0
- package/src/components/Chart/stories/args/events.ts +343 -0
- package/src/components/Chart/stories/args/index.ts +178 -0
- package/src/components/Chart/stories/args/labelListArgs.ts +124 -0
- package/src/components/Chart/stories/args/legendArgs.ts +205 -0
- package/src/components/Chart/stories/args/lineArgs.ts +87 -0
- package/src/components/Chart/stories/args/lineChartArgs.ts +17 -0
- package/src/components/Chart/stories/args/pieArgs.ts +18 -0
- package/src/components/Chart/stories/args/pieChartArgs.ts +7 -0
- package/src/components/Chart/stories/args/polarChartArgs.ts +86 -0
- package/src/components/Chart/stories/args/polarSharedArgs.ts +42 -0
- package/src/components/Chart/stories/args/radarArgs.ts +26 -0
- package/src/components/Chart/stories/args/radarChartArgs.ts +26 -0
- package/src/components/Chart/stories/args/radialBarArgs.ts +36 -0
- package/src/components/Chart/stories/args/radialBarChartArgs.ts +16 -0
- package/src/components/Chart/stories/args/rectangleArgs.ts +35 -0
- package/src/components/Chart/stories/args/referenceSharedArgs.ts +75 -0
- package/src/components/Chart/stories/args/sectorArgs.ts +106 -0
- package/src/components/Chart/stories/args/sharedAxisArgs.ts +332 -0
- package/src/components/Chart/stories/args/stylesArgs.ts +258 -0
- package/src/components/Chart/stories/args/textArgs.ts +97 -0
- package/src/components/Chart/stories/args/tooltipArgs.ts +41 -0
- package/src/components/Chart/stories/args/types.ts +23 -0
- package/src/components/Chart/stories/args/xAxisArgs.ts +109 -0
- package/src/components/Chart/stories/args/yAxisArgs.ts +73 -0
- package/src/components/Chart/stories/examples/AreaChart.tsx +156 -0
- package/src/components/Chart/stories/examples/BarChart.tsx +425 -0
- package/src/components/Chart/stories/examples/LineChart.tsx +144 -0
- package/src/components/Chart/stories/examples/PieChart.tsx +238 -0
- package/src/components/Chart/stories/examples/RadarChart.tsx +261 -0
- package/src/components/Chart/stories/examples/RadialChart.tsx +239 -0
- package/src/components/Chart/stories/examples/Tooltip.tsx +68 -0
- package/src/components/Chart/stories/helpers/index.tsx +75 -0
- package/src/components/Chart/types.ts +39 -0
- package/src/components/DatetimePicker/DatetimeGrid.tsx +59 -0
- package/src/components/DatetimePicker/DatetimePicker.tsx +59 -0
- package/src/components/DatetimePicker/constants.ts +102 -0
- package/src/components/DatetimePicker/index.ts +1 -0
- package/src/components/DatetimePicker/types.ts +36 -0
- package/src/components/Form/DatetimeInput/DatetimeCalendar.tsx +68 -0
- package/src/components/Form/DatetimeInput/DatetimeInput.tsx +90 -0
- package/src/components/Form/DatetimeInput/DatetimeInputContext.tsx +4 -0
- package/src/components/Form/DatetimeInput/NaturalLanguageInput.tsx +73 -0
- package/src/components/Form/DatetimeInput/TimePicker.tsx +202 -0
- package/src/components/Form/DatetimeInput/constants.ts +135 -0
- package/src/components/Form/DatetimeInput/helpers.ts +93 -0
- package/src/components/Form/DatetimeInput/hooks/index.ts +1 -0
- package/src/components/Form/DatetimeInput/hooks/useDateInput/index.ts +1 -0
- package/src/components/Form/DatetimeInput/hooks/useDateInput/useDateInput.ts +10 -0
- package/src/components/Form/DatetimeInput/index.ts +1 -0
- package/src/components/Form/DatetimeInput/types.ts +36 -0
- package/src/components/Form/MultiSelect/MultiSelect.tsx +348 -0
- package/src/components/Form/MultiSelect/MultiSelectContext.tsx +4 -0
- package/src/components/Form/MultiSelect/constants.ts +103 -0
- package/src/components/Form/MultiSelect/hooks/index.ts +1 -0
- package/src/components/Form/MultiSelect/hooks/useMultiSelect/index.ts +1 -0
- package/src/components/Form/MultiSelect/hooks/useMultiSelect/useMultiSelect.ts +10 -0
- package/src/components/Form/MultiSelect/index.ts +1 -0
- package/src/components/Form/MultiSelect/types.ts +46 -0
- package/src/components/Form/TagsInput/TagsInput.tsx +278 -0
- package/src/components/Form/TagsInput/constants.ts +87 -0
- package/src/components/Form/TagsInput/index.ts +1 -0
- package/src/components/Form/TagsInput/types.ts +10 -0
- package/src/components/Form/index.ts +2 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type VariantProps } from "../../../libs";
|
|
2
|
+
import { type CalendarProps } from "../../Calendar/types";
|
|
3
|
+
import { type InputProps } from "../Input/types";
|
|
4
|
+
import { getDatetimeInputContainerClasses } from "./constants";
|
|
5
|
+
|
|
6
|
+
type DisabledType = { disabled?: boolean };
|
|
7
|
+
type VariantType = VariantProps<typeof getDatetimeInputContainerClasses>;
|
|
8
|
+
type NaturalLanguageInputType = DisabledType & {
|
|
9
|
+
locale?: Intl.LocalesArgument;
|
|
10
|
+
} & Omit<InputProps, "type" | "ref" | "value" | "defaultValue" | "onBlur" | "disabled">;
|
|
11
|
+
type CalendarType = Omit<CalendarProps, "mode" | "disabled">;
|
|
12
|
+
|
|
13
|
+
export type DatetimeInputProps = NaturalLanguageInputType &
|
|
14
|
+
VariantType & {
|
|
15
|
+
date?: Date;
|
|
16
|
+
calendar?: CalendarType;
|
|
17
|
+
defaultDate?: Date;
|
|
18
|
+
onDateChange?: (date: Date | undefined) => void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type DatetimeInputContextProps = {
|
|
22
|
+
value?: Date;
|
|
23
|
+
onDateChange: (date: Date | undefined) => void;
|
|
24
|
+
time: TimeString;
|
|
25
|
+
onTimeChange: (time: TimeString) => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type DatetimeCalendarProps = VariantType & CalendarType & DisabledType;
|
|
29
|
+
|
|
30
|
+
export type NaturalLanguageInputProps = NaturalLanguageInputType;
|
|
31
|
+
|
|
32
|
+
type Hours = `${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12}`;
|
|
33
|
+
type Minutes = `${0 | 1 | 2 | 3 | 4 | 5}${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`;
|
|
34
|
+
type PM_AM = "AM" | "PM";
|
|
35
|
+
|
|
36
|
+
export type TimeString = `${Hours}:${Minutes} ${PM_AM}` | "";
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { ComponentRef, KeyboardEvent, MouseEvent, SyntheticEvent } from "react";
|
|
4
|
+
import { forwardRef, useCallback, useRef, useState } from "react";
|
|
5
|
+
import { CheckIcon, Cross2Icon } from "@radix-ui/react-icons";
|
|
6
|
+
import { Command as CommandPrimitive } from "cmdk";
|
|
7
|
+
import type {
|
|
8
|
+
Item,
|
|
9
|
+
MultiSelectContentProps,
|
|
10
|
+
MultiSelectInputProps,
|
|
11
|
+
MultiSelectItemProps,
|
|
12
|
+
MultiSelectListProps,
|
|
13
|
+
MultiSelectProps,
|
|
14
|
+
MultiSelectTriggerProps,
|
|
15
|
+
} from "./types";
|
|
16
|
+
import { cn } from "../../../utilities";
|
|
17
|
+
import { Badge } from "../../Badge";
|
|
18
|
+
import { Command } from "../../Command";
|
|
19
|
+
import {
|
|
20
|
+
getMultiSelectClasses,
|
|
21
|
+
getMultiSelectItemClasses,
|
|
22
|
+
getMultiSelectListClasses,
|
|
23
|
+
getMultiSelectTagClasses,
|
|
24
|
+
getMultiSelectTriggerClasses,
|
|
25
|
+
} from "./constants";
|
|
26
|
+
import { useMultiSelect } from "./hooks";
|
|
27
|
+
import { MultiSelectContext } from "./MultiSelectContext";
|
|
28
|
+
|
|
29
|
+
// TODO : expose the visibility of the popup
|
|
30
|
+
const MultiSelect = ({
|
|
31
|
+
values,
|
|
32
|
+
onValuesChange,
|
|
33
|
+
loop = false,
|
|
34
|
+
className,
|
|
35
|
+
children,
|
|
36
|
+
dir,
|
|
37
|
+
...props
|
|
38
|
+
}: MultiSelectProps) => {
|
|
39
|
+
const [inputValue, setInputValue] = useState("");
|
|
40
|
+
const [open, setOpen] = useState<boolean>(false);
|
|
41
|
+
const [activeIndex, setActiveIndex] = useState<number>(-1);
|
|
42
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
43
|
+
const [isValueSelected, setIsValueSelected] = useState(false);
|
|
44
|
+
const [selectedValue, setSelectedValue] = useState("");
|
|
45
|
+
const [items, setItems] = useState<Item[]>(
|
|
46
|
+
(values ?? []).map((value) => ({ value, label: undefined }))
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const onValueChangeHandler = useCallback(
|
|
50
|
+
(value: string, label?: string) => {
|
|
51
|
+
if (values.includes(value)) {
|
|
52
|
+
setItems(items.filter(({ value: val }) => val !== value));
|
|
53
|
+
onValuesChange(values.filter((val) => val !== value));
|
|
54
|
+
} else {
|
|
55
|
+
setItems([...items, { value, label }]);
|
|
56
|
+
onValuesChange([...values, value]);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
60
|
+
[values]
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const handleSelect = useCallback(
|
|
64
|
+
(e: SyntheticEvent<HTMLInputElement>) => {
|
|
65
|
+
const target = e.currentTarget;
|
|
66
|
+
const selection = target.value.substring(
|
|
67
|
+
target.selectionStart ?? 0,
|
|
68
|
+
target.selectionEnd ?? 0
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
setSelectedValue(selection);
|
|
72
|
+
setIsValueSelected(selection === inputValue);
|
|
73
|
+
},
|
|
74
|
+
[inputValue]
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const handleKeyDown = useCallback(
|
|
78
|
+
(e: KeyboardEvent<HTMLDivElement>) => {
|
|
79
|
+
const target = inputRef.current;
|
|
80
|
+
|
|
81
|
+
if (!target) return;
|
|
82
|
+
|
|
83
|
+
if (["ArrowLeft", "ArrowRight", "Backspace", "Delete", "Escape", "Enter"].includes(e.key)) {
|
|
84
|
+
e.stopPropagation();
|
|
85
|
+
|
|
86
|
+
const moveNext = () => {
|
|
87
|
+
const nextIndex = activeIndex + 1;
|
|
88
|
+
setActiveIndex(nextIndex > values.length - 1 ? (loop ? 0 : -1) : nextIndex);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const movePrev = () => {
|
|
92
|
+
const prevIndex = activeIndex - 1;
|
|
93
|
+
setActiveIndex(prevIndex < 0 ? values.length - 1 : prevIndex);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const moveCurrent = () => {
|
|
97
|
+
const newIndex =
|
|
98
|
+
activeIndex - 1 <= 0 ? (values.length - 1 === 0 ? -1 : 0) : activeIndex - 1;
|
|
99
|
+
setActiveIndex(newIndex);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
switch (e.key) {
|
|
103
|
+
case "ArrowLeft":
|
|
104
|
+
if (dir === "rtl") {
|
|
105
|
+
if (values.length > 0 && (activeIndex !== -1 || loop)) {
|
|
106
|
+
moveNext();
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
if (values.length > 0 && target.selectionStart === 0) {
|
|
110
|
+
movePrev();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
break;
|
|
114
|
+
|
|
115
|
+
case "ArrowRight":
|
|
116
|
+
if (dir === "rtl") {
|
|
117
|
+
if (values.length > 0 && target.selectionStart === 0) {
|
|
118
|
+
movePrev();
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
if (values.length > 0 && (activeIndex !== -1 || loop)) {
|
|
122
|
+
moveNext();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
|
|
127
|
+
case "Backspace":
|
|
128
|
+
case "Delete":
|
|
129
|
+
if (values.length > 0) {
|
|
130
|
+
if (activeIndex !== -1 && activeIndex < values.length) {
|
|
131
|
+
onValueChangeHandler(values[activeIndex]);
|
|
132
|
+
moveCurrent();
|
|
133
|
+
} else {
|
|
134
|
+
if (target.selectionStart === 0) {
|
|
135
|
+
if (selectedValue === inputValue || isValueSelected) {
|
|
136
|
+
onValueChangeHandler(values[values.length - 1]);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
|
|
143
|
+
case "Enter":
|
|
144
|
+
setOpen(true);
|
|
145
|
+
break;
|
|
146
|
+
|
|
147
|
+
case "Escape":
|
|
148
|
+
if (activeIndex !== -1) {
|
|
149
|
+
setActiveIndex(-1);
|
|
150
|
+
} else if (open) {
|
|
151
|
+
setOpen(false);
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
158
|
+
[dir, values, inputValue, activeIndex, loop]
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<MultiSelectContext.Provider
|
|
163
|
+
value={{
|
|
164
|
+
items,
|
|
165
|
+
values,
|
|
166
|
+
onValueChange: onValueChangeHandler,
|
|
167
|
+
open,
|
|
168
|
+
setOpen,
|
|
169
|
+
inputValue,
|
|
170
|
+
setInputValue,
|
|
171
|
+
activeIndex,
|
|
172
|
+
setActiveIndex,
|
|
173
|
+
ref: inputRef,
|
|
174
|
+
handleSelect,
|
|
175
|
+
}}
|
|
176
|
+
>
|
|
177
|
+
<Command
|
|
178
|
+
onKeyDown={handleKeyDown}
|
|
179
|
+
className={cn(getMultiSelectClasses(), className)}
|
|
180
|
+
dir={dir}
|
|
181
|
+
{...props}
|
|
182
|
+
>
|
|
183
|
+
{children}
|
|
184
|
+
</Command>
|
|
185
|
+
</MultiSelectContext.Provider>
|
|
186
|
+
);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export const MultiSelectTrigger = ({
|
|
190
|
+
className,
|
|
191
|
+
children,
|
|
192
|
+
size,
|
|
193
|
+
wide,
|
|
194
|
+
outline,
|
|
195
|
+
variant,
|
|
196
|
+
disabled,
|
|
197
|
+
...props
|
|
198
|
+
}: MultiSelectTriggerProps) => {
|
|
199
|
+
const { items, onValueChange, activeIndex } = useMultiSelect();
|
|
200
|
+
|
|
201
|
+
const handleMouseDown = useCallback((e: MouseEvent) => {
|
|
202
|
+
e.preventDefault();
|
|
203
|
+
e.stopPropagation();
|
|
204
|
+
}, []);
|
|
205
|
+
|
|
206
|
+
return (
|
|
207
|
+
<div
|
|
208
|
+
className={cn(
|
|
209
|
+
getMultiSelectTriggerClasses({ size, wide, outline, variant, disabled }),
|
|
210
|
+
className
|
|
211
|
+
)}
|
|
212
|
+
{...props}
|
|
213
|
+
>
|
|
214
|
+
{items.map((item, index) => {
|
|
215
|
+
const label = item.label ?? item.value;
|
|
216
|
+
|
|
217
|
+
return (
|
|
218
|
+
<Badge
|
|
219
|
+
key={item.value}
|
|
220
|
+
className={cn(
|
|
221
|
+
getMultiSelectTagClasses({ size }),
|
|
222
|
+
"px-1 rounded-xl flex items-center gap-1",
|
|
223
|
+
activeIndex === index && "ring-2 ring-muted-foreground "
|
|
224
|
+
)}
|
|
225
|
+
size={size}
|
|
226
|
+
variant={variant === "default" ? "neutral" : variant}
|
|
227
|
+
>
|
|
228
|
+
<span className="text-xs">{label}</span>
|
|
229
|
+
<button
|
|
230
|
+
type="button"
|
|
231
|
+
aria-label={`Remove ${label} option`}
|
|
232
|
+
aria-roledescription="button to remove option"
|
|
233
|
+
onMouseDown={handleMouseDown}
|
|
234
|
+
onClick={() => onValueChange(item.value, item.label)}
|
|
235
|
+
disabled={disabled}
|
|
236
|
+
>
|
|
237
|
+
<span className="sr-only">Remove {label} option</span>
|
|
238
|
+
<Cross2Icon className="h-4 w-4 hover:stroke-destructive" />
|
|
239
|
+
</button>
|
|
240
|
+
</Badge>
|
|
241
|
+
);
|
|
242
|
+
})}
|
|
243
|
+
{children}
|
|
244
|
+
</div>
|
|
245
|
+
);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
MultiSelectTrigger.displayName = "MultiSelectTrigger";
|
|
249
|
+
|
|
250
|
+
const MultiSelectInput = ({ className, ...props }: MultiSelectInputProps) => {
|
|
251
|
+
const {
|
|
252
|
+
setOpen,
|
|
253
|
+
inputValue,
|
|
254
|
+
setInputValue,
|
|
255
|
+
activeIndex,
|
|
256
|
+
setActiveIndex,
|
|
257
|
+
handleSelect,
|
|
258
|
+
ref: inputRef,
|
|
259
|
+
} = useMultiSelect();
|
|
260
|
+
|
|
261
|
+
return (
|
|
262
|
+
<CommandPrimitive.Input
|
|
263
|
+
{...props}
|
|
264
|
+
tabIndex={0}
|
|
265
|
+
ref={inputRef}
|
|
266
|
+
value={inputValue}
|
|
267
|
+
onValueChange={activeIndex === -1 ? setInputValue : undefined}
|
|
268
|
+
onSelect={handleSelect}
|
|
269
|
+
onBlur={() => setOpen(false)}
|
|
270
|
+
onFocus={() => setOpen(true)}
|
|
271
|
+
onClick={() => setActiveIndex(-1)}
|
|
272
|
+
className={cn(
|
|
273
|
+
"bg-transparent outline-none flex-1",
|
|
274
|
+
className,
|
|
275
|
+
activeIndex !== -1 && "caret-transparent"
|
|
276
|
+
)}
|
|
277
|
+
/>
|
|
278
|
+
);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
MultiSelectInput.displayName = "MultiSelectInput";
|
|
282
|
+
|
|
283
|
+
const MultiSelectContent = forwardRef<HTMLDivElement, MultiSelectContentProps>(
|
|
284
|
+
({ children }, ref) => {
|
|
285
|
+
const { open } = useMultiSelect();
|
|
286
|
+
return (
|
|
287
|
+
<div ref={ref} className="relative">
|
|
288
|
+
{open && children}
|
|
289
|
+
</div>
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
MultiSelectContent.displayName = "MultiSelectContent";
|
|
295
|
+
|
|
296
|
+
const MultiSelectList = forwardRef<ComponentRef<typeof Command.List>, MultiSelectListProps>(
|
|
297
|
+
({ className, size, children }, ref) => {
|
|
298
|
+
return (
|
|
299
|
+
<Command.List ref={ref} className={cn(getMultiSelectListClasses({ size }), className)}>
|
|
300
|
+
{children}
|
|
301
|
+
<Command.Empty>
|
|
302
|
+
<span className="text-muted-foreground">No results found</span>
|
|
303
|
+
</Command.Empty>
|
|
304
|
+
</Command.List>
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
MultiSelectList.displayName = "MultiSelectList";
|
|
310
|
+
|
|
311
|
+
const MultiSelectItem = forwardRef<ComponentRef<typeof Command.Item>, MultiSelectItemProps>(
|
|
312
|
+
({ className, value, label, children, size, disabled, ...props }, ref) => {
|
|
313
|
+
const { values: options, onValueChange, setInputValue } = useMultiSelect();
|
|
314
|
+
|
|
315
|
+
const mousePreventDefault = useCallback((e: MouseEvent) => {
|
|
316
|
+
e.preventDefault();
|
|
317
|
+
e.stopPropagation();
|
|
318
|
+
}, []);
|
|
319
|
+
|
|
320
|
+
const included = options.includes(value);
|
|
321
|
+
return (
|
|
322
|
+
<Command.Item
|
|
323
|
+
ref={ref}
|
|
324
|
+
{...props}
|
|
325
|
+
disabled={disabled}
|
|
326
|
+
onSelect={() => {
|
|
327
|
+
onValueChange(value, label);
|
|
328
|
+
setInputValue("");
|
|
329
|
+
}}
|
|
330
|
+
className={cn(getMultiSelectItemClasses({ included, disabled, size }), className)}
|
|
331
|
+
onMouseDown={mousePreventDefault}
|
|
332
|
+
>
|
|
333
|
+
{children}
|
|
334
|
+
{included && <CheckIcon className="h-4 w-4" />}
|
|
335
|
+
</Command.Item>
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
MultiSelectItem.displayName = "MultiSelectItem";
|
|
341
|
+
|
|
342
|
+
MultiSelect.Trigger = MultiSelectTrigger;
|
|
343
|
+
MultiSelect.Input = MultiSelectInput;
|
|
344
|
+
MultiSelect.Content = MultiSelectContent;
|
|
345
|
+
MultiSelect.List = MultiSelectList;
|
|
346
|
+
MultiSelect.Item = MultiSelectItem;
|
|
347
|
+
|
|
348
|
+
export { MultiSelect };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { cva } from "../../../libs";
|
|
2
|
+
|
|
3
|
+
export const getMultiSelectClasses = cva(
|
|
4
|
+
"overflow-visible bg-transparent flex flex-col h-fit w-fit"
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
export const getMultiSelectTriggerClasses = cva(
|
|
8
|
+
"flex items-center flex-wrap gap-1 h-fit px-1 py-2 rounded-md transition-all overflow-hidden",
|
|
9
|
+
{
|
|
10
|
+
variants: {
|
|
11
|
+
variant: {
|
|
12
|
+
default: "border-0",
|
|
13
|
+
accent: "border border-accent",
|
|
14
|
+
error: "border border-error",
|
|
15
|
+
ghost: "border border-ghost",
|
|
16
|
+
info: "border border-info",
|
|
17
|
+
primary: "border border-primary",
|
|
18
|
+
secondary: "border border-secondary",
|
|
19
|
+
success: "border border-success",
|
|
20
|
+
warning: "border border-warning",
|
|
21
|
+
},
|
|
22
|
+
outline: {
|
|
23
|
+
true: "border",
|
|
24
|
+
},
|
|
25
|
+
disabled: {
|
|
26
|
+
true: "border-base-300",
|
|
27
|
+
},
|
|
28
|
+
wide: {
|
|
29
|
+
true: "w-full",
|
|
30
|
+
},
|
|
31
|
+
size: {
|
|
32
|
+
xs: "min-h-6 leading-relaxed px-2 py-1 text-xs",
|
|
33
|
+
sm: "min-h-8 leading-8 px-3 py-1.5 text-sm",
|
|
34
|
+
md: "min-h-12 leading-loose px-4 text-sm",
|
|
35
|
+
lg: "min-h-16 leading-loose px-6 text-lg",
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
compoundVariants: [
|
|
39
|
+
{
|
|
40
|
+
size: undefined,
|
|
41
|
+
className: "min-h-10 px-4",
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
variant: undefined,
|
|
45
|
+
outline: true,
|
|
46
|
+
className: "border-neutral-content",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
variant: "default",
|
|
50
|
+
outline: true,
|
|
51
|
+
className: "border-base-content",
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
}
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
export const getMultiSelectItemClasses = cva(
|
|
58
|
+
"rounded-md cursor-pointer px-2 py-1 transition-colors flex justify-between",
|
|
59
|
+
{
|
|
60
|
+
variants: {
|
|
61
|
+
included: {
|
|
62
|
+
true: "opacity-50 cursor-default",
|
|
63
|
+
},
|
|
64
|
+
disabled: {
|
|
65
|
+
true: "opacity-50 cursor-not-allowed",
|
|
66
|
+
},
|
|
67
|
+
size: {
|
|
68
|
+
xs: "p-1 text-xs",
|
|
69
|
+
sm: "px-1.5 text-sm",
|
|
70
|
+
md: "px-2 text-sm",
|
|
71
|
+
lg: "px-3 text-lg",
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
export const getMultiSelectListClasses = cva(
|
|
78
|
+
"p-2 flex flex-col gap-2 rounded-md scrollbar-thin scrollbar-track-transparent transition-colors scrollbar-thumb-muted-foreground dark:scrollbar-thumb-muted scrollbar-thumb-rounded-lg w-full absolute bg-background shadow-md z-10 border border-muted top-0",
|
|
79
|
+
{
|
|
80
|
+
variants: {
|
|
81
|
+
size: {
|
|
82
|
+
xs: "p-1",
|
|
83
|
+
sm: "p-1.5",
|
|
84
|
+
md: "p-2",
|
|
85
|
+
lg: "p-3",
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
export const getMultiSelectTagClasses = cva(
|
|
92
|
+
"relative flex items-center gap-1 [&>span]:truncate [&>span]:max-w-24 aria-disabled:opacity-50 aria-disabled:cursor-not-allowed focus-visible:outline-none",
|
|
93
|
+
{
|
|
94
|
+
variants: {
|
|
95
|
+
size: {
|
|
96
|
+
xs: "px-0.5 [&>span]:max-w-20 rounded",
|
|
97
|
+
sm: "px-1",
|
|
98
|
+
md: "px-1.5 [&>span]:max-w-28",
|
|
99
|
+
lg: "px-2 [&>span]:max-w-32",
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useMultiSelect } from "./useMultiSelect";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useMultiSelect } from "./useMultiSelect";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { MultiSelectContext } from "../../MultiSelectContext";
|
|
3
|
+
|
|
4
|
+
export const useMultiSelect = () => {
|
|
5
|
+
const context = useContext(MultiSelectContext);
|
|
6
|
+
if (!context) {
|
|
7
|
+
throw new Error("useMultiSelect must be used within MultiSelectProvider");
|
|
8
|
+
}
|
|
9
|
+
return context;
|
|
10
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MultiSelect } from "./MultiSelect";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ComponentPropsWithoutRef, HTMLAttributes } from "react";
|
|
2
|
+
import { VariantProps } from "../../../libs";
|
|
3
|
+
import { Command } from "../../Command";
|
|
4
|
+
import {
|
|
5
|
+
getMultiSelectItemClasses,
|
|
6
|
+
getMultiSelectListClasses,
|
|
7
|
+
getMultiSelectTriggerClasses,
|
|
8
|
+
} from "./constants";
|
|
9
|
+
|
|
10
|
+
export type Item = {
|
|
11
|
+
value: string;
|
|
12
|
+
label?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type MultiSelectProps = React.ComponentPropsWithoutRef<typeof Command> & {
|
|
16
|
+
loop?: boolean;
|
|
17
|
+
values: Item["value"][];
|
|
18
|
+
onValuesChange: (value: string[]) => void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type MultiSelectContextProps = {
|
|
22
|
+
values: Item["value"][];
|
|
23
|
+
onValueChange: (value: Item["value"], label?: Item["label"]) => void;
|
|
24
|
+
items: Item[];
|
|
25
|
+
open: boolean;
|
|
26
|
+
setOpen: (value: boolean) => void;
|
|
27
|
+
inputValue: string;
|
|
28
|
+
setInputValue: React.Dispatch<React.SetStateAction<string>>;
|
|
29
|
+
activeIndex: number;
|
|
30
|
+
setActiveIndex: React.Dispatch<React.SetStateAction<number>>;
|
|
31
|
+
ref: React.RefObject<HTMLInputElement>;
|
|
32
|
+
handleSelect: (e: React.SyntheticEvent<HTMLInputElement>) => void;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type MultiSelectTriggerProps = HTMLAttributes<HTMLDivElement> &
|
|
36
|
+
VariantProps<typeof getMultiSelectTriggerClasses>;
|
|
37
|
+
|
|
38
|
+
export type MultiSelectInputProps = ComponentPropsWithoutRef<typeof Command.Input>;
|
|
39
|
+
|
|
40
|
+
export type MultiSelectContentProps = HTMLAttributes<HTMLDivElement>;
|
|
41
|
+
|
|
42
|
+
export type MultiSelectListProps = ComponentPropsWithoutRef<typeof Command.List> &
|
|
43
|
+
VariantProps<typeof getMultiSelectListClasses>;
|
|
44
|
+
export type MultiSelectItemProps = Item &
|
|
45
|
+
ComponentPropsWithoutRef<typeof Command.Item> &
|
|
46
|
+
VariantProps<typeof getMultiSelectItemClasses>;
|