@osrd-project/ui-core 0.0.84 → 0.0.85
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/index.d.ts +382 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2101 -0
- package/dist/index.js.map +1 -0
- package/dist/theme.css +2866 -0
- package/package.json +15 -15
package/dist/index.js
ADDED
|
@@ -0,0 +1,2101 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useEffectEvent, useMemo, useRef, useState } from "react";
|
|
2
|
+
import cx from "classnames";
|
|
3
|
+
import { Alert, Blocked, Calendar as Calendar$1, CheckCircle, ChevronDown, ChevronLeft, ChevronRight, ChevronUp, Eye, EyeClosed, Gear, Info, RequiredInput, X, XCircle } from "@osrd-project/ui-icons";
|
|
4
|
+
// src/components/Button.tsx
|
|
5
|
+
const Button = ({ label, variant = "Primary", isLoading = false, isDisabled = false, leadingIcon = null, counter = null, size = "large", onClick, dataTestID, ...btnAttrs }) => {
|
|
6
|
+
const handleClick = () => {
|
|
7
|
+
if (!isLoading && !isDisabled && onClick) onClick();
|
|
8
|
+
};
|
|
9
|
+
return /* @__PURE__ */ React.createElement("button", {
|
|
10
|
+
...btnAttrs,
|
|
11
|
+
className: cx("ui-button flex items-center", btnAttrs.className, variant.toLowerCase(), size.toLowerCase(), { loading: isLoading }),
|
|
12
|
+
"data-testid": dataTestID,
|
|
13
|
+
onClick: handleClick,
|
|
14
|
+
disabled: isDisabled || isLoading
|
|
15
|
+
}, leadingIcon && /* @__PURE__ */ React.createElement("span", { className: "leading-icon mr-2" }, leadingIcon), label, counter !== null && /* @__PURE__ */ React.createElement("span", { className: "counter ml-2" }, counter));
|
|
16
|
+
};
|
|
17
|
+
// src/hooks/useOutsideClick.tsx
|
|
18
|
+
const useOutsideClick = (ref, handler) => {
|
|
19
|
+
const handleClickOutside = useEffectEvent((event) => {
|
|
20
|
+
if (ref?.current && !ref.current.contains(event.target)) handler(event);
|
|
21
|
+
});
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
24
|
+
return () => {
|
|
25
|
+
document.removeEventListener("mousedown", handleClickOutside);
|
|
26
|
+
};
|
|
27
|
+
}, []);
|
|
28
|
+
};
|
|
29
|
+
// src/hooks/useFocusByTab.tsx
|
|
30
|
+
function useFocusByTab({ onBlur, onKeyUp }) {
|
|
31
|
+
const [isFocusByTab, setIsFocusByTab] = useState(false);
|
|
32
|
+
return {
|
|
33
|
+
handleKeyUp: useCallback((event) => {
|
|
34
|
+
if (event.key === "Tab") setIsFocusByTab(true);
|
|
35
|
+
onKeyUp?.(event);
|
|
36
|
+
}, [onKeyUp]),
|
|
37
|
+
handleBlur: useCallback((event) => {
|
|
38
|
+
setIsFocusByTab(false);
|
|
39
|
+
onBlur?.(event);
|
|
40
|
+
}, [onBlur]),
|
|
41
|
+
isFocusByTab
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
// src/components/Hint.tsx
|
|
45
|
+
const Hint = ({ text }) => /* @__PURE__ */ React.createElement("span", { className: "ui-hint" }, text);
|
|
46
|
+
// src/components/InputStatusIcon.tsx
|
|
47
|
+
const InputStatusIcon = ({ status, small, className }) => {
|
|
48
|
+
const size = small ? "sm" : "lg";
|
|
49
|
+
return /* @__PURE__ */ React.createElement("span", { className: cx("ui-status-icon", className, status) }, status === "loading" && /* @__PURE__ */ React.createElement(Gear, { size }), status === "info" && /* @__PURE__ */ React.createElement(Info, {
|
|
50
|
+
variant: "fill",
|
|
51
|
+
size
|
|
52
|
+
}), status === "success" && /* @__PURE__ */ React.createElement(CheckCircle, {
|
|
53
|
+
variant: "fill",
|
|
54
|
+
size
|
|
55
|
+
}), status === "warning" && /* @__PURE__ */ React.createElement(Alert, {
|
|
56
|
+
variant: "fill",
|
|
57
|
+
size
|
|
58
|
+
}), status === "error" && /* @__PURE__ */ React.createElement(Blocked, {
|
|
59
|
+
variant: "fill",
|
|
60
|
+
size
|
|
61
|
+
}));
|
|
62
|
+
};
|
|
63
|
+
// src/components/Label.tsx
|
|
64
|
+
const Label = ({ htmlFor, text, required, hasHint, disabled, small = false }) => /* @__PURE__ */ React.createElement("div", { className: cx("ui-label-wrapper", {
|
|
65
|
+
"has-hint": hasHint,
|
|
66
|
+
small
|
|
67
|
+
}) }, required && /* @__PURE__ */ React.createElement("span", { className: "required" }, /* @__PURE__ */ React.createElement(RequiredInput, null)), /* @__PURE__ */ React.createElement("label", {
|
|
68
|
+
className: cx("label", { disabled }),
|
|
69
|
+
htmlFor
|
|
70
|
+
}, text));
|
|
71
|
+
// src/components/StatusMessage.tsx
|
|
72
|
+
const StatusMessage = ({ statusWithMessage, showIcon, small, onClose, testIdPrefix }) => {
|
|
73
|
+
const { tooltip, status, message } = statusWithMessage;
|
|
74
|
+
if (message === void 0) return null;
|
|
75
|
+
return /* @__PURE__ */ React.createElement("div", { className: cx({
|
|
76
|
+
"ui-status-message-wrapper": !tooltip,
|
|
77
|
+
"ui-status-message-wrapper-tooltip": tooltip,
|
|
78
|
+
"tooltip-left": tooltip === "left",
|
|
79
|
+
"tooltip-right": tooltip === "right",
|
|
80
|
+
[status]: status
|
|
81
|
+
}) }, showIcon && /* @__PURE__ */ React.createElement(InputStatusIcon, {
|
|
82
|
+
status,
|
|
83
|
+
small
|
|
84
|
+
}), /* @__PURE__ */ React.createElement("span", {
|
|
85
|
+
"data-testid": testIdPrefix ? `${testIdPrefix}-${status}` : void 0,
|
|
86
|
+
className: cx("status-message", { [status]: status })
|
|
87
|
+
}, message), status === "info" && tooltip && /* @__PURE__ */ React.createElement("button", {
|
|
88
|
+
className: "status-close",
|
|
89
|
+
onClick: onClose
|
|
90
|
+
}, /* @__PURE__ */ React.createElement(X, { size: "sm" })));
|
|
91
|
+
};
|
|
92
|
+
// src/components/FieldWrapper.tsx
|
|
93
|
+
const FieldWrapper = ({ id, label, hint, required, disabled, statusWithMessage, statusIconPosition = "next-to-field", narrow = false, small = false, className, children, onCloseStatusMessage }) => {
|
|
94
|
+
const statusClassname = statusWithMessage ? { [statusWithMessage.status]: true } : {};
|
|
95
|
+
const defaultTooltip = narrow ? "left" : void 0;
|
|
96
|
+
if (narrow && required) throw new Error("narrow should not be used with required for now. This breaks the input UI.");
|
|
97
|
+
return /* @__PURE__ */ React.createElement("div", { className: cx("ui-feedback", statusClassname, className, {
|
|
98
|
+
small,
|
|
99
|
+
narrow
|
|
100
|
+
}) }, /* @__PURE__ */ React.createElement("div", { className: "custom-field" }, label && /* @__PURE__ */ React.createElement(Label, {
|
|
101
|
+
htmlFor: id,
|
|
102
|
+
text: label,
|
|
103
|
+
required,
|
|
104
|
+
hasHint: Boolean(hint),
|
|
105
|
+
disabled,
|
|
106
|
+
small
|
|
107
|
+
}), hint && /* @__PURE__ */ React.createElement(Hint, { text: hint }), /* @__PURE__ */ React.createElement("div", { className: "field-and-status-icon" }, children, statusWithMessage && statusIconPosition === "next-to-field" && /* @__PURE__ */ React.createElement(InputStatusIcon, {
|
|
108
|
+
status: statusWithMessage.status,
|
|
109
|
+
small,
|
|
110
|
+
className: "next-to-field"
|
|
111
|
+
})), statusWithMessage && /* @__PURE__ */ React.createElement(StatusMessage, {
|
|
112
|
+
testIdPrefix: "status-message",
|
|
113
|
+
small,
|
|
114
|
+
statusWithMessage: {
|
|
115
|
+
...statusWithMessage,
|
|
116
|
+
tooltip: statusWithMessage?.tooltip ?? defaultTooltip
|
|
117
|
+
},
|
|
118
|
+
showIcon: statusIconPosition === "before-status-message",
|
|
119
|
+
onClose: onCloseStatusMessage
|
|
120
|
+
})));
|
|
121
|
+
};
|
|
122
|
+
// src/components/Input.tsx
|
|
123
|
+
const InputAffix = ({ value, type, disabled, readOnly }) => {
|
|
124
|
+
const isContentWithCallback = typeof value === "object" && value !== null && "onClickCallback" in value;
|
|
125
|
+
const spanContent = isContentWithCallback ? value.content : value;
|
|
126
|
+
const wrapperProps = isContentWithCallback ? { onClick: value.onClickCallback } : {};
|
|
127
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
128
|
+
className: cx(`${type}-content-wrapper`, {
|
|
129
|
+
disabled,
|
|
130
|
+
"read-only": readOnly
|
|
131
|
+
}),
|
|
132
|
+
...wrapperProps
|
|
133
|
+
}, /* @__PURE__ */ React.createElement("span", { className: `${type}-content` }, spanContent));
|
|
134
|
+
};
|
|
135
|
+
const DEFAULT_ICONS = [];
|
|
136
|
+
const Input = React.forwardRef(({ id, label, type, hint, leadingContent, trailingContent, required, disabled = false, readOnly = false, statusWithMessage, narrow, inputFieldWrapperClassname = "", small = false, withIcons = DEFAULT_ICONS, onKeyUp, onBlur, onCloseStatusMessage, testIdPrefix, ...rest }, ref) => {
|
|
137
|
+
const { handleKeyUp, handleBlur, isFocusByTab } = useFocusByTab({
|
|
138
|
+
onBlur,
|
|
139
|
+
onKeyUp
|
|
140
|
+
});
|
|
141
|
+
return /* @__PURE__ */ React.createElement(FieldWrapper, {
|
|
142
|
+
id,
|
|
143
|
+
label,
|
|
144
|
+
hint,
|
|
145
|
+
statusWithMessage,
|
|
146
|
+
disabled,
|
|
147
|
+
required,
|
|
148
|
+
small,
|
|
149
|
+
statusIconPosition: statusWithMessage?.tooltip || narrow ? "before-status-message" : void 0,
|
|
150
|
+
narrow,
|
|
151
|
+
className: cx("ui-input-field-wrapper", inputFieldWrapperClassname),
|
|
152
|
+
onCloseStatusMessage
|
|
153
|
+
}, leadingContent && /* @__PURE__ */ React.createElement(InputAffix, {
|
|
154
|
+
value: leadingContent,
|
|
155
|
+
type: "leading",
|
|
156
|
+
disabled,
|
|
157
|
+
readOnly
|
|
158
|
+
}), /* @__PURE__ */ React.createElement("div", { className: cx("input-container", { "focused-by-tab": isFocusByTab }) }, /* @__PURE__ */ React.createElement("input", {
|
|
159
|
+
"data-testid": testIdPrefix ? `${testIdPrefix}-input` : void 0,
|
|
160
|
+
ref,
|
|
161
|
+
className: cx("input", {
|
|
162
|
+
"with-leading-only": leadingContent && !trailingContent,
|
|
163
|
+
"with-trailing-only": trailingContent && !leadingContent,
|
|
164
|
+
"with-leading-and-trailing": leadingContent && trailingContent,
|
|
165
|
+
[`with-icons-${withIcons.length}`]: withIcons.length > 0,
|
|
166
|
+
[statusWithMessage?.status || ""]: !!statusWithMessage
|
|
167
|
+
}),
|
|
168
|
+
id,
|
|
169
|
+
type,
|
|
170
|
+
disabled,
|
|
171
|
+
readOnly,
|
|
172
|
+
onKeyUp: handleKeyUp,
|
|
173
|
+
onBlur: handleBlur,
|
|
174
|
+
...rest
|
|
175
|
+
}), /* @__PURE__ */ React.createElement("div", { className: cx("input-icons", { small }) }, withIcons.map((iconConfig, index) => /* @__PURE__ */ React.createElement("span", {
|
|
176
|
+
key: index,
|
|
177
|
+
className: iconConfig?.className,
|
|
178
|
+
onClick: iconConfig.action,
|
|
179
|
+
"data-testid": iconConfig.testId ?? (testIdPrefix ? `${testIdPrefix}-icon` : void 0)
|
|
180
|
+
}, iconConfig.icon)))), trailingContent && /* @__PURE__ */ React.createElement(InputAffix, {
|
|
181
|
+
value: trailingContent,
|
|
182
|
+
type: "trailing",
|
|
183
|
+
disabled,
|
|
184
|
+
readOnly
|
|
185
|
+
}));
|
|
186
|
+
});
|
|
187
|
+
Input.displayName = "Input";
|
|
188
|
+
// src/components/ComboBox/ComboBox.tsx
|
|
189
|
+
/**
|
|
190
|
+
* ComboBox component
|
|
191
|
+
*
|
|
192
|
+
* You can use the hook useDefaultComboBox to get the default behavior.
|
|
193
|
+
* See the stories.
|
|
194
|
+
*
|
|
195
|
+
* When `allowCustomValue` is true, an option appears at the bottom of the dropdown
|
|
196
|
+
* when the typed value doesn't match any existing suggestion.
|
|
197
|
+
* Defocusing without selecting cancels the input.
|
|
198
|
+
*
|
|
199
|
+
* When `emptyValue` is given (ex: a "None" option), it is displayed when there is no value,
|
|
200
|
+
* so the field is never left empty. The clear icon on a real value goes back to `emptyValue`.
|
|
201
|
+
*/
|
|
202
|
+
const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, value, small, getSuggestionLabel, onChange, onSelectSuggestion, resetSuggestions, testIdPrefix, renderListElementComponent, renderFooterItem, allowCustomValue = false, onAddCustomValue, addCustomValueLabel, emptyValue, ...inputProps }) => {
|
|
203
|
+
const [activeSuggestionIndex, setActiveSuggestionIndex] = useState(-1);
|
|
204
|
+
const displayedValue = value ?? emptyValue;
|
|
205
|
+
const suggestionLabel = displayedValue ? getSuggestionLabel(displayedValue) : "";
|
|
206
|
+
const [inputValue, setInputValue] = useState(suggestionLabel);
|
|
207
|
+
const [prevSuggestionLabel, setPrevSuggestionLabel] = useState(suggestionLabel);
|
|
208
|
+
if (suggestionLabel !== prevSuggestionLabel) {
|
|
209
|
+
setPrevSuggestionLabel(suggestionLabel);
|
|
210
|
+
setInputValue(suggestionLabel);
|
|
211
|
+
}
|
|
212
|
+
const [isInputFocused, setIsInputFocused] = useState(false);
|
|
213
|
+
const suggestionRefs = useRef([]);
|
|
214
|
+
const inputRef = useRef(null);
|
|
215
|
+
const wrapperRef = useRef(null);
|
|
216
|
+
const removeFocus = useCallback(() => {
|
|
217
|
+
setIsInputFocused(false);
|
|
218
|
+
setActiveSuggestionIndex(-1);
|
|
219
|
+
setTimeout(() => {
|
|
220
|
+
inputRef.current?.blur();
|
|
221
|
+
}, 0);
|
|
222
|
+
resetSuggestions();
|
|
223
|
+
}, [resetSuggestions]);
|
|
224
|
+
const closeSuggestions = useCallback(() => {
|
|
225
|
+
setInputValue(displayedValue ? getSuggestionLabel(displayedValue) : "");
|
|
226
|
+
removeFocus();
|
|
227
|
+
}, [
|
|
228
|
+
displayedValue,
|
|
229
|
+
getSuggestionLabel,
|
|
230
|
+
removeFocus
|
|
231
|
+
]);
|
|
232
|
+
const focusInput = useCallback(() => {
|
|
233
|
+
if (isInputFocused) closeSuggestions();
|
|
234
|
+
else inputRef.current?.focus();
|
|
235
|
+
}, [
|
|
236
|
+
inputRef,
|
|
237
|
+
isInputFocused,
|
|
238
|
+
closeSuggestions
|
|
239
|
+
]);
|
|
240
|
+
const normalizedInputValue = useMemo(() => inputValue.trim().toLowerCase(), [inputValue]);
|
|
241
|
+
const suggestionsByLabel = useMemo(() => {
|
|
242
|
+
const map = /* @__PURE__ */ new Map();
|
|
243
|
+
for (const suggestion of suggestions) map.set(getSuggestionLabel(suggestion).trim().toLowerCase(), suggestion);
|
|
244
|
+
return map;
|
|
245
|
+
}, [suggestions, getSuggestionLabel]);
|
|
246
|
+
const showAddCustomValue = useMemo(() => {
|
|
247
|
+
if (!allowCustomValue || !normalizedInputValue) return false;
|
|
248
|
+
return !suggestionsByLabel.has(normalizedInputValue);
|
|
249
|
+
}, [
|
|
250
|
+
allowCustomValue,
|
|
251
|
+
normalizedInputValue,
|
|
252
|
+
suggestionsByLabel
|
|
253
|
+
]);
|
|
254
|
+
const showSuggestions = useMemo(() => isInputFocused && (suggestions.length > 0 || showAddCustomValue) && !inputProps.disabled, [
|
|
255
|
+
isInputFocused,
|
|
256
|
+
suggestions.length,
|
|
257
|
+
showAddCustomValue,
|
|
258
|
+
inputProps.disabled
|
|
259
|
+
]);
|
|
260
|
+
const handleInputChange = (e) => {
|
|
261
|
+
onChange?.(e.currentTarget.value);
|
|
262
|
+
setInputValue(e.currentTarget.value);
|
|
263
|
+
};
|
|
264
|
+
const selectSuggestion = (selectedSuggestion) => {
|
|
265
|
+
onSelectSuggestion(selectedSuggestion);
|
|
266
|
+
setInputValue(getSuggestionLabel(selectedSuggestion));
|
|
267
|
+
removeFocus();
|
|
268
|
+
};
|
|
269
|
+
const confirmCustomValue = () => {
|
|
270
|
+
const trimmed = inputValue.trim();
|
|
271
|
+
if (!trimmed) return;
|
|
272
|
+
onAddCustomValue?.(trimmed);
|
|
273
|
+
removeFocus();
|
|
274
|
+
};
|
|
275
|
+
const onFieldBlur = () => {
|
|
276
|
+
const exactSuggestion = suggestionsByLabel.get(normalizedInputValue);
|
|
277
|
+
if (activeSuggestionIndex === -1 && !showAddCustomValue) {
|
|
278
|
+
if (normalizedInputValue === "") onSelectSuggestion(void 0);
|
|
279
|
+
else if (exactSuggestion) onSelectSuggestion(exactSuggestion);
|
|
280
|
+
else onAddCustomValue?.(inputValue);
|
|
281
|
+
}
|
|
282
|
+
closeSuggestions();
|
|
283
|
+
};
|
|
284
|
+
const totalItems = suggestions.length + (showAddCustomValue ? 1 : 0);
|
|
285
|
+
const customValueIndex = showAddCustomValue ? suggestions.length : -1;
|
|
286
|
+
const handleKeyDown = (e) => {
|
|
287
|
+
const confirmSelected = () => {
|
|
288
|
+
if (showAddCustomValue && activeSuggestionIndex === customValueIndex) confirmCustomValue();
|
|
289
|
+
const suggestion = suggestions.at(activeSuggestionIndex);
|
|
290
|
+
if (suggestion) selectSuggestion(suggestion);
|
|
291
|
+
};
|
|
292
|
+
switch (e.key) {
|
|
293
|
+
case "ArrowDown":
|
|
294
|
+
setActiveSuggestionIndex((prev) => {
|
|
295
|
+
const newIndex = prev < totalItems - 1 ? prev + 1 : prev;
|
|
296
|
+
suggestionRefs.current[newIndex]?.scrollIntoView({ block: "nearest" });
|
|
297
|
+
return newIndex;
|
|
298
|
+
});
|
|
299
|
+
break;
|
|
300
|
+
case "ArrowUp":
|
|
301
|
+
setActiveSuggestionIndex((prev) => {
|
|
302
|
+
const newIndex = prev > 0 ? prev - 1 : prev;
|
|
303
|
+
suggestionRefs.current[newIndex]?.scrollIntoView({ block: "nearest" });
|
|
304
|
+
return newIndex;
|
|
305
|
+
});
|
|
306
|
+
break;
|
|
307
|
+
case "Enter":
|
|
308
|
+
e.preventDefault();
|
|
309
|
+
if (activeSuggestionIndex >= 0) confirmSelected();
|
|
310
|
+
else {
|
|
311
|
+
const exactSuggestion = suggestionsByLabel.get(normalizedInputValue);
|
|
312
|
+
if (exactSuggestion) selectSuggestion(exactSuggestion);
|
|
313
|
+
else if (showAddCustomValue) confirmCustomValue();
|
|
314
|
+
else onAddCustomValue?.(inputValue);
|
|
315
|
+
}
|
|
316
|
+
break;
|
|
317
|
+
case "Tab":
|
|
318
|
+
if (activeSuggestionIndex >= 0) confirmSelected();
|
|
319
|
+
onFieldBlur();
|
|
320
|
+
break;
|
|
321
|
+
case "Escape": onFieldBlur();
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
const handleInputFocus = (e) => {
|
|
325
|
+
e.stopPropagation();
|
|
326
|
+
setIsInputFocused(true);
|
|
327
|
+
};
|
|
328
|
+
const clearInput = useCallback(() => {
|
|
329
|
+
if (emptyValue && value) {
|
|
330
|
+
onSelectSuggestion(void 0);
|
|
331
|
+
resetSuggestions();
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
setInputValue("");
|
|
335
|
+
onChange?.("");
|
|
336
|
+
resetSuggestions();
|
|
337
|
+
setIsInputFocused(true);
|
|
338
|
+
inputRef.current?.focus();
|
|
339
|
+
}, [
|
|
340
|
+
emptyValue,
|
|
341
|
+
value,
|
|
342
|
+
onSelectSuggestion,
|
|
343
|
+
resetSuggestions,
|
|
344
|
+
onChange
|
|
345
|
+
]);
|
|
346
|
+
useOutsideClick(showSuggestions || isInputFocused ? wrapperRef : null, onFieldBlur);
|
|
347
|
+
const inputIcons = useMemo(() => {
|
|
348
|
+
if (inputProps.readOnly || inputProps.disabled) return void 0;
|
|
349
|
+
return [...inputValue ? [{
|
|
350
|
+
icon: /* @__PURE__ */ React.createElement(XCircle, { variant: "fill" }),
|
|
351
|
+
action: clearInput,
|
|
352
|
+
className: "clear-icon",
|
|
353
|
+
testId: "clear-icon"
|
|
354
|
+
}] : [], {
|
|
355
|
+
icon: /* @__PURE__ */ React.createElement(ChevronDown, { size: small ? "sm" : "lg" }),
|
|
356
|
+
action: focusInput,
|
|
357
|
+
className: cx("chevron-icon", {
|
|
358
|
+
disabled: inputProps.disabled,
|
|
359
|
+
open: isInputFocused
|
|
360
|
+
})
|
|
361
|
+
}];
|
|
362
|
+
}, [
|
|
363
|
+
inputProps.readOnly,
|
|
364
|
+
inputProps.disabled,
|
|
365
|
+
clearInput,
|
|
366
|
+
inputValue,
|
|
367
|
+
small,
|
|
368
|
+
focusInput,
|
|
369
|
+
isInputFocused
|
|
370
|
+
]);
|
|
371
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
372
|
+
"data-testid": testIdPrefix ? testIdPrefix : void 0,
|
|
373
|
+
className: "ui-combo-box",
|
|
374
|
+
style: { "--number-of-suggestions": numberOfSuggestionsToShow },
|
|
375
|
+
ref: wrapperRef
|
|
376
|
+
}, customLabel && /* @__PURE__ */ React.createElement("label", { htmlFor: inputProps.id }, customLabel), /* @__PURE__ */ React.createElement(Input, {
|
|
377
|
+
testIdPrefix: inputProps.id,
|
|
378
|
+
...inputProps,
|
|
379
|
+
ref: inputRef,
|
|
380
|
+
value: inputValue,
|
|
381
|
+
onChange: handleInputChange,
|
|
382
|
+
onKeyDown: handleKeyDown,
|
|
383
|
+
onFocus: handleInputFocus,
|
|
384
|
+
withIcons: inputIcons,
|
|
385
|
+
small
|
|
386
|
+
}), showSuggestions && /* @__PURE__ */ React.createElement("ul", {
|
|
387
|
+
className: "suggestions-list",
|
|
388
|
+
"data-testid": testIdPrefix ? `${testIdPrefix}-list` : void 0,
|
|
389
|
+
onMouseLeave: () => setActiveSuggestionIndex(-1),
|
|
390
|
+
tabIndex: -1
|
|
391
|
+
}, suggestions.map((suggestion, index) => /* @__PURE__ */ React.createElement("li", {
|
|
392
|
+
ref: (el) => {
|
|
393
|
+
if (suggestionRefs) suggestionRefs.current[index] = el;
|
|
394
|
+
},
|
|
395
|
+
key: `${getSuggestionLabel(suggestion)}-${index}`,
|
|
396
|
+
"data-testid": testIdPrefix ? `${testIdPrefix}-item` : void 0,
|
|
397
|
+
className: cx("suggestion-item", {
|
|
398
|
+
active: index === activeSuggestionIndex,
|
|
399
|
+
selected: displayedValue && getSuggestionLabel(displayedValue) === getSuggestionLabel(suggestion),
|
|
400
|
+
small,
|
|
401
|
+
"suggestion-item--custom": renderListElementComponent
|
|
402
|
+
}),
|
|
403
|
+
onClick: () => selectSuggestion(suggestion),
|
|
404
|
+
onMouseEnter: () => setActiveSuggestionIndex(index)
|
|
405
|
+
}, renderListElementComponent ? renderListElementComponent({
|
|
406
|
+
suggestion,
|
|
407
|
+
index,
|
|
408
|
+
isActive: index === activeSuggestionIndex,
|
|
409
|
+
isSelected: !!displayedValue && getSuggestionLabel(displayedValue) === getSuggestionLabel(suggestion)
|
|
410
|
+
}) : getSuggestionLabel(suggestion))), showAddCustomValue && /* @__PURE__ */ React.createElement("li", {
|
|
411
|
+
ref: (el) => {
|
|
412
|
+
suggestionRefs.current[suggestions.length] = el;
|
|
413
|
+
},
|
|
414
|
+
className: cx("suggestion-item", "suggestion-item--add-custom", {
|
|
415
|
+
active: activeSuggestionIndex === customValueIndex,
|
|
416
|
+
small
|
|
417
|
+
}),
|
|
418
|
+
onClick: confirmCustomValue,
|
|
419
|
+
onMouseEnter: () => setActiveSuggestionIndex(customValueIndex),
|
|
420
|
+
"data-testid": testIdPrefix ? `${testIdPrefix}-add-custom` : void 0
|
|
421
|
+
}, addCustomValueLabel ?? `Add "${inputValue.trim()}"`), renderFooterItem && renderFooterItem()));
|
|
422
|
+
};
|
|
423
|
+
// src/components/ComboBox/useDefaultComboBox.ts
|
|
424
|
+
/**
|
|
425
|
+
* Removes the accents and lowercases the input.
|
|
426
|
+
*/
|
|
427
|
+
const normalizeString = (str) => str.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
|
|
428
|
+
/**
|
|
429
|
+
* Filter the options, keeping only the one that have a suggestion label that contain
|
|
430
|
+
* the query, and sorting them so the one that starts with the query come first.
|
|
431
|
+
*/
|
|
432
|
+
const defaultFilterSuggestions = (labels, query) => {
|
|
433
|
+
const input = normalizeString(query).trim();
|
|
434
|
+
if (!input) return labels.map(({ suggestion }) => suggestion);
|
|
435
|
+
const getSuggestionScore = (label) => {
|
|
436
|
+
if (label.startsWith(input)) return 2;
|
|
437
|
+
if (label.includes(input)) return 1;
|
|
438
|
+
return 0;
|
|
439
|
+
};
|
|
440
|
+
return labels.map(({ label, suggestion }) => ({
|
|
441
|
+
suggestion,
|
|
442
|
+
score: getSuggestionScore(label)
|
|
443
|
+
})).filter(({ score }) => score > 0).sort(({ score: scoreA }, { score: scoreB }) => scoreB - scoreA).map(({ suggestion }) => suggestion);
|
|
444
|
+
};
|
|
445
|
+
/**
|
|
446
|
+
* Create relevant props for a basic combo box with suggestions, ready to be spread in a `ComboBox`
|
|
447
|
+
* component. Suggestions are filtered and ordered using a very simple algorithm: options with
|
|
448
|
+
* a labels that starts with the query are shown first, and other options that contains the query
|
|
449
|
+
* follows, trimming spaces, accents and ignoring case.
|
|
450
|
+
*
|
|
451
|
+
* The second parameter, `getSuggestionLabel`, should be stable to prevent rerender and useless
|
|
452
|
+
* recomputations (see `useCallback`).
|
|
453
|
+
*/
|
|
454
|
+
const useDefaultComboBox = (suggestions, getSuggestionLabel) => {
|
|
455
|
+
const [query, setQuery] = useState("");
|
|
456
|
+
const labels = useMemo(() => suggestions.map((suggestion) => ({
|
|
457
|
+
label: normalizeString(getSuggestionLabel(suggestion)),
|
|
458
|
+
suggestion
|
|
459
|
+
})), [suggestions, getSuggestionLabel]);
|
|
460
|
+
return {
|
|
461
|
+
suggestions: useMemo(() => defaultFilterSuggestions(labels, query), [labels, query]),
|
|
462
|
+
onChange: useCallback((newValue) => {
|
|
463
|
+
setQuery(newValue);
|
|
464
|
+
}, []),
|
|
465
|
+
resetSuggestions: useCallback(() => {
|
|
466
|
+
setQuery("");
|
|
467
|
+
}, [])
|
|
468
|
+
};
|
|
469
|
+
};
|
|
470
|
+
// src/components/Checkbox/Checkbox.tsx
|
|
471
|
+
const Checkbox = ({ label, small, hint, disabled, checked, readOnly, isIndeterminate = false, onClick, children, ref, ...rest }) => {
|
|
472
|
+
const handleClick = (e) => {
|
|
473
|
+
if (readOnly) {
|
|
474
|
+
e.preventDefault();
|
|
475
|
+
e.stopPropagation();
|
|
476
|
+
} else onClick?.(e);
|
|
477
|
+
};
|
|
478
|
+
return /* @__PURE__ */ React.createElement("label", { className: cx("ui-checkbox", {
|
|
479
|
+
small,
|
|
480
|
+
"with-label": label || children,
|
|
481
|
+
"with-hint": hint
|
|
482
|
+
}) }, /* @__PURE__ */ React.createElement("input", {
|
|
483
|
+
className: cx({ "read-only": readOnly }),
|
|
484
|
+
type: "checkbox",
|
|
485
|
+
checked,
|
|
486
|
+
disabled,
|
|
487
|
+
ref: (input) => {
|
|
488
|
+
if (input) input.indeterminate = isIndeterminate;
|
|
489
|
+
if (ref) ref.current = input;
|
|
490
|
+
},
|
|
491
|
+
onClick: handleClick,
|
|
492
|
+
...rest
|
|
493
|
+
}), label && /* @__PURE__ */ React.createElement("div", { className: "label" }, label), children && /* @__PURE__ */ React.createElement("div", { className: "label" }, children), hint && /* @__PURE__ */ React.createElement("span", { className: "hint" }, hint));
|
|
494
|
+
};
|
|
495
|
+
// src/components/Checkbox/CheckboxList.tsx
|
|
496
|
+
const CheckboxList = (checkBoxListprops) => {
|
|
497
|
+
const { items, onClickItem, small = false, label = "", readOnly = false, disabled = false } = checkBoxListprops;
|
|
498
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("span", { className: "checkbox-list-label" }, label), /* @__PURE__ */ React.createElement("ul", { className: cx("checkbox-list", { small }) }, items.map((item) => {
|
|
499
|
+
const { id, props, items: subItems } = item;
|
|
500
|
+
return /* @__PURE__ */ React.createElement("li", { key: id }, /* @__PURE__ */ React.createElement(Checkbox, {
|
|
501
|
+
small,
|
|
502
|
+
onClick: (e) => onClickItem(e, item),
|
|
503
|
+
checked: item.props.checked,
|
|
504
|
+
isIndeterminate: item.props.isIndeterminate,
|
|
505
|
+
...props,
|
|
506
|
+
disabled,
|
|
507
|
+
readOnly
|
|
508
|
+
}), subItems && /* @__PURE__ */ React.createElement(CheckboxList, {
|
|
509
|
+
...checkBoxListprops,
|
|
510
|
+
items: subItems
|
|
511
|
+
}));
|
|
512
|
+
})));
|
|
513
|
+
};
|
|
514
|
+
// src/components/Checkbox/utils.ts
|
|
515
|
+
const traverseTree = (items, onVisit, parent) => {
|
|
516
|
+
items.forEach((item) => {
|
|
517
|
+
onVisit(item, parent);
|
|
518
|
+
if (item.items && item.items.length > 0) traverseTree(item.items, onVisit, item);
|
|
519
|
+
});
|
|
520
|
+
};
|
|
521
|
+
const buildRelationshipMaps = (items) => {
|
|
522
|
+
const parentChildrenMap = {};
|
|
523
|
+
const childrenParentMap = {};
|
|
524
|
+
traverseTree(items, (item, parent) => {
|
|
525
|
+
if (parent?.id !== void 0) {
|
|
526
|
+
if (!parentChildrenMap[parent.id]) parentChildrenMap[parent.id] = [];
|
|
527
|
+
parentChildrenMap[parent.id].push(item.id);
|
|
528
|
+
childrenParentMap[item.id] = parent.id;
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
return {
|
|
532
|
+
parentChildrenMap,
|
|
533
|
+
childrenParentMap
|
|
534
|
+
};
|
|
535
|
+
};
|
|
536
|
+
const updateItemAndDescendantsStates = (itemId, newItemStates, parentChildrenMap, itemStates) => {
|
|
537
|
+
itemStates[itemId] = newItemStates;
|
|
538
|
+
parentChildrenMap[itemId]?.forEach((child) => updateItemAndDescendantsStates(child, newItemStates, parentChildrenMap, itemStates));
|
|
539
|
+
};
|
|
540
|
+
const updateAscendantsStates = (childId, itemStates, relastionShipMap) => {
|
|
541
|
+
const { parentChildrenMap, childrenParentMap } = relastionShipMap;
|
|
542
|
+
const parentId = childrenParentMap[childId];
|
|
543
|
+
if (parentId === void 0) return;
|
|
544
|
+
const siblingStates = parentChildrenMap[parentId].map((siblingId) => itemStates[siblingId]);
|
|
545
|
+
let parentState;
|
|
546
|
+
if (siblingStates.every((state) => state === 1)) parentState = 1;
|
|
547
|
+
else if (siblingStates.every((state) => state === 0)) parentState = 0;
|
|
548
|
+
else parentState = 2;
|
|
549
|
+
itemStates[parentId] = parentState;
|
|
550
|
+
updateAscendantsStates(parentId, itemStates, {
|
|
551
|
+
parentChildrenMap,
|
|
552
|
+
childrenParentMap
|
|
553
|
+
});
|
|
554
|
+
};
|
|
555
|
+
const updateItemStates = (itemsTree, initialItemStates, clickedItemId) => {
|
|
556
|
+
const newItemStates = { ...initialItemStates };
|
|
557
|
+
const newClickedItemState = newItemStates[clickedItemId] === 1 ? 0 : 1;
|
|
558
|
+
const { parentChildrenMap, childrenParentMap } = buildRelationshipMaps(itemsTree);
|
|
559
|
+
updateItemAndDescendantsStates(clickedItemId, newClickedItemState, parentChildrenMap, newItemStates);
|
|
560
|
+
updateAscendantsStates(clickedItemId, newItemStates, {
|
|
561
|
+
parentChildrenMap,
|
|
562
|
+
childrenParentMap
|
|
563
|
+
});
|
|
564
|
+
return newItemStates;
|
|
565
|
+
};
|
|
566
|
+
const computeInitialItemStates = (items) => {
|
|
567
|
+
const itemStates = {};
|
|
568
|
+
traverseTree(items, (item) => {
|
|
569
|
+
if (item.props.isIndeterminate) itemStates[item.id] = 2;
|
|
570
|
+
else itemStates[item.id] = item.props.checked ? 1 : 0;
|
|
571
|
+
});
|
|
572
|
+
return itemStates;
|
|
573
|
+
};
|
|
574
|
+
const applyStateToItem = (itemsTree, itemStates) => {
|
|
575
|
+
const clonedItemsTree = JSON.parse(JSON.stringify(itemsTree));
|
|
576
|
+
traverseTree(clonedItemsTree, (item) => {
|
|
577
|
+
const { checked, isIndeterminate, ...otherProps } = item.props;
|
|
578
|
+
item.props = otherProps;
|
|
579
|
+
if (itemStates[item.id] === 2) item.props.isIndeterminate = true;
|
|
580
|
+
else if (itemStates[item.id] === 1) item.props.checked = true;
|
|
581
|
+
else item.props.checked = false;
|
|
582
|
+
});
|
|
583
|
+
return clonedItemsTree;
|
|
584
|
+
};
|
|
585
|
+
const computeNewItemsTree = (itemsTree, clickedItem) => {
|
|
586
|
+
const initialItemStates = computeInitialItemStates(itemsTree);
|
|
587
|
+
const updatedItemStates = updateItemStates(itemsTree, initialItemStates, clickedItem.id);
|
|
588
|
+
return applyStateToItem(itemsTree, updatedItemStates);
|
|
589
|
+
};
|
|
590
|
+
// src/components/Checkbox/CheckboxTree.tsx
|
|
591
|
+
const CheckboxesTree = ({ items, small, id, label, hint, statusWithMessage, disabled, required, readOnly, onChange, computeNewItemsTree: computeNewItemsTree$1 }) => {
|
|
592
|
+
const handleClick = (e, item) => {
|
|
593
|
+
const newItems = computeNewItemsTree$1 ? computeNewItemsTree$1(items, item) : computeNewItemsTree(items, item);
|
|
594
|
+
onChange?.(newItems, item);
|
|
595
|
+
item.props.onClick?.(e);
|
|
596
|
+
};
|
|
597
|
+
return /* @__PURE__ */ React.createElement(FieldWrapper, {
|
|
598
|
+
id,
|
|
599
|
+
label,
|
|
600
|
+
hint,
|
|
601
|
+
statusWithMessage,
|
|
602
|
+
statusIconPosition: "before-status-message",
|
|
603
|
+
disabled,
|
|
604
|
+
required,
|
|
605
|
+
small
|
|
606
|
+
}, /* @__PURE__ */ React.createElement(CheckboxList, {
|
|
607
|
+
small,
|
|
608
|
+
items,
|
|
609
|
+
disabled,
|
|
610
|
+
readOnly,
|
|
611
|
+
onClickItem: handleClick
|
|
612
|
+
}));
|
|
613
|
+
};
|
|
614
|
+
// src/hooks/usePopoverPosition.tsx
|
|
615
|
+
const usePopoverPosition = (inputRef, popoverRef, verticalOffset = -3, align = "center", verticalAnchor = "bottom") => {
|
|
616
|
+
const [popoverPosition, setPopoverPosition] = useState({
|
|
617
|
+
top: 0,
|
|
618
|
+
left: 0
|
|
619
|
+
});
|
|
620
|
+
const calculatePosition = useCallback(() => {
|
|
621
|
+
if (inputRef.current) {
|
|
622
|
+
const inputRect = inputRef.current.getBoundingClientRect();
|
|
623
|
+
const popoverWidth = popoverRef.current?.getBoundingClientRect()?.width ?? 0;
|
|
624
|
+
const top = (verticalAnchor === "top" ? inputRect.top : inputRect.bottom) + window.scrollY + verticalOffset;
|
|
625
|
+
const isCentered = align === "center";
|
|
626
|
+
const popoverViewportLeft = inputRect.left + window.scrollX + (isCentered ? inputRect.width / 2 : 0);
|
|
627
|
+
const offsetX = isCentered ? popoverWidth / 2 : 0;
|
|
628
|
+
const left = Math.max(offsetX, Math.min(popoverViewportLeft, window.innerWidth - popoverWidth + offsetX));
|
|
629
|
+
setPopoverPosition({
|
|
630
|
+
top,
|
|
631
|
+
left
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
}, [
|
|
635
|
+
inputRef,
|
|
636
|
+
popoverRef,
|
|
637
|
+
verticalOffset,
|
|
638
|
+
align,
|
|
639
|
+
verticalAnchor
|
|
640
|
+
]);
|
|
641
|
+
useEffect(() => {
|
|
642
|
+
const handleResizeScroll = () => calculatePosition();
|
|
643
|
+
window.addEventListener("resize", handleResizeScroll);
|
|
644
|
+
window.addEventListener("scroll", handleResizeScroll, true);
|
|
645
|
+
return () => {
|
|
646
|
+
window.removeEventListener("resize", handleResizeScroll);
|
|
647
|
+
window.removeEventListener("scroll", handleResizeScroll, true);
|
|
648
|
+
};
|
|
649
|
+
}, [calculatePosition]);
|
|
650
|
+
return {
|
|
651
|
+
popoverPosition,
|
|
652
|
+
calculatePosition
|
|
653
|
+
};
|
|
654
|
+
};
|
|
655
|
+
// src/components/InputPopover.tsx
|
|
656
|
+
const InputPopover = ({ inputRef, isOpen, onClose, children, verticalOffset, testIdPrefix }) => {
|
|
657
|
+
const popoverRef = useRef(null);
|
|
658
|
+
const { popoverPosition, calculatePosition } = usePopoverPosition(inputRef, popoverRef, verticalOffset);
|
|
659
|
+
useEffect(() => {
|
|
660
|
+
if (isOpen && popoverRef.current && !popoverRef.current.matches(":popover-open")) {
|
|
661
|
+
popoverRef.current.showPopover();
|
|
662
|
+
calculatePosition();
|
|
663
|
+
}
|
|
664
|
+
}, [isOpen, calculatePosition]);
|
|
665
|
+
useOutsideClick(popoverRef, onClose);
|
|
666
|
+
if (!isOpen) return null;
|
|
667
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
668
|
+
ref: popoverRef,
|
|
669
|
+
className: "input-popover",
|
|
670
|
+
style: {
|
|
671
|
+
top: popoverPosition.top,
|
|
672
|
+
left: popoverPosition.left
|
|
673
|
+
},
|
|
674
|
+
popover: "manual"
|
|
675
|
+
}, /* @__PURE__ */ React.createElement("button", {
|
|
676
|
+
"data-testid": testIdPrefix ? `${testIdPrefix}-close-button` : void 0,
|
|
677
|
+
className: "close-button",
|
|
678
|
+
onClick: onClose
|
|
679
|
+
}, /* @__PURE__ */ React.createElement(X, { size: "lg" })), children);
|
|
680
|
+
};
|
|
681
|
+
// src/components/DatePicker/utils.ts
|
|
682
|
+
const INVALID_SELECTED_SLOT_ERROR = "Invalid selectedSlot: If start and end are defined, the start date must be before the end date.";
|
|
683
|
+
const INVALID_SELECTABLE_SLOT_ERROR = "Invalid selectableSlot: If start and end are defined, the start date must be before the end date.";
|
|
684
|
+
const INVALID_SELECTED_SLOT_BASED_ON_SELECTABLE_SLOT_ERROR = "selectedSlot must be within selectableSlot";
|
|
685
|
+
const INVALID_INITIAL_DATE_ERROR = "initialDate must be within selectableSlot";
|
|
686
|
+
const containsOnlyNumbersAndSlashes = (s) => /^[0-9/]+$/.test(s);
|
|
687
|
+
/**
|
|
688
|
+
* Normalize the given date to midnight
|
|
689
|
+
* @param date The date to normalize
|
|
690
|
+
* @returns The normalized date
|
|
691
|
+
*/
|
|
692
|
+
function normalizeDate(date) {
|
|
693
|
+
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Check if the given slot is valid
|
|
697
|
+
* @param slot The slot to check
|
|
698
|
+
* @returns True if the slot is valid, false otherwise
|
|
699
|
+
*/
|
|
700
|
+
function isValidSlot(slot) {
|
|
701
|
+
if (slot.start && slot.end) return normalizeDate(slot.start).getTime() <= normalizeDate(slot.end).getTime();
|
|
702
|
+
return true;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Get the dates from the previous month that are in the first week of the current month
|
|
706
|
+
* @param month The month of the current date
|
|
707
|
+
* @param fullYear The year of the current date
|
|
708
|
+
* @returns An array of dates from the previous month that are in the first week of the current month
|
|
709
|
+
*/
|
|
710
|
+
function getDatesFromPreviousMonthInFirstWeek(month, fullYear) {
|
|
711
|
+
const firstDayOfWeek = new Date(fullYear, month, 1).getDay();
|
|
712
|
+
if (firstDayOfWeek === 1) return [];
|
|
713
|
+
const daysFromPrevMonth = firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1;
|
|
714
|
+
const lastDayPrevMonth = new Date(fullYear, month, 0);
|
|
715
|
+
const dates = [];
|
|
716
|
+
for (let i = 0; i < daysFromPrevMonth; i++) {
|
|
717
|
+
const day = lastDayPrevMonth.getDate() - i;
|
|
718
|
+
dates.unshift(new Date(lastDayPrevMonth.getFullYear(), lastDayPrevMonth.getMonth(), day));
|
|
719
|
+
}
|
|
720
|
+
return dates;
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Get the dates from the next month that are in the last week of the current month
|
|
724
|
+
* @param month The month of the current date
|
|
725
|
+
* @param fullYear The year of the current date
|
|
726
|
+
* @returns An array of dates from the next month that are in the last week of the current month
|
|
727
|
+
*/
|
|
728
|
+
function getDatesFromNextMonthInLastWeek(month, fullYear) {
|
|
729
|
+
const daysFromNextMonth = (7 - new Date(fullYear, month + 1, 0).getDay()) % 7;
|
|
730
|
+
if (daysFromNextMonth === 0) return [];
|
|
731
|
+
const nextMonth = new Date(fullYear, month + 1, 1);
|
|
732
|
+
const dates = [];
|
|
733
|
+
for (let i = 0; i < daysFromNextMonth; i++) {
|
|
734
|
+
const day = i + 1;
|
|
735
|
+
dates.push(new Date(nextMonth.getFullYear(), nextMonth.getMonth(), day));
|
|
736
|
+
}
|
|
737
|
+
return dates;
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Get all the dates in the month of the given date
|
|
741
|
+
* @param month The month of the current date
|
|
742
|
+
* @param fullYear The year of the current date
|
|
743
|
+
* @returns An array of all the dates in the month of the given date
|
|
744
|
+
*/
|
|
745
|
+
function getAllDatesInMonth(month, fullYear) {
|
|
746
|
+
const totalDaysInMonth = new Date(fullYear, month + 1, 0).getDate();
|
|
747
|
+
const dates = [];
|
|
748
|
+
for (let day = 1; day <= totalDaysInMonth; day++) dates.push(new Date(fullYear, month, day));
|
|
749
|
+
return dates;
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Check if the given dates are the same day
|
|
753
|
+
* @param date1 The first date
|
|
754
|
+
* @param date2 The second date
|
|
755
|
+
* @returns True if the dates are the same day, false otherwise
|
|
756
|
+
*/
|
|
757
|
+
function isSameDay(date1, date2) {
|
|
758
|
+
if (!date1 || !date2) return false;
|
|
759
|
+
return date1.getDate() === date2.getDate() && date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear();
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Check if the given date is within the given slot
|
|
763
|
+
* if the slot is not provided, the date is considered to be within the slot
|
|
764
|
+
* if a boundary of the slot is not provided, it's considered to be an open boundary
|
|
765
|
+
* @param date The date to check
|
|
766
|
+
* @param slot The slot to check
|
|
767
|
+
* @returns True if the date is within the slot, false otherwise
|
|
768
|
+
*/
|
|
769
|
+
function isWithinInterval(date, slot) {
|
|
770
|
+
if (!slot) return true;
|
|
771
|
+
const normalizedDate = normalizeDate(date);
|
|
772
|
+
const normalizedStart = slot.start ? normalizeDate(slot.start) : void 0;
|
|
773
|
+
const normalizedEnd = slot.end ? normalizeDate(slot.end) : void 0;
|
|
774
|
+
return (normalizedStart ? normalizedDate >= normalizedStart : true) && (normalizedEnd ? normalizedDate <= normalizedEnd : true);
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Generates a list of dates, each representing the start of a month,
|
|
778
|
+
* starting from a given date and continuing for a specified number of months.
|
|
779
|
+
* This function correctly handles year rollover.
|
|
780
|
+
* @param startDate - The date from which to start generating month start dates.
|
|
781
|
+
* @param monthsCount - The number of month start dates to generate.
|
|
782
|
+
* @returns An array of Dates, each set to the first day of consecutive months starting from startDate.
|
|
783
|
+
*/
|
|
784
|
+
function generateSequentialDates(startDate, monthsCount) {
|
|
785
|
+
const dates = [];
|
|
786
|
+
for (let i = 0; i < monthsCount; i++) {
|
|
787
|
+
const date = new Date(startDate);
|
|
788
|
+
date.setMonth(startDate.getMonth() + i, 1);
|
|
789
|
+
dates.push(new Date(date.getFullYear(), date.getMonth(), 1));
|
|
790
|
+
}
|
|
791
|
+
return dates;
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* Format the given date as a string in the format 'dd/mm/yy'.
|
|
795
|
+
* @param date The date to format
|
|
796
|
+
* @returns The formatted date string
|
|
797
|
+
*/
|
|
798
|
+
function formatDateString(date) {
|
|
799
|
+
if (!date) return "";
|
|
800
|
+
return `${String(date.getDate()).padStart(2, "0")}/${String(date.getMonth() + 1).padStart(2, "0")}/${String(date.getFullYear()).slice(-2)}`;
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* Handles the logic for when a day is clicked on the calendar.
|
|
804
|
+
*
|
|
805
|
+
* @param clickedDate - The date that was clicked.
|
|
806
|
+
* @param isRangeMode - Whether the calendar is in range mode.
|
|
807
|
+
* @param prevSelectedSlot - The previously selected slot.
|
|
808
|
+
* Spec 1: If the user clicks on a single date, that date is set as the start date of the slot.
|
|
809
|
+
* Spec 2: If the user selects the same end date as the start date, clear the slot
|
|
810
|
+
* Spec 3: If the user clicks on a date that is before the currently selected start date, the new date becomes the start date and the previous start date becomes the end date.
|
|
811
|
+
* Spec 4: If the user clicks on a date that is after the currently selected start date, that date becomes the end date of the slot.
|
|
812
|
+
* Spec 5: If a slot is already defined (i.e., both start and end dates are defined) and the user clicks on a new date, the existing slot is cleared and the new date becomes the start date of the new slot.
|
|
813
|
+
*/
|
|
814
|
+
function computeNewSelectedSlot(clickedDate, prevSelectedSlot) {
|
|
815
|
+
if (!prevSelectedSlot || prevSelectedSlot?.start === null) return {
|
|
816
|
+
start: clickedDate,
|
|
817
|
+
end: null
|
|
818
|
+
};
|
|
819
|
+
else if (!prevSelectedSlot.end) {
|
|
820
|
+
if (normalizeDate(clickedDate).getTime() === normalizeDate(prevSelectedSlot.start).getTime()) return;
|
|
821
|
+
else if (normalizeDate(clickedDate) < normalizeDate(prevSelectedSlot.start)) return {
|
|
822
|
+
start: clickedDate,
|
|
823
|
+
end: prevSelectedSlot.start
|
|
824
|
+
};
|
|
825
|
+
else return {
|
|
826
|
+
start: prevSelectedSlot.start,
|
|
827
|
+
end: clickedDate
|
|
828
|
+
};
|
|
829
|
+
} else return {
|
|
830
|
+
start: clickedDate,
|
|
831
|
+
end: null
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* Format the selected slot to a string that can be displayed in the input field.
|
|
836
|
+
* @param slot The new selected slot
|
|
837
|
+
* @returns The formatted string
|
|
838
|
+
* Spec 0: If the slot is undefined, return an empty string.
|
|
839
|
+
* Spec 1: If the start and end dates are the same, return the formatted date of the start date.
|
|
840
|
+
* Spec 2: If the start and end dates are in the same year, return the formatted start date without the year and end date.
|
|
841
|
+
* Spec 3: If the start and end dates are in different years, return the formatted start date and end date with the year.
|
|
842
|
+
*/
|
|
843
|
+
function formatSlotToInputValue(slot) {
|
|
844
|
+
let formattedDate = "";
|
|
845
|
+
if (slot) {
|
|
846
|
+
const { start, end } = slot;
|
|
847
|
+
if (isSameDay(start, end)) formattedDate = formatDateString(start);
|
|
848
|
+
else {
|
|
849
|
+
let formattedStartDate = formatDateString(start);
|
|
850
|
+
if (start && end && start.getFullYear() === end.getFullYear()) formattedStartDate = formattedStartDate.slice(0, 5);
|
|
851
|
+
formattedDate = `${formattedStartDate} - ${formatDateString(end)}`;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
return formattedDate;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Format the new value to a string that can be displayed in the input field.
|
|
858
|
+
* @param value The new value
|
|
859
|
+
* @param isRangeMode Whether the calendar is in range mode
|
|
860
|
+
* @returns The formatted string
|
|
861
|
+
*/
|
|
862
|
+
function formatInputValue({ value, isRangeMode }) {
|
|
863
|
+
if (isRangeMode) return formatSlotToInputValue(value);
|
|
864
|
+
return formatDateString(value);
|
|
865
|
+
}
|
|
866
|
+
function formatValueToSlot({ value, isRangeMode }) {
|
|
867
|
+
if (isRangeMode) return value;
|
|
868
|
+
return value ? {
|
|
869
|
+
start: value,
|
|
870
|
+
end: value
|
|
871
|
+
} : value;
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* Validates the selected and selectable slots as well as the initial date.
|
|
875
|
+
* @param selectedSlot The selected slot to validate.
|
|
876
|
+
* @param selectableSlot The selectable slot to validate.
|
|
877
|
+
* @param date The date to validate.
|
|
878
|
+
*
|
|
879
|
+
* Three main validation checks are performed:
|
|
880
|
+
* - Check if the selected and selectable slots are valid.
|
|
881
|
+
* - Ensures the selected slot is within the selectable slot.
|
|
882
|
+
* - Ensures the initial date is within the selectable slot
|
|
883
|
+
*/
|
|
884
|
+
function validateSlots(selectedSlot, selectableSlot, date) {
|
|
885
|
+
if (selectedSlot && !isValidSlot(selectedSlot)) throw new Error(INVALID_SELECTED_SLOT_ERROR);
|
|
886
|
+
if (selectableSlot && !isValidSlot(selectableSlot)) throw new Error(INVALID_SELECTABLE_SLOT_ERROR);
|
|
887
|
+
if (selectedSlot?.start && selectedSlot?.end && selectableSlot?.start && selectableSlot?.end && (normalizeDate(selectedSlot.start) < normalizeDate(selectableSlot.start) || normalizeDate(selectedSlot.end) > normalizeDate(selectableSlot.end))) throw new Error(INVALID_SELECTED_SLOT_BASED_ON_SELECTABLE_SLOT_ERROR);
|
|
888
|
+
if (date && selectableSlot?.start && selectableSlot?.end && (normalizeDate(date) < normalizeDate(selectableSlot.start) || normalizeDate(date) > normalizeDate(selectableSlot.end))) throw new Error(INVALID_INITIAL_DATE_ERROR);
|
|
889
|
+
}
|
|
890
|
+
// src/components/DatePicker/useCalendar.ts
|
|
891
|
+
const getLocalizedWeekdays = (locale = navigator.language) => {
|
|
892
|
+
const formatter = new Intl.DateTimeFormat(locale, { weekday: "narrow" });
|
|
893
|
+
return [...Array(7).keys()].map((dayIndex) => {
|
|
894
|
+
const date = new Date(Date.UTC(2024, 0, 1 + dayIndex));
|
|
895
|
+
return formatter.format(date);
|
|
896
|
+
});
|
|
897
|
+
};
|
|
898
|
+
function useCalendar({ displayedMonthStartDate, selectableSlot, selectedSlot, locale }) {
|
|
899
|
+
const now = /* @__PURE__ */ new Date();
|
|
900
|
+
const weekDays = getLocalizedWeekdays(locale);
|
|
901
|
+
now.setHours(0, 0, 0, 0);
|
|
902
|
+
const referenceDate = selectableSlot?.start ?? now;
|
|
903
|
+
referenceDate.setHours(0, 0, 0, 0);
|
|
904
|
+
const displayedYear = displayedMonthStartDate.getFullYear();
|
|
905
|
+
const displayedMonth = displayedMonthStartDate.getMonth();
|
|
906
|
+
const daysInMonth = getAllDatesInMonth(displayedMonth, displayedYear);
|
|
907
|
+
const daysInLastMonth = getDatesFromPreviousMonthInFirstWeek(displayedMonth, displayedYear);
|
|
908
|
+
const daysInNextMonth = getDatesFromNextMonthInLastWeek(displayedMonth, displayedYear);
|
|
909
|
+
const allDays = [
|
|
910
|
+
...daysInLastMonth,
|
|
911
|
+
...daysInMonth,
|
|
912
|
+
...daysInNextMonth
|
|
913
|
+
];
|
|
914
|
+
const buildDayWrapperClassName = (date) => {
|
|
915
|
+
const isStart = selectedSlot && isSameDay(date, selectedSlot.start) || false;
|
|
916
|
+
const isEnd = selectedSlot && isSameDay(date, selectedSlot.end) || false;
|
|
917
|
+
const withinSelectedSlot = selectedSlot?.start && selectedSlot.end && isWithinInterval(date, selectedSlot) || isStart || isEnd;
|
|
918
|
+
const insideSelectableSlot = isWithinInterval(date, selectableSlot);
|
|
919
|
+
let classNames = {
|
|
920
|
+
"inside-selectable-slot": insideSelectableSlot,
|
|
921
|
+
"outside-selectable-slot": !insideSelectableSlot,
|
|
922
|
+
"current-month": date.getMonth() === displayedMonth,
|
|
923
|
+
past: normalizeDate(date) < normalizeDate(referenceDate)
|
|
924
|
+
};
|
|
925
|
+
if (selectedSlot) classNames = {
|
|
926
|
+
...classNames,
|
|
927
|
+
start: isStart,
|
|
928
|
+
end: isEnd,
|
|
929
|
+
"within-selected-slot": withinSelectedSlot
|
|
930
|
+
};
|
|
931
|
+
return cx("day-wrapper", classNames);
|
|
932
|
+
};
|
|
933
|
+
const isDateSelectable = (date) => isWithinInterval(date, selectableSlot);
|
|
934
|
+
return {
|
|
935
|
+
days: allDays,
|
|
936
|
+
isToday: (date) => normalizeDate(date).getTime() === normalizeDate(now).getTime(),
|
|
937
|
+
buildDayWrapperClassName,
|
|
938
|
+
isDateSelectable,
|
|
939
|
+
weekDays
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
// src/components/DatePicker/Calendar.tsx
|
|
943
|
+
const Day = ({ date, isToday, isSelectable, dayWrapperClassName, onClick }) => /* @__PURE__ */ React.createElement("div", {
|
|
944
|
+
onClick: () => {
|
|
945
|
+
if (isSelectable) onClick(date);
|
|
946
|
+
},
|
|
947
|
+
className: "day-background"
|
|
948
|
+
}, /* @__PURE__ */ React.createElement("div", { className: dayWrapperClassName }, /* @__PURE__ */ React.createElement("span", { className: "day" }, date.getDate()), isToday && /* @__PURE__ */ React.createElement("span", { className: "current-date-highlight" })));
|
|
949
|
+
const Calendar = (props) => {
|
|
950
|
+
const { days, isToday, isDateSelectable, buildDayWrapperClassName, weekDays } = useCalendar(props);
|
|
951
|
+
const { displayedMonthStartDate, onDayClick, locale } = props;
|
|
952
|
+
return /* @__PURE__ */ React.createElement("div", { className: "calendar-wrapper" }, /* @__PURE__ */ React.createElement("div", { className: "calendar-anatomy" }, /* @__PURE__ */ React.createElement("p", { className: "calendar-month-label" }, displayedMonthStartDate.toLocaleString(locale || navigator.language, { month: "short" })), /* @__PURE__ */ React.createElement("div", { className: "calendar-grid-wrapper" }, /* @__PURE__ */ React.createElement("div", { className: "calendar-weekday-labels" }, weekDays.map((label, index) => /* @__PURE__ */ React.createElement("p", { key: `${label}-${index}` }, label))), /* @__PURE__ */ React.createElement("div", { className: "calendar-days-grid" }, days.map((date) => /* @__PURE__ */ React.createElement(Day, {
|
|
953
|
+
key: date.getTime(),
|
|
954
|
+
date,
|
|
955
|
+
isToday: isToday(date),
|
|
956
|
+
isSelectable: isDateSelectable(date),
|
|
957
|
+
dayWrapperClassName: buildDayWrapperClassName(date),
|
|
958
|
+
onClick: onDayClick
|
|
959
|
+
}))))));
|
|
960
|
+
};
|
|
961
|
+
// src/components/DatePicker/useCalendarPicker.ts
|
|
962
|
+
function useCalendarPicker({ initialDate, selectedSlot, selectableSlot, numberOfMonths = 1 }) {
|
|
963
|
+
validateSlots(selectedSlot, selectableSlot, initialDate);
|
|
964
|
+
const [activeDate, setActiveDate] = useState(() => /* @__PURE__ */ new Date());
|
|
965
|
+
const displayedMonthsStartDates = generateSequentialDates(activeDate, numberOfMonths);
|
|
966
|
+
const activeYear = activeDate.getFullYear();
|
|
967
|
+
const activeMonth = activeDate.getMonth();
|
|
968
|
+
const firstDayOfMonth = new Date(activeYear, activeMonth, 1);
|
|
969
|
+
const lastDayOfMonth = new Date(activeYear, activeMonth + 1, 0);
|
|
970
|
+
const canGoToPreviousMonth = selectableSlot === void 0 || selectableSlot.start === null ? true : selectableSlot.start < firstDayOfMonth;
|
|
971
|
+
const canGoToNextMonth = selectableSlot === void 0 || selectableSlot.end === null ? true : selectableSlot.end > lastDayOfMonth;
|
|
972
|
+
const showNavigationBtn = canGoToPreviousMonth || canGoToNextMonth;
|
|
973
|
+
const handleGoToPreviousMonth = () => {
|
|
974
|
+
if (canGoToPreviousMonth) {
|
|
975
|
+
const previousActiveMonth = activeMonth === 0 ? 11 : activeMonth - 1;
|
|
976
|
+
const previousActiveYear = activeMonth === 0 ? activeYear - 1 : activeYear;
|
|
977
|
+
setActiveDate(new Date(previousActiveYear, previousActiveMonth, 1));
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
const handleGoToNextMonth = () => {
|
|
981
|
+
if (canGoToNextMonth) {
|
|
982
|
+
const nextActiveMonth = activeMonth === 11 ? 0 : activeMonth + 1;
|
|
983
|
+
const nextActiveYear = activeMonth === 11 ? activeYear + 1 : activeYear;
|
|
984
|
+
setActiveDate(new Date(nextActiveYear, nextActiveMonth, 1));
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
useEffect(() => {
|
|
988
|
+
setActiveDate(initialDate ?? selectedSlot?.start ?? selectableSlot?.start ?? /* @__PURE__ */ new Date());
|
|
989
|
+
}, [
|
|
990
|
+
initialDate,
|
|
991
|
+
selectedSlot,
|
|
992
|
+
selectableSlot
|
|
993
|
+
]);
|
|
994
|
+
return {
|
|
995
|
+
displayedMonthsStartDates,
|
|
996
|
+
showNavigationBtn,
|
|
997
|
+
canGoToPreviousMonth,
|
|
998
|
+
canGoToNextMonth,
|
|
999
|
+
handleGoToPreviousMonth,
|
|
1000
|
+
handleGoToNextMonth
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
// src/components/DatePicker/CalendarPicker.tsx
|
|
1004
|
+
const CalendarPicker = ({ initialDate, selectedSlot, selectableSlot, numberOfMonths = 1, onDayClick, calendarPickerRef, locale }) => {
|
|
1005
|
+
const { displayedMonthsStartDates, showNavigationBtn, canGoToNextMonth, canGoToPreviousMonth, handleGoToNextMonth, handleGoToPreviousMonth } = useCalendarPicker({
|
|
1006
|
+
initialDate,
|
|
1007
|
+
selectedSlot,
|
|
1008
|
+
selectableSlot,
|
|
1009
|
+
numberOfMonths
|
|
1010
|
+
});
|
|
1011
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1012
|
+
ref: calendarPickerRef,
|
|
1013
|
+
className: "calendar-picker"
|
|
1014
|
+
}, showNavigationBtn && /* @__PURE__ */ React.createElement("span", {
|
|
1015
|
+
className: cx("calendar-navigation-btn", "previous", { disabled: !canGoToPreviousMonth }),
|
|
1016
|
+
onClick: handleGoToPreviousMonth
|
|
1017
|
+
}, /* @__PURE__ */ React.createElement(ChevronLeft, { size: "lg" })), /* @__PURE__ */ React.createElement("div", { className: cx("calendar-list", { "navigation-btn-hidden": !showNavigationBtn }) }, displayedMonthsStartDates.map((date) => /* @__PURE__ */ React.createElement(Calendar, {
|
|
1018
|
+
key: date.getTime(),
|
|
1019
|
+
displayedMonthStartDate: date,
|
|
1020
|
+
selectableSlot,
|
|
1021
|
+
selectedSlot,
|
|
1022
|
+
onDayClick,
|
|
1023
|
+
locale
|
|
1024
|
+
}))), showNavigationBtn && /* @__PURE__ */ React.createElement("span", {
|
|
1025
|
+
className: cx("calendar-navigation-btn", "next", { disabled: !canGoToNextMonth }),
|
|
1026
|
+
onClick: handleGoToNextMonth
|
|
1027
|
+
}, /* @__PURE__ */ React.createElement(ChevronRight, { size: "lg" })));
|
|
1028
|
+
};
|
|
1029
|
+
// src/components/DatePicker/useDatePicker.ts
|
|
1030
|
+
const SINGLE_DATE_REGEX = /^\d{2}\/\d{2}\/\d{2}$/;
|
|
1031
|
+
const FIXABLE_DATE_REGEX = /^\d{1,2}\/\d{1,2}\/\d{1,2}$/;
|
|
1032
|
+
function useDatePicker(datePickerProps) {
|
|
1033
|
+
const { value, isRangeMode, selectableSlot, errorMessages, onDateChange } = datePickerProps;
|
|
1034
|
+
const [showPicker, setShowPicker] = useState(false);
|
|
1035
|
+
const [inputValue, setInputValue] = useState(formatInputValue(datePickerProps));
|
|
1036
|
+
const [selectedSlot, setSelectedSlot] = useState(formatValueToSlot(datePickerProps));
|
|
1037
|
+
const [statusWithMessage, setStatusWithMessage] = useState();
|
|
1038
|
+
const calendarPickerRef = useRef(null);
|
|
1039
|
+
const inputRef = useRef(null);
|
|
1040
|
+
const handleInputClick = () => {
|
|
1041
|
+
if (isRangeMode) setShowPicker(true);
|
|
1042
|
+
};
|
|
1043
|
+
const handleDayClick = (clickedDate) => {
|
|
1044
|
+
if (isRangeMode) {
|
|
1045
|
+
const newSelectedSlot = computeNewSelectedSlot(clickedDate, value);
|
|
1046
|
+
onDateChange(clickedDate, newSelectedSlot);
|
|
1047
|
+
} else onDateChange(clickedDate);
|
|
1048
|
+
};
|
|
1049
|
+
const handleInputOnChange = (newValue) => {
|
|
1050
|
+
if (isRangeMode) return;
|
|
1051
|
+
setInputValue(newValue);
|
|
1052
|
+
if (SINGLE_DATE_REGEX.test(newValue)) {
|
|
1053
|
+
const [day, month, year] = newValue.split("/").map(Number);
|
|
1054
|
+
const date = new Date(year + 2e3, month - 1, day);
|
|
1055
|
+
if (!Number.isNaN(date.getTime()) && isWithinInterval(date, selectableSlot)) {
|
|
1056
|
+
setStatusWithMessage(void 0);
|
|
1057
|
+
onDateChange(date);
|
|
1058
|
+
} else {
|
|
1059
|
+
setStatusWithMessage({
|
|
1060
|
+
status: "error",
|
|
1061
|
+
message: errorMessages?.invalidDate
|
|
1062
|
+
});
|
|
1063
|
+
onDateChange(void 0);
|
|
1064
|
+
}
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
if (containsOnlyNumbersAndSlashes(newValue) && newValue.length < 8) {
|
|
1068
|
+
setStatusWithMessage(void 0);
|
|
1069
|
+
onDateChange(void 0);
|
|
1070
|
+
return;
|
|
1071
|
+
}
|
|
1072
|
+
onDateChange(void 0);
|
|
1073
|
+
setStatusWithMessage({
|
|
1074
|
+
status: "error",
|
|
1075
|
+
message: errorMessages?.invalidInput
|
|
1076
|
+
});
|
|
1077
|
+
};
|
|
1078
|
+
const handleBlur = () => {
|
|
1079
|
+
const isDate = SINGLE_DATE_REGEX.test(inputValue);
|
|
1080
|
+
if (!isDate && FIXABLE_DATE_REGEX.test(inputValue)) {
|
|
1081
|
+
const [day, month, year] = inputValue.split("/").map((s) => s.length === 1 ? `0${s}` : s);
|
|
1082
|
+
handleInputOnChange(`${day}/${month}/${year}`);
|
|
1083
|
+
} else if (!isDate) setStatusWithMessage({
|
|
1084
|
+
status: "error",
|
|
1085
|
+
message: errorMessages?.invalidInput
|
|
1086
|
+
});
|
|
1087
|
+
};
|
|
1088
|
+
useEffect(() => {
|
|
1089
|
+
const newInput = formatInputValue(datePickerProps);
|
|
1090
|
+
if (newInput !== inputValue) setInputValue(newInput);
|
|
1091
|
+
setStatusWithMessage(void 0);
|
|
1092
|
+
setSelectedSlot(formatValueToSlot(datePickerProps));
|
|
1093
|
+
}, [value]);
|
|
1094
|
+
return {
|
|
1095
|
+
showPicker,
|
|
1096
|
+
inputValue,
|
|
1097
|
+
statusWithMessage,
|
|
1098
|
+
selectedSlot,
|
|
1099
|
+
inputRef,
|
|
1100
|
+
calendarPickerRef,
|
|
1101
|
+
setShowPicker,
|
|
1102
|
+
handleDayClick,
|
|
1103
|
+
handleInputClick,
|
|
1104
|
+
handleInputOnChange,
|
|
1105
|
+
handleBlur
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
// src/components/DatePicker/DatePicker.tsx
|
|
1109
|
+
const DatePicker = (props) => {
|
|
1110
|
+
const { inputValue, statusWithMessage, selectedSlot, showPicker, inputRef, calendarPickerRef, setShowPicker, handleDayClick, handleInputClick, handleInputOnChange, handleBlur } = useDatePicker(props);
|
|
1111
|
+
const { inputFieldWrapperClassname, ...otherInputProps } = props.inputProps;
|
|
1112
|
+
const { selectableSlot } = props;
|
|
1113
|
+
const { testIdPrefix } = props;
|
|
1114
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1115
|
+
"data-testid": testIdPrefix ? testIdPrefix : void 0,
|
|
1116
|
+
className: "ui-date-picker"
|
|
1117
|
+
}, /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Input, {
|
|
1118
|
+
testIdPrefix,
|
|
1119
|
+
...otherInputProps,
|
|
1120
|
+
ref: inputRef,
|
|
1121
|
+
value: inputValue,
|
|
1122
|
+
onClick: handleInputClick,
|
|
1123
|
+
type: "text",
|
|
1124
|
+
trailingContent: {
|
|
1125
|
+
content: /* @__PURE__ */ React.createElement(Calendar$1, null),
|
|
1126
|
+
onClickCallback: () => setShowPicker(!showPicker)
|
|
1127
|
+
},
|
|
1128
|
+
inputFieldWrapperClassname: cx("date-picker-input", inputFieldWrapperClassname, { "range-mode": props.isRangeMode }),
|
|
1129
|
+
autoComplete: "off",
|
|
1130
|
+
onChange: (e) => handleInputOnChange(e.target.value),
|
|
1131
|
+
onBlur: handleBlur,
|
|
1132
|
+
statusWithMessage
|
|
1133
|
+
})), /* @__PURE__ */ React.createElement(InputPopover, {
|
|
1134
|
+
inputRef,
|
|
1135
|
+
isOpen: showPicker,
|
|
1136
|
+
onClose: () => setShowPicker(false),
|
|
1137
|
+
testIdPrefix: "modal"
|
|
1138
|
+
}, /* @__PURE__ */ React.createElement(CalendarPicker, {
|
|
1139
|
+
...props.calendarPickerProps,
|
|
1140
|
+
selectedSlot,
|
|
1141
|
+
onDayClick: handleDayClick,
|
|
1142
|
+
calendarPickerRef,
|
|
1143
|
+
selectableSlot
|
|
1144
|
+
})));
|
|
1145
|
+
};
|
|
1146
|
+
// src/components/Switch.tsx
|
|
1147
|
+
const Switch = React.forwardRef(({ label, size, id: providedId, dataTestId, ...rest }, ref) => {
|
|
1148
|
+
const generatedId = React.useId();
|
|
1149
|
+
const id = providedId ?? generatedId;
|
|
1150
|
+
return /* @__PURE__ */ React.createElement("label", {
|
|
1151
|
+
className: cx("ui-switch", {
|
|
1152
|
+
[`size-${size}`]: size,
|
|
1153
|
+
"with-label": !!label
|
|
1154
|
+
}),
|
|
1155
|
+
htmlFor: id
|
|
1156
|
+
}, /* @__PURE__ */ React.createElement("input", {
|
|
1157
|
+
ref,
|
|
1158
|
+
id,
|
|
1159
|
+
type: "checkbox",
|
|
1160
|
+
"data-testid": dataTestId,
|
|
1161
|
+
...rest
|
|
1162
|
+
}), label && /* @__PURE__ */ React.createElement("div", { className: "label" }, label));
|
|
1163
|
+
});
|
|
1164
|
+
Switch.displayName = "Switch";
|
|
1165
|
+
// src/components/DurationInput/DurationInput.tsx
|
|
1166
|
+
const PRESETS = {
|
|
1167
|
+
h: {
|
|
1168
|
+
label: "h",
|
|
1169
|
+
digits: 2,
|
|
1170
|
+
msPerUnit: 36e5
|
|
1171
|
+
},
|
|
1172
|
+
m: {
|
|
1173
|
+
label: "m",
|
|
1174
|
+
digits: 2,
|
|
1175
|
+
msPerUnit: 6e4
|
|
1176
|
+
},
|
|
1177
|
+
s: {
|
|
1178
|
+
label: "s",
|
|
1179
|
+
digits: 2,
|
|
1180
|
+
msPerUnit: 1e3
|
|
1181
|
+
},
|
|
1182
|
+
ms: {
|
|
1183
|
+
label: "ms",
|
|
1184
|
+
digits: 3,
|
|
1185
|
+
msPerUnit: 1
|
|
1186
|
+
}
|
|
1187
|
+
};
|
|
1188
|
+
function normalizeUnits(units, padChar, max) {
|
|
1189
|
+
const configs = units.map((u) => {
|
|
1190
|
+
if (typeof u === "string") return {
|
|
1191
|
+
key: u,
|
|
1192
|
+
...PRESETS[u]
|
|
1193
|
+
};
|
|
1194
|
+
const key = u.key ?? u.label;
|
|
1195
|
+
const base = PRESETS[key] ?? {};
|
|
1196
|
+
return {
|
|
1197
|
+
key,
|
|
1198
|
+
label: u.label ?? base.label ?? key,
|
|
1199
|
+
msPerUnit: u.msPerUnit ?? base.msPerUnit ?? 1,
|
|
1200
|
+
digits: u.digits ?? base.digits ?? 2
|
|
1201
|
+
};
|
|
1202
|
+
});
|
|
1203
|
+
configs.sort((a, b) => b.msPerUnit - a.msPerUnit);
|
|
1204
|
+
return configs.map((unit, idx) => {
|
|
1205
|
+
const prev = idx > 0 ? configs[idx - 1] : null;
|
|
1206
|
+
const digits = prev ? unit.digits : max < Infinity ? String(Math.floor(max / unit.msPerUnit)).length : void 0;
|
|
1207
|
+
return {
|
|
1208
|
+
...unit,
|
|
1209
|
+
digits,
|
|
1210
|
+
max: prev ? Math.floor(prev.msPerUnit / unit.msPerUnit) : Infinity,
|
|
1211
|
+
padChar
|
|
1212
|
+
};
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1215
|
+
function toUnitField(ms, unit) {
|
|
1216
|
+
const value = Math.floor(ms / (unit.msPerUnit || 1)) % (unit.max ?? Infinity);
|
|
1217
|
+
return [unit.key, String(value || unit.padChar).padStart(unit.digits ?? 0, unit.padChar)];
|
|
1218
|
+
}
|
|
1219
|
+
function toFormattedValues(units, ms) {
|
|
1220
|
+
return Object.fromEntries(units.map((u) => toUnitField(ms, u)));
|
|
1221
|
+
}
|
|
1222
|
+
function toTotalMilliseconds(fields, values) {
|
|
1223
|
+
return fields.reduce((acc, f) => {
|
|
1224
|
+
const cleaned = values[f.key].replace(/[^0-9]/g, "");
|
|
1225
|
+
return acc + (parseInt(cleaned, 10) || 0) * (f.msPerUnit || 1);
|
|
1226
|
+
}, 0);
|
|
1227
|
+
}
|
|
1228
|
+
function useDurationInput({ units, value, onChange, padChar, max, containerRef }) {
|
|
1229
|
+
const fields = useMemo(() => normalizeUnits(units, padChar, max), [
|
|
1230
|
+
units,
|
|
1231
|
+
padChar,
|
|
1232
|
+
max
|
|
1233
|
+
]);
|
|
1234
|
+
const inputRefs = useRef({});
|
|
1235
|
+
const [initialValue, setInitialValue] = useState(value);
|
|
1236
|
+
const [formattedValues, setFormattedValues] = useState(() => toFormattedValues(fields, Math.min(max, value)));
|
|
1237
|
+
if (initialValue !== value) {
|
|
1238
|
+
setInitialValue(value);
|
|
1239
|
+
setFormattedValues(toFormattedValues(fields, Math.min(max, value)));
|
|
1240
|
+
}
|
|
1241
|
+
const focusField = useCallback((key) => {
|
|
1242
|
+
requestAnimationFrame(() => {
|
|
1243
|
+
inputRefs.current[key]?.focus();
|
|
1244
|
+
inputRefs.current[key]?.select();
|
|
1245
|
+
});
|
|
1246
|
+
}, []);
|
|
1247
|
+
const focusNext = useCallback((currentKey) => {
|
|
1248
|
+
const idx = fields.findIndex((f) => f.key === currentKey);
|
|
1249
|
+
const next = fields[idx + 1];
|
|
1250
|
+
if (next) focusField(next.key);
|
|
1251
|
+
}, [fields, focusField]);
|
|
1252
|
+
const focusPrevious = useCallback((currentKey) => {
|
|
1253
|
+
const idx = fields.findIndex((f) => f.key === currentKey);
|
|
1254
|
+
const previous = fields[idx - 1];
|
|
1255
|
+
if (previous) focusField(previous.key);
|
|
1256
|
+
}, [fields, focusField]);
|
|
1257
|
+
const handleChange = useCallback((setting, inputValue) => {
|
|
1258
|
+
const cleaned = inputValue.replace(/[^0-9]/g, "").slice(0, setting.digits);
|
|
1259
|
+
setFormattedValues((prev) => ({
|
|
1260
|
+
...prev,
|
|
1261
|
+
[setting.key]: cleaned
|
|
1262
|
+
}));
|
|
1263
|
+
if (cleaned.length === setting.digits) focusNext(setting.key);
|
|
1264
|
+
}, [focusNext]);
|
|
1265
|
+
const submitChange = useCallback(() => {
|
|
1266
|
+
const ms = toTotalMilliseconds(fields, formattedValues);
|
|
1267
|
+
const next = toFormattedValues(fields, Math.min(max, ms));
|
|
1268
|
+
setFormattedValues(next);
|
|
1269
|
+
onChange?.(toTotalMilliseconds(fields, next));
|
|
1270
|
+
}, [
|
|
1271
|
+
fields,
|
|
1272
|
+
formattedValues,
|
|
1273
|
+
max,
|
|
1274
|
+
onChange
|
|
1275
|
+
]);
|
|
1276
|
+
const handleKeyDown = useCallback((setting, e) => {
|
|
1277
|
+
const input = e.currentTarget;
|
|
1278
|
+
if (!input) return;
|
|
1279
|
+
const cursorAtTheEnd = input.selectionStart === input.value.length;
|
|
1280
|
+
const keyFirstLetter = setting.key.at(0)?.toLowerCase();
|
|
1281
|
+
if ((e.key === "ArrowRight" || e.key === ":" || e.key.toLowerCase() === keyFirstLetter) && cursorAtTheEnd) {
|
|
1282
|
+
e.preventDefault();
|
|
1283
|
+
focusNext(setting.key);
|
|
1284
|
+
}
|
|
1285
|
+
if (e.key === "ArrowLeft" && input.selectionStart === 0) {
|
|
1286
|
+
e.preventDefault();
|
|
1287
|
+
focusPrevious(setting.key);
|
|
1288
|
+
}
|
|
1289
|
+
if (e.key === "Enter") submitChange();
|
|
1290
|
+
}, [
|
|
1291
|
+
focusNext,
|
|
1292
|
+
focusPrevious,
|
|
1293
|
+
submitChange
|
|
1294
|
+
]);
|
|
1295
|
+
return {
|
|
1296
|
+
fields,
|
|
1297
|
+
formattedValues,
|
|
1298
|
+
inputRefs,
|
|
1299
|
+
containerRef,
|
|
1300
|
+
handleChange,
|
|
1301
|
+
handleFocus: useCallback((setting) => focusField(setting.key), [focusField]),
|
|
1302
|
+
handleBlur: useCallback((e) => {
|
|
1303
|
+
const nextTarget = e.relatedTarget;
|
|
1304
|
+
if (nextTarget && containerRef.current?.contains(nextTarget)) return;
|
|
1305
|
+
submitChange();
|
|
1306
|
+
}, [containerRef, submitChange]),
|
|
1307
|
+
handleKeyDown
|
|
1308
|
+
};
|
|
1309
|
+
}
|
|
1310
|
+
const UnitField = function UnitField({ setting, value, inputRef, onChange, onFocus, onBlur, onKeyDown, tabIndex, dataTestId }) {
|
|
1311
|
+
const { label, digits, padChar } = setting;
|
|
1312
|
+
const placeholder = padChar.repeat(digits ?? 2);
|
|
1313
|
+
return /* @__PURE__ */ React.createElement("span", { className: "unit-field" }, /* @__PURE__ */ React.createElement("input", {
|
|
1314
|
+
ref: inputRef,
|
|
1315
|
+
type: "text",
|
|
1316
|
+
inputMode: "numeric",
|
|
1317
|
+
style: { width: digits ? `${digits ?? 2}ch` : `${value.length || 1}ch` },
|
|
1318
|
+
tabIndex,
|
|
1319
|
+
value,
|
|
1320
|
+
placeholder,
|
|
1321
|
+
"aria-label": label,
|
|
1322
|
+
onChange: (e) => onChange(e.target.value),
|
|
1323
|
+
onFocus,
|
|
1324
|
+
onBlur,
|
|
1325
|
+
onKeyDown,
|
|
1326
|
+
"data-testid": dataTestId
|
|
1327
|
+
}), /* @__PURE__ */ React.createElement("span", {
|
|
1328
|
+
"aria-hidden": true,
|
|
1329
|
+
onClick: onFocus
|
|
1330
|
+
}, label));
|
|
1331
|
+
};
|
|
1332
|
+
const DEFAULT_UNITS = ["m", "s"];
|
|
1333
|
+
/**
|
|
1334
|
+
* Duration units can be set up via their shorthand strings (h, m, s, ms)
|
|
1335
|
+
* or more precisely through objects.
|
|
1336
|
+
* The value of the component is a number representing the milliseconds of the duration.
|
|
1337
|
+
*/
|
|
1338
|
+
const DurationInput = ({ id, units = DEFAULT_UNITS, value = 0, onChange, padChar = "0", small = false, max = Infinity, label, hint, statusWithMessage, dataTestId, ...rest }) => {
|
|
1339
|
+
const containerRef = useRef(null);
|
|
1340
|
+
const { fields, formattedValues, inputRefs, handleChange, handleFocus, handleBlur, handleKeyDown } = useDurationInput({
|
|
1341
|
+
units,
|
|
1342
|
+
value,
|
|
1343
|
+
onChange,
|
|
1344
|
+
containerRef,
|
|
1345
|
+
padChar,
|
|
1346
|
+
max
|
|
1347
|
+
});
|
|
1348
|
+
return /* @__PURE__ */ React.createElement(FieldWrapper, {
|
|
1349
|
+
id,
|
|
1350
|
+
label,
|
|
1351
|
+
hint,
|
|
1352
|
+
small,
|
|
1353
|
+
statusWithMessage
|
|
1354
|
+
}, /* @__PURE__ */ React.createElement("div", {
|
|
1355
|
+
ref: containerRef,
|
|
1356
|
+
id,
|
|
1357
|
+
className: cx("ui-duration-input", { small }),
|
|
1358
|
+
role: "group",
|
|
1359
|
+
"aria-label": "Duration Input",
|
|
1360
|
+
onMouseDown: (e) => {
|
|
1361
|
+
if (e.currentTarget === e.target) inputRefs.current[fields[0].key]?.focus();
|
|
1362
|
+
},
|
|
1363
|
+
...rest
|
|
1364
|
+
}, fields.map((setting, idx) => /* @__PURE__ */ React.createElement(UnitField, {
|
|
1365
|
+
key: setting.key,
|
|
1366
|
+
setting,
|
|
1367
|
+
value: formattedValues[setting.key],
|
|
1368
|
+
inputRef: (el) => {
|
|
1369
|
+
if (el) inputRefs.current[setting.key] = el;
|
|
1370
|
+
},
|
|
1371
|
+
tabIndex: idx === 0 ? 0 : -1,
|
|
1372
|
+
onChange: (val) => handleChange(setting, val),
|
|
1373
|
+
onFocus: () => handleFocus(setting),
|
|
1374
|
+
onBlur: (e) => handleBlur(e),
|
|
1375
|
+
onKeyDown: (e) => handleKeyDown(setting, e),
|
|
1376
|
+
dataTestId: dataTestId ? `${dataTestId}-${setting.key}` : void 0
|
|
1377
|
+
}))));
|
|
1378
|
+
};
|
|
1379
|
+
// src/components/PasswordInput.tsx
|
|
1380
|
+
const PasswordInput = React.forwardRef(({ inputFieldWrapperClassname, ...otherProps }, ref) => {
|
|
1381
|
+
const [showPassword, toggleShowPassword] = useState(false);
|
|
1382
|
+
return /* @__PURE__ */ React.createElement(Input, {
|
|
1383
|
+
...otherProps,
|
|
1384
|
+
type: showPassword ? "text" : "password",
|
|
1385
|
+
trailingContent: {
|
|
1386
|
+
content: showPassword ? /* @__PURE__ */ React.createElement(EyeClosed, null) : /* @__PURE__ */ React.createElement(Eye, null),
|
|
1387
|
+
onClickCallback: () => toggleShowPassword(!showPassword)
|
|
1388
|
+
},
|
|
1389
|
+
inputFieldWrapperClassname: cx("ui-password-input", inputFieldWrapperClassname),
|
|
1390
|
+
ref
|
|
1391
|
+
});
|
|
1392
|
+
});
|
|
1393
|
+
PasswordInput.displayName = "PasswordInput";
|
|
1394
|
+
// src/components/RadioButton.tsx
|
|
1395
|
+
const RadioButton = ({ id, label, value, hint, readOnly, onClick, small, children, ref, ...rest }) => {
|
|
1396
|
+
const handleClick = (e) => {
|
|
1397
|
+
if (readOnly) {
|
|
1398
|
+
e.preventDefault();
|
|
1399
|
+
e.stopPropagation();
|
|
1400
|
+
} else onClick?.(e);
|
|
1401
|
+
};
|
|
1402
|
+
return /* @__PURE__ */ React.createElement("label", { className: cx("ui-radio-button", {
|
|
1403
|
+
small,
|
|
1404
|
+
"with-label": label || children,
|
|
1405
|
+
"with-hint": hint
|
|
1406
|
+
}) }, /* @__PURE__ */ React.createElement("input", {
|
|
1407
|
+
className: cx({ "read-only": readOnly }),
|
|
1408
|
+
type: "radio",
|
|
1409
|
+
id: id || value,
|
|
1410
|
+
value,
|
|
1411
|
+
onClick: handleClick,
|
|
1412
|
+
ref,
|
|
1413
|
+
...rest
|
|
1414
|
+
}), label && /* @__PURE__ */ React.createElement("div", { className: "radio-label" }, label), children && /* @__PURE__ */ React.createElement("div", { className: "radio-label" }, children), hint && /* @__PURE__ */ React.createElement("div", { className: "hint" }, hint));
|
|
1415
|
+
};
|
|
1416
|
+
// src/components/RadioGroup.tsx
|
|
1417
|
+
const RadioGroup = ({ label, value, subtitle, readOnly, disabled, options, statusWithMessage, required, small, onChange }) => {
|
|
1418
|
+
const [selectedValue, setSelectedValue] = useState(value);
|
|
1419
|
+
useEffect(() => {
|
|
1420
|
+
setSelectedValue(value);
|
|
1421
|
+
}, [value]);
|
|
1422
|
+
const statusClassname = statusWithMessage?.status ? { [statusWithMessage.status]: true } : {};
|
|
1423
|
+
const handleOptionChange = (e, nextOption) => {
|
|
1424
|
+
if (!readOnly) {
|
|
1425
|
+
setSelectedValue(nextOption.value);
|
|
1426
|
+
nextOption.onChange?.(e);
|
|
1427
|
+
onChange?.(nextOption.value);
|
|
1428
|
+
}
|
|
1429
|
+
};
|
|
1430
|
+
return /* @__PURE__ */ React.createElement("div", { className: cx("ui-radio-button-wrapper", statusClassname) }, label && /* @__PURE__ */ React.createElement("label", { className: "label" }, required && /* @__PURE__ */ React.createElement("span", { className: "required" }, /* @__PURE__ */ React.createElement(RequiredInput, null)), label, " ", subtitle && /* @__PURE__ */ React.createElement("span", { className: "subtitle" }, subtitle)), options.map((option) => /* @__PURE__ */ React.createElement(RadioButton, {
|
|
1431
|
+
key: option.value,
|
|
1432
|
+
...option,
|
|
1433
|
+
readOnly,
|
|
1434
|
+
disabled,
|
|
1435
|
+
checked: selectedValue === option.value,
|
|
1436
|
+
onChange: (e) => {
|
|
1437
|
+
handleOptionChange(e, option);
|
|
1438
|
+
},
|
|
1439
|
+
small
|
|
1440
|
+
})), statusWithMessage?.message && /* @__PURE__ */ React.createElement("div", { className: "status-with-message" }, /* @__PURE__ */ React.createElement(InputStatusIcon, { status: statusWithMessage.status }), /* @__PURE__ */ React.createElement("span", { className: cx("status-message", statusClassname) }, statusWithMessage.message)));
|
|
1441
|
+
};
|
|
1442
|
+
// src/components/Select.tsx
|
|
1443
|
+
const PLACEHOLDER_VALUE = "__PLACEHOLDER__";
|
|
1444
|
+
const Select = ({ id, label, hint, value, options, placeholder, statusWithMessage, required, disabled, narrow, readOnly, small, getOptionLabel, getOptionValue, onChange, dataTestId, ...props }) => {
|
|
1445
|
+
const [selectedOption, setSelectedOption] = useState(value);
|
|
1446
|
+
const handleOnChange = (e) => {
|
|
1447
|
+
const newSelectedOption = e.target.value === PLACEHOLDER_VALUE ? void 0 : options.find((option) => getOptionValue(option) === e.target.value);
|
|
1448
|
+
setSelectedOption(newSelectedOption);
|
|
1449
|
+
onChange(newSelectedOption);
|
|
1450
|
+
};
|
|
1451
|
+
useEffect(() => {
|
|
1452
|
+
setSelectedOption(value);
|
|
1453
|
+
}, [value]);
|
|
1454
|
+
return /* @__PURE__ */ React.createElement(FieldWrapper, {
|
|
1455
|
+
id,
|
|
1456
|
+
label,
|
|
1457
|
+
hint,
|
|
1458
|
+
statusWithMessage,
|
|
1459
|
+
required,
|
|
1460
|
+
disabled,
|
|
1461
|
+
small,
|
|
1462
|
+
narrow
|
|
1463
|
+
}, /* @__PURE__ */ React.createElement("select", {
|
|
1464
|
+
id,
|
|
1465
|
+
"data-testid": dataTestId,
|
|
1466
|
+
className: cx("ui-select", statusWithMessage?.status, {
|
|
1467
|
+
"placeholder-selected": placeholder && !selectedOption,
|
|
1468
|
+
small,
|
|
1469
|
+
"read-only": readOnly
|
|
1470
|
+
}),
|
|
1471
|
+
value: selectedOption ? getOptionValue(selectedOption) : PLACEHOLDER_VALUE,
|
|
1472
|
+
required,
|
|
1473
|
+
disabled: disabled || readOnly,
|
|
1474
|
+
onChange: handleOnChange,
|
|
1475
|
+
...props
|
|
1476
|
+
}, placeholder ? /* @__PURE__ */ React.createElement("option", {
|
|
1477
|
+
value: PLACEHOLDER_VALUE,
|
|
1478
|
+
className: "placeholder-option"
|
|
1479
|
+
}, `– ${placeholder} –`) : /* @__PURE__ */ React.createElement("option", {
|
|
1480
|
+
value: PLACEHOLDER_VALUE,
|
|
1481
|
+
hidden: true
|
|
1482
|
+
}), options.map((option) => /* @__PURE__ */ React.createElement("option", {
|
|
1483
|
+
key: getOptionValue(option),
|
|
1484
|
+
value: getOptionValue(option)
|
|
1485
|
+
}, getOptionLabel(option)))));
|
|
1486
|
+
};
|
|
1487
|
+
// src/components/TextArea.tsx
|
|
1488
|
+
const CHAR_COUNT_ERROR_THRESHOLD = 40;
|
|
1489
|
+
const TextArea = ({ id, label, value = "", hint, required, disabled, statusWithMessage, maxLength, onChange, onKeyUp, onBlur, ...rest }) => {
|
|
1490
|
+
const charCount = value?.length || 0;
|
|
1491
|
+
const { handleKeyUp, handleBlur, isFocusByTab } = useFocusByTab({
|
|
1492
|
+
onBlur,
|
|
1493
|
+
onKeyUp
|
|
1494
|
+
});
|
|
1495
|
+
return /* @__PURE__ */ React.createElement(FieldWrapper, {
|
|
1496
|
+
id,
|
|
1497
|
+
label,
|
|
1498
|
+
hint,
|
|
1499
|
+
statusWithMessage,
|
|
1500
|
+
disabled,
|
|
1501
|
+
required,
|
|
1502
|
+
className: "ui-text-area-field-wrapper"
|
|
1503
|
+
}, /* @__PURE__ */ React.createElement("div", { className: cx("text-area-wrapper", { "focused-by-tab": isFocusByTab }) }, maxLength && /* @__PURE__ */ React.createElement("div", { className: cx("char-count", {
|
|
1504
|
+
error: charCount === maxLength,
|
|
1505
|
+
warning: maxLength - charCount <= CHAR_COUNT_ERROR_THRESHOLD
|
|
1506
|
+
}) }, charCount, "/", maxLength), /* @__PURE__ */ React.createElement("textarea", {
|
|
1507
|
+
className: cx("text-area", { [statusWithMessage?.status || ""]: !!statusWithMessage }),
|
|
1508
|
+
id,
|
|
1509
|
+
value,
|
|
1510
|
+
disabled,
|
|
1511
|
+
maxLength,
|
|
1512
|
+
onChange,
|
|
1513
|
+
onBlur: handleBlur,
|
|
1514
|
+
onKeyUp: handleKeyUp,
|
|
1515
|
+
...rest
|
|
1516
|
+
})));
|
|
1517
|
+
};
|
|
1518
|
+
// src/components/TimePicker.tsx
|
|
1519
|
+
const TimeRange = ({ range, selectedItem, className, onSelectItem }) => /* @__PURE__ */ React.createElement("div", {
|
|
1520
|
+
className: "time-grid",
|
|
1521
|
+
"data-testid": "time-grid"
|
|
1522
|
+
}, range.map((item) => {
|
|
1523
|
+
const formattedItem = item.toString().padStart(2, "0");
|
|
1524
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1525
|
+
key: item,
|
|
1526
|
+
className: cx(className, { selected: selectedItem === item }),
|
|
1527
|
+
"data-testid": className === "hour" ? `hour-${formattedItem}` : `minute-${formattedItem}`,
|
|
1528
|
+
onClick: () => onSelectItem(item)
|
|
1529
|
+
}, formattedItem);
|
|
1530
|
+
}));
|
|
1531
|
+
function formatTimeValue(value, max) {
|
|
1532
|
+
return Math.max(0, Math.min(max, value)).toString().padStart(2, "0");
|
|
1533
|
+
}
|
|
1534
|
+
const TimePicker = ({ onTimeChange, hours = 0, minutes = 0, seconds = 0, displaySeconds = false, testIdPrefix, ...otherProps }) => {
|
|
1535
|
+
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
1536
|
+
const inputRef = useRef(null);
|
|
1537
|
+
const incrementMinute = useCallback((increment) => {
|
|
1538
|
+
let newMinutes = minutes + increment;
|
|
1539
|
+
let newHours = hours;
|
|
1540
|
+
if (newMinutes >= 60) {
|
|
1541
|
+
newMinutes = 0;
|
|
1542
|
+
newHours = (newHours + 1) % 24;
|
|
1543
|
+
} else if (newMinutes < 0) {
|
|
1544
|
+
newMinutes = 59;
|
|
1545
|
+
newHours = (newHours + 23) % 24;
|
|
1546
|
+
}
|
|
1547
|
+
onTimeChange({
|
|
1548
|
+
hours: newHours,
|
|
1549
|
+
minutes: newMinutes,
|
|
1550
|
+
seconds
|
|
1551
|
+
});
|
|
1552
|
+
}, [
|
|
1553
|
+
hours,
|
|
1554
|
+
minutes,
|
|
1555
|
+
seconds,
|
|
1556
|
+
onTimeChange
|
|
1557
|
+
]);
|
|
1558
|
+
const incrementSeconds = useCallback((increment) => {
|
|
1559
|
+
let newSeconds = seconds + increment;
|
|
1560
|
+
let newMinutes = minutes;
|
|
1561
|
+
let newHours = hours;
|
|
1562
|
+
if (newSeconds >= 60) {
|
|
1563
|
+
newSeconds = 0;
|
|
1564
|
+
newMinutes += 1;
|
|
1565
|
+
if (newMinutes >= 60) {
|
|
1566
|
+
newMinutes = 0;
|
|
1567
|
+
newHours = (newHours + 1) % 24;
|
|
1568
|
+
}
|
|
1569
|
+
} else if (newSeconds < 0) {
|
|
1570
|
+
newSeconds = 59;
|
|
1571
|
+
newMinutes -= 1;
|
|
1572
|
+
if (newMinutes < 0) {
|
|
1573
|
+
newMinutes = 59;
|
|
1574
|
+
newHours = (newHours + 23) % 24;
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
onTimeChange({
|
|
1578
|
+
hours: newHours,
|
|
1579
|
+
minutes: newMinutes,
|
|
1580
|
+
seconds: newSeconds
|
|
1581
|
+
});
|
|
1582
|
+
}, [
|
|
1583
|
+
hours,
|
|
1584
|
+
minutes,
|
|
1585
|
+
seconds,
|
|
1586
|
+
onTimeChange
|
|
1587
|
+
]);
|
|
1588
|
+
const handleChange = useCallback((event) => {
|
|
1589
|
+
const value = event.target.value;
|
|
1590
|
+
if (!displaySeconds) {
|
|
1591
|
+
const [h, m] = value.split(":");
|
|
1592
|
+
if (h !== void 0 && m !== void 0) onTimeChange({
|
|
1593
|
+
hours: parseInt(h, 10),
|
|
1594
|
+
minutes: parseInt(m, 10)
|
|
1595
|
+
});
|
|
1596
|
+
} else {
|
|
1597
|
+
const [h, m, s] = value.split(":");
|
|
1598
|
+
if (h !== void 0 && m !== void 0 && s !== void 0) onTimeChange({
|
|
1599
|
+
hours: parseInt(h, 10),
|
|
1600
|
+
minutes: parseInt(m, 10),
|
|
1601
|
+
seconds: parseInt(s, 10)
|
|
1602
|
+
});
|
|
1603
|
+
}
|
|
1604
|
+
}, [displaySeconds, onTimeChange]);
|
|
1605
|
+
const displayedSecondsPart = displaySeconds ? `:${formatTimeValue(seconds, 59)}` : "";
|
|
1606
|
+
const selectedTime = `${formatTimeValue(hours, 23)}:${formatTimeValue(minutes, 59)}${displayedSecondsPart}`;
|
|
1607
|
+
const hoursRange = [...Array(24).keys()];
|
|
1608
|
+
const minutesAndSecondsRange = [...Array(12).keys()].map((i) => i * 5);
|
|
1609
|
+
const openModal = useCallback(() => setIsModalOpen(true), []);
|
|
1610
|
+
const closeModal = useCallback(() => setIsModalOpen(false), []);
|
|
1611
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1612
|
+
"data-testid": testIdPrefix ? testIdPrefix : void 0,
|
|
1613
|
+
className: "ui-time-picker"
|
|
1614
|
+
}, /* @__PURE__ */ React.createElement(Input, {
|
|
1615
|
+
testIdPrefix: testIdPrefix ? testIdPrefix : void 0,
|
|
1616
|
+
...otherProps,
|
|
1617
|
+
className: cx("input", "time-input", otherProps.className),
|
|
1618
|
+
type: "time",
|
|
1619
|
+
name: "time",
|
|
1620
|
+
value: selectedTime,
|
|
1621
|
+
onClick: openModal,
|
|
1622
|
+
onChange: handleChange,
|
|
1623
|
+
ref: inputRef,
|
|
1624
|
+
step: displaySeconds ? 1 : 60
|
|
1625
|
+
}), /* @__PURE__ */ React.createElement(InputPopover, {
|
|
1626
|
+
inputRef,
|
|
1627
|
+
isOpen: isModalOpen,
|
|
1628
|
+
onClose: closeModal,
|
|
1629
|
+
testIdPrefix: testIdPrefix ? `${testIdPrefix}-modal` : void 0
|
|
1630
|
+
}, /* @__PURE__ */ React.createElement("div", { className: "time-picker-container" }, /* @__PURE__ */ React.createElement(TimeRange, {
|
|
1631
|
+
range: hoursRange,
|
|
1632
|
+
selectedItem: hours,
|
|
1633
|
+
className: "hour",
|
|
1634
|
+
onSelectItem: (h) => onTimeChange({
|
|
1635
|
+
hours: h,
|
|
1636
|
+
minutes,
|
|
1637
|
+
seconds
|
|
1638
|
+
})
|
|
1639
|
+
}), /* @__PURE__ */ React.createElement("div", { className: "time-separator" }, ":"), /* @__PURE__ */ React.createElement("div", { className: "minute-container" }, /* @__PURE__ */ React.createElement(TimeRange, {
|
|
1640
|
+
range: minutesAndSecondsRange,
|
|
1641
|
+
selectedItem: minutes,
|
|
1642
|
+
className: "minute",
|
|
1643
|
+
onSelectItem: (m) => onTimeChange({
|
|
1644
|
+
hours,
|
|
1645
|
+
minutes: m,
|
|
1646
|
+
seconds
|
|
1647
|
+
})
|
|
1648
|
+
}), /* @__PURE__ */ React.createElement("div", { className: "minute-buttons" }, /* @__PURE__ */ React.createElement("button", {
|
|
1649
|
+
"data-testid": testIdPrefix ? `${testIdPrefix}-decrement-minute` : void 0,
|
|
1650
|
+
onClick: () => incrementMinute(-1),
|
|
1651
|
+
className: "minute-button"
|
|
1652
|
+
}, "-1mn"), /* @__PURE__ */ React.createElement("button", {
|
|
1653
|
+
"data-testid": testIdPrefix ? `${testIdPrefix}-increment-minute` : void 0,
|
|
1654
|
+
onClick: () => incrementMinute(1),
|
|
1655
|
+
className: "minute-button"
|
|
1656
|
+
}, "+1mn"))), displaySeconds && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "time-separator" }, ":"), /* @__PURE__ */ React.createElement("div", { className: "second-container" }, /* @__PURE__ */ React.createElement(TimeRange, {
|
|
1657
|
+
range: minutesAndSecondsRange,
|
|
1658
|
+
selectedItem: seconds,
|
|
1659
|
+
className: "second",
|
|
1660
|
+
onSelectItem: (s) => onTimeChange({
|
|
1661
|
+
hours,
|
|
1662
|
+
minutes,
|
|
1663
|
+
seconds: s
|
|
1664
|
+
})
|
|
1665
|
+
}), /* @__PURE__ */ React.createElement("div", { className: "second-buttons" }, /* @__PURE__ */ React.createElement("button", {
|
|
1666
|
+
onClick: () => incrementSeconds(-1),
|
|
1667
|
+
className: "second-button"
|
|
1668
|
+
}, "-1s"), /* @__PURE__ */ React.createElement("button", {
|
|
1669
|
+
onClick: () => incrementSeconds(1),
|
|
1670
|
+
className: "second-button"
|
|
1671
|
+
}, "+1s")))))));
|
|
1672
|
+
};
|
|
1673
|
+
// src/components/Slider.tsx
|
|
1674
|
+
const Slider = ({ id, value: initialValue, min = 0, max = 100, step = 1, width = 112, onChange, onChangeCommitted, disabled, className, containerClassName, ...rest }) => {
|
|
1675
|
+
const [value, setValue] = useState(initialValue !== void 0 ? Number(initialValue) : Number(min));
|
|
1676
|
+
useEffect(() => {
|
|
1677
|
+
setValue(Number(initialValue));
|
|
1678
|
+
}, [initialValue]);
|
|
1679
|
+
const handleChange = (e) => {
|
|
1680
|
+
const newValue = Number(e.target.value);
|
|
1681
|
+
setValue(newValue);
|
|
1682
|
+
onChange?.(e);
|
|
1683
|
+
};
|
|
1684
|
+
const handleCommit = (e) => {
|
|
1685
|
+
if (onChangeCommitted) onChangeCommitted(e);
|
|
1686
|
+
};
|
|
1687
|
+
const visualMargin = 5;
|
|
1688
|
+
const visualMin = Number(min) - visualMargin;
|
|
1689
|
+
const visualMax = Number(max) + visualMargin;
|
|
1690
|
+
const visualProgress = (value - visualMin) / (visualMax - visualMin) * 100;
|
|
1691
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1692
|
+
className: cx("ui-range-wrapper", containerClassName, { disabled }),
|
|
1693
|
+
style: { width: `${width}px` }
|
|
1694
|
+
}, /* @__PURE__ */ React.createElement("input", {
|
|
1695
|
+
"data-testid": "range-slider",
|
|
1696
|
+
type: "range",
|
|
1697
|
+
className: cx("range-slider", className),
|
|
1698
|
+
id,
|
|
1699
|
+
value,
|
|
1700
|
+
min,
|
|
1701
|
+
max,
|
|
1702
|
+
step,
|
|
1703
|
+
onChange: handleChange,
|
|
1704
|
+
onMouseUp: handleCommit,
|
|
1705
|
+
onKeyUp: handleCommit,
|
|
1706
|
+
disabled,
|
|
1707
|
+
style: { "--slider-progress": `${visualProgress}%` },
|
|
1708
|
+
...rest
|
|
1709
|
+
}));
|
|
1710
|
+
};
|
|
1711
|
+
// src/components/TolerancePicker/consts.ts
|
|
1712
|
+
const TOLERANCE_RANGES = [
|
|
1713
|
+
{
|
|
1714
|
+
label: "00",
|
|
1715
|
+
value: 0
|
|
1716
|
+
},
|
|
1717
|
+
{
|
|
1718
|
+
label: "05",
|
|
1719
|
+
value: 300
|
|
1720
|
+
},
|
|
1721
|
+
{
|
|
1722
|
+
label: "10",
|
|
1723
|
+
value: 600
|
|
1724
|
+
},
|
|
1725
|
+
{
|
|
1726
|
+
label: "15",
|
|
1727
|
+
value: 900
|
|
1728
|
+
},
|
|
1729
|
+
{
|
|
1730
|
+
label: "20",
|
|
1731
|
+
value: 1200
|
|
1732
|
+
},
|
|
1733
|
+
{
|
|
1734
|
+
label: "25",
|
|
1735
|
+
value: 1500
|
|
1736
|
+
},
|
|
1737
|
+
{
|
|
1738
|
+
label: "30",
|
|
1739
|
+
value: 1800
|
|
1740
|
+
},
|
|
1741
|
+
{
|
|
1742
|
+
label: "35",
|
|
1743
|
+
value: 2100
|
|
1744
|
+
},
|
|
1745
|
+
{
|
|
1746
|
+
label: "40",
|
|
1747
|
+
value: 2400
|
|
1748
|
+
},
|
|
1749
|
+
{
|
|
1750
|
+
label: "45",
|
|
1751
|
+
value: 2700
|
|
1752
|
+
},
|
|
1753
|
+
{
|
|
1754
|
+
label: "60",
|
|
1755
|
+
value: 3600
|
|
1756
|
+
},
|
|
1757
|
+
{
|
|
1758
|
+
label: "75",
|
|
1759
|
+
value: 4500
|
|
1760
|
+
},
|
|
1761
|
+
{
|
|
1762
|
+
label: "90",
|
|
1763
|
+
value: 5400
|
|
1764
|
+
},
|
|
1765
|
+
{
|
|
1766
|
+
label: "105",
|
|
1767
|
+
value: 6300
|
|
1768
|
+
},
|
|
1769
|
+
{
|
|
1770
|
+
label: "120",
|
|
1771
|
+
value: 7200
|
|
1772
|
+
},
|
|
1773
|
+
{
|
|
1774
|
+
label: "150",
|
|
1775
|
+
value: 9e3
|
|
1776
|
+
},
|
|
1777
|
+
{
|
|
1778
|
+
label: "180",
|
|
1779
|
+
value: 10800
|
|
1780
|
+
},
|
|
1781
|
+
{
|
|
1782
|
+
label: "210",
|
|
1783
|
+
value: 12600
|
|
1784
|
+
},
|
|
1785
|
+
{
|
|
1786
|
+
label: "240",
|
|
1787
|
+
value: 14400
|
|
1788
|
+
}
|
|
1789
|
+
];
|
|
1790
|
+
// src/components/TolerancePicker/ToleranceRangeGrid.tsx
|
|
1791
|
+
const ToleranceRangeGrid = ({ onSelection, selectedTolerance, toleranceSign }) => /* @__PURE__ */ React.createElement("div", { className: "tolerance-picker-section" }, /* @__PURE__ */ React.createElement("div", { className: `tolerance-grid ${toleranceSign}-tolerance` }, TOLERANCE_RANGES.map(({ label, value }, index) => /* @__PURE__ */ React.createElement("button", {
|
|
1792
|
+
key: `${label}-${index}`,
|
|
1793
|
+
className: cx("minute", { selected: selectedTolerance === value }),
|
|
1794
|
+
style: { gridArea: `a${index + 1}` },
|
|
1795
|
+
onClick: () => onSelection(value)
|
|
1796
|
+
}, `${toleranceSign === "minus" ? "-" : "+"}${label}`))));
|
|
1797
|
+
// src/components/TolerancePicker/TolerancePicker.tsx
|
|
1798
|
+
const DEFAULT_TOLERANCE_VALUES = {
|
|
1799
|
+
minusTolerance: TOLERANCE_RANGES[0].value,
|
|
1800
|
+
plusTolerance: TOLERANCE_RANGES[0].value
|
|
1801
|
+
};
|
|
1802
|
+
const TolerancePicker = ({ onToleranceChange, toleranceValues: { minusTolerance, plusTolerance } = DEFAULT_TOLERANCE_VALUES, translateWarningMessage, testIdPrefix, ...inputProps }) => {
|
|
1803
|
+
const formatToleranceValue = useCallback((minusIndex, plusIndex) => `-${TOLERANCE_RANGES[minusIndex]?.label || ""}/+${TOLERANCE_RANGES[plusIndex]?.label || ""}`, []);
|
|
1804
|
+
const [showPicker, setShowPicker] = useState(false);
|
|
1805
|
+
const [inputValue, setInputValue] = useState(formatToleranceValue(0, 0));
|
|
1806
|
+
const [warningStatus, setWarningStatus] = useState(void 0);
|
|
1807
|
+
const inputRef = useRef(null);
|
|
1808
|
+
useEffect(() => {
|
|
1809
|
+
const minusToleranceIndex = TOLERANCE_RANGES.findIndex((range) => range.value === minusTolerance);
|
|
1810
|
+
const plusToleranceIndex = TOLERANCE_RANGES.findIndex((range) => range.value === plusTolerance);
|
|
1811
|
+
if (minusToleranceIndex < 0 || plusToleranceIndex < 0) {
|
|
1812
|
+
const invalidTolerance = minusToleranceIndex < 0 ? minusTolerance : plusTolerance;
|
|
1813
|
+
setWarningStatus({
|
|
1814
|
+
status: "warning",
|
|
1815
|
+
message: translateWarningMessage?.(invalidTolerance) || `${invalidTolerance} is not a valid tolerance value.`
|
|
1816
|
+
});
|
|
1817
|
+
} else setWarningStatus(void 0);
|
|
1818
|
+
setInputValue(formatToleranceValue(minusToleranceIndex, plusToleranceIndex));
|
|
1819
|
+
}, [
|
|
1820
|
+
formatToleranceValue,
|
|
1821
|
+
minusTolerance,
|
|
1822
|
+
plusTolerance,
|
|
1823
|
+
translateWarningMessage
|
|
1824
|
+
]);
|
|
1825
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1826
|
+
"data-testid": testIdPrefix ? testIdPrefix : void 0,
|
|
1827
|
+
className: "ui-tolerance-picker"
|
|
1828
|
+
}, /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Input, {
|
|
1829
|
+
testIdPrefix,
|
|
1830
|
+
...inputProps,
|
|
1831
|
+
value: inputValue,
|
|
1832
|
+
statusWithMessage: warningStatus,
|
|
1833
|
+
onClick: () => setShowPicker(!showPicker),
|
|
1834
|
+
type: "text",
|
|
1835
|
+
ref: inputRef
|
|
1836
|
+
})), /* @__PURE__ */ React.createElement(InputPopover, {
|
|
1837
|
+
testIdPrefix: "modal",
|
|
1838
|
+
inputRef,
|
|
1839
|
+
isOpen: showPicker,
|
|
1840
|
+
onClose: () => setShowPicker(false)
|
|
1841
|
+
}, /* @__PURE__ */ React.createElement("div", { className: "time-tolerance" }, /* @__PURE__ */ React.createElement(ToleranceRangeGrid, {
|
|
1842
|
+
onSelection: (e) => onToleranceChange({
|
|
1843
|
+
minusTolerance: e,
|
|
1844
|
+
plusTolerance
|
|
1845
|
+
}),
|
|
1846
|
+
selectedTolerance: minusTolerance,
|
|
1847
|
+
toleranceSign: "minus"
|
|
1848
|
+
}), /* @__PURE__ */ React.createElement("div", { className: "divider" }), /* @__PURE__ */ React.createElement(ToleranceRangeGrid, {
|
|
1849
|
+
onSelection: (e) => onToleranceChange({
|
|
1850
|
+
minusTolerance,
|
|
1851
|
+
plusTolerance: e
|
|
1852
|
+
}),
|
|
1853
|
+
selectedTolerance: plusTolerance,
|
|
1854
|
+
toleranceSign: "plus"
|
|
1855
|
+
}))));
|
|
1856
|
+
};
|
|
1857
|
+
// src/components/TokenInput.tsx
|
|
1858
|
+
const TokenInput = ({ label, tokens: initialTokens, small, onChange, onBlur, className, dataTestId, autoFocus }) => {
|
|
1859
|
+
const [tokens, setTokens] = useState(initialTokens);
|
|
1860
|
+
const [prevInitialTokens, setPrevInitialTokens] = useState(initialTokens);
|
|
1861
|
+
if (initialTokens !== prevInitialTokens) {
|
|
1862
|
+
setPrevInitialTokens(initialTokens);
|
|
1863
|
+
setTokens(initialTokens);
|
|
1864
|
+
}
|
|
1865
|
+
const [newToken, setNewToken] = useState("");
|
|
1866
|
+
const [selectedToken, setSelectedToken] = useState(null);
|
|
1867
|
+
const inputRef = useRef(null);
|
|
1868
|
+
const addToken = (token) => {
|
|
1869
|
+
const newTokens = [...tokens, token];
|
|
1870
|
+
setTokens(newTokens);
|
|
1871
|
+
onChange?.(newTokens);
|
|
1872
|
+
};
|
|
1873
|
+
const removeToken = (index) => {
|
|
1874
|
+
const newTokens = tokens.toSpliced(index, 1);
|
|
1875
|
+
setTokens(newTokens);
|
|
1876
|
+
onChange?.(newTokens);
|
|
1877
|
+
};
|
|
1878
|
+
const handleKeyDown = (e) => {
|
|
1879
|
+
switch (e.key) {
|
|
1880
|
+
case "Enter":
|
|
1881
|
+
e.preventDefault();
|
|
1882
|
+
if (newToken.trim() !== "") {
|
|
1883
|
+
addToken(newToken);
|
|
1884
|
+
setNewToken("");
|
|
1885
|
+
}
|
|
1886
|
+
break;
|
|
1887
|
+
case "Backspace": if (tokens.length > 0 && newToken === "") removeToken(tokens.length - 1);
|
|
1888
|
+
}
|
|
1889
|
+
};
|
|
1890
|
+
const focusInput = (e) => {
|
|
1891
|
+
if (e.target === e.currentTarget) inputRef.current?.focus();
|
|
1892
|
+
};
|
|
1893
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1894
|
+
className: cx("ui-token-input-wrapper", { small }, className),
|
|
1895
|
+
"data-testid": dataTestId
|
|
1896
|
+
}, /* @__PURE__ */ React.createElement("label", { className: "input-label" }, label), /* @__PURE__ */ React.createElement("div", {
|
|
1897
|
+
className: "tokens-wrapper",
|
|
1898
|
+
onClick: focusInput
|
|
1899
|
+
}, tokens.map((token, index) => /* @__PURE__ */ React.createElement("div", {
|
|
1900
|
+
key: `${token}-${index}`,
|
|
1901
|
+
onClick: () => setSelectedToken(index),
|
|
1902
|
+
className: cx("token-item-wrapper", { selected: selectedToken === index }),
|
|
1903
|
+
"data-testid": dataTestId ? `${dataTestId}-token` : void 0
|
|
1904
|
+
}, /* @__PURE__ */ React.createElement("span", { className: "token-label" }, token), /* @__PURE__ */ React.createElement("span", {
|
|
1905
|
+
className: "token-close-btn",
|
|
1906
|
+
onClick: () => removeToken(index),
|
|
1907
|
+
"data-testid": dataTestId ? `${dataTestId}-token-remove` : void 0
|
|
1908
|
+
}, /* @__PURE__ */ React.createElement(X, { size: small ? "sm" : "lg" })))), /* @__PURE__ */ React.createElement("input", {
|
|
1909
|
+
type: "text",
|
|
1910
|
+
value: newToken,
|
|
1911
|
+
onChange: (e) => setNewToken(e.target.value),
|
|
1912
|
+
onKeyDown: handleKeyDown,
|
|
1913
|
+
onBlur,
|
|
1914
|
+
className: "token-input",
|
|
1915
|
+
ref: inputRef,
|
|
1916
|
+
"data-testid": dataTestId ? `${dataTestId}-input` : void 0,
|
|
1917
|
+
autoFocus
|
|
1918
|
+
})));
|
|
1919
|
+
};
|
|
1920
|
+
// src/components/Table/tableAmbientThemes.ts
|
|
1921
|
+
const colors = {
|
|
1922
|
+
ambientA5: "rgb(246, 248, 249)",
|
|
1923
|
+
ambientA10: "rgb(239, 243, 245)",
|
|
1924
|
+
ambientA15: "rgb(233, 239, 242)",
|
|
1925
|
+
ambientB5: "rgb(250, 249, 245)",
|
|
1926
|
+
ambientB10: "rgb(247, 246, 238)",
|
|
1927
|
+
ambientB15: "rgb(242, 240, 228)",
|
|
1928
|
+
ambientC5: "rgb(246, 249, 246)",
|
|
1929
|
+
ambientC10: "rgb(240, 244, 241)",
|
|
1930
|
+
ambientC15: "rgb(234, 240, 235)",
|
|
1931
|
+
ambientD5: "rgb(248, 247, 253)",
|
|
1932
|
+
ambientD10: "rgb(244, 243, 252)",
|
|
1933
|
+
ambientD15: "rgb(239, 237, 247)",
|
|
1934
|
+
blackAlpha: "rgba(0, 0, 0)"
|
|
1935
|
+
};
|
|
1936
|
+
const TABLE_AMBIENT_THEMES = {
|
|
1937
|
+
A: {
|
|
1938
|
+
mix1: colors.blackAlpha,
|
|
1939
|
+
mix2: colors.ambientA5,
|
|
1940
|
+
odd: colors.ambientA10,
|
|
1941
|
+
even: colors.ambientA15
|
|
1942
|
+
},
|
|
1943
|
+
B: {
|
|
1944
|
+
mix1: colors.blackAlpha,
|
|
1945
|
+
mix2: colors.ambientB5,
|
|
1946
|
+
odd: colors.ambientB10,
|
|
1947
|
+
even: colors.ambientB15
|
|
1948
|
+
},
|
|
1949
|
+
C: {
|
|
1950
|
+
mix1: colors.blackAlpha,
|
|
1951
|
+
mix2: colors.ambientC5,
|
|
1952
|
+
odd: colors.ambientC10,
|
|
1953
|
+
even: colors.ambientC15
|
|
1954
|
+
},
|
|
1955
|
+
D: {
|
|
1956
|
+
mix1: colors.blackAlpha,
|
|
1957
|
+
mix2: colors.ambientD5,
|
|
1958
|
+
odd: colors.ambientD10,
|
|
1959
|
+
even: colors.ambientD15
|
|
1960
|
+
}
|
|
1961
|
+
};
|
|
1962
|
+
// src/components/Table/Table.tsx
|
|
1963
|
+
const Table = ({ columns, data, ambientVariant = "B" }) => {
|
|
1964
|
+
const theme = TABLE_AMBIENT_THEMES[ambientVariant];
|
|
1965
|
+
return /* @__PURE__ */ React.createElement("table", {
|
|
1966
|
+
className: "table-container ambient",
|
|
1967
|
+
style: {
|
|
1968
|
+
"--mix-color-1": theme.mix1,
|
|
1969
|
+
"--mix-color-2": theme.mix2,
|
|
1970
|
+
"--row-bg-odd": theme.odd,
|
|
1971
|
+
"--row-bg-even": theme.even
|
|
1972
|
+
}
|
|
1973
|
+
}, /* @__PURE__ */ React.createElement("thead", null, /* @__PURE__ */ React.createElement("tr", null, columns.map((col) => /* @__PURE__ */ React.createElement("td", {
|
|
1974
|
+
key: col.key,
|
|
1975
|
+
className: col.className
|
|
1976
|
+
}, col.label)))), /* @__PURE__ */ React.createElement("tbody", null, data.map((row, i) => /* @__PURE__ */ React.createElement("tr", { key: i }, columns.map((col) => /* @__PURE__ */ React.createElement("td", {
|
|
1977
|
+
key: col.key,
|
|
1978
|
+
className: col.className
|
|
1979
|
+
}, row[col.key]))))));
|
|
1980
|
+
};
|
|
1981
|
+
// src/components/Dialog.tsx
|
|
1982
|
+
/**
|
|
1983
|
+
* Display a styled dialog box.
|
|
1984
|
+
* You are in charge of its lifecycle (open & close), this componant just display it.
|
|
1985
|
+
*
|
|
1986
|
+
* Children must be an array of 3 items, where :
|
|
1987
|
+
* - the first one is for the header
|
|
1988
|
+
* - the second one is for the content
|
|
1989
|
+
* - the last is for the footer
|
|
1990
|
+
*
|
|
1991
|
+
* NB: Inside header, only title tags have the good style (font weight and size)
|
|
1992
|
+
*/
|
|
1993
|
+
const Dialog = ({ children, className, fullscreen = false, header, headerClassname, footer, footerClassname, bodyClassname, ...dialogProps }) => {
|
|
1994
|
+
const overlayRef = useRef(null);
|
|
1995
|
+
const dialogRef = useRef(null);
|
|
1996
|
+
/**
|
|
1997
|
+
* When the dialog height change
|
|
1998
|
+
* => check if there is a scroll on the overlay and if so add a 'scroll' class on it
|
|
1999
|
+
*/
|
|
2000
|
+
useEffect(() => {
|
|
2001
|
+
if (fullscreen) return;
|
|
2002
|
+
if (!overlayRef.current || !dialogRef.current) return;
|
|
2003
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
2004
|
+
if (overlayRef.current) {
|
|
2005
|
+
if (overlayRef.current.scrollHeight > overlayRef.current.clientHeight) overlayRef.current.classList.add("scroll");
|
|
2006
|
+
else overlayRef.current.classList.remove("scroll");
|
|
2007
|
+
}
|
|
2008
|
+
});
|
|
2009
|
+
resizeObserver.observe(dialogRef.current);
|
|
2010
|
+
return () => resizeObserver.disconnect();
|
|
2011
|
+
}, [
|
|
2012
|
+
overlayRef,
|
|
2013
|
+
dialogRef,
|
|
2014
|
+
fullscreen
|
|
2015
|
+
]);
|
|
2016
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
2017
|
+
ref: overlayRef,
|
|
2018
|
+
className: "ui-dialog-overlay flex items-center justify-center",
|
|
2019
|
+
tabIndex: -1
|
|
2020
|
+
}, /* @__PURE__ */ React.createElement("dialog", {
|
|
2021
|
+
ref: dialogRef,
|
|
2022
|
+
...dialogProps,
|
|
2023
|
+
className: cx("dialog-content", className, fullscreen && "fullscreen"),
|
|
2024
|
+
open: true
|
|
2025
|
+
}, /* @__PURE__ */ React.createElement("header", { className: cx(headerClassname) }, header), /* @__PURE__ */ React.createElement("section", { className: cx(bodyClassname) }, children), /* @__PURE__ */ React.createElement("footer", { className: cx(footerClassname) }, footer)));
|
|
2026
|
+
};
|
|
2027
|
+
// src/components/OptionGroup.tsx
|
|
2028
|
+
const OptionGroup = ({ sections, selectedValue, onSelect, getItemKey }) => {
|
|
2029
|
+
const triggerRef = useRef(null);
|
|
2030
|
+
const panelRef = useRef(null);
|
|
2031
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
2032
|
+
const { popoverPosition, calculatePosition } = usePopoverPosition(triggerRef, panelRef, 0, "start", "top");
|
|
2033
|
+
const selectedKey = selectedValue !== void 0 ? getItemKey(selectedValue) : void 0;
|
|
2034
|
+
const selectedSection = sections.find(({ items }) => items.some(({ value }) => getItemKey(value) === selectedKey));
|
|
2035
|
+
const selectedItem = selectedSection?.items.find(({ value }) => getItemKey(value) === selectedKey);
|
|
2036
|
+
useEffect(() => {
|
|
2037
|
+
if (isOpen && panelRef.current && !panelRef.current.matches(":popover-open")) {
|
|
2038
|
+
panelRef.current.showPopover();
|
|
2039
|
+
calculatePosition();
|
|
2040
|
+
}
|
|
2041
|
+
}, [isOpen, calculatePosition]);
|
|
2042
|
+
useOutsideClick(isOpen ? panelRef : null, () => setIsOpen(false));
|
|
2043
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("button", {
|
|
2044
|
+
ref: triggerRef,
|
|
2045
|
+
type: "button",
|
|
2046
|
+
className: "ui-option-group-trigger",
|
|
2047
|
+
title: selectedItem?.label,
|
|
2048
|
+
onClick: () => setIsOpen(true)
|
|
2049
|
+
}, !isOpen && selectedSection?.icon, /* @__PURE__ */ React.createElement("span", { className: "option-group-trigger-label" }, selectedItem?.label), isOpen ? /* @__PURE__ */ React.createElement(ChevronUp, { size: "sm" }) : /* @__PURE__ */ React.createElement(ChevronDown, { size: "sm" })), isOpen && /* @__PURE__ */ React.createElement("div", {
|
|
2050
|
+
ref: panelRef,
|
|
2051
|
+
className: "ui-option-group",
|
|
2052
|
+
popover: "manual",
|
|
2053
|
+
style: {
|
|
2054
|
+
top: popoverPosition.top,
|
|
2055
|
+
left: popoverPosition.left
|
|
2056
|
+
}
|
|
2057
|
+
}, sections.map((section) => /* @__PURE__ */ React.createElement("div", {
|
|
2058
|
+
key: section.title,
|
|
2059
|
+
className: "option-group-section"
|
|
2060
|
+
}, /* @__PURE__ */ React.createElement("div", { className: "option-group-section-title" }, section.title), section.items.map((item) => {
|
|
2061
|
+
const itemKey = getItemKey(item.value);
|
|
2062
|
+
return /* @__PURE__ */ React.createElement("button", {
|
|
2063
|
+
key: itemKey,
|
|
2064
|
+
type: "button",
|
|
2065
|
+
className: cx("option-group-item", { selected: itemKey === selectedKey }),
|
|
2066
|
+
title: item.label,
|
|
2067
|
+
onClick: () => {
|
|
2068
|
+
onSelect(item.value);
|
|
2069
|
+
setIsOpen(false);
|
|
2070
|
+
}
|
|
2071
|
+
}, /* @__PURE__ */ React.createElement("span", { className: "option-group-item-label" }, item.label));
|
|
2072
|
+
})))));
|
|
2073
|
+
};
|
|
2074
|
+
// src/components/SegmentedControl.tsx
|
|
2075
|
+
const SegmentedControl = ({ options, onChange, value, getOptionLabel, getOptionValue, getOptionIcon, small = false, ...inputProps }) => {
|
|
2076
|
+
const opts = useMemo(() => options.map((e) => ({
|
|
2077
|
+
value: getOptionValue(e),
|
|
2078
|
+
label: getOptionLabel(e),
|
|
2079
|
+
icon: getOptionIcon ? getOptionIcon(e) : null,
|
|
2080
|
+
source: e
|
|
2081
|
+
})), [
|
|
2082
|
+
options,
|
|
2083
|
+
getOptionValue,
|
|
2084
|
+
getOptionLabel,
|
|
2085
|
+
getOptionIcon
|
|
2086
|
+
]);
|
|
2087
|
+
return /* @__PURE__ */ React.createElement("div", { className: cx("segmented-control", { small }) }, opts.map((option) => /* @__PURE__ */ React.createElement("label", {
|
|
2088
|
+
key: option.value,
|
|
2089
|
+
className: cx("segmented-control-option", option.value === getOptionValue(value) && "checked"),
|
|
2090
|
+
title: option.label,
|
|
2091
|
+
"data-testid": `segmented-control-${option.value}`
|
|
2092
|
+
}, /* @__PURE__ */ React.createElement("div", { className: "segmented-control-option-content" }, option.icon ? option.icon : option.label), /* @__PURE__ */ React.createElement("input", {
|
|
2093
|
+
...inputProps,
|
|
2094
|
+
type: "radio",
|
|
2095
|
+
checked: option.value === getOptionValue(value),
|
|
2096
|
+
onChange: () => onChange(option.source)
|
|
2097
|
+
}))));
|
|
2098
|
+
};
|
|
2099
|
+
export { colors as AMBIENT_COLORS, Button, Checkbox, CheckboxList, CheckboxesTree, ComboBox, DatePicker, Dialog, DurationInput, Input, OptionGroup, PasswordInput, RadioButton, RadioGroup, SegmentedControl, Select, Slider, Switch, Table, TextArea, TimePicker, TokenInput, TolerancePicker, useDefaultComboBox, useOutsideClick };
|
|
2100
|
+
|
|
2101
|
+
//# sourceMappingURL=index.js.map
|