@osrd-project/ui-core 0.0.82 → 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.
@@ -1,9 +1,9 @@
1
1
  import React, { type InputHTMLAttributes } from 'react';
2
- export type CheckboxProps = InputHTMLAttributes<HTMLInputElement> & {
2
+ export type CheckboxProps = React.PropsWithChildren<Omit<InputHTMLAttributes<HTMLInputElement>, 'children'> & {
3
3
  label?: string;
4
4
  small?: boolean;
5
5
  hint?: string;
6
6
  isIndeterminate?: boolean;
7
- };
8
- declare const Checkbox: ({ label, small, hint, disabled, checked, readOnly, isIndeterminate, onClick, onKeyUp, onBlur, ...rest }: CheckboxProps) => React.JSX.Element;
7
+ }>;
8
+ declare const Checkbox: ({ label, small, hint, disabled, checked, readOnly, isIndeterminate, onClick, children, ...rest }: CheckboxProps) => React.JSX.Element;
9
9
  export default Checkbox;
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
- import { type CheckboxTreeItem } from './type';
3
2
  import { type FieldWrapperProps } from '../FieldWrapper';
3
+ import { type CheckboxTreeItem } from './type';
4
4
  export type CheckboxesTreeProps = Omit<FieldWrapperProps, 'children'> & {
5
5
  readOnly?: boolean;
6
6
  items: CheckboxTreeItem[];
@@ -16,12 +16,19 @@ export type ComboBoxProps<T> = Omit<InputProps, 'value'> & {
16
16
  isSelected: boolean;
17
17
  }) => ReactNode;
18
18
  renderFooterItem?: () => ReactNode;
19
+ allowCustomValue?: boolean;
20
+ onAddCustomValue?: (value: string) => void;
21
+ addCustomValueLabel?: string;
19
22
  };
20
23
  /**
21
24
  * ComboBox component
22
25
  *
23
26
  * You can use the hook useDefaultComboBox to get the default behavior.
24
27
  * See the stories.
28
+ *
29
+ * When `allowCustomValue` is true, an option appears at the bottom of the dropdown
30
+ * when the typed value doesn't match any existing suggestion.
31
+ * Defocusing without selecting cancels the input.
25
32
  */
26
- declare const ComboBox: <T>({ suggestions, customLabel, numberOfSuggestionsToShow, value, small, getSuggestionLabel, onChange, onSelectSuggestion, resetSuggestions, testIdPrefix, renderListElementComponent, renderFooterItem, ...inputProps }: ComboBoxProps<T>) => React.JSX.Element;
33
+ declare const ComboBox: <T>({ suggestions, customLabel, numberOfSuggestionsToShow, value, small, getSuggestionLabel, onChange, onSelectSuggestion, resetSuggestions, testIdPrefix, renderListElementComponent, renderFooterItem, allowCustomValue, onAddCustomValue, addCustomValueLabel, ...inputProps }: ComboBoxProps<T>) => React.JSX.Element;
27
34
  export default ComboBox;
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { type CalendarSlot } from '.';
3
- import { type CalendarPickerPublicProps } from './CalendarPicker';
4
3
  import { type InputProps } from '../Input';
4
+ import { type CalendarPickerPublicProps } from './CalendarPicker';
5
5
  type BaseDatePickerProps = {
6
6
  selectableSlot?: CalendarSlot;
7
7
  inputProps: InputProps;
@@ -1,5 +1,5 @@
1
- import type { DatePickerProps } from './DatePicker';
2
1
  import type { StatusWithMessage } from '../StatusMessage';
2
+ import type { DatePickerProps } from './DatePicker';
3
3
  export default function useDatePicker(datePickerProps: DatePickerProps): {
4
4
  showPicker: boolean;
5
5
  inputValue: string;
@@ -6,6 +6,7 @@ export type SegmentedControlProps<T> = Omit<React.InputHTMLAttributes<HTMLInputE
6
6
  getOptionLabel: (option: T) => string;
7
7
  getOptionValue: (option: T) => string;
8
8
  getOptionIcon?: (option: T) => React.ReactNode;
9
+ small?: boolean;
9
10
  };
10
- declare const SegmentedControl: <T>({ options, onChange, value, getOptionLabel, getOptionValue, getOptionIcon, ...inputProps }: SegmentedControlProps<T>) => React.JSX.Element;
11
+ declare const SegmentedControl: <T>({ options, onChange, value, getOptionLabel, getOptionValue, getOptionIcon, small, ...inputProps }: SegmentedControlProps<T>) => React.JSX.Element;
11
12
  export default SegmentedControl;
@@ -3,9 +3,10 @@ import { type FieldWrapperProps } from './FieldWrapper';
3
3
  export type SelectProps<T> = Omit<React.InputHTMLAttributes<HTMLSelectElement>, 'value' | 'onChange'> & Omit<FieldWrapperProps, 'children'> & {
4
4
  options: Array<T>;
5
5
  value?: T;
6
+ dataTestId?: string;
6
7
  getOptionLabel: (option: T) => string;
7
8
  getOptionValue: (option: T) => string;
8
9
  onChange: (option?: T) => void;
9
10
  };
10
- declare const Select: <T>({ id, label, hint, value, options, placeholder, statusWithMessage, required, disabled, narrow, readOnly, small, getOptionLabel, getOptionValue, onChange, ...props }: SelectProps<T>) => React.JSX.Element;
11
+ declare const Select: <T>({ id, label, hint, value, options, placeholder, statusWithMessage, required, disabled, narrow, readOnly, small, getOptionLabel, getOptionValue, onChange, dataTestId, ...props }: SelectProps<T>) => React.JSX.Element;
11
12
  export default Select;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,2 @@
1
- declare const useOutsideClick: (ref: React.RefObject<HTMLElement | null>, handler: (event: MouseEvent) => void) => void;
1
+ declare const useOutsideClick: (ref: React.RefObject<HTMLElement | null> | null, handler: (event: MouseEvent) => void) => void;
2
2
  export default useOutsideClick;
package/dist/index.d.ts CHANGED
@@ -17,3 +17,4 @@ export { default as TokenInput, TokenInputProps } from './components/TokenInput'
17
17
  export { default as Table } from './components/Table/Table';
18
18
  export { default as Dialog } from './components/Dialog';
19
19
  export { default as SegmentedControl, SegmentedControlProps } from './components/SegmentedControl';
20
+ export { default as useOutsideClick } from './hooks/useOutsideClick';
package/dist/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useEffect, useState, useCallback, useRef, useMemo } from 'react';
1
+ import React, { useEffectEvent, useEffect, useState, useCallback, useRef, useMemo } from 'react';
2
2
  import cx from 'classnames';
3
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
 
@@ -17,12 +17,12 @@ const Button = ({ label, variant = 'Primary', isLoading = false, isDisabled = fa
17
17
  };
18
18
 
19
19
  const useOutsideClick = (ref, handler) => {
20
+ const handleClickOutside = useEffectEvent((event) => {
21
+ if (ref?.current && !ref.current.contains(event.target)) {
22
+ handler(event);
23
+ }
24
+ });
20
25
  useEffect(() => {
21
- const handleClickOutside = (event) => {
22
- if (ref.current && !ref.current.contains(event.target)) {
23
- handler(event);
24
- }
25
- };
26
26
  document.addEventListener('mousedown', handleClickOutside);
27
27
  return () => {
28
28
  document.removeEventListener('mousedown', handleClickOutside);
@@ -30,6 +30,20 @@ const useOutsideClick = (ref, handler) => {
30
30
  }, [ref, handler]);
31
31
  };
32
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
+
33
47
  const Hint = ({ text }) => React.createElement("span", { className: "ui-hint" }, text);
34
48
 
35
49
  const InputStatusIcon = ({ status, small, className }) => {
@@ -83,20 +97,6 @@ const FieldWrapper = ({ id, label, hint, required, disabled, statusWithMessage,
83
97
  }, showIcon: statusIconPosition === 'before-status-message', onClose: onCloseStatusMessage })))));
84
98
  };
85
99
 
86
- function useFocusByTab({ onBlur, onKeyUp }) {
87
- const [isFocusByTab, setIsFocusByTab] = useState(false);
88
- const handleKeyUp = useCallback((event) => {
89
- if (event.key === 'Tab')
90
- setIsFocusByTab(true);
91
- onKeyUp?.(event);
92
- }, [onKeyUp]);
93
- const handleBlur = useCallback((event) => {
94
- setIsFocusByTab(false);
95
- onBlur?.(event);
96
- }, [onBlur]);
97
- return { handleKeyUp, handleBlur, isFocusByTab };
98
- }
99
-
100
100
  const InputAffix = ({ value, type, disabled, readOnly }) => {
101
101
  const isContentWithCallback = typeof value === 'object' && value !== null && 'onClickCallback' in value;
102
102
  const spanContent = isContentWithCallback
@@ -134,8 +134,12 @@ Input.displayName = 'Input';
134
134
  *
135
135
  * You can use the hook useDefaultComboBox to get the default behavior.
136
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.
137
141
  */
138
- const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, value, small, getSuggestionLabel, onChange, onSelectSuggestion, resetSuggestions, testIdPrefix, renderListElementComponent, renderFooterItem, ...inputProps }) => {
142
+ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, value, small, getSuggestionLabel, onChange, onSelectSuggestion, resetSuggestions, testIdPrefix, renderListElementComponent, renderFooterItem, allowCustomValue = false, onAddCustomValue, addCustomValueLabel, ...inputProps }) => {
139
143
  const [activeSuggestionIndex, setActiveSuggestionIndex] = useState(-1);
140
144
  const [inputValue, setInputValue] = useState('');
141
145
  const [isInputFocused, setIsInputFocused] = useState(false);
@@ -162,13 +166,20 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
162
166
  /* eslint-disable react-hooks/exhaustive-deps */
163
167
  useEffect(() => {
164
168
  if (value) {
169
+ // TODO: fix this lint
170
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
165
171
  setInputValue(getSuggestionLabel(value));
166
172
  }
167
173
  else {
168
174
  setInputValue('');
169
175
  }
170
176
  }, [value]);
171
- 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]);
172
183
  // behavior
173
184
  const handleInputChange = (e) => {
174
185
  onChange?.(e);
@@ -180,15 +191,24 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
180
191
  setInputValue(getSuggestionLabel(selectedSuggestion));
181
192
  removeFocus();
182
193
  };
194
+ const confirmCustomValue = () => {
195
+ const trimmed = inputValue.trim();
196
+ if (!trimmed)
197
+ return;
198
+ onAddCustomValue?.(trimmed);
199
+ removeFocus();
200
+ };
183
201
  const closeSuggestions = () => {
184
202
  setInputValue(value ? getSuggestionLabel(value) : '');
185
203
  removeFocus();
186
204
  };
205
+ const totalItems = suggestions.length + (showAddCustomValue ? 1 : 0);
206
+ const customValueIndex = showAddCustomValue ? suggestions.length : -1;
187
207
  const handleKeyDown = (e) => {
188
208
  switch (e.key) {
189
209
  case 'ArrowDown': {
190
210
  setActiveSuggestionIndex((prev) => {
191
- const newIndex = prev < suggestions.length - 1 ? prev + 1 : prev;
211
+ const newIndex = prev < totalItems - 1 ? prev + 1 : prev;
192
212
  suggestionRefs.current[newIndex]?.scrollIntoView({ block: 'nearest' });
193
213
  return newIndex;
194
214
  });
@@ -204,7 +224,10 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
204
224
  }
205
225
  case 'Enter': {
206
226
  e.preventDefault();
207
- if (activeSuggestionIndex >= 0) {
227
+ if (activeSuggestionIndex === customValueIndex) {
228
+ confirmCustomValue();
229
+ }
230
+ else if (activeSuggestionIndex >= 0) {
208
231
  selectSuggestion(activeSuggestionIndex);
209
232
  }
210
233
  else if (suggestions.length === 1) {
@@ -213,7 +236,10 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
213
236
  break;
214
237
  }
215
238
  case 'Tab': {
216
- if (activeSuggestionIndex >= 0) {
239
+ if (activeSuggestionIndex === customValueIndex) {
240
+ confirmCustomValue();
241
+ }
242
+ else if (activeSuggestionIndex >= 0) {
217
243
  selectSuggestion(activeSuggestionIndex);
218
244
  }
219
245
  break;
@@ -234,7 +260,8 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
234
260
  resetSuggestions();
235
261
  focusInput();
236
262
  };
237
- useOutsideClick(wrapperRef, closeSuggestions);
263
+ useOutsideClick(showSuggestions ? wrapperRef : null, // Only trigger when the suggestions are displayed
264
+ closeSuggestions);
238
265
  const inputIcons = useMemo(() => {
239
266
  if (inputProps.readOnly || inputProps.disabled)
240
267
  return undefined;
@@ -289,6 +316,12 @@ const ComboBox = ({ suggestions, customLabel, numberOfSuggestionsToShow = 5, val
289
316
  isSelected: !!value && getSuggestionLabel(value) === getSuggestionLabel(suggestion),
290
317
  })
291
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()}"`)),
292
325
  renderFooterItem && renderFooterItem()))));
293
326
  };
294
327
 
@@ -331,8 +364,7 @@ const useDefaultComboBox = (suggestions, getSuggestionLabel) => {
331
364
  return { suggestions: filteredSuggestions, onChange, resetSuggestions };
332
365
  };
333
366
 
334
- const Checkbox = ({ label, small, hint, disabled, checked, readOnly, isIndeterminate = false, onClick, onKeyUp, onBlur, ...rest }) => {
335
- const { handleKeyUp, handleBlur, isFocusByTab } = useFocusByTab({ onBlur, onKeyUp });
367
+ const Checkbox = ({ label, small, hint, disabled, checked, readOnly, isIndeterminate = false, onClick, children, ...rest }) => {
336
368
  const handleClick = (e) => {
337
369
  if (readOnly) {
338
370
  e.preventDefault();
@@ -342,16 +374,16 @@ const Checkbox = ({ label, small, hint, disabled, checked, readOnly, isIndetermi
342
374
  onClick?.(e);
343
375
  }
344
376
  };
345
- 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 }) },
346
378
  React.createElement("input", {
347
- //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
348
380
  className: cx({ 'read-only': readOnly }), type: "checkbox", checked: checked, disabled: disabled, ref: (input) => {
349
381
  if (input) {
350
382
  input.indeterminate = isIndeterminate;
351
383
  }
352
- }, onClick: handleClick, onBlur: handleBlur, onKeyUp: handleKeyUp, ...rest }),
353
- React.createElement("span", { className: cx('checkmark', { 'focused-by-tab': isFocusByTab }) }),
384
+ }, onClick: handleClick, ...rest }),
354
385
  label && React.createElement("div", { className: "label" }, label),
386
+ children && React.createElement("div", { className: "label" }, children),
355
387
  hint && React.createElement("span", { className: "hint" }, hint)));
356
388
  };
357
389
 
@@ -473,6 +505,56 @@ const CheckboxesTree = ({ items, small, id, label, hint, statusWithMessage, disa
473
505
  React.createElement(CheckboxList, { small: small, items: items, disabled: disabled, readOnly: readOnly, onClickItem: handleClick })));
474
506
  };
475
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
+
476
558
  const INVALID_SELECTED_SLOT_ERROR = 'Invalid selectedSlot: If start and end are defined, the start date must be before the end date.';
477
559
  const INVALID_SELECTABLE_SLOT_ERROR = 'Invalid selectableSlot: If start and end are defined, the start date must be before the end date.';
478
560
  const INVALID_SELECTED_SLOT_BASED_ON_SELECTABLE_SLOT_ERROR = 'selectedSlot must be within selectableSlot';
@@ -857,38 +939,6 @@ const CalendarPicker = ({ initialDate, selectedSlot, selectableSlot, numberOfMon
857
939
  React.createElement(ChevronRight, { size: "lg" })))));
858
940
  };
859
941
 
860
- const useModalPosition = (inputRef, modalRef, offset = 3 // Default offset below the input
861
- ) => {
862
- const [modalPosition, setModalPosition] = useState({
863
- top: 0,
864
- left: 0,
865
- });
866
- const calculatePosition = useCallback(() => {
867
- if (inputRef.current && modalRef.current) {
868
- const inputRect = inputRef.current.getBoundingClientRect();
869
- const modal = modalRef.current;
870
- const modalRect = modal.getBoundingClientRect();
871
- const offsetParent = modal.offsetParent;
872
- const parentRect = offsetParent ? offsetParent.getBoundingClientRect() : { top: 0, left: 0 };
873
- // Adjust the top position: place it below the input while considering an offset
874
- const top = inputRect.bottom - parentRect.top - offset;
875
- // Center the modal horizontally relative to the input
876
- const modalViewportLeft = inputRect.left + (inputRect.width - modalRect.width) / 2;
877
- // Keep modal to stay within viewport bounds
878
- const adjustedViewportLeft = Math.max(0, Math.min(modalViewportLeft, window.innerWidth - modalRect.width));
879
- const left = adjustedViewportLeft - parentRect.left;
880
- setModalPosition({ top, left });
881
- }
882
- }, [inputRef, modalRef, offset]);
883
- useEffect(() => {
884
- calculatePosition();
885
- const handleResize = () => calculatePosition();
886
- window.addEventListener('resize', handleResize);
887
- return () => window.removeEventListener('resize', handleResize);
888
- }, [calculatePosition]);
889
- return { modalPosition, calculatePosition };
890
- };
891
-
892
942
  const MODAL_VERTICAL_OFFSET = 3;
893
943
  // Regex for "xx/xx/xx"
894
944
  const SINGLE_DATE_REGEX = /^\d{2}\/\d{2}\/\d{2}$/;
@@ -964,6 +1014,8 @@ function useDatePicker(datePickerProps) {
964
1014
  if (newInput !== inputValue) {
965
1015
  // we only set the input value if it has changed
966
1016
  // otherwise the user loses the focus
1017
+ // TODO: fix this lint
1018
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
967
1019
  setInputValue(newInput);
968
1020
  }
969
1021
  setStatusWithMessage(undefined);
@@ -985,24 +1037,6 @@ function useDatePicker(datePickerProps) {
985
1037
  };
986
1038
  }
987
1039
 
988
- const InputModal = ({ inputRef, isOpen, onClose, children, testIdPrefix }) => {
989
- const modalRef = useRef(null);
990
- const { modalPosition, calculatePosition } = useModalPosition(inputRef, modalRef);
991
- useEffect(() => {
992
- if (isOpen) {
993
- calculatePosition();
994
- }
995
- }, [calculatePosition, isOpen]);
996
- useOutsideClick(modalRef, onClose);
997
- if (!isOpen)
998
- return null;
999
- return (React.createElement("div", { className: "ui-input-modal-overlay" },
1000
- React.createElement("div", { ref: modalRef, className: "modal-content", style: { top: modalPosition.top, left: modalPosition.left } },
1001
- React.createElement("button", { "data-testid": testIdPrefix ? `${testIdPrefix}-close-button` : undefined, className: "close-button", onClick: onClose },
1002
- React.createElement(X, { size: "lg" })),
1003
- children)));
1004
- };
1005
-
1006
1040
  const DatePicker = (props) => {
1007
1041
  const { inputValue, statusWithMessage, selectedSlot, showPicker, modalPosition, inputRef, calendarPickerRef, setShowPicker, handleDayClick, handleInputClick, handleInputOnChange, } = useDatePicker(props);
1008
1042
  const { inputFieldWrapperClassname, ...otherInputProps } = props.inputProps;
@@ -1043,6 +1077,7 @@ const RadioButton = ({ id, label, value, hint, readOnly, onBlur, onKeyUp, small,
1043
1077
  const RadioGroup = ({ label, value, subtitle, readOnly, disabled, options, statusWithMessage, required, small, onChange, }) => {
1044
1078
  const [selectedValue, setSelectedValue] = useState(value);
1045
1079
  useEffect(() => {
1080
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
1046
1081
  setSelectedValue(value);
1047
1082
  }, [value]);
1048
1083
  const statusClassname = statusWithMessage?.status ? { [statusWithMessage.status]: true } : {};
@@ -1069,7 +1104,7 @@ const RadioGroup = ({ label, value, subtitle, readOnly, disabled, options, statu
1069
1104
  };
1070
1105
 
1071
1106
  const PLACEHOLDER_VALUE = '__PLACEHOLDER__';
1072
- 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 }) => {
1073
1108
  const [selectedOption, setSelectedOption] = useState(value);
1074
1109
  const handleOnChange = (e) => {
1075
1110
  const newSelectedOption = e.target.value === PLACEHOLDER_VALUE
@@ -1079,10 +1114,12 @@ const Select = ({ id, label, hint, value, options, placeholder, statusWithMessag
1079
1114
  onChange(newSelectedOption);
1080
1115
  };
1081
1116
  useEffect(() => {
1117
+ // TODO: fix this lint
1118
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
1082
1119
  setSelectedOption(value);
1083
1120
  }, [value]);
1084
1121
  return (React.createElement(FieldWrapper, { id: id, label: label, hint: hint, statusWithMessage: statusWithMessage, required: required, disabled: disabled, small: small, narrow: narrow },
1085
- 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, {
1086
1123
  'placeholder-selected': placeholder && !selectedOption,
1087
1124
  small,
1088
1125
  'read-only': readOnly,
@@ -1107,7 +1144,10 @@ const TextArea = ({ id, label, value = '', hint, required, disabled, statusWithM
1107
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 }))));
1108
1145
  };
1109
1146
 
1110
- 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
+ })));
1111
1151
  function formatTimeValue(value, max) {
1112
1152
  return Math.max(0, Math.min(max, value)).toString().padStart(2, '0');
1113
1153
  }
@@ -1194,6 +1234,8 @@ const TimePicker = ({ onTimeChange, hours = 0, minutes = 0, seconds = 0, display
1194
1234
  const Slider = ({ id, value: initialValue, min = 0, max = 100, step = 1, width = 112, onChange, onChangeCommitted, disabled, className, containerClassName, ...rest }) => {
1195
1235
  const [value, setValue] = useState(initialValue !== undefined ? Number(initialValue) : Number(min));
1196
1236
  useEffect(() => {
1237
+ // TODO: fix this lint
1238
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
1197
1239
  setValue(Number(initialValue));
1198
1240
  }, [initialValue]);
1199
1241
  const handleChange = (e) => {
@@ -1257,6 +1299,8 @@ const TolerancePicker = ({ onToleranceChange, toleranceValues: { minusTolerance,
1257
1299
  const plusToleranceIndex = TOLERANCE_RANGES.findIndex((range) => range.value === plusTolerance);
1258
1300
  if (minusToleranceIndex < 0 || plusToleranceIndex < 0) {
1259
1301
  const invalidTolerance = minusToleranceIndex < 0 ? minusTolerance : plusTolerance;
1302
+ // TODO: fix this lint
1303
+ /* eslint-disable-next-line react-hooks/set-state-in-effect */
1260
1304
  setWarningStatus({
1261
1305
  status: 'warning',
1262
1306
  message: translateWarningMessage?.(invalidTolerance) ||
@@ -1402,17 +1446,17 @@ const Dialog = ({ children, className, fullscreen = false, header, headerClassna
1402
1446
  React.createElement("footer", { className: cx(footerClassname) }, footer))));
1403
1447
  };
1404
1448
 
1405
- const SegmentedControl = ({ options, onChange, value, getOptionLabel, getOptionValue, getOptionIcon, ...inputProps }) => {
1449
+ const SegmentedControl = ({ options, onChange, value, getOptionLabel, getOptionValue, getOptionIcon, small = false, ...inputProps }) => {
1406
1450
  const opts = useMemo(() => options.map((e) => ({
1407
1451
  value: getOptionValue(e),
1408
1452
  label: getOptionLabel(e),
1409
1453
  icon: getOptionIcon ? getOptionIcon(e) : null,
1410
1454
  source: e,
1411
1455
  })), [options, getOptionValue, getOptionLabel, getOptionIcon]);
1412
- return (React.createElement("div", { className: "segmented-control" }, opts.map((option) => (React.createElement("label", { key: option.value, className: cx('segmented-control-option', option.value === getOptionValue(value) && 'checked'), title: option.label },
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 },
1413
1457
  React.createElement("div", { className: "segmented-control-option-content" }, option.icon ? option.icon : option.label),
1414
1458
  React.createElement("input", { ...inputProps, type: "radio", checked: option.value === getOptionValue(value), onChange: () => onChange(option.source) }))))));
1415
1459
  };
1416
1460
 
1417
- export { Button, Checkbox, CheckboxList, CheckboxesTree, ComboBox, DatePicker, Dialog, Input, PasswordInput, RadioButton, RadioGroup, SegmentedControl, Select, Slider, Table, TextArea, TimePicker, TokenInput, TolerancePicker, useDefaultComboBox };
1461
+ export { Button, Checkbox, CheckboxList, CheckboxesTree, ComboBox, DatePicker, Dialog, Input, PasswordInput, RadioButton, RadioGroup, SegmentedControl, Select, Slider, Table, TextArea, TimePicker, TokenInput, TolerancePicker, useDefaultComboBox, useOutsideClick };
1418
1462
  //# sourceMappingURL=index.esm.js.map