fis-component 0.0.88 → 0.0.90

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,13 +1,14 @@
1
1
  import { InputLabelProps } from "../Input/InputLabel";
2
2
  import { MenuProps } from "../MenuSelect/types";
3
3
  import { SelectFieldProps } from "../SelectItem";
4
+ export interface SelectOptionItem<T = string | number> {
5
+ label: string;
6
+ description?: string;
7
+ value: T;
8
+ }
4
9
  export interface SelectOption<T = string | number> {
5
10
  groupLabel?: string;
6
- items: {
7
- label: string;
8
- description?: string;
9
- value: T;
10
- }[];
11
+ items: SelectOptionItem<T>[];
11
12
  }
12
13
  type BaseSelectProps<T extends string | number> = Partial<InputLabelProps> & Omit<MenuProps, "groups" | "multi" | "selectedValues" | "onChangeSelected" | "onClickMenu" | "size" | "placeholder"> & Omit<SelectFieldProps, "value" | "onChange"> & {
13
14
  options: SelectOption<T>[];
@@ -25,13 +26,13 @@ export type SingleSelectProps<T extends string | number> = BaseSelectProps<T> &
25
26
  multi?: false;
26
27
  value: T;
27
28
  onChange: (value: T) => void;
28
- displayValue?: (value: SelectOption<T>) => string;
29
+ displayValue?: (item: SelectOptionItem<T>) => string;
29
30
  };
30
31
  export type MultiSelectProps<T extends string | number> = BaseSelectProps<T> & {
31
32
  multi: true;
32
33
  value: T[];
33
34
  onChange: (value: T[]) => void;
34
- displayValue?: (value: SelectOption<T>[]) => string;
35
+ displayValue?: (items: SelectOptionItem<T>[]) => string;
35
36
  };
36
37
  export type SelectProps<T extends string | number> = SingleSelectProps<T> | MultiSelectProps<T>;
37
38
  export {};
package/dist/esm/index.js CHANGED
@@ -73642,45 +73642,21 @@ const FISSelect = forwardRef(({ className, style, size = "md", options, value, d
73642
73642
  strategy: "fixed",
73643
73643
  }), []);
73644
73644
  const { styles, attributes } = usePopper(referenceElement, popperElement, popperOptions);
73645
- const originalOptionsRef = useRef(options);
73646
- useEffect(() => {
73647
- // Keep track of all items including selected ones
73648
- const allItems = new Map();
73649
- // Add items from current options
73650
- options.forEach((group) => {
73651
- group.items.forEach((item) => {
73652
- allItems.set(item.value, item);
73653
- });
73654
- });
73655
- // Add selected items that might not be in current options
73656
- if (multi && Array.isArray(value)) {
73657
- const selectedItems = originalOptionsRef.current
73658
- .flatMap((group) => group.items)
73659
- .filter((item) => value.includes(item.value));
73660
- selectedItems.forEach((item) => {
73661
- allItems.set(item.value, item);
73662
- });
73663
- }
73664
- // Update originalOptionsRef with all items
73665
- originalOptionsRef.current = [
73666
- {
73667
- items: Array.from(allItems.values()),
73668
- },
73669
- ];
73670
- }, [options, value, multi]);
73671
73645
  const selectedItems = useMemo$1(() => {
73672
73646
  const allItems = options.flatMap((group) => group.items);
73673
73647
  if (multi) {
73674
- return allItems.filter((item) => value.includes(item.value));
73648
+ const selectedArray = Array.isArray(value) ? value : [];
73649
+ return allItems.filter((item) => selectedArray.includes(item.value));
73675
73650
  }
73676
73651
  else {
73677
73652
  const found = allItems.find((item) => item.value === value);
73678
73653
  return found ? [found] : [];
73679
73654
  }
73680
73655
  }, [value, multi, options]);
73681
- const computedDisplayValue = () => {
73656
+ const computedDisplayValue = useMemo$1(() => {
73682
73657
  if (multi) {
73683
- const count = value.length;
73658
+ const selectedArray = Array.isArray(value) ? value : [];
73659
+ const count = selectedArray.length;
73684
73660
  if (count === 0) {
73685
73661
  return "";
73686
73662
  }
@@ -73689,17 +73665,18 @@ const FISSelect = forwardRef(({ className, style, size = "md", options, value, d
73689
73665
  : `Selected ${count.toString().padStart(2, "0")} option${count !== 1 ? "s" : ""}`;
73690
73666
  }
73691
73667
  else {
73692
- if (!selectedItems[0])
73668
+ const firstItem = selectedItems[0];
73669
+ if (!firstItem)
73693
73670
  return "";
73694
- return (displayValue?.(selectedItems[0]) || selectedItems[0].label);
73671
+ return displayValue?.(firstItem) || firstItem.label;
73695
73672
  }
73696
- };
73673
+ }, [multi, value, multiDisplayText, selectedItems, displayValue]);
73697
73674
  const handleToggle = useCallback(() => {
73698
73675
  if (!disabled)
73699
73676
  setIsOpen((prev) => !prev);
73700
73677
  }, [disabled]);
73701
73678
  const handleOptionRemove = useCallback((option) => {
73702
- if (multi) {
73679
+ if (multi && Array.isArray(value)) {
73703
73680
  const newValues = value.filter((v) => v !== option.value);
73704
73681
  onChange(newValues);
73705
73682
  }
@@ -73711,13 +73688,15 @@ const FISSelect = forwardRef(({ className, style, size = "md", options, value, d
73711
73688
  else {
73712
73689
  if (values.length > 0) {
73713
73690
  onChange(values[0]);
73714
- setIsOpen(false);
73691
+ // Delay closing to ensure react-hook-form processes the change first
73692
+ setTimeout(() => setIsOpen(false), 0);
73715
73693
  }
73716
73694
  }
73717
73695
  }, [multi, onChange]);
73718
73696
  const handleMenuClick = useCallback(() => {
73719
73697
  if (!multi) {
73720
- setIsOpen(false);
73698
+ // Delay closing to ensure react-hook-form processes the change first
73699
+ setTimeout(() => setIsOpen(false), 0);
73721
73700
  }
73722
73701
  }, [multi]);
73723
73702
  const menuProps = useMemo$1(() => ({
@@ -73727,8 +73706,10 @@ const FISSelect = forwardRef(({ className, style, size = "md", options, value, d
73727
73706
  multi,
73728
73707
  size: isMenuSize(size) ? size : "md",
73729
73708
  selectedValues: multi
73730
- ? value
73731
- : value
73709
+ ? Array.isArray(value)
73710
+ ? value
73711
+ : []
73712
+ : value !== undefined && value !== null
73732
73713
  ? [value]
73733
73714
  : [],
73734
73715
  onChangeSelected: handleMenuChange,
@@ -73752,18 +73733,25 @@ const FISSelect = forwardRef(({ className, style, size = "md", options, value, d
73752
73733
  restProps,
73753
73734
  ]);
73754
73735
  useEffect(() => {
73736
+ if (!isOpen)
73737
+ return;
73755
73738
  const handleClickOutside = (event) => {
73739
+ const target = event.target;
73756
73740
  if (referenceElement &&
73757
- !referenceElement.contains(event.target) &&
73741
+ !referenceElement.contains(target) &&
73758
73742
  popperElement &&
73759
- !popperElement.contains(event.target)) {
73743
+ !popperElement.contains(target)) {
73760
73744
  setIsOpen(false);
73761
73745
  }
73762
73746
  };
73763
73747
  document.addEventListener("mousedown", handleClickOutside);
73764
73748
  return () => document.removeEventListener("mousedown", handleClickOutside);
73765
- }, [referenceElement, popperElement]);
73766
- return (jsxs(DivWrapperSC$1, { className: className, style: style, children: [(textLabel || iconLabel) && (jsx(FISInputLabel, { textLabel: textLabel, required: required, iconLabel: iconLabel, onClickIconLabel: onClickIconLabel })), jsx(DivInputWrapperSC, { ref: setReferenceElement, onClick: handleToggle, "$hasValue": !!value, children: jsx(FISSelectItem, { ...restProps, ref: ref, size: size, placeholder: placeholder, iconSuffix: isOpen ? jsx(ArrowUpIcon, {}) : jsx(ArrowDownIcon, {}), value: computedDisplayValue(), disabled: disabled, activeDropdown: isOpen, negative: negative }) }), message && (jsx(SpanHintSC$3, { className: classNames({ disabled, negative, positive }), children: message })), multi && value.length > 0 && (jsx(SelectedTagsWrapper, { children: jsx(MultipleValue, { options: selectedItems, onRemove: handleOptionRemove }) })), isOpen && (jsx(Portal, { portal: portal, children: jsx(DivDropdownMenuSC, { ref: setPopperElement, style: {
73749
+ }, [isOpen, referenceElement, popperElement]);
73750
+ return (jsxs(DivWrapperSC$1, { className: className, style: style, children: [(textLabel || iconLabel) && (jsx(FISInputLabel, { textLabel: textLabel, required: required, iconLabel: iconLabel, onClickIconLabel: onClickIconLabel })), jsxs(DivInputWrapperSC, { ref: setReferenceElement, onClick: handleToggle, "$hasValue": !!value, children: [jsx("input", { ...restProps, ref: ref, type: "hidden", value: multi
73751
+ ? Array.isArray(value)
73752
+ ? value.join(",")
73753
+ : ""
73754
+ : (value ?? "") }), jsx(FISSelectItem, { size: size, placeholder: placeholder, iconSuffix: isOpen ? jsx(ArrowUpIcon, {}) : jsx(ArrowDownIcon, {}), value: computedDisplayValue, disabled: disabled, activeDropdown: isOpen, negative: negative, readOnly: true })] }), message && (jsx(SpanHintSC$3, { className: classNames({ disabled, negative, positive }), children: message })), multi && Array.isArray(value) && value.length > 0 && (jsx(SelectedTagsWrapper, { children: jsx(MultipleValue, { options: selectedItems, onRemove: handleOptionRemove }) })), isOpen && (jsx(Portal, { portal: portal, children: jsx(DivDropdownMenuSC, { ref: setPopperElement, style: {
73767
73755
  ...styles.popper,
73768
73756
  width: referenceElement?.offsetWidth,
73769
73757
  zIndex: 9999,