@rockshin/tao-ui 0.0.26 → 0.0.27
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/date-picker/date-picker.d.ts +1 -1
- package/dist/components/date-picker/date-picker.js +57 -43
- package/dist/components/date-picker/range-picker.js +68 -23
- package/dist/components/select/mobile-select.js +9 -20
- package/dist/components/select/select-model.d.ts +7 -1
- package/dist/components/select/select-model.js +30 -7
- package/dist/components/select/select.js +48 -37
- package/dist/components/sonner/sonner.d.ts +2 -1
- package/dist/components/sonner/sonner.js +74 -81
- package/dist/docs/component-docs.js +11 -1
- package/package.json +1 -1
|
@@ -34,4 +34,4 @@ export interface DatePickerProps {
|
|
|
34
34
|
classNames?: SemanticClassNames<DatePickerSemanticPart>;
|
|
35
35
|
styles?: SemanticStyles<DatePickerSemanticPart>;
|
|
36
36
|
}
|
|
37
|
-
export declare function DatePicker({ value, onChange, picker, showTime, format: formatProp, disabledDate, minDate, maxDate, presets, placeholder, size, variant, disabled, allowClear, prefix, suffixIcon, status, mobile, open: controlledOpen, onOpenChange, className, style, classNames, styles, ref, }: DatePickerProps): import("react/jsx-runtime").JSX.Element;
|
|
37
|
+
export declare function DatePicker({ value, defaultValue, onChange, picker, showTime, format: formatProp, disabledDate, minDate, maxDate, presets, placeholder, size, variant, disabled, allowClear, prefix, suffixIcon, status, mobile, open: controlledOpen, onOpenChange, className, style, classNames, styles, ref, }: DatePickerProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -4,9 +4,9 @@ import dayjs from "dayjs";
|
|
|
4
4
|
import customParseFormat from "dayjs/plugin/customParseFormat";
|
|
5
5
|
import { useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
|
|
6
6
|
import { TaoPortalScope, useTaoConfig } from "../../provider/tao-provider.js";
|
|
7
|
+
import { useIsMobile } from "../../utils/breakpoint.js";
|
|
7
8
|
import { cx } from "../../utils/semantic.js";
|
|
8
9
|
import { Drawer } from "../drawer/drawer.js";
|
|
9
|
-
import { useIsMobile } from "../../utils/breakpoint.js";
|
|
10
10
|
import { CalendarGrid } from "./calendar/calendar-grid.js";
|
|
11
11
|
import { CalendarHeader } from "./calendar/calendar-header.js";
|
|
12
12
|
import { MonthGrid } from "./calendar/month-grid.js";
|
|
@@ -17,7 +17,7 @@ import { Presets } from "./presets.js";
|
|
|
17
17
|
import { getDefaultFormat } from "./types.js";
|
|
18
18
|
import * as __rspack_external__radix_ui_react_popover_c8444160 from "@radix-ui/react-popover";
|
|
19
19
|
dayjs.extend(customParseFormat);
|
|
20
|
-
function DatePicker({ value, onChange, picker = 'date', showTime, format: formatProp, disabledDate, minDate, maxDate, presets, placeholder, size, variant, disabled, allowClear = true, prefix, suffixIcon, status, mobile, open: controlledOpen, onOpenChange, className, style, classNames, styles, ref }) {
|
|
20
|
+
function DatePicker({ value, defaultValue, onChange, picker = 'date', showTime, format: formatProp, disabledDate, minDate, maxDate, presets, placeholder, size, variant, disabled, allowClear = true, prefix, suffixIcon, status, mobile, open: controlledOpen, onOpenChange, className, style, classNames, styles, ref }) {
|
|
21
21
|
const ctx = useTaoConfig();
|
|
22
22
|
const resolvedSize = size ?? ctx.size;
|
|
23
23
|
const resolvedVariant = variant ?? ctx.variant;
|
|
@@ -41,8 +41,18 @@ function DatePicker({ value, onChange, picker = 'date', showTime, format: format
|
|
|
41
41
|
const fmt = formatProp ?? getDefaultFormat(picker, showTime);
|
|
42
42
|
const defaultPlaceholder = placeholder ?? fmt;
|
|
43
43
|
const drawerTitle = showTime ? 'Date & Time' : defaultPlaceholder;
|
|
44
|
+
const isValueControlled = void 0 !== value;
|
|
45
|
+
const [internalValue, setInternalValue] = useState(defaultValue ?? null);
|
|
46
|
+
const resolvedValue = isValueControlled ? value : internalValue;
|
|
47
|
+
const emitChange = useCallback((next, dateString)=>{
|
|
48
|
+
if (!isValueControlled) setInternalValue(next);
|
|
49
|
+
onChange?.(next, dateString);
|
|
50
|
+
}, [
|
|
51
|
+
isValueControlled,
|
|
52
|
+
onChange
|
|
53
|
+
]);
|
|
44
54
|
const [internalOpen, setInternalOpen] = useState(false);
|
|
45
|
-
const displayValue =
|
|
55
|
+
const displayValue = resolvedValue ? resolvedValue.format(fmt) : '';
|
|
46
56
|
const [inputValue, setInputValue] = useState(displayValue);
|
|
47
57
|
const isOpen = controlledOpen ?? internalOpen;
|
|
48
58
|
const setOpen = useCallback((v)=>{
|
|
@@ -56,45 +66,45 @@ function DatePicker({ value, onChange, picker = 'date', showTime, format: format
|
|
|
56
66
|
}, [
|
|
57
67
|
displayValue
|
|
58
68
|
]);
|
|
59
|
-
const [viewDate, setViewDate] = useState(()=>
|
|
69
|
+
const [viewDate, setViewDate] = useState(()=>resolvedValue ?? dayjs());
|
|
60
70
|
const [panelMode, setPanelMode] = useState('year' === picker ? 'year' : 'month' === picker ? 'month' : 'date');
|
|
61
71
|
const [timeScrollKey, setTimeScrollKey] = useState(0);
|
|
62
72
|
useEffect(()=>{
|
|
63
73
|
if (isOpen) {
|
|
64
|
-
setViewDate(
|
|
74
|
+
setViewDate(resolvedValue ?? dayjs());
|
|
65
75
|
setPanelMode('year' === picker ? 'year' : 'month' === picker ? 'month' : 'date');
|
|
66
76
|
setTimeScrollKey((k)=>k + 1);
|
|
67
77
|
}
|
|
68
78
|
}, [
|
|
69
79
|
isOpen,
|
|
70
80
|
picker,
|
|
71
|
-
|
|
81
|
+
resolvedValue
|
|
72
82
|
]);
|
|
73
83
|
const timeValue = useMemo(()=>({
|
|
74
|
-
hour:
|
|
75
|
-
minute:
|
|
76
|
-
second:
|
|
84
|
+
hour: resolvedValue?.hour() ?? 0,
|
|
85
|
+
minute: resolvedValue?.minute() ?? 0,
|
|
86
|
+
second: resolvedValue?.second() ?? 0
|
|
77
87
|
}), [
|
|
78
|
-
|
|
88
|
+
resolvedValue
|
|
79
89
|
]);
|
|
80
90
|
const handleDateSelect = useCallback((date)=>{
|
|
81
91
|
let result = date;
|
|
82
|
-
if (showTime &&
|
|
83
|
-
if (showTime)
|
|
92
|
+
if (showTime && resolvedValue) result = date.hour(resolvedValue.hour()).minute(resolvedValue.minute()).second(resolvedValue.second());
|
|
93
|
+
if (showTime) emitChange(result, result.format(fmt));
|
|
84
94
|
else {
|
|
85
|
-
|
|
95
|
+
emitChange(result, result.format(fmt));
|
|
86
96
|
setOpen(false);
|
|
87
97
|
}
|
|
88
98
|
}, [
|
|
89
|
-
|
|
99
|
+
emitChange,
|
|
90
100
|
fmt,
|
|
91
101
|
showTime,
|
|
92
|
-
|
|
102
|
+
resolvedValue,
|
|
93
103
|
setOpen
|
|
94
104
|
]);
|
|
95
105
|
const handleMonthSelect = useCallback((date_0)=>{
|
|
96
106
|
if ('month' === picker) {
|
|
97
|
-
|
|
107
|
+
emitChange(date_0, date_0.format(fmt));
|
|
98
108
|
setOpen(false);
|
|
99
109
|
} else {
|
|
100
110
|
setViewDate(date_0);
|
|
@@ -102,13 +112,13 @@ function DatePicker({ value, onChange, picker = 'date', showTime, format: format
|
|
|
102
112
|
}
|
|
103
113
|
}, [
|
|
104
114
|
picker,
|
|
105
|
-
|
|
115
|
+
emitChange,
|
|
106
116
|
fmt,
|
|
107
117
|
setOpen
|
|
108
118
|
]);
|
|
109
119
|
const handleYearSelect = useCallback((date_1)=>{
|
|
110
120
|
if ('year' === picker) {
|
|
111
|
-
|
|
121
|
+
emitChange(date_1, date_1.format(fmt));
|
|
112
122
|
setOpen(false);
|
|
113
123
|
} else {
|
|
114
124
|
setViewDate(date_1);
|
|
@@ -116,31 +126,31 @@ function DatePicker({ value, onChange, picker = 'date', showTime, format: format
|
|
|
116
126
|
}
|
|
117
127
|
}, [
|
|
118
128
|
picker,
|
|
119
|
-
|
|
129
|
+
emitChange,
|
|
120
130
|
fmt,
|
|
121
131
|
setOpen
|
|
122
132
|
]);
|
|
123
133
|
const handleTimeChange = useCallback((h, m, s)=>{
|
|
124
|
-
const base =
|
|
134
|
+
const base = resolvedValue ?? dayjs();
|
|
125
135
|
const result_0 = base.hour(h).minute(m).second(s);
|
|
126
|
-
|
|
136
|
+
emitChange(result_0, result_0.format(fmt));
|
|
127
137
|
}, [
|
|
128
|
-
|
|
129
|
-
|
|
138
|
+
resolvedValue,
|
|
139
|
+
emitChange,
|
|
130
140
|
fmt
|
|
131
141
|
]);
|
|
132
142
|
const handleClear = useCallback((e)=>{
|
|
133
143
|
e.stopPropagation();
|
|
134
|
-
|
|
144
|
+
emitChange(null, '');
|
|
135
145
|
}, [
|
|
136
|
-
|
|
146
|
+
emitChange
|
|
137
147
|
]);
|
|
138
148
|
const handlePresetSelect = useCallback((val)=>{
|
|
139
149
|
const d = val;
|
|
140
|
-
|
|
150
|
+
emitChange(d, d.format(fmt));
|
|
141
151
|
setOpen(false);
|
|
142
152
|
}, [
|
|
143
|
-
|
|
153
|
+
emitChange,
|
|
144
154
|
fmt,
|
|
145
155
|
setOpen
|
|
146
156
|
]);
|
|
@@ -151,20 +161,20 @@ function DatePicker({ value, onChange, picker = 'date', showTime, format: format
|
|
|
151
161
|
]);
|
|
152
162
|
const handleNow = useCallback(()=>{
|
|
153
163
|
const now = dayjs();
|
|
154
|
-
|
|
164
|
+
emitChange(now, now.format(fmt));
|
|
155
165
|
setViewDate(now);
|
|
156
166
|
setPanelMode('date');
|
|
157
167
|
setTimeScrollKey((k_0)=>k_0 + 1);
|
|
158
168
|
}, [
|
|
159
|
-
|
|
169
|
+
emitChange,
|
|
160
170
|
fmt
|
|
161
171
|
]);
|
|
162
|
-
const showClearBtn = allowClear &&
|
|
172
|
+
const showClearBtn = allowClear && resolvedValue && !resolvedDisabled;
|
|
163
173
|
const commitInputValue = useCallback(()=>{
|
|
164
174
|
const text = inputValue.trim();
|
|
165
175
|
if (!text) {
|
|
166
|
-
|
|
167
|
-
setInputValue('');
|
|
176
|
+
if (displayValue) emitChange(null, '');
|
|
177
|
+
setInputValue(isValueControlled ? displayValue : '');
|
|
168
178
|
setOpen(false);
|
|
169
179
|
return;
|
|
170
180
|
}
|
|
@@ -172,18 +182,19 @@ function DatePicker({ value, onChange, picker = 'date', showTime, format: format
|
|
|
172
182
|
const isBlocked = !parsed.isValid() || disabledDate?.(parsed) || null != minDate && parsed.isBefore(minDate, 'day') || null != maxDate && parsed.isAfter(maxDate, 'day');
|
|
173
183
|
if (isBlocked) return void setInputValue(displayValue);
|
|
174
184
|
const formatted = parsed.format(fmt);
|
|
175
|
-
setInputValue(formatted);
|
|
185
|
+
setInputValue(isValueControlled ? displayValue : formatted);
|
|
176
186
|
setViewDate(parsed);
|
|
177
|
-
|
|
187
|
+
if (formatted !== displayValue) emitChange(parsed, formatted);
|
|
178
188
|
setOpen(false);
|
|
179
189
|
}, [
|
|
180
190
|
disabledDate,
|
|
181
191
|
displayValue,
|
|
182
192
|
fmt,
|
|
183
193
|
inputValue,
|
|
194
|
+
isValueControlled,
|
|
184
195
|
maxDate,
|
|
185
196
|
minDate,
|
|
186
|
-
|
|
197
|
+
emitChange,
|
|
187
198
|
setOpen
|
|
188
199
|
]);
|
|
189
200
|
const showTimePanel = !!(showTime && 'date' === panelMode);
|
|
@@ -200,7 +211,7 @@ function DatePicker({ value, onChange, picker = 'date', showTime, format: format
|
|
|
200
211
|
children: [
|
|
201
212
|
'date' === panelMode && /*#__PURE__*/ jsx(CalendarGrid, {
|
|
202
213
|
viewDate: viewDate,
|
|
203
|
-
value:
|
|
214
|
+
value: resolvedValue,
|
|
204
215
|
onSelect: handleDateSelect,
|
|
205
216
|
onViewDateChange: setViewDate,
|
|
206
217
|
disabledDate: disabledDate,
|
|
@@ -213,13 +224,13 @@ function DatePicker({ value, onChange, picker = 'date', showTime, format: format
|
|
|
213
224
|
}),
|
|
214
225
|
'month' === panelMode && /*#__PURE__*/ jsx(MonthGrid, {
|
|
215
226
|
viewDate: viewDate,
|
|
216
|
-
value:
|
|
227
|
+
value: resolvedValue,
|
|
217
228
|
onSelect: handleMonthSelect,
|
|
218
229
|
disabledDate: disabledDate
|
|
219
230
|
}),
|
|
220
231
|
('year' === panelMode || 'decade' === panelMode) && /*#__PURE__*/ jsx(YearGrid, {
|
|
221
232
|
viewDate: viewDate,
|
|
222
|
-
value:
|
|
233
|
+
value: resolvedValue,
|
|
223
234
|
onSelect: handleYearSelect,
|
|
224
235
|
disabledDate: disabledDate
|
|
225
236
|
})
|
|
@@ -378,12 +389,15 @@ function DatePicker({ value, onChange, picker = 'date', showTime, format: format
|
|
|
378
389
|
disabled: resolvedDisabled,
|
|
379
390
|
"aria-invalid": 'error' === status || void 0,
|
|
380
391
|
onChange: (event_1)=>setInputValue(event_1.target.value),
|
|
381
|
-
onBlur:
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
392
|
+
onBlur: (event_2)=>{
|
|
393
|
+
if (event_2.relatedTarget instanceof HTMLElement && event_2.relatedTarget.closest('[data-tao-picker-dropdown]')) return;
|
|
394
|
+
commitInputValue();
|
|
395
|
+
},
|
|
396
|
+
onKeyDown: (event_3)=>{
|
|
397
|
+
if ('Enter' === event_3.key) {
|
|
398
|
+
event_3.preventDefault();
|
|
385
399
|
commitInputValue();
|
|
386
|
-
} else if ('Escape' ===
|
|
400
|
+
} else if ('Escape' === event_3.key) {
|
|
387
401
|
setInputValue(displayValue);
|
|
388
402
|
setOpen(false);
|
|
389
403
|
}
|
|
@@ -4,9 +4,9 @@ import dayjs from "dayjs";
|
|
|
4
4
|
import customParseFormat from "dayjs/plugin/customParseFormat";
|
|
5
5
|
import { useCallback, useEffect, useImperativeHandle, useRef, useState } from "react";
|
|
6
6
|
import { TaoPortalScope, useTaoConfig } from "../../provider/tao-provider.js";
|
|
7
|
+
import { useIsMobile } from "../../utils/breakpoint.js";
|
|
7
8
|
import { cx } from "../../utils/semantic.js";
|
|
8
9
|
import { Drawer } from "../drawer/drawer.js";
|
|
9
|
-
import { useIsMobile } from "../../utils/breakpoint.js";
|
|
10
10
|
import { CalendarGrid } from "./calendar/calendar-grid.js";
|
|
11
11
|
import "./date-picker.css";
|
|
12
12
|
import { Presets } from "./presets.js";
|
|
@@ -53,25 +53,53 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
53
53
|
]);
|
|
54
54
|
const [internalOpen, setInternalOpen] = useState(false);
|
|
55
55
|
const isOpen = controlledOpen ?? internalOpen;
|
|
56
|
+
const [interaction, setInteraction] = useState({
|
|
57
|
+
phase: 'idle',
|
|
58
|
+
activeInput: 'start'
|
|
59
|
+
});
|
|
60
|
+
const controlledValueKey = isValueControlled ? `${resolvedValue?.[0]?.valueOf() ?? 'null'}:${resolvedValue?.[1]?.valueOf() ?? 'null'}` : void 0;
|
|
56
61
|
const setOpen = useCallback((v)=>{
|
|
57
62
|
setInternalOpen(v);
|
|
58
63
|
onOpenChange?.(v);
|
|
59
64
|
}, [
|
|
60
65
|
onOpenChange
|
|
61
66
|
]);
|
|
67
|
+
useEffect(()=>{
|
|
68
|
+
if (!isOpen) setInteraction({
|
|
69
|
+
phase: 'idle',
|
|
70
|
+
activeInput: 'start'
|
|
71
|
+
});
|
|
72
|
+
}, [
|
|
73
|
+
isOpen
|
|
74
|
+
]);
|
|
75
|
+
useEffect(()=>{
|
|
76
|
+
if (void 0 !== controlledValueKey) setInteraction({
|
|
77
|
+
phase: 'idle',
|
|
78
|
+
activeInput: 'start'
|
|
79
|
+
});
|
|
80
|
+
}, [
|
|
81
|
+
controlledValueKey
|
|
82
|
+
]);
|
|
62
83
|
const [viewDateStart, setViewDateStart] = useState(()=>resolvedValue?.[0] ?? dayjs());
|
|
63
84
|
const viewDateEnd = viewDateStart.add(1, 'month');
|
|
64
|
-
const
|
|
65
|
-
const [
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
85
|
+
const rangeStart = 'pickingEnd' === interaction.phase ? interaction.start : resolvedValue?.[0] ?? null;
|
|
86
|
+
const rangeEnd = 'pickingEnd' === interaction.phase ? null : resolvedValue?.[1] ?? null;
|
|
87
|
+
const hoverDate = 'pickingEnd' === interaction.phase ? interaction.hover : null;
|
|
88
|
+
const setHoverDate = useCallback((hover)=>{
|
|
89
|
+
setInteraction((current)=>'pickingEnd' === current.phase ? {
|
|
90
|
+
...current,
|
|
91
|
+
hover
|
|
92
|
+
} : current);
|
|
93
|
+
}, []);
|
|
69
94
|
const handleDateSelect = useCallback((date)=>{
|
|
70
|
-
if ('start' ===
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
95
|
+
if ('idle' === interaction.phase && 'start' === interaction.activeInput) setInteraction({
|
|
96
|
+
phase: 'pickingEnd',
|
|
97
|
+
activeInput: 'end',
|
|
98
|
+
start: date,
|
|
99
|
+
hover: null
|
|
100
|
+
});
|
|
101
|
+
else {
|
|
102
|
+
let start = 'pickingEnd' === interaction.phase ? interaction.start : rangeStart;
|
|
75
103
|
let end = date;
|
|
76
104
|
if (start && date.isBefore(start)) [start, end] = [
|
|
77
105
|
date,
|
|
@@ -84,14 +112,14 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
84
112
|
start?.format(fmt) ?? '',
|
|
85
113
|
end.format(fmt)
|
|
86
114
|
]);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
115
|
+
setInteraction({
|
|
116
|
+
phase: 'idle',
|
|
117
|
+
activeInput: 'start'
|
|
118
|
+
});
|
|
90
119
|
if (!showTime) setOpen(false);
|
|
91
120
|
}
|
|
92
121
|
}, [
|
|
93
|
-
|
|
94
|
-
pendingStart,
|
|
122
|
+
interaction,
|
|
95
123
|
rangeStart,
|
|
96
124
|
emitChange,
|
|
97
125
|
fmt,
|
|
@@ -104,6 +132,10 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
104
132
|
'',
|
|
105
133
|
''
|
|
106
134
|
]);
|
|
135
|
+
setInteraction({
|
|
136
|
+
phase: 'idle',
|
|
137
|
+
activeInput: 'start'
|
|
138
|
+
});
|
|
107
139
|
}, [
|
|
108
140
|
emitChange
|
|
109
141
|
]);
|
|
@@ -116,6 +148,10 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
116
148
|
s.format(fmt),
|
|
117
149
|
e_0.format(fmt)
|
|
118
150
|
]);
|
|
151
|
+
setInteraction({
|
|
152
|
+
phase: 'idle',
|
|
153
|
+
activeInput: 'start'
|
|
154
|
+
});
|
|
119
155
|
setOpen(false);
|
|
120
156
|
}, [
|
|
121
157
|
emitChange,
|
|
@@ -152,7 +188,10 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
152
188
|
nextStart,
|
|
153
189
|
nextEnd
|
|
154
190
|
] : null, nextStrings);
|
|
155
|
-
|
|
191
|
+
setInteraction({
|
|
192
|
+
phase: 'idle',
|
|
193
|
+
activeInput: position_0
|
|
194
|
+
});
|
|
156
195
|
if (parsed) setViewDateStart(parsed);
|
|
157
196
|
if ('start' === position_0) setInputStart(nextStrings[0]);
|
|
158
197
|
else setInputEnd(nextStrings[1]);
|
|
@@ -334,7 +373,7 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
334
373
|
children: [
|
|
335
374
|
/*#__PURE__*/ jsx("span", {
|
|
336
375
|
"data-tao-range-input": "",
|
|
337
|
-
"data-tao-active": 'start' ===
|
|
376
|
+
"data-tao-active": 'start' === interaction.activeInput && isOpen || void 0,
|
|
338
377
|
className: classNames?.input,
|
|
339
378
|
style: styles?.input,
|
|
340
379
|
children: displayStart || /*#__PURE__*/ jsx("span", {
|
|
@@ -350,7 +389,7 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
350
389
|
}),
|
|
351
390
|
/*#__PURE__*/ jsx("span", {
|
|
352
391
|
"data-tao-range-input": "",
|
|
353
|
-
"data-tao-active": 'end' ===
|
|
392
|
+
"data-tao-active": 'end' === interaction.activeInput && isOpen || void 0,
|
|
354
393
|
className: classNames?.input,
|
|
355
394
|
style: styles?.input,
|
|
356
395
|
children: displayEnd || /*#__PURE__*/ jsx("span", {
|
|
@@ -377,7 +416,7 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
377
416
|
/*#__PURE__*/ jsx("input", {
|
|
378
417
|
ref: startInputRef,
|
|
379
418
|
"data-tao-range-input": "",
|
|
380
|
-
"data-tao-active": 'start' ===
|
|
419
|
+
"data-tao-active": 'start' === interaction.activeInput && isOpen || void 0,
|
|
381
420
|
className: classNames?.input,
|
|
382
421
|
style: styles?.input,
|
|
383
422
|
value: inputStart,
|
|
@@ -385,7 +424,10 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
385
424
|
disabled: resolvedDisabled,
|
|
386
425
|
"aria-invalid": 'error' === status || void 0,
|
|
387
426
|
onFocus: ()=>{
|
|
388
|
-
|
|
427
|
+
setInteraction({
|
|
428
|
+
phase: 'idle',
|
|
429
|
+
activeInput: 'start'
|
|
430
|
+
});
|
|
389
431
|
},
|
|
390
432
|
onChange: (event_1)=>setInputStart(event_1.target.value),
|
|
391
433
|
onBlur: ()=>commitInputValue('start'),
|
|
@@ -408,7 +450,7 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
408
450
|
/*#__PURE__*/ jsx("input", {
|
|
409
451
|
ref: endInputRef,
|
|
410
452
|
"data-tao-range-input": "",
|
|
411
|
-
"data-tao-active": 'end' ===
|
|
453
|
+
"data-tao-active": 'end' === interaction.activeInput && isOpen || void 0,
|
|
412
454
|
className: classNames?.input,
|
|
413
455
|
style: styles?.input,
|
|
414
456
|
value: inputEnd,
|
|
@@ -416,7 +458,10 @@ function RangePicker({ value, defaultValue, onChange, picker = 'date', showTime,
|
|
|
416
458
|
disabled: resolvedDisabled,
|
|
417
459
|
"aria-invalid": 'error' === status || void 0,
|
|
418
460
|
onFocus: ()=>{
|
|
419
|
-
|
|
461
|
+
setInteraction({
|
|
462
|
+
phase: 'idle',
|
|
463
|
+
activeInput: 'end'
|
|
464
|
+
});
|
|
420
465
|
},
|
|
421
466
|
onChange: (event_3)=>setInputEnd(event_3.target.value),
|
|
422
467
|
onBlur: ()=>commitInputValue('end'),
|
|
@@ -3,25 +3,8 @@ import { c } from "react/compiler-runtime";
|
|
|
3
3
|
import { useCallback, useMemo, useState } from "react";
|
|
4
4
|
import { Drawer } from "../drawer/drawer.js";
|
|
5
5
|
import { SearchIcon } from "./select-icons.js";
|
|
6
|
+
import { countLeafOptions, defaultFilter, filterTree } from "./select-model.js";
|
|
6
7
|
import "./mobile-select.css";
|
|
7
|
-
const defaultFilter = (input, option)=>{
|
|
8
|
-
const label = 'string' == typeof option.label ? option.label : String(option.label);
|
|
9
|
-
return label.toLowerCase().includes(input.toLowerCase());
|
|
10
|
-
};
|
|
11
|
-
function filterTree(options, fn, input) {
|
|
12
|
-
const result = [];
|
|
13
|
-
for (const o of options)if (o.options) {
|
|
14
|
-
const kids = filterTree(o.options, fn, input);
|
|
15
|
-
if (kids.length) result.push({
|
|
16
|
-
...o,
|
|
17
|
-
options: kids
|
|
18
|
-
});
|
|
19
|
-
} else if (fn(input, o)) result.push(o);
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
function countLeaves(options) {
|
|
23
|
-
return options.reduce((n, o)=>n + (o.options ? countLeaves(o.options) : 1), 0);
|
|
24
|
-
}
|
|
25
8
|
function MobileSelectContentInner({ options, value, onChange, onMultiChange, showSearch, filterOption, notFoundContent = 'No results', optionRender, onClose, multiple }) {
|
|
26
9
|
const [search, setSearch] = useState('');
|
|
27
10
|
const valueArray = useMemo(()=>{
|
|
@@ -34,6 +17,9 @@ function MobileSelectContentInner({ options, value, onChange, onMultiChange, sho
|
|
|
34
17
|
value
|
|
35
18
|
]);
|
|
36
19
|
const [draft, setDraft] = useState(valueArray);
|
|
20
|
+
const draftSet = useMemo(()=>new Set(draft), [
|
|
21
|
+
draft
|
|
22
|
+
]);
|
|
37
23
|
const filteredOptions = useMemo(()=>{
|
|
38
24
|
if (!showSearch || !search) return options;
|
|
39
25
|
const fn = filterOption ?? defaultFilter;
|
|
@@ -44,6 +30,9 @@ function MobileSelectContentInner({ options, value, onChange, onMultiChange, sho
|
|
|
44
30
|
showSearch,
|
|
45
31
|
filterOption
|
|
46
32
|
]);
|
|
33
|
+
const resultCount = useMemo(()=>countLeafOptions(filteredOptions), [
|
|
34
|
+
filteredOptions
|
|
35
|
+
]);
|
|
47
36
|
const handleSelect = useCallback((optValue)=>{
|
|
48
37
|
if (multiple) setDraft((prev)=>prev.includes(optValue) ? prev.filter((v)=>v !== optValue) : [
|
|
49
38
|
...prev,
|
|
@@ -69,7 +58,7 @@ function MobileSelectContentInner({ options, value, onChange, onMultiChange, sho
|
|
|
69
58
|
let optIndex = 0;
|
|
70
59
|
const renderItem = (opt)=>{
|
|
71
60
|
const index = optIndex++;
|
|
72
|
-
const isSelected = multiple ?
|
|
61
|
+
const isSelected = multiple ? draftSet.has(opt.value) : opt.value === value;
|
|
73
62
|
return /*#__PURE__*/ jsxs("button", {
|
|
74
63
|
type: "button",
|
|
75
64
|
"data-tao-sheet-item": "",
|
|
@@ -137,7 +126,7 @@ function MobileSelectContentInner({ options, value, onChange, onMultiChange, sho
|
|
|
137
126
|
}),
|
|
138
127
|
/*#__PURE__*/ jsx("div", {
|
|
139
128
|
"data-tao-sheet-list": "",
|
|
140
|
-
children:
|
|
129
|
+
children: resultCount > 0 ? renderTree(filteredOptions) : /*#__PURE__*/ jsx("div", {
|
|
141
130
|
"data-tao-sheet-empty": "",
|
|
142
131
|
children: notFoundContent
|
|
143
132
|
})
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { SelectOption } from './select-types';
|
|
2
|
+
export interface SelectIndex {
|
|
3
|
+
optionByValue: Map<string, SelectOption>;
|
|
4
|
+
optionByToken: Map<string, SelectOption>;
|
|
5
|
+
}
|
|
6
|
+
export declare function buildSelectIndex(options: SelectOption[]): SelectIndex;
|
|
7
|
+
export declare function flattenLeafOptions(options: SelectOption[]): SelectOption[];
|
|
8
|
+
export declare function countLeafOptions(options: SelectOption[]): number;
|
|
2
9
|
export declare function defaultFilter(input: string, option: SelectOption): boolean;
|
|
3
|
-
export declare function flattenOptions(options: SelectOption[]): SelectOption[];
|
|
4
10
|
export declare function filterTree(options: SelectOption[], filter: (input: string, option: SelectOption) => boolean, input: string): SelectOption[];
|
|
@@ -1,13 +1,36 @@
|
|
|
1
|
-
function
|
|
2
|
-
const
|
|
3
|
-
|
|
1
|
+
function forEachLeafOption(options, visit) {
|
|
2
|
+
for (const option of options)if (option.options) forEachLeafOption(option.options, visit);
|
|
3
|
+
else visit(option);
|
|
4
4
|
}
|
|
5
|
-
function
|
|
5
|
+
function buildSelectIndex(options) {
|
|
6
|
+
const optionByValue = new Map();
|
|
7
|
+
const optionByToken = new Map();
|
|
8
|
+
forEachLeafOption(options, (option)=>{
|
|
9
|
+
if (!optionByValue.has(option.value)) optionByValue.set(option.value, option);
|
|
10
|
+
if (!optionByToken.has(option.value)) optionByToken.set(option.value, option);
|
|
11
|
+
if ('string' == typeof option.label && !optionByToken.has(option.label)) optionByToken.set(option.label, option);
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
optionByValue,
|
|
15
|
+
optionByToken
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function flattenLeafOptions(options) {
|
|
6
19
|
const result = [];
|
|
7
|
-
|
|
8
|
-
else result.push(option);
|
|
20
|
+
forEachLeafOption(options, (option)=>result.push(option));
|
|
9
21
|
return result;
|
|
10
22
|
}
|
|
23
|
+
function countLeafOptions(options) {
|
|
24
|
+
let count = 0;
|
|
25
|
+
forEachLeafOption(options, ()=>{
|
|
26
|
+
count += 1;
|
|
27
|
+
});
|
|
28
|
+
return count;
|
|
29
|
+
}
|
|
30
|
+
function defaultFilter(input, option) {
|
|
31
|
+
const label = 'string' == typeof option.label ? option.label : String(option.label);
|
|
32
|
+
return label.toLowerCase().includes(input.toLowerCase());
|
|
33
|
+
}
|
|
11
34
|
function filterTree(options, filter, input) {
|
|
12
35
|
const result = [];
|
|
13
36
|
for (const option of options)if (option.options) {
|
|
@@ -19,4 +42,4 @@ function filterTree(options, filter, input) {
|
|
|
19
42
|
} else if (filter(input, option)) result.push(option);
|
|
20
43
|
return result;
|
|
21
44
|
}
|
|
22
|
-
export { defaultFilter, filterTree,
|
|
45
|
+
export { buildSelectIndex, countLeafOptions, defaultFilter, filterTree, flattenLeafOptions };
|
|
@@ -4,7 +4,7 @@ import { TaoPortalScope, useTaoConfig } from "../../provider/tao-provider.js";
|
|
|
4
4
|
import { cx } from "../../utils/semantic.js";
|
|
5
5
|
import { MobileSelectDrawer } from "./mobile-select.js";
|
|
6
6
|
import { CheckIcon, ChevronDownIcon, CloseIcon, LoadingIcon, SearchIcon } from "./select-icons.js";
|
|
7
|
-
import { defaultFilter, filterTree,
|
|
7
|
+
import { buildSelectIndex, defaultFilter, filterTree, flattenLeafOptions } from "./select-model.js";
|
|
8
8
|
import { SearchableSelectList } from "./select-searchable-list.js";
|
|
9
9
|
import { SelectTags } from "./select-tags.js";
|
|
10
10
|
import "./select.css";
|
|
@@ -72,9 +72,12 @@ function Select(props) {
|
|
|
72
72
|
const [activeSearchIndex, setActiveSearchIndex] = useState(0);
|
|
73
73
|
const [hovering, setHovering] = useState(false);
|
|
74
74
|
const suppressCloseRef = useRef(false);
|
|
75
|
-
const
|
|
75
|
+
const { optionByToken, optionByValue } = useMemo(()=>buildSelectIndex(options), [
|
|
76
76
|
options
|
|
77
77
|
]);
|
|
78
|
+
const selectedValueSet = useMemo(()=>new Set(valueArray), [
|
|
79
|
+
valueArray
|
|
80
|
+
]);
|
|
78
81
|
const handleValueChange = useCallback((next)=>{
|
|
79
82
|
if (isMultiple) {
|
|
80
83
|
if (tokenSeparators && tokenSeparators.length > 0) {
|
|
@@ -82,8 +85,8 @@ function Select(props) {
|
|
|
82
85
|
if (sep) {
|
|
83
86
|
const tokens = next.split(sep).map((t)=>t.trim()).filter(Boolean);
|
|
84
87
|
const toAdd = tokens.filter((t_0)=>{
|
|
85
|
-
const opt =
|
|
86
|
-
return opt && !
|
|
88
|
+
const opt = optionByToken.get(t_0);
|
|
89
|
+
return opt && !selectedValueSet.has(opt.value);
|
|
87
90
|
});
|
|
88
91
|
if (toAdd.length > 0) {
|
|
89
92
|
const nextArray = [
|
|
@@ -92,10 +95,10 @@ function Select(props) {
|
|
|
92
95
|
];
|
|
93
96
|
if (null != maxCount && nextArray.length > maxCount) return;
|
|
94
97
|
if (!isValueControlled) setValueInternal(nextArray);
|
|
95
|
-
const selectedOpts = nextArray.map((v)=>
|
|
98
|
+
const selectedOpts = nextArray.map((v)=>optionByValue.get(v)).filter(Boolean);
|
|
96
99
|
onChange?.(nextArray, selectedOpts);
|
|
97
100
|
for (const v_0 of toAdd){
|
|
98
|
-
const opt_0 =
|
|
101
|
+
const opt_0 = optionByValue.get(v_0);
|
|
99
102
|
if (opt_0) onSelect?.(v_0, opt_0);
|
|
100
103
|
}
|
|
101
104
|
}
|
|
@@ -104,22 +107,22 @@ function Select(props) {
|
|
|
104
107
|
}
|
|
105
108
|
if (!hasInlineSearch) suppressCloseRef.current = true;
|
|
106
109
|
const current = valueArray;
|
|
107
|
-
const existed =
|
|
110
|
+
const existed = selectedValueSet.has(next);
|
|
108
111
|
const nextArray_0 = existed ? current.filter((v_1)=>v_1 !== next) : [
|
|
109
112
|
...current,
|
|
110
113
|
next
|
|
111
114
|
];
|
|
112
115
|
if (!existed && null != maxCount && nextArray_0.length > maxCount) return;
|
|
113
116
|
if (!isValueControlled) setValueInternal(nextArray_0);
|
|
114
|
-
const selectedOpts_0 = nextArray_0.map((v_2)=>
|
|
117
|
+
const selectedOpts_0 = nextArray_0.map((v_2)=>optionByValue.get(v_2)).filter(Boolean);
|
|
115
118
|
onChange?.(nextArray_0, selectedOpts_0);
|
|
116
|
-
const opt_1 =
|
|
119
|
+
const opt_1 = optionByValue.get(next);
|
|
117
120
|
if (opt_1) if (existed) onDeselect?.(next, opt_1);
|
|
118
121
|
else onSelect?.(next, opt_1);
|
|
119
122
|
if (autoClearSearchValue && !isSearchControlled) setSearchInternal('');
|
|
120
123
|
return;
|
|
121
124
|
}
|
|
122
|
-
const opt_2 =
|
|
125
|
+
const opt_2 = optionByValue.get(next);
|
|
123
126
|
if (!isValueControlled) setValueInternal(next);
|
|
124
127
|
onChange?.(next, opt_2);
|
|
125
128
|
if (opt_2) onSelect?.(next, opt_2);
|
|
@@ -130,7 +133,9 @@ function Select(props) {
|
|
|
130
133
|
maxCount,
|
|
131
134
|
tokenSeparators,
|
|
132
135
|
isValueControlled,
|
|
133
|
-
|
|
136
|
+
optionByToken,
|
|
137
|
+
optionByValue,
|
|
138
|
+
selectedValueSet,
|
|
134
139
|
onChange,
|
|
135
140
|
onSelect,
|
|
136
141
|
onDeselect,
|
|
@@ -204,35 +209,37 @@ function Select(props) {
|
|
|
204
209
|
if (!isMultiple) return;
|
|
205
210
|
const nextArray_1 = valueArray.filter((v_4)=>v_4 !== removedValue);
|
|
206
211
|
if (!isValueControlled) setValueInternal(nextArray_1);
|
|
207
|
-
const selectedOpts_1 = nextArray_1.map((v_5)=>
|
|
212
|
+
const selectedOpts_1 = nextArray_1.map((v_5)=>optionByValue.get(v_5)).filter(Boolean);
|
|
208
213
|
onChange?.(nextArray_1, selectedOpts_1);
|
|
209
|
-
const opt_3 =
|
|
214
|
+
const opt_3 = optionByValue.get(removedValue);
|
|
210
215
|
if (opt_3) onDeselect?.(removedValue, opt_3);
|
|
211
216
|
}, [
|
|
212
217
|
isMultiple,
|
|
213
218
|
valueArray,
|
|
214
219
|
isValueControlled,
|
|
215
|
-
|
|
220
|
+
optionByValue,
|
|
216
221
|
onChange,
|
|
217
222
|
onDeselect
|
|
218
223
|
]);
|
|
219
224
|
const handleMultiChange = useCallback((nextArray_2)=>{
|
|
220
225
|
if (!isValueControlled) setValueInternal(nextArray_2);
|
|
221
|
-
const selectedOpts_2 = nextArray_2.map((v_6)=>
|
|
226
|
+
const selectedOpts_2 = nextArray_2.map((v_6)=>optionByValue.get(v_6)).filter(Boolean);
|
|
222
227
|
onChange?.(nextArray_2, selectedOpts_2);
|
|
223
228
|
const prev = valueArray;
|
|
224
|
-
|
|
225
|
-
|
|
229
|
+
const nextSet = new Set(nextArray_2);
|
|
230
|
+
for (const v_7 of nextArray_2)if (!selectedValueSet.has(v_7)) {
|
|
231
|
+
const opt_4 = optionByValue.get(v_7);
|
|
226
232
|
if (opt_4) onSelect?.(v_7, opt_4);
|
|
227
233
|
}
|
|
228
|
-
for (const v_8 of prev)if (!
|
|
229
|
-
const opt_5 =
|
|
234
|
+
for (const v_8 of prev)if (!nextSet.has(v_8)) {
|
|
235
|
+
const opt_5 = optionByValue.get(v_8);
|
|
230
236
|
if (opt_5) onDeselect?.(v_8, opt_5);
|
|
231
237
|
}
|
|
232
238
|
}, [
|
|
233
239
|
valueArray,
|
|
240
|
+
selectedValueSet,
|
|
234
241
|
isValueControlled,
|
|
235
|
-
|
|
242
|
+
optionByValue,
|
|
236
243
|
onChange,
|
|
237
244
|
onSelect,
|
|
238
245
|
onDeselect
|
|
@@ -242,13 +249,13 @@ function Select(props) {
|
|
|
242
249
|
if (hideSelected && isMultiple && valueArray.length > 0) {
|
|
243
250
|
const filterSelected = (list)=>{
|
|
244
251
|
const result = [];
|
|
245
|
-
for (const
|
|
246
|
-
const kids = filterSelected(
|
|
252
|
+
for (const o of list)if (o.options) {
|
|
253
|
+
const kids = filterSelected(o.options);
|
|
247
254
|
if (kids.length) result.push({
|
|
248
|
-
...
|
|
255
|
+
...o,
|
|
249
256
|
options: kids
|
|
250
257
|
});
|
|
251
|
-
} else if (!
|
|
258
|
+
} else if (!selectedValueSet.has(o.value)) result.push(o);
|
|
252
259
|
return result;
|
|
253
260
|
};
|
|
254
261
|
opts = filterSelected(opts);
|
|
@@ -263,23 +270,27 @@ function Select(props) {
|
|
|
263
270
|
filterOption,
|
|
264
271
|
hideSelected,
|
|
265
272
|
isMultiple,
|
|
266
|
-
|
|
273
|
+
selectedValueSet,
|
|
274
|
+
valueArray.length
|
|
275
|
+
]);
|
|
276
|
+
const filteredFlatOptions = useMemo(()=>flattenLeafOptions(filteredOptions), [
|
|
277
|
+
filteredOptions
|
|
267
278
|
]);
|
|
268
279
|
const allowClearEnabled = !!allowClear;
|
|
269
280
|
const clearIcon = 'object' == typeof allowClear && allowClear.clearIcon || /*#__PURE__*/ jsx(CloseIcon, {});
|
|
270
281
|
const showClearBtn = allowClearEnabled && (isMultiple ? valueArray.length > 0 : !!value) && !resolvedDisabled;
|
|
271
|
-
const selectedOption = useMemo(()=>!isMultiple && value ?
|
|
282
|
+
const selectedOption = useMemo(()=>!isMultiple && value ? optionByValue.get(value) : void 0, [
|
|
272
283
|
isMultiple,
|
|
273
284
|
value,
|
|
274
|
-
|
|
285
|
+
optionByValue
|
|
275
286
|
]);
|
|
276
|
-
const selectedOptions = useMemo(()=>isMultiple ? valueArray.map((v_9)=>
|
|
287
|
+
const selectedOptions = useMemo(()=>isMultiple ? valueArray.map((v_9)=>optionByValue.get(v_9) ?? ('tags' === mode ? {
|
|
277
288
|
label: v_9,
|
|
278
289
|
value: v_9
|
|
279
290
|
} : void 0)).filter(Boolean) : [], [
|
|
280
291
|
isMultiple,
|
|
281
292
|
valueArray,
|
|
282
|
-
|
|
293
|
+
optionByValue,
|
|
283
294
|
mode
|
|
284
295
|
]);
|
|
285
296
|
const statusProps = {
|
|
@@ -449,13 +460,13 @@ function Select(props) {
|
|
|
449
460
|
};
|
|
450
461
|
if ('number' == typeof popupMatchSelectWidth) contentStyle.width = popupMatchSelectWidth;
|
|
451
462
|
const radixValue = isMultiple ? '' : value || void 0;
|
|
452
|
-
const resultCount =
|
|
463
|
+
const resultCount = filteredFlatOptions.length;
|
|
453
464
|
const overMaxCount = isMultiple && null != maxCount && valueArray.length >= maxCount;
|
|
454
465
|
let optIndex = 0;
|
|
455
466
|
const renderItem = (opt_6)=>{
|
|
456
467
|
const index = optIndex++;
|
|
457
468
|
const textLabel = 'string' == typeof opt_6.label ? opt_6.label : String(opt_6.label);
|
|
458
|
-
const isSelected =
|
|
469
|
+
const isSelected = selectedValueSet.has(opt_6.value);
|
|
459
470
|
const itemDisabled = opt_6.disabled || !isSelected && overMaxCount;
|
|
460
471
|
return /*#__PURE__*/ jsxs(__rspack_external__radix_ui_react_select_4606f4d3.Item, {
|
|
461
472
|
value: opt_6.value,
|
|
@@ -514,7 +525,7 @@ function Select(props) {
|
|
|
514
525
|
]
|
|
515
526
|
}, opt_6.value);
|
|
516
527
|
};
|
|
517
|
-
const renderTree = (opts_0)=>opts_0.map((
|
|
528
|
+
const renderTree = (opts_0)=>opts_0.map((o_0)=>o_0.options ? /*#__PURE__*/ jsxs(__rspack_external__radix_ui_react_select_4606f4d3.Group, {
|
|
518
529
|
"data-tao-select-group": "",
|
|
519
530
|
className: cx(classNames?.group),
|
|
520
531
|
style: styles?.group,
|
|
@@ -523,11 +534,11 @@ function Select(props) {
|
|
|
523
534
|
"data-tao-select-group-label": "",
|
|
524
535
|
className: cx(classNames?.groupLabel),
|
|
525
536
|
style: styles?.groupLabel,
|
|
526
|
-
children:
|
|
537
|
+
children: o_0.label
|
|
527
538
|
}),
|
|
528
|
-
|
|
539
|
+
o_0.options.map(renderItem)
|
|
529
540
|
]
|
|
530
|
-
},
|
|
541
|
+
}, o_0.value) : renderItem(o_0));
|
|
531
542
|
const menuNode = /*#__PURE__*/ jsx(__rspack_external__radix_ui_react_select_4606f4d3.Viewport, {
|
|
532
543
|
"data-tao-select-viewport": "",
|
|
533
544
|
style: {
|
|
@@ -540,8 +551,8 @@ function Select(props) {
|
|
|
540
551
|
children: notFoundContent
|
|
541
552
|
})
|
|
542
553
|
});
|
|
543
|
-
const isSearchableOptionDisabled = (option)=>!!option.disabled || overMaxCount && !
|
|
544
|
-
const searchableOptions =
|
|
554
|
+
const isSearchableOptionDisabled = (option)=>!!option.disabled || overMaxCount && !selectedValueSet.has(option.value);
|
|
555
|
+
const searchableOptions = filteredFlatOptions.filter((option_0)=>!isSearchableOptionDisabled(option_0));
|
|
545
556
|
const activeSearchOption = searchableOptions[Math.min(activeSearchIndex, Math.max(searchableOptions.length - 1, 0))];
|
|
546
557
|
const searchableMenuNode = /*#__PURE__*/ jsx(SearchableSelectList, {
|
|
547
558
|
id: searchListboxId,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type SemanticClassNames, type SemanticStyles } from '../../utils/semantic';
|
|
2
1
|
import type { CSSProperties, ReactNode } from 'react';
|
|
2
|
+
import { type SemanticClassNames, type SemanticStyles } from '../../utils/semantic';
|
|
3
3
|
import './sonner.css';
|
|
4
4
|
export type SonnerSemanticPart = 'viewport' | 'toast' | 'content' | 'title' | 'description' | 'actions' | 'action' | 'cancel' | 'close';
|
|
5
5
|
export type SonnerType = 'default' | 'success' | 'error' | 'warning' | 'info' | 'loading' | 'custom';
|
|
@@ -27,6 +27,7 @@ export interface SonnerToastItem extends SonnerToastOptions {
|
|
|
27
27
|
type: SonnerType;
|
|
28
28
|
createdAt: number;
|
|
29
29
|
custom?: ReactNode;
|
|
30
|
+
/** @deprecated Toast lifecycle state is internal and no longer exposed. */
|
|
30
31
|
removing?: boolean;
|
|
31
32
|
}
|
|
32
33
|
export interface SonnerProps {
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
3
|
import { TaoPortalScope, useTaoConfig } from "../../provider/tao-provider.js";
|
|
3
4
|
import { cx } from "../../utils/semantic.js";
|
|
4
5
|
import { Button } from "../button/button.js";
|
|
5
|
-
import { useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
6
6
|
import "./sonner.css";
|
|
7
7
|
const listeners = new Set();
|
|
8
8
|
let state = {
|
|
9
9
|
toasts: []
|
|
10
10
|
};
|
|
11
11
|
let seed = 0;
|
|
12
|
-
|
|
13
|
-
const
|
|
12
|
+
let generationSeed = 0;
|
|
13
|
+
const runtimes = new Map();
|
|
14
14
|
const REMOVE_DELAY = 220;
|
|
15
15
|
function emit() {
|
|
16
16
|
listeners.forEach((listener)=>{
|
|
@@ -24,69 +24,47 @@ function subscribe(listener) {
|
|
|
24
24
|
function getSnapshot() {
|
|
25
25
|
return state;
|
|
26
26
|
}
|
|
27
|
-
function
|
|
28
|
-
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function clearRemoveTimer(id) {
|
|
35
|
-
const timer = removeTimers.get(id);
|
|
36
|
-
if (timer) {
|
|
37
|
-
clearTimeout(timer);
|
|
38
|
-
removeTimers.delete(id);
|
|
39
|
-
}
|
|
27
|
+
function clearRuntimeTimers(runtime) {
|
|
28
|
+
if (runtime.dismissTimer) clearTimeout(runtime.dismissTimer);
|
|
29
|
+
if (runtime.removeTimer) clearTimeout(runtime.removeTimer);
|
|
30
|
+
runtime.dismissTimer = void 0;
|
|
31
|
+
runtime.removeTimer = void 0;
|
|
40
32
|
}
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
});
|
|
46
|
-
state.toasts.forEach((toast)=>{
|
|
47
|
-
clearRemoveTimer(toast.id);
|
|
48
|
-
});
|
|
49
|
-
state = {
|
|
50
|
-
toasts: state.toasts.map((toast)=>({
|
|
51
|
-
...toast,
|
|
52
|
-
removing: true
|
|
53
|
-
}))
|
|
54
|
-
};
|
|
55
|
-
emit();
|
|
56
|
-
state.toasts.forEach((toast)=>{
|
|
57
|
-
removeTimers.set(toast.id, setTimeout(()=>{
|
|
58
|
-
removeTimers.delete(toast.id);
|
|
59
|
-
state = {
|
|
60
|
-
toasts: state.toasts.filter((item)=>item.id !== toast.id)
|
|
61
|
-
};
|
|
62
|
-
emit();
|
|
63
|
-
}, REMOVE_DELAY));
|
|
64
|
-
});
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
clearTimer(id);
|
|
68
|
-
clearRemoveTimer(id);
|
|
33
|
+
function beginExit(id) {
|
|
34
|
+
const runtime = runtimes.get(id);
|
|
35
|
+
if (!runtime || !state.toasts.some((toast)=>toast.id === id)) return;
|
|
36
|
+
clearRuntimeTimers(runtime);
|
|
69
37
|
state = {
|
|
70
38
|
toasts: state.toasts.map((toast)=>toast.id === id ? {
|
|
71
39
|
...toast,
|
|
72
|
-
|
|
40
|
+
phase: 'exiting'
|
|
73
41
|
} : toast)
|
|
74
42
|
};
|
|
75
43
|
emit();
|
|
76
|
-
|
|
77
|
-
|
|
44
|
+
const generation = runtime.generation;
|
|
45
|
+
runtime.removeTimer = setTimeout(()=>{
|
|
46
|
+
const current = runtimes.get(id);
|
|
47
|
+
if (!current || current.generation !== generation) return;
|
|
48
|
+
runtimes.delete(id);
|
|
78
49
|
state = {
|
|
79
50
|
toasts: state.toasts.filter((toast)=>toast.id !== id)
|
|
80
51
|
};
|
|
81
52
|
emit();
|
|
82
|
-
}, REMOVE_DELAY)
|
|
53
|
+
}, REMOVE_DELAY);
|
|
54
|
+
}
|
|
55
|
+
function dismiss(id) {
|
|
56
|
+
if (id) return void beginExit(id);
|
|
57
|
+
for (const toast of state.toasts)beginExit(toast.id);
|
|
83
58
|
}
|
|
84
59
|
function scheduleDismiss(id, duration) {
|
|
85
|
-
|
|
60
|
+
const runtime = runtimes.get(id);
|
|
61
|
+
if (!runtime) return;
|
|
62
|
+
if (runtime.dismissTimer) clearTimeout(runtime.dismissTimer);
|
|
86
63
|
if (null == duration || duration <= 0) return;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
64
|
+
const generation = runtime.generation;
|
|
65
|
+
runtime.dismissTimer = setTimeout(()=>{
|
|
66
|
+
if (runtimes.get(id)?.generation === generation) beginExit(id);
|
|
67
|
+
}, duration);
|
|
90
68
|
}
|
|
91
69
|
function isToastOptions(value) {
|
|
92
70
|
return null != value && 'object' == typeof value && !Array.isArray(value) && !('$$typeof' in value);
|
|
@@ -99,12 +77,19 @@ function normalizeOptions(options) {
|
|
|
99
77
|
function create(type, options, custom) {
|
|
100
78
|
const normalized = normalizeOptions(options);
|
|
101
79
|
const id = normalized.id ?? `toast-${Date.now()}-${seed++}`;
|
|
80
|
+
const previousRuntime = runtimes.get(id);
|
|
81
|
+
if (previousRuntime) clearRuntimeTimers(previousRuntime);
|
|
82
|
+
const runtime = {
|
|
83
|
+
generation: generationSeed++
|
|
84
|
+
};
|
|
85
|
+
runtimes.set(id, runtime);
|
|
102
86
|
const next = {
|
|
103
87
|
...normalized,
|
|
104
88
|
id,
|
|
105
89
|
type,
|
|
106
90
|
createdAt: Date.now(),
|
|
107
|
-
custom
|
|
91
|
+
custom,
|
|
92
|
+
phase: 'active'
|
|
108
93
|
};
|
|
109
94
|
state = {
|
|
110
95
|
toasts: [
|
|
@@ -116,6 +101,13 @@ function create(type, options, custom) {
|
|
|
116
101
|
scheduleDismiss(id, normalized.duration);
|
|
117
102
|
return id;
|
|
118
103
|
}
|
|
104
|
+
function replacePromiseToast(id, type, options) {
|
|
105
|
+
const normalized = normalizeOptions(options);
|
|
106
|
+
create(type, {
|
|
107
|
+
...normalized,
|
|
108
|
+
id
|
|
109
|
+
});
|
|
110
|
+
}
|
|
119
111
|
function resolveDataType(type, richColors) {
|
|
120
112
|
if ('custom' === type) return 'custom';
|
|
121
113
|
if ('loading' === type) return 'loading';
|
|
@@ -146,9 +138,12 @@ function renderPromiseState(stateValue, payload) {
|
|
|
146
138
|
function Sonner({ position = 'bottom-right', visibleToasts = 3, offset = 24, gap = 12, expand = true, richColors = false, closeButton = false, duration = 4000, className, style, classNames, styles }) {
|
|
147
139
|
useTaoConfig();
|
|
148
140
|
const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
149
|
-
const [heights, setHeights] = useState(
|
|
141
|
+
const [heights, setHeights] = useState(()=>new Map());
|
|
150
142
|
useEffect(()=>{
|
|
151
|
-
for (const toast of snapshot.toasts)
|
|
143
|
+
for (const toast of snapshot.toasts){
|
|
144
|
+
const runtime = runtimes.get(toast.id);
|
|
145
|
+
if ('active' === toast.phase && runtime && !runtime.dismissTimer) scheduleDismiss(toast.id, toast.duration ?? duration);
|
|
146
|
+
}
|
|
152
147
|
}, [
|
|
153
148
|
duration,
|
|
154
149
|
snapshot.toasts
|
|
@@ -162,7 +157,8 @@ function Sonner({ position = 'bottom-right', visibleToasts = 3, offset = 24, gap
|
|
|
162
157
|
snapshot.toasts,
|
|
163
158
|
visibleToasts
|
|
164
159
|
]);
|
|
165
|
-
const frontToastHeight = heights[0]?.
|
|
160
|
+
const frontToastHeight = heights.get(visible[0]?.id ?? '') ?? 0;
|
|
161
|
+
let nextOffset = 0;
|
|
166
162
|
return /*#__PURE__*/ jsx(TaoPortalScope, {
|
|
167
163
|
children: /*#__PURE__*/ jsx("div", {
|
|
168
164
|
className: cx(classNames?.viewport, className),
|
|
@@ -178,6 +174,8 @@ function Sonner({ position = 'bottom-right', visibleToasts = 3, offset = 24, gap
|
|
|
178
174
|
children: visible.map((toast_0, index)=>{
|
|
179
175
|
const showClose = toast_0.closable ?? closeButton;
|
|
180
176
|
const yPosition = position.startsWith('top') ? 'top' : 'bottom';
|
|
177
|
+
const toastOffset = nextOffset;
|
|
178
|
+
nextOffset += (heights.get(toast_0.id) ?? 0) + gap;
|
|
181
179
|
return /*#__PURE__*/ jsx(ToastView, {
|
|
182
180
|
toast: toast_0,
|
|
183
181
|
richColors: richColors,
|
|
@@ -186,8 +184,7 @@ function Sonner({ position = 'bottom-right', visibleToasts = 3, offset = 24, gap
|
|
|
186
184
|
index: index,
|
|
187
185
|
visibleToasts: visible.length,
|
|
188
186
|
expanded: expand,
|
|
189
|
-
|
|
190
|
-
heights: heights,
|
|
187
|
+
offset: toastOffset,
|
|
191
188
|
frontToastHeight: frontToastHeight,
|
|
192
189
|
setHeights: setHeights,
|
|
193
190
|
classNames: classNames,
|
|
@@ -197,41 +194,37 @@ function Sonner({ position = 'bottom-right', visibleToasts = 3, offset = 24, gap
|
|
|
197
194
|
})
|
|
198
195
|
});
|
|
199
196
|
}
|
|
200
|
-
function ToastView({ toast, richColors, showClose, yPosition, index, visibleToasts, expanded,
|
|
197
|
+
function ToastView({ toast, richColors, showClose, yPosition, index, visibleToasts, expanded, offset, frontToastHeight, setHeights, classNames, styles }) {
|
|
201
198
|
const [mounted, setMounted] = useState(false);
|
|
202
199
|
const [offsetBeforeRemove, setOffsetBeforeRemove] = useState(0);
|
|
203
200
|
const ref = useRef(null);
|
|
204
|
-
const offset = useMemo(()=>heights.slice(0, index).reduce((sum, item)=>sum + item.height, 0) + index * gap, [
|
|
205
|
-
gap,
|
|
206
|
-
heights,
|
|
207
|
-
index
|
|
208
|
-
]);
|
|
209
201
|
useEffect(()=>{
|
|
210
202
|
const frame = requestAnimationFrame(()=>setMounted(true));
|
|
211
203
|
return ()=>cancelAnimationFrame(frame);
|
|
212
204
|
}, []);
|
|
213
205
|
useEffect(()=>{
|
|
214
|
-
if (toast.
|
|
206
|
+
if ('exiting' === toast.phase) setOffsetBeforeRemove(offset);
|
|
215
207
|
}, [
|
|
216
208
|
offset,
|
|
217
|
-
toast.
|
|
209
|
+
toast.phase
|
|
218
210
|
]);
|
|
219
211
|
useLayoutEffect(()=>{
|
|
220
212
|
const node = ref.current;
|
|
221
213
|
if (!node) return;
|
|
222
214
|
const nextHeight = node.getBoundingClientRect().height;
|
|
223
215
|
setHeights((current)=>{
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
id: toast.id,
|
|
229
|
-
height: nextHeight
|
|
230
|
-
}
|
|
231
|
-
];
|
|
216
|
+
if (current.get(toast.id) === nextHeight) return current;
|
|
217
|
+
const next = new Map(current);
|
|
218
|
+
next.set(toast.id, nextHeight);
|
|
219
|
+
return next;
|
|
232
220
|
});
|
|
233
221
|
return ()=>{
|
|
234
|
-
setHeights((current_0)=>
|
|
222
|
+
setHeights((current_0)=>{
|
|
223
|
+
if (!current_0.has(toast.id)) return current_0;
|
|
224
|
+
const next_0 = new Map(current_0);
|
|
225
|
+
next_0.delete(toast.id);
|
|
226
|
+
return next_0;
|
|
227
|
+
});
|
|
235
228
|
};
|
|
236
229
|
}, [
|
|
237
230
|
setHeights,
|
|
@@ -244,7 +237,7 @@ function ToastView({ toast, richColors, showClose, yPosition, index, visibleToas
|
|
|
244
237
|
]);
|
|
245
238
|
const isFront = 0 === index;
|
|
246
239
|
const isVisible = index < visibleToasts;
|
|
247
|
-
const activeOffset = toast.
|
|
240
|
+
const activeOffset = 'exiting' === toast.phase ? offsetBeforeRemove : offset;
|
|
248
241
|
return /*#__PURE__*/ jsx("div", {
|
|
249
242
|
ref: ref,
|
|
250
243
|
className: cx(classNames?.toast),
|
|
@@ -255,7 +248,7 @@ function ToastView({ toast, richColors, showClose, yPosition, index, visibleToas
|
|
|
255
248
|
"data-tao-type": resolveDataType(toast.type, richColors),
|
|
256
249
|
"data-tao-mounted": mounted,
|
|
257
250
|
"data-tao-y-position": yPosition,
|
|
258
|
-
"data-tao-removing": toast.
|
|
251
|
+
"data-tao-removing": 'exiting' === toast.phase ? 'true' : 'false',
|
|
259
252
|
"data-tao-front": isFront ? 'true' : 'false',
|
|
260
253
|
"data-tao-expanded": expanded ? 'true' : 'false',
|
|
261
254
|
"data-tao-visible": isVisible ? 'true' : 'false',
|
|
@@ -354,14 +347,14 @@ const sonner_toast = Object.assign(baseToast, {
|
|
|
354
347
|
});
|
|
355
348
|
try {
|
|
356
349
|
const value = await promise;
|
|
357
|
-
dismiss(id);
|
|
358
350
|
const success = renderPromiseState(messages.success, value);
|
|
359
|
-
if (success)
|
|
351
|
+
if (success) replacePromiseToast(id, 'success', success);
|
|
352
|
+
else dismiss(id);
|
|
360
353
|
return value;
|
|
361
354
|
} catch (error) {
|
|
362
|
-
dismiss(id);
|
|
363
355
|
const failure = renderPromiseState(messages.error, error);
|
|
364
|
-
if (failure)
|
|
356
|
+
if (failure) replacePromiseToast(id, 'error', failure);
|
|
357
|
+
else dismiss(id);
|
|
365
358
|
throw error;
|
|
366
359
|
}
|
|
367
360
|
},
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { alertDoc } from "./components/alert.doc.js";
|
|
1
2
|
import { breadcrumbDoc } from "./components/breadcrumb.doc.js";
|
|
2
3
|
import { buttonDoc } from "./components/button.doc.js";
|
|
3
4
|
import { checkboxDoc } from "./components/checkbox.doc.js";
|
|
@@ -6,6 +7,7 @@ import { contextMenuDoc } from "./components/context-menu.doc.js";
|
|
|
6
7
|
import { datePickerDoc } from "./components/date-picker.doc.js";
|
|
7
8
|
import { drawerDoc } from "./components/drawer.doc.js";
|
|
8
9
|
import { dropdownDoc } from "./components/dropdown.doc.js";
|
|
10
|
+
import { formDoc } from "./components/form.doc.js";
|
|
9
11
|
import { formActionsDoc } from "./components/form-actions.doc.js";
|
|
10
12
|
import { formFieldDoc } from "./components/form-field.doc.js";
|
|
11
13
|
import { formLayoutDoc } from "./components/form-layout.doc.js";
|
|
@@ -15,6 +17,8 @@ import { inputDoc } from "./components/input.doc.js";
|
|
|
15
17
|
import { inputNumberDoc } from "./components/input-number.doc.js";
|
|
16
18
|
import { modalDoc } from "./components/modal.doc.js";
|
|
17
19
|
import { paginationDoc } from "./components/pagination.doc.js";
|
|
20
|
+
import { popconfirmDoc } from "./components/popconfirm.doc.js";
|
|
21
|
+
import { popoverDoc } from "./components/popover.doc.js";
|
|
18
22
|
import { radioDoc } from "./components/radio.doc.js";
|
|
19
23
|
import { rangePickerDoc } from "./components/range-picker.doc.js";
|
|
20
24
|
import { scrollAreaDoc } from "./components/scroll-area.doc.js";
|
|
@@ -29,7 +33,9 @@ import { tabsDoc } from "./components/tabs.doc.js";
|
|
|
29
33
|
import { tagDoc } from "./components/tag.doc.js";
|
|
30
34
|
import { taoProviderDoc } from "./components/tao-provider.doc.js";
|
|
31
35
|
import { textareaDoc } from "./components/textarea.doc.js";
|
|
36
|
+
import { tooltipDoc } from "./components/tooltip.doc.js";
|
|
32
37
|
const componentDocs = [
|
|
38
|
+
alertDoc,
|
|
33
39
|
breadcrumbDoc,
|
|
34
40
|
buttonDoc,
|
|
35
41
|
checkboxDoc,
|
|
@@ -38,6 +44,7 @@ const componentDocs = [
|
|
|
38
44
|
datePickerDoc,
|
|
39
45
|
drawerDoc,
|
|
40
46
|
dropdownDoc,
|
|
47
|
+
formDoc,
|
|
41
48
|
formActionsDoc,
|
|
42
49
|
formFieldDoc,
|
|
43
50
|
formLayoutDoc,
|
|
@@ -47,6 +54,8 @@ const componentDocs = [
|
|
|
47
54
|
inputNumberDoc,
|
|
48
55
|
modalDoc,
|
|
49
56
|
paginationDoc,
|
|
57
|
+
popconfirmDoc,
|
|
58
|
+
popoverDoc,
|
|
50
59
|
radioDoc,
|
|
51
60
|
rangePickerDoc,
|
|
52
61
|
scrollAreaDoc,
|
|
@@ -60,7 +69,8 @@ const componentDocs = [
|
|
|
60
69
|
tabsDoc,
|
|
61
70
|
tagDoc,
|
|
62
71
|
taoProviderDoc,
|
|
63
|
-
textareaDoc
|
|
72
|
+
textareaDoc,
|
|
73
|
+
tooltipDoc
|
|
64
74
|
];
|
|
65
75
|
function getComponentDoc(name) {
|
|
66
76
|
const normalizedName = name.toLowerCase();
|
package/package.json
CHANGED