@osrd-project/ui-core 0.0.80 → 0.0.83

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.
Files changed (55) hide show
  1. package/dist/components/{inputs/Checkbox → Checkbox}/Checkbox.d.ts +3 -3
  2. package/dist/components/ComboBox/ComboBox.d.ts +34 -0
  3. package/dist/components/Dialog.d.ts +22 -0
  4. package/dist/components/SegmentedControl.d.ts +12 -0
  5. package/dist/components/Select.d.ts +3 -2
  6. package/dist/components/Table/Table.d.ts +15 -0
  7. package/dist/components/Table/tableAmbientThemes.d.ts +22 -0
  8. package/dist/components/{inputs/TextArea.d.ts → TextArea.d.ts} +1 -1
  9. package/dist/hooks/__tests__/useOutsideClick.spec.d.ts +1 -0
  10. package/dist/hooks/useOutsideClick.d.ts +1 -1
  11. package/dist/index.d.ts +18 -14
  12. package/dist/index.esm.js +321 -135
  13. package/dist/index.esm.js.map +1 -1
  14. package/dist/theme.css +436 -258
  15. package/dist/theme.css.map +1 -1
  16. package/package.json +11 -10
  17. package/dist/components/inputs/ComboBox/ComboBox.d.ts +0 -20
  18. package/dist/components/{buttons/Button.d.ts → Button.d.ts} +0 -0
  19. package/dist/components/{inputs/Checkbox → Checkbox}/CheckboxList.d.ts +0 -0
  20. package/dist/components/{inputs/Checkbox → Checkbox}/CheckboxTree.d.ts +1 -1
  21. package/dist/components/{inputs/Checkbox → Checkbox}/__tests__/utils.spec.d.ts +0 -0
  22. package/dist/components/{inputs/Checkbox → Checkbox}/index.d.ts +0 -0
  23. package/dist/components/{inputs/Checkbox → Checkbox}/type.d.ts +0 -0
  24. package/dist/components/{inputs/Checkbox → Checkbox}/utils.d.ts +0 -0
  25. package/dist/components/{inputs/ComboBox → ComboBox}/index.d.ts +0 -0
  26. package/dist/components/{inputs/ComboBox → ComboBox}/useDefaultComboBox.d.ts +0 -0
  27. package/dist/components/{inputs/datePicker → DatePicker}/Calendar.d.ts +0 -0
  28. package/dist/components/{inputs/datePicker → DatePicker}/CalendarPicker.d.ts +0 -0
  29. package/dist/components/{inputs/datePicker → DatePicker}/DatePicker.d.ts +1 -1
  30. package/dist/components/{inputs/datePicker → DatePicker}/__tests__/useCalendar.spec.d.ts +0 -0
  31. package/dist/components/{inputs/datePicker → DatePicker}/__tests__/useCalendarPicker.spec.d.ts +0 -0
  32. package/dist/components/{inputs/datePicker → DatePicker}/__tests__/useDatePicker.spec.d.ts +0 -0
  33. package/dist/components/{inputs/datePicker → DatePicker}/__tests__/utils.spec.d.ts +0 -0
  34. package/dist/components/{inputs/datePicker → DatePicker}/index.d.ts +0 -0
  35. package/dist/components/{inputs/datePicker → DatePicker}/type.d.ts +0 -0
  36. package/dist/components/{inputs/datePicker → DatePicker}/useCalendar.d.ts +0 -0
  37. package/dist/components/{inputs/datePicker → DatePicker}/useCalendarPicker.d.ts +0 -0
  38. package/dist/components/{inputs/datePicker → DatePicker}/useDatePicker.d.ts +1 -1
  39. /package/dist/components/{inputs/datePicker → DatePicker}/utils.d.ts +0 -0
  40. /package/dist/components/{inputs/FieldWrapper.d.ts → FieldWrapper.d.ts} +0 -0
  41. /package/dist/components/{inputs/Hint.d.ts → Hint.d.ts} +0 -0
  42. /package/dist/components/{inputs/Input.d.ts → Input.d.ts} +0 -0
  43. /package/dist/components/{Modal.d.ts → InputModal.d.ts} +0 -0
  44. /package/dist/components/{inputs/InputStatusIcon.d.ts → InputStatusIcon.d.ts} +0 -0
  45. /package/dist/components/{inputs/Label.d.ts → Label.d.ts} +0 -0
  46. /package/dist/components/{inputs/PasswordInput.d.ts → PasswordInput.d.ts} +0 -0
  47. /package/dist/components/{inputs/RadioButton.d.ts → RadioButton.d.ts} +0 -0
  48. /package/dist/components/{inputs/RadioGroup.d.ts → RadioGroup.d.ts} +0 -0
  49. /package/dist/components/{inputs/Slider.d.ts → Slider.d.ts} +0 -0
  50. /package/dist/components/{inputs/StatusMessage.d.ts → StatusMessage.d.ts} +0 -0
  51. /package/dist/components/{inputs/TimePicker.d.ts → TimePicker.d.ts} +0 -0
  52. /package/dist/components/{inputs/TokenInput.d.ts → TokenInput.d.ts} +0 -0
  53. /package/dist/components/{inputs/tolerancePicker → TolerancePicker}/TolerancePicker.d.ts +0 -0
  54. /package/dist/components/{inputs/tolerancePicker → TolerancePicker}/ToleranceRangeGrid.d.ts +0 -0
  55. /package/dist/components/{inputs/tolerancePicker → TolerancePicker}/consts.d.ts +0 -0
package/dist/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
- import React, { useEffect, useState, useCallback, useRef, useMemo } from 'react';
2
- import { Gear, Info, CheckCircle, Alert, Blocked, RequiredInput, X, XCircle, ChevronDown, ChevronLeft, ChevronRight, Calendar as Calendar$1, EyeClosed, Eye } from '@osrd-project/ui-icons';
1
+ import React, { useEffectEvent, useEffect, useState, useCallback, useRef, useMemo } from 'react';
3
2
  import cx from 'classnames';
3
+ import { Gear, Info, CheckCircle, Alert, Blocked, RequiredInput, X, XCircle, ChevronDown, ChevronLeft, ChevronRight, Calendar as Calendar$1, EyeClosed, Eye } from '@osrd-project/ui-icons';
4
4
 
5
5
  const Button = ({ label, variant = 'Primary', isLoading = false, isDisabled = false, leadingIcon = null, counter = null, size = 'large', onClick, dataTestID, ...btnAttrs }) => {
6
6
  const handleClick = () => {
@@ -10,20 +10,19 @@ const Button = ({ label, variant = 'Primary', isLoading = false, isDisabled = fa
10
10
  };
11
11
  return (React.createElement("button", { ...btnAttrs, className: cx('ui-button flex items-center', btnAttrs.className, variant.toLowerCase(), size.toLowerCase(), {
12
12
  loading: isLoading,
13
- }), "data-testid": dataTestID, onClick: handleClick, disabled: isDisabled || isLoading }, isLoading ? (React.createElement("span", { className: "icon" },
14
- React.createElement(Gear, { variant: "fill", size: "lg" }))) : (React.createElement(React.Fragment, null,
13
+ }), "data-testid": dataTestID, onClick: handleClick, disabled: isDisabled || isLoading },
15
14
  leadingIcon && React.createElement("span", { className: "leading-icon mr-2" }, leadingIcon),
16
15
  label,
17
- counter !== null && React.createElement("span", { className: "counter ml-2" }, counter)))));
16
+ counter !== null && React.createElement("span", { className: "counter ml-2" }, counter)));
18
17
  };
19
18
 
20
19
  const useOutsideClick = (ref, handler) => {
20
+ const handleClickOutside = useEffectEvent((event) => {
21
+ if (ref?.current && !ref.current.contains(event.target)) {
22
+ handler(event);
23
+ }
24
+ });
21
25
  useEffect(() => {
22
- const handleClickOutside = (event) => {
23
- if (ref.current && !ref.current.contains(event.target)) {
24
- handler(event);
25
- }
26
- };
27
26
  document.addEventListener('mousedown', handleClickOutside);
28
27
  return () => {
29
28
  document.removeEventListener('mousedown', handleClickOutside);
@@ -31,6 +30,20 @@ const useOutsideClick = (ref, handler) => {
31
30
  }, [ref, handler]);
32
31
  };
33
32
 
33
+ function useFocusByTab({ onBlur, onKeyUp }) {
34
+ const [isFocusByTab, setIsFocusByTab] = useState(false);
35
+ const handleKeyUp = useCallback((event) => {
36
+ if (event.key === 'Tab')
37
+ setIsFocusByTab(true);
38
+ onKeyUp?.(event);
39
+ }, [onKeyUp]);
40
+ const handleBlur = useCallback((event) => {
41
+ setIsFocusByTab(false);
42
+ onBlur?.(event);
43
+ }, [onBlur]);
44
+ return { handleKeyUp, handleBlur, isFocusByTab };
45
+ }
46
+
34
47
  const Hint = ({ text }) => React.createElement("span", { className: "ui-hint" }, text);
35
48
 
36
49
  const InputStatusIcon = ({ status, small, className }) => {
@@ -84,20 +97,6 @@ const FieldWrapper = ({ id, label, hint, required, disabled, statusWithMessage,
84
97
  }, showIcon: statusIconPosition === 'before-status-message', onClose: onCloseStatusMessage })))));
85
98
  };
86
99
 
87
- function useFocusByTab({ onBlur, onKeyUp }) {
88
- const [isFocusByTab, setIsFocusByTab] = useState(false);
89
- const handleKeyUp = useCallback((event) => {
90
- if (event.key === 'Tab')
91
- setIsFocusByTab(true);
92
- onKeyUp?.(event);
93
- }, [onKeyUp]);
94
- const handleBlur = useCallback((event) => {
95
- setIsFocusByTab(false);
96
- onBlur?.(event);
97
- }, [onBlur]);
98
- return { handleKeyUp, handleBlur, isFocusByTab };
99
- }
100
-
101
100
  const InputAffix = ({ value, type, disabled, readOnly }) => {
102
101
  const isContentWithCallback = typeof value === 'object' && value !== null && 'onClickCallback' in value;
103
102
  const spanContent = isContentWithCallback
@@ -135,17 +134,18 @@ Input.displayName = 'Input';
135
134
  *
136
135
  * You can use the hook useDefaultComboBox to get the default behavior.
137
136
  * See the stories.
137
+ *
138
+ * When `allowCustomValue` is true, an option appears at the bottom of the dropdown
139
+ * when the typed value doesn't match any existing suggestion.
140
+ * Defocusing without selecting cancels the input.
138
141
  */
139
- const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, value, small, getSuggestionLabel, onChange, onSelectSuggestion, resetSuggestions, testIdPrefix, ...inputProps }) => {
142
+ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, value, small, getSuggestionLabel, onChange, onSelectSuggestion, resetSuggestions, testIdPrefix, renderListElementComponent, renderFooterItem, allowCustomValue = false, onAddCustomValue, addCustomValueLabel, ...inputProps }) => {
140
143
  const [activeSuggestionIndex, setActiveSuggestionIndex] = useState(-1);
141
144
  const [inputValue, setInputValue] = useState('');
142
145
  const [isInputFocused, setIsInputFocused] = useState(false);
143
146
  const suggestionRefs = useRef([]);
144
147
  const inputRef = useRef(null);
145
148
  const wrapperRef = useRef(null);
146
- const focusInput = useCallback(() => {
147
- inputRef.current?.focus();
148
- }, [inputRef]);
149
149
  const removeFocus = () => {
150
150
  setIsInputFocused(false);
151
151
  setActiveSuggestionIndex(-1);
@@ -155,15 +155,31 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
155
155
  resetSuggestions();
156
156
  };
157
157
  /* eslint-disable react-hooks/exhaustive-deps */
158
+ const focusInput = useCallback(() => {
159
+ if (isInputFocused) {
160
+ removeFocus();
161
+ }
162
+ else {
163
+ inputRef.current?.focus();
164
+ }
165
+ }, [inputRef, isInputFocused]);
166
+ /* eslint-disable react-hooks/exhaustive-deps */
158
167
  useEffect(() => {
159
168
  if (value) {
169
+ // TODO: fix this lint
170
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
160
171
  setInputValue(getSuggestionLabel(value));
161
172
  }
162
173
  else {
163
174
  setInputValue('');
164
175
  }
165
176
  }, [value]);
166
- const showSuggestions = useMemo(() => isInputFocused && suggestions.length > 0 && !inputProps.disabled, [isInputFocused, suggestions.length, inputProps.disabled]);
177
+ const showAddCustomValue = useMemo(() => {
178
+ if (!allowCustomValue || !inputValue.trim())
179
+ return false;
180
+ return !suggestions.some((s) => getSuggestionLabel(s).toLowerCase() === inputValue.trim().toLowerCase());
181
+ }, [allowCustomValue, inputValue, suggestions, getSuggestionLabel]);
182
+ const showSuggestions = useMemo(() => isInputFocused && (suggestions.length > 0 || showAddCustomValue) && !inputProps.disabled, [isInputFocused, suggestions.length, showAddCustomValue, inputProps.disabled]);
167
183
  // behavior
168
184
  const handleInputChange = (e) => {
169
185
  onChange?.(e);
@@ -175,38 +191,63 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
175
191
  setInputValue(getSuggestionLabel(selectedSuggestion));
176
192
  removeFocus();
177
193
  };
194
+ const confirmCustomValue = () => {
195
+ const trimmed = inputValue.trim();
196
+ if (!trimmed)
197
+ return;
198
+ onAddCustomValue?.(trimmed);
199
+ removeFocus();
200
+ };
178
201
  const closeSuggestions = () => {
179
202
  setInputValue(value ? getSuggestionLabel(value) : '');
180
203
  removeFocus();
181
204
  };
205
+ const totalItems = suggestions.length + (showAddCustomValue ? 1 : 0);
206
+ const customValueIndex = showAddCustomValue ? suggestions.length : -1;
182
207
  const handleKeyDown = (e) => {
183
- if (e.key === 'ArrowDown') {
184
- setActiveSuggestionIndex((prev) => {
185
- const newIndex = prev < suggestions.length - 1 ? prev + 1 : prev;
186
- if (suggestionRefs.current[newIndex]) {
187
- suggestionRefs.current[newIndex].scrollIntoView({
188
- block: 'nearest',
189
- });
208
+ switch (e.key) {
209
+ case 'ArrowDown': {
210
+ setActiveSuggestionIndex((prev) => {
211
+ const newIndex = prev < totalItems - 1 ? prev + 1 : prev;
212
+ suggestionRefs.current[newIndex]?.scrollIntoView({ block: 'nearest' });
213
+ return newIndex;
214
+ });
215
+ break;
216
+ }
217
+ case 'ArrowUp': {
218
+ setActiveSuggestionIndex((prev) => {
219
+ const newIndex = prev > 0 ? prev - 1 : prev;
220
+ suggestionRefs.current[newIndex]?.scrollIntoView({ block: 'nearest' });
221
+ return newIndex;
222
+ });
223
+ break;
224
+ }
225
+ case 'Enter': {
226
+ e.preventDefault();
227
+ if (activeSuggestionIndex === customValueIndex) {
228
+ confirmCustomValue();
190
229
  }
191
- return newIndex;
192
- });
193
- }
194
- else if (e.key === 'ArrowUp') {
195
- setActiveSuggestionIndex((prev) => {
196
- const newIndex = prev > 0 ? prev - 1 : prev;
197
- if (suggestionRefs.current[newIndex]) {
198
- suggestionRefs.current[newIndex].scrollIntoView({
199
- block: 'nearest',
200
- });
230
+ else if (activeSuggestionIndex >= 0) {
231
+ selectSuggestion(activeSuggestionIndex);
201
232
  }
202
- return newIndex;
203
- });
204
- }
205
- else if ((e.key === 'Enter' || e.key === 'Tab') && activeSuggestionIndex >= 0) {
206
- selectSuggestion(activeSuggestionIndex);
207
- }
208
- else if (e.key === 'Escape') {
209
- closeSuggestions();
233
+ else if (suggestions.length === 1) {
234
+ selectSuggestion(0);
235
+ }
236
+ break;
237
+ }
238
+ case 'Tab': {
239
+ if (activeSuggestionIndex === customValueIndex) {
240
+ confirmCustomValue();
241
+ }
242
+ else if (activeSuggestionIndex >= 0) {
243
+ selectSuggestion(activeSuggestionIndex);
244
+ }
245
+ break;
246
+ }
247
+ case 'Escape': {
248
+ closeSuggestions();
249
+ break;
250
+ }
210
251
  }
211
252
  };
212
253
  const handleInputFocus = (e) => {
@@ -219,7 +260,8 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
219
260
  resetSuggestions();
220
261
  focusInput();
221
262
  };
222
- useOutsideClick(wrapperRef, closeSuggestions);
263
+ useOutsideClick(showSuggestions ? wrapperRef : null, // Only trigger when the suggestions are displayed
264
+ closeSuggestions);
223
265
  const inputIcons = useMemo(() => {
224
266
  if (inputProps.readOnly || inputProps.disabled)
225
267
  return undefined;
@@ -240,24 +282,47 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
240
282
  action: focusInput,
241
283
  className: cx('chevron-icon', {
242
284
  disabled: inputProps.disabled,
285
+ open: isInputFocused,
243
286
  }),
244
287
  },
245
288
  ];
246
- }, [inputProps.readOnly, inputProps.disabled, clearInput, value, small, focusInput]);
289
+ }, [
290
+ inputProps.readOnly,
291
+ inputProps.disabled,
292
+ clearInput,
293
+ value,
294
+ small,
295
+ focusInput,
296
+ isInputFocused,
297
+ ]);
247
298
  return (React.createElement("div", { "data-testid": testIdPrefix ? `${testIdPrefix}` : undefined, className: "ui-combo-box", style: { '--number-of-suggestions': numberOfSuggestionsToShow }, ref: wrapperRef },
248
299
  customLabel && React.createElement("label", { htmlFor: inputProps.id }, customLabel),
249
300
  React.createElement(Input, { testIdPrefix: inputProps.id, ...inputProps, ref: inputRef, value: inputValue, onChange: handleInputChange, onKeyDown: handleKeyDown, onFocus: handleInputFocus, withIcons: inputIcons, small: small }),
250
- showSuggestions && (React.createElement("ul", { className: "suggestions-list", "data-testid": testIdPrefix ? `${testIdPrefix}-list` : undefined }, suggestions.map((suggestion, index) => (React.createElement("li", { ref: (el) => {
251
- if (suggestionRefs) {
252
- suggestionRefs.current[index] = el;
253
- }
254
- }, key: `${getSuggestionLabel(suggestion)}-${index}`, "data-testid": testIdPrefix ? `${testIdPrefix}-item` : undefined, className: cx('suggestion-item', {
255
- active: index === activeSuggestionIndex,
256
- selected: value && getSuggestionLabel(value) === getSuggestionLabel(suggestion),
257
- small,
258
- }), onClick: () => selectSuggestion(index), onMouseDown: (e) => e.preventDefault(), onMouseEnter: () => {
259
- setActiveSuggestionIndex(index);
260
- } }, getSuggestionLabel(suggestion))))))));
301
+ showSuggestions && (React.createElement("ul", { className: "suggestions-list", "data-testid": testIdPrefix ? `${testIdPrefix}-list` : undefined, onMouseLeave: () => setActiveSuggestionIndex(-1) },
302
+ suggestions.map((suggestion, index) => (React.createElement("li", { ref: (el) => {
303
+ if (suggestionRefs) {
304
+ suggestionRefs.current[index] = el;
305
+ }
306
+ }, key: `${getSuggestionLabel(suggestion)}-${index}`, "data-testid": testIdPrefix ? `${testIdPrefix}-item` : undefined, className: cx('suggestion-item', {
307
+ active: index === activeSuggestionIndex,
308
+ selected: value && getSuggestionLabel(value) === getSuggestionLabel(suggestion),
309
+ small,
310
+ 'suggestion-item--custom': renderListElementComponent,
311
+ }), onClick: () => selectSuggestion(index), onMouseEnter: () => setActiveSuggestionIndex(index) }, renderListElementComponent
312
+ ? renderListElementComponent({
313
+ suggestion,
314
+ index,
315
+ isActive: index === activeSuggestionIndex,
316
+ isSelected: !!value && getSuggestionLabel(value) === getSuggestionLabel(suggestion),
317
+ })
318
+ : getSuggestionLabel(suggestion)))),
319
+ showAddCustomValue && (React.createElement("li", { ref: (el) => {
320
+ suggestionRefs.current[suggestions.length] = el;
321
+ }, className: cx('suggestion-item', 'suggestion-item--add-custom', {
322
+ active: activeSuggestionIndex === customValueIndex,
323
+ small,
324
+ }), onClick: confirmCustomValue, onMouseEnter: () => setActiveSuggestionIndex(customValueIndex), "data-testid": testIdPrefix ? `${testIdPrefix}-add-custom` : undefined }, addCustomValueLabel ?? `Add "${inputValue.trim()}"`)),
325
+ renderFooterItem && renderFooterItem()))));
261
326
  };
262
327
 
263
328
  const normalizeString = (str) => str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
@@ -299,8 +364,7 @@ const useDefaultComboBox = (suggestions, getSuggestionLabel) => {
299
364
  return { suggestions: filteredSuggestions, onChange, resetSuggestions };
300
365
  };
301
366
 
302
- const Checkbox = ({ label, small, hint, disabled, checked, readOnly, isIndeterminate = false, onClick, onKeyUp, onBlur, ...rest }) => {
303
- const { handleKeyUp, handleBlur, isFocusByTab } = useFocusByTab({ onBlur, onKeyUp });
367
+ const Checkbox = ({ label, small, hint, disabled, checked, readOnly, isIndeterminate = false, onClick, children, ...rest }) => {
304
368
  const handleClick = (e) => {
305
369
  if (readOnly) {
306
370
  e.preventDefault();
@@ -310,16 +374,16 @@ const Checkbox = ({ label, small, hint, disabled, checked, readOnly, isIndetermi
310
374
  onClick?.(e);
311
375
  }
312
376
  };
313
- return (React.createElement("label", { className: cx('ui-checkbox', { small }) },
377
+ return (React.createElement("label", { className: cx('ui-checkbox', { small, 'with-label': label || children, 'with-hint': hint }) },
314
378
  React.createElement("input", {
315
- //read-only state is not working on checkbox as well explain here : https://stackoverflow.com/a/70375659
379
+ // Browsers' read-only attribute is not working on checkboxes. See: https://stackoverflow.com/a/70375659
316
380
  className: cx({ 'read-only': readOnly }), type: "checkbox", checked: checked, disabled: disabled, ref: (input) => {
317
381
  if (input) {
318
382
  input.indeterminate = isIndeterminate;
319
383
  }
320
- }, onClick: handleClick, onBlur: handleBlur, onKeyUp: handleKeyUp, ...rest }),
321
- React.createElement("span", { className: cx('checkmark', { 'focused-by-tab': isFocusByTab }) }),
384
+ }, onClick: handleClick, ...rest }),
322
385
  label && React.createElement("div", { className: "label" }, label),
386
+ children && React.createElement("div", { className: "label" }, children),
323
387
  hint && React.createElement("span", { className: "hint" }, hint)));
324
388
  };
325
389
 
@@ -441,6 +505,56 @@ const CheckboxesTree = ({ items, small, id, label, hint, statusWithMessage, disa
441
505
  React.createElement(CheckboxList, { small: small, items: items, disabled: disabled, readOnly: readOnly, onClickItem: handleClick })));
442
506
  };
443
507
 
508
+ const useModalPosition = (inputRef, modalRef, offset = 3 // Default offset below the input
509
+ ) => {
510
+ const [modalPosition, setModalPosition] = useState({
511
+ top: 0,
512
+ left: 0,
513
+ });
514
+ const calculatePosition = useCallback(() => {
515
+ if (inputRef.current && modalRef.current) {
516
+ const inputRect = inputRef.current.getBoundingClientRect();
517
+ const modal = modalRef.current;
518
+ const modalRect = modal.getBoundingClientRect();
519
+ const offsetParent = modal.offsetParent;
520
+ const parentRect = offsetParent ? offsetParent.getBoundingClientRect() : { top: 0, left: 0 };
521
+ // Adjust the top position: place it below the input while considering an offset
522
+ const top = inputRect.bottom - parentRect.top - offset;
523
+ // Center the modal horizontally relative to the input
524
+ const modalViewportLeft = inputRect.left + (inputRect.width - modalRect.width) / 2;
525
+ // Keep modal to stay within viewport bounds
526
+ const adjustedViewportLeft = Math.max(0, Math.min(modalViewportLeft, window.innerWidth - modalRect.width));
527
+ const left = adjustedViewportLeft - parentRect.left;
528
+ setModalPosition({ top, left });
529
+ }
530
+ }, [inputRef, modalRef, offset]);
531
+ useEffect(() => {
532
+ calculatePosition();
533
+ const handleResize = () => calculatePosition();
534
+ window.addEventListener('resize', handleResize);
535
+ return () => window.removeEventListener('resize', handleResize);
536
+ }, [calculatePosition]);
537
+ return { modalPosition, calculatePosition };
538
+ };
539
+
540
+ const InputModal = ({ inputRef, isOpen, onClose, children, testIdPrefix }) => {
541
+ const modalRef = useRef(null);
542
+ const { modalPosition, calculatePosition } = useModalPosition(inputRef, modalRef);
543
+ useEffect(() => {
544
+ if (isOpen) {
545
+ calculatePosition();
546
+ }
547
+ }, [calculatePosition, isOpen]);
548
+ useOutsideClick(modalRef, onClose);
549
+ if (!isOpen)
550
+ return null;
551
+ return (React.createElement("div", { className: "ui-input-modal-overlay" },
552
+ React.createElement("div", { ref: modalRef, className: "modal-content", style: { top: modalPosition.top, left: modalPosition.left } },
553
+ React.createElement("button", { "data-testid": testIdPrefix ? `${testIdPrefix}-close-button` : undefined, className: "close-button", onClick: onClose },
554
+ React.createElement(X, { size: "lg" })),
555
+ children)));
556
+ };
557
+
444
558
  const INVALID_SELECTED_SLOT_ERROR = 'Invalid selectedSlot: If start and end are defined, the start date must be before the end date.';
445
559
  const INVALID_SELECTABLE_SLOT_ERROR = 'Invalid selectableSlot: If start and end are defined, the start date must be before the end date.';
446
560
  const INVALID_SELECTED_SLOT_BASED_ON_SELECTABLE_SLOT_ERROR = 'selectedSlot must be within selectableSlot';
@@ -792,6 +906,8 @@ function useCalendarPicker({ initialDate, selectedSlot, selectableSlot, numberOf
792
906
  }
793
907
  };
794
908
  useEffect(() => {
909
+ // TODO: fix this lint
910
+ // eslint-disable-next-line react-hooks/set-state-in-effect
795
911
  setActiveDate(initialDate ?? selectedSlot?.start ?? selectableSlot?.start ?? new Date());
796
912
  }, [initialDate, selectedSlot, selectableSlot]);
797
913
  return {
@@ -823,38 +939,6 @@ const CalendarPicker = ({ initialDate, selectedSlot, selectableSlot, numberOfMon
823
939
  React.createElement(ChevronRight, { size: "lg" })))));
824
940
  };
825
941
 
826
- const useModalPosition = (inputRef, modalRef, offset = 3 // Default offset below the input
827
- ) => {
828
- const [modalPosition, setModalPosition] = useState({
829
- top: 0,
830
- left: 0,
831
- });
832
- const calculatePosition = useCallback(() => {
833
- if (inputRef.current && modalRef.current) {
834
- const inputRect = inputRef.current.getBoundingClientRect();
835
- const modal = modalRef.current;
836
- const modalRect = modal.getBoundingClientRect();
837
- const offsetParent = modal.offsetParent;
838
- const parentRect = offsetParent ? offsetParent.getBoundingClientRect() : { top: 0, left: 0 };
839
- // Adjust the top position: place it below the input while considering an offset
840
- const top = inputRect.bottom - parentRect.top - offset;
841
- // Center the modal horizontally relative to the input
842
- const modalViewportLeft = inputRect.left + (inputRect.width - modalRect.width) / 2;
843
- // Keep modal to stay within viewport bounds
844
- const adjustedViewportLeft = Math.max(0, Math.min(modalViewportLeft, window.innerWidth - modalRect.width));
845
- const left = adjustedViewportLeft - parentRect.left;
846
- setModalPosition({ top, left });
847
- }
848
- }, [inputRef, modalRef, offset]);
849
- useEffect(() => {
850
- calculatePosition();
851
- const handleResize = () => calculatePosition();
852
- window.addEventListener('resize', handleResize);
853
- return () => window.removeEventListener('resize', handleResize);
854
- }, [calculatePosition]);
855
- return { modalPosition, calculatePosition };
856
- };
857
-
858
942
  const MODAL_VERTICAL_OFFSET = 3;
859
943
  // Regex for "xx/xx/xx"
860
944
  const SINGLE_DATE_REGEX = /^\d{2}\/\d{2}\/\d{2}$/;
@@ -930,6 +1014,8 @@ function useDatePicker(datePickerProps) {
930
1014
  if (newInput !== inputValue) {
931
1015
  // we only set the input value if it has changed
932
1016
  // otherwise the user loses the focus
1017
+ // TODO: fix this lint
1018
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
933
1019
  setInputValue(newInput);
934
1020
  }
935
1021
  setStatusWithMessage(undefined);
@@ -951,24 +1037,6 @@ function useDatePicker(datePickerProps) {
951
1037
  };
952
1038
  }
953
1039
 
954
- const InputModal = ({ inputRef, isOpen, onClose, children, testIdPrefix }) => {
955
- const modalRef = useRef(null);
956
- const { modalPosition, calculatePosition } = useModalPosition(inputRef, modalRef);
957
- useEffect(() => {
958
- if (isOpen) {
959
- calculatePosition();
960
- }
961
- }, [calculatePosition, isOpen]);
962
- useOutsideClick(modalRef, onClose);
963
- if (!isOpen)
964
- return null;
965
- return (React.createElement("div", { className: "ui-modal-overlay" },
966
- React.createElement("div", { ref: modalRef, className: "modal-content", style: { top: modalPosition.top, left: modalPosition.left } },
967
- React.createElement("button", { "data-testid": testIdPrefix ? `${testIdPrefix}-close-button` : undefined, className: "close-button", onClick: onClose },
968
- React.createElement(X, { size: "lg" })),
969
- children)));
970
- };
971
-
972
1040
  const DatePicker = (props) => {
973
1041
  const { inputValue, statusWithMessage, selectedSlot, showPicker, modalPosition, inputRef, calendarPickerRef, setShowPicker, handleDayClick, handleInputClick, handleInputOnChange, } = useDatePicker(props);
974
1042
  const { inputFieldWrapperClassname, ...otherInputProps } = props.inputProps;
@@ -1008,6 +1076,11 @@ const RadioButton = ({ id, label, value, hint, readOnly, onBlur, onKeyUp, small,
1008
1076
 
1009
1077
  const RadioGroup = ({ label, value, subtitle, readOnly, disabled, options, statusWithMessage, required, small, onChange, }) => {
1010
1078
  const [selectedValue, setSelectedValue] = useState(value);
1079
+ useEffect(() => {
1080
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
1081
+ setSelectedValue(value);
1082
+ }, [value]);
1083
+ const statusClassname = statusWithMessage?.status ? { [statusWithMessage.status]: true } : {};
1011
1084
  const handleOptionChange = (e, nextOption) => {
1012
1085
  if (!readOnly) {
1013
1086
  setSelectedValue(nextOption.value);
@@ -1015,7 +1088,6 @@ const RadioGroup = ({ label, value, subtitle, readOnly, disabled, options, statu
1015
1088
  onChange?.(nextOption.value);
1016
1089
  }
1017
1090
  };
1018
- const statusClassname = statusWithMessage?.status ? { [statusWithMessage.status]: true } : {};
1019
1091
  return (React.createElement("div", { className: cx('ui-radio-button-wrapper', statusClassname) },
1020
1092
  label && (React.createElement("label", { className: "label" },
1021
1093
  required && (React.createElement("span", { className: "required" },
@@ -1032,7 +1104,7 @@ const RadioGroup = ({ label, value, subtitle, readOnly, disabled, options, statu
1032
1104
  };
1033
1105
 
1034
1106
  const PLACEHOLDER_VALUE = '__PLACEHOLDER__';
1035
- const Select = ({ id, label, hint, value, options, placeholder, statusWithMessage, required, disabled, narrow, readOnly, small, getOptionLabel, getOptionValue, onChange, ...props }) => {
1107
+ const Select = ({ id, label, hint, value, options, placeholder, statusWithMessage, required, disabled, narrow, readOnly, small, getOptionLabel, getOptionValue, onChange, dataTestId, ...props }) => {
1036
1108
  const [selectedOption, setSelectedOption] = useState(value);
1037
1109
  const handleOnChange = (e) => {
1038
1110
  const newSelectedOption = e.target.value === PLACEHOLDER_VALUE
@@ -1042,10 +1114,12 @@ const Select = ({ id, label, hint, value, options, placeholder, statusWithMessag
1042
1114
  onChange(newSelectedOption);
1043
1115
  };
1044
1116
  useEffect(() => {
1117
+ // TODO: fix this lint
1118
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
1045
1119
  setSelectedOption(value);
1046
1120
  }, [value]);
1047
1121
  return (React.createElement(FieldWrapper, { id: id, label: label, hint: hint, statusWithMessage: statusWithMessage, required: required, disabled: disabled, small: small, narrow: narrow },
1048
- React.createElement("select", { id: id, className: cx('ui-select', statusWithMessage?.status, {
1122
+ React.createElement("select", { id: id, "data-testid": dataTestId, className: cx('ui-select', statusWithMessage?.status, {
1049
1123
  'placeholder-selected': placeholder && !selectedOption,
1050
1124
  small,
1051
1125
  'read-only': readOnly,
@@ -1055,14 +1129,9 @@ const Select = ({ id, label, hint, value, options, placeholder, statusWithMessag
1055
1129
  };
1056
1130
 
1057
1131
  const CHAR_COUNT_ERROR_THRESHOLD = 40;
1058
- const TextArea = ({ id, label, value: initialValue, hint, required, disabled, statusWithMessage, maxLength, onChange, onKeyUp, onBlur, ...rest }) => {
1059
- const [value, setValue] = useState(initialValue);
1132
+ const TextArea = ({ id, label, value = '', hint, required, disabled, statusWithMessage, maxLength, onChange, onKeyUp, onBlur, ...rest }) => {
1060
1133
  const charCount = value?.length || 0;
1061
1134
  const { handleKeyUp, handleBlur, isFocusByTab } = useFocusByTab({ onBlur, onKeyUp });
1062
- const handleChange = (e) => {
1063
- setValue(e.target.value);
1064
- onChange?.(e);
1065
- };
1066
1135
  return (React.createElement(FieldWrapper, { id: id, label: label, hint: hint, statusWithMessage: statusWithMessage, disabled: disabled, required: required, className: "ui-text-area-field-wrapper" },
1067
1136
  React.createElement("div", { className: cx('text-area-wrapper', { 'focused-by-tab': isFocusByTab }) },
1068
1137
  maxLength && (React.createElement("div", { className: cx('char-count', {
@@ -1072,10 +1141,13 @@ const TextArea = ({ id, label, value: initialValue, hint, required, disabled, st
1072
1141
  charCount,
1073
1142
  "/",
1074
1143
  maxLength)),
1075
- React.createElement("textarea", { className: cx('text-area', { [statusWithMessage?.status || '']: !!statusWithMessage }), id: id, value: value, disabled: disabled, maxLength: maxLength, onChange: handleChange, onBlur: handleBlur, onKeyUp: handleKeyUp, ...rest }))));
1144
+ React.createElement("textarea", { className: cx('text-area', { [statusWithMessage?.status || '']: !!statusWithMessage }), id: id, value: value, disabled: disabled, maxLength: maxLength, onChange: onChange, onBlur: handleBlur, onKeyUp: handleKeyUp, ...rest }))));
1076
1145
  };
1077
1146
 
1078
- const TimeRange = ({ range, selectedItem, className, onSelectItem }) => (React.createElement("div", { className: "time-grid" }, range.map((item) => (React.createElement("div", { key: item, className: cx(className, { selected: selectedItem === item }), onClick: () => onSelectItem(item) }, item.toString().padStart(2, '0'))))));
1147
+ const TimeRange = ({ range, selectedItem, className, onSelectItem }) => (React.createElement("div", { className: "time-grid", "data-testid": "time-grid" }, range.map((item) => {
1148
+ const formattedItem = item.toString().padStart(2, '0');
1149
+ return (React.createElement("div", { key: item, className: cx(className, { selected: selectedItem === item }), "data-testid": className === 'hour' ? `hour-${formattedItem}` : `minute-${formattedItem}`, onClick: () => onSelectItem(item) }, formattedItem));
1150
+ })));
1079
1151
  function formatTimeValue(value, max) {
1080
1152
  return Math.max(0, Math.min(max, value)).toString().padStart(2, '0');
1081
1153
  }
@@ -1162,6 +1234,8 @@ const TimePicker = ({ onTimeChange, hours = 0, minutes = 0, seconds = 0, display
1162
1234
  const Slider = ({ id, value: initialValue, min = 0, max = 100, step = 1, width = 112, onChange, onChangeCommitted, disabled, className, containerClassName, ...rest }) => {
1163
1235
  const [value, setValue] = useState(initialValue !== undefined ? Number(initialValue) : Number(min));
1164
1236
  useEffect(() => {
1237
+ // TODO: fix this lint
1238
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
1165
1239
  setValue(Number(initialValue));
1166
1240
  }, [initialValue]);
1167
1241
  const handleChange = (e) => {
@@ -1225,6 +1299,8 @@ const TolerancePicker = ({ onToleranceChange, toleranceValues: { minusTolerance,
1225
1299
  const plusToleranceIndex = TOLERANCE_RANGES.findIndex((range) => range.value === plusTolerance);
1226
1300
  if (minusToleranceIndex < 0 || plusToleranceIndex < 0) {
1227
1301
  const invalidTolerance = minusToleranceIndex < 0 ? minusTolerance : plusTolerance;
1302
+ // TODO: fix this lint
1303
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
1228
1304
  setWarningStatus({
1229
1305
  status: 'warning',
1230
1306
  message: translateWarningMessage?.(invalidTolerance) ||
@@ -1272,5 +1348,115 @@ const TokenInput = ({ label, tokens: initialTokens }) => {
1272
1348
  React.createElement("input", { type: "text", value: newToken, onChange: (e) => setNewToken(e.target.value), onKeyDown: addToken, className: "token-input", ref: inputRef }))));
1273
1349
  };
1274
1350
 
1275
- export { Button, Checkbox, CheckboxList, CheckboxesTree, ComboBox, DatePicker, Input, PasswordInput, RadioButton, RadioGroup, Select, Slider, TextArea, TimePicker, TokenInput, TolerancePicker, useDefaultComboBox };
1351
+ const colors = {
1352
+ ambientA5: 'rgb(246, 248, 249)',
1353
+ ambientA10: 'rgb(239, 243, 245)',
1354
+ ambientA15: 'rgb(233, 239, 242)',
1355
+ ambientB5: 'rgb(250, 249, 245)',
1356
+ ambientB10: 'rgb(247, 246, 238)',
1357
+ ambientB15: 'rgb(242, 240, 228)',
1358
+ ambientC5: 'rgb(246, 249, 246)',
1359
+ ambientC10: 'rgb(240, 244, 241)',
1360
+ ambientC15: 'rgb(234, 240, 235)',
1361
+ ambientD5: 'rgb(248, 247, 253)',
1362
+ ambientD10: 'rgb(244, 243, 252)',
1363
+ ambientD15: 'rgb(239, 237, 247)',
1364
+ blackAlpha: 'rgba(0, 0, 0)',
1365
+ };
1366
+ const TABLE_AMBIENT_THEMES = {
1367
+ A: {
1368
+ mix1: colors.blackAlpha,
1369
+ mix2: colors.ambientA5,
1370
+ odd: colors.ambientA10,
1371
+ even: colors.ambientA15,
1372
+ },
1373
+ B: {
1374
+ mix1: colors.blackAlpha,
1375
+ mix2: colors.ambientB5,
1376
+ odd: colors.ambientB10,
1377
+ even: colors.ambientB15,
1378
+ },
1379
+ C: {
1380
+ mix1: colors.blackAlpha,
1381
+ mix2: colors.ambientC5,
1382
+ odd: colors.ambientC10,
1383
+ even: colors.ambientC15,
1384
+ },
1385
+ D: {
1386
+ mix1: colors.blackAlpha,
1387
+ mix2: colors.ambientD5,
1388
+ odd: colors.ambientD10,
1389
+ even: colors.ambientD15,
1390
+ },
1391
+ };
1392
+
1393
+ const Table = ({ columns, data, ambientVariant = 'B' }) => {
1394
+ const theme = TABLE_AMBIENT_THEMES[ambientVariant];
1395
+ return (React.createElement("table", { className: "table-container ambient", style: {
1396
+ '--mix-color-1': theme.mix1,
1397
+ '--mix-color-2': theme.mix2,
1398
+ '--row-bg-odd': theme.odd,
1399
+ '--row-bg-even': theme.even,
1400
+ } },
1401
+ React.createElement("thead", null,
1402
+ React.createElement("tr", null, columns.map((col) => (React.createElement("td", { key: col.key, className: col.className }, col.label))))),
1403
+ React.createElement("tbody", null, data.map((row, i) => (React.createElement("tr", { key: i }, columns.map((col) => (React.createElement("td", { key: col.key, className: col.className }, row[col.key])))))))));
1404
+ };
1405
+
1406
+ /**
1407
+ * Display a styled dialog box.
1408
+ * You are in charge of its lifecycle (open & close), this componant just display it.
1409
+ *
1410
+ * Children must be an array of 3 items, where :
1411
+ * - the first one is for the header
1412
+ * - the second one is for the content
1413
+ * - the last is for the footer
1414
+ *
1415
+ * NB: Inside header, only title tags have the good style (font weight and size)
1416
+ */
1417
+ const Dialog = ({ children, className, fullscreen = false, header, headerClassname, footer, footerClassname, bodyClassname, ...dialogProps }) => {
1418
+ const overlayRef = useRef(null);
1419
+ const dialogRef = useRef(null);
1420
+ /**
1421
+ * When the dialog height change
1422
+ * => check if there is a scroll on the overlay and if so add a 'scroll' class on it
1423
+ */
1424
+ useEffect(() => {
1425
+ if (fullscreen)
1426
+ return;
1427
+ if (!overlayRef.current || !dialogRef.current)
1428
+ return;
1429
+ const resizeObserver = new ResizeObserver(() => {
1430
+ if (overlayRef.current) {
1431
+ if (overlayRef.current.scrollHeight > overlayRef.current.clientHeight) {
1432
+ overlayRef.current.classList.add('scroll');
1433
+ }
1434
+ else {
1435
+ overlayRef.current.classList.remove('scroll');
1436
+ }
1437
+ }
1438
+ });
1439
+ resizeObserver.observe(dialogRef.current);
1440
+ return () => resizeObserver.disconnect();
1441
+ }, [overlayRef, dialogRef, fullscreen]);
1442
+ return (React.createElement("div", { ref: overlayRef, className: "ui-dialog-overlay flex items-center justify-center", tabIndex: -1 },
1443
+ React.createElement("dialog", { ref: dialogRef, ...dialogProps, className: cx('dialog-content', className, fullscreen && 'fullscreen'), open: true },
1444
+ React.createElement("header", { className: cx(headerClassname) }, header),
1445
+ React.createElement("section", { className: cx(bodyClassname) }, children),
1446
+ React.createElement("footer", { className: cx(footerClassname) }, footer))));
1447
+ };
1448
+
1449
+ const SegmentedControl = ({ options, onChange, value, getOptionLabel, getOptionValue, getOptionIcon, small = false, ...inputProps }) => {
1450
+ const opts = useMemo(() => options.map((e) => ({
1451
+ value: getOptionValue(e),
1452
+ label: getOptionLabel(e),
1453
+ icon: getOptionIcon ? getOptionIcon(e) : null,
1454
+ source: e,
1455
+ })), [options, getOptionValue, getOptionLabel, getOptionIcon]);
1456
+ return (React.createElement("div", { className: cx('segmented-control', { small }) }, opts.map((option) => (React.createElement("label", { key: option.value, className: cx('segmented-control-option', option.value === getOptionValue(value) && 'checked'), title: option.label },
1457
+ React.createElement("div", { className: "segmented-control-option-content" }, option.icon ? option.icon : option.label),
1458
+ React.createElement("input", { ...inputProps, type: "radio", checked: option.value === getOptionValue(value), onChange: () => onChange(option.source) }))))));
1459
+ };
1460
+
1461
+ export { Button, Checkbox, CheckboxList, CheckboxesTree, ComboBox, DatePicker, Dialog, Input, PasswordInput, RadioButton, RadioGroup, SegmentedControl, Select, Slider, Table, TextArea, TimePicker, TokenInput, TolerancePicker, useDefaultComboBox, useOutsideClick };
1276
1462
  //# sourceMappingURL=index.esm.js.map