analytica-frontend-lib 1.1.72 → 1.1.73

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.js CHANGED
@@ -2855,11 +2855,15 @@ var Search = (0, import_react14.forwardRef)(
2855
2855
  value,
2856
2856
  onChange,
2857
2857
  placeholder = "Buscar...",
2858
+ onKeyDown: userOnKeyDown,
2858
2859
  ...props
2859
2860
  }, ref) => {
2860
2861
  const [dropdownOpen, setDropdownOpen] = (0, import_react14.useState)(false);
2862
+ const [forceClose, setForceClose] = (0, import_react14.useState)(false);
2863
+ const justSelectedRef = (0, import_react14.useRef)(false);
2861
2864
  const dropdownStore = (0, import_react14.useRef)(createDropdownStore()).current;
2862
2865
  const dropdownRef = (0, import_react14.useRef)(null);
2866
+ const inputElRef = (0, import_react14.useRef)(null);
2863
2867
  const filteredOptions = (0, import_react14.useMemo)(() => {
2864
2868
  if (!options.length) {
2865
2869
  return [];
@@ -2867,24 +2871,35 @@ var Search = (0, import_react14.forwardRef)(
2867
2871
  const filtered = filterOptions(options, value || "");
2868
2872
  return filtered;
2869
2873
  }, [options, value]);
2870
- const showDropdown = controlledShowDropdown ?? (dropdownOpen && value && String(value).length > 0);
2874
+ const showDropdown = !forceClose && (controlledShowDropdown ?? (dropdownOpen && value && String(value).length > 0));
2875
+ const setOpenAndNotify = (open) => {
2876
+ setDropdownOpen(open);
2877
+ dropdownStore.setState({ open });
2878
+ onDropdownChange?.(open);
2879
+ };
2871
2880
  (0, import_react14.useEffect)(() => {
2881
+ if (justSelectedRef.current) {
2882
+ justSelectedRef.current = false;
2883
+ return;
2884
+ }
2885
+ if (forceClose) {
2886
+ setOpenAndNotify(false);
2887
+ return;
2888
+ }
2872
2889
  const shouldShow = Boolean(value && String(value).length > 0);
2873
- setDropdownOpen(shouldShow);
2874
- dropdownStore.setState({ open: shouldShow });
2875
- onDropdownChange?.(shouldShow);
2876
- }, [value, onDropdownChange, dropdownStore]);
2890
+ setOpenAndNotify(shouldShow);
2891
+ }, [value, forceClose, onDropdownChange, dropdownStore]);
2877
2892
  const handleSelectOption = (option) => {
2893
+ justSelectedRef.current = true;
2894
+ setForceClose(true);
2878
2895
  onSelect?.(option);
2879
- setDropdownOpen(false);
2880
- dropdownStore.setState({ open: false });
2896
+ setOpenAndNotify(false);
2881
2897
  updateInputValue(option, ref, onChange);
2882
2898
  };
2883
2899
  (0, import_react14.useEffect)(() => {
2884
2900
  const handleClickOutside = (event) => {
2885
2901
  if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
2886
- setDropdownOpen(false);
2887
- dropdownStore.setState({ open: false });
2902
+ setOpenAndNotify(false);
2888
2903
  }
2889
2904
  };
2890
2905
  if (showDropdown) {
@@ -2893,9 +2908,10 @@ var Search = (0, import_react14.forwardRef)(
2893
2908
  return () => {
2894
2909
  document.removeEventListener("mousedown", handleClickOutside);
2895
2910
  };
2896
- }, [showDropdown, dropdownStore]);
2911
+ }, [showDropdown, dropdownStore, onDropdownChange]);
2897
2912
  const generatedId = (0, import_react14.useId)();
2898
2913
  const inputId = id ?? `search-${generatedId}`;
2914
+ const dropdownId = `${inputId}-dropdown`;
2899
2915
  const handleClear = () => {
2900
2916
  if (onClear) {
2901
2917
  onClear();
@@ -2908,21 +2924,40 @@ var Search = (0, import_react14.forwardRef)(
2908
2924
  e.stopPropagation();
2909
2925
  handleClear();
2910
2926
  };
2911
- const handleLeftIconClick = () => {
2912
- if (ref && "current" in ref && ref.current) {
2913
- ref.current.blur();
2914
- }
2927
+ const handleSearchIconClick = (e) => {
2928
+ e.preventDefault();
2929
+ e.stopPropagation();
2930
+ setTimeout(() => {
2931
+ inputElRef.current?.focus();
2932
+ }, 0);
2915
2933
  };
2916
2934
  const handleInputChange = (e) => {
2935
+ setForceClose(false);
2917
2936
  onChange?.(e);
2918
2937
  onSearch?.(e.target.value);
2919
2938
  };
2939
+ const handleKeyDown = (e) => {
2940
+ userOnKeyDown?.(e);
2941
+ if (e.defaultPrevented) return;
2942
+ if (e.key === "Enter") {
2943
+ e.preventDefault();
2944
+ if (showDropdown && filteredOptions.length > 0) {
2945
+ handleSelectOption(filteredOptions[0]);
2946
+ } else if (value) {
2947
+ onSearch?.(String(value));
2948
+ setForceClose(true);
2949
+ setOpenAndNotify(false);
2950
+ }
2951
+ }
2952
+ };
2920
2953
  const getInputStateClasses = (disabled2, readOnly2) => {
2921
2954
  if (disabled2) return "cursor-not-allowed opacity-40";
2922
2955
  if (readOnly2) return "cursor-default focus:outline-none !text-text-900";
2923
2956
  return "hover:border-border-400";
2924
2957
  };
2925
- const showClearButton = value && !disabled && !readOnly;
2958
+ const hasValue = String(value ?? "").length > 0;
2959
+ const showClearButton = hasValue && !disabled && !readOnly;
2960
+ const showSearchIcon = !hasValue && !disabled && !readOnly;
2926
2961
  return /* @__PURE__ */ (0, import_jsx_runtime21.jsxs)(
2927
2962
  "div",
2928
2963
  {
@@ -2930,30 +2965,30 @@ var Search = (0, import_react14.forwardRef)(
2930
2965
  className: `w-full max-w-lg md:w-[488px] ${containerClassName}`,
2931
2966
  children: [
2932
2967
  /* @__PURE__ */ (0, import_jsx_runtime21.jsxs)("div", { className: "relative flex items-center", children: [
2933
- /* @__PURE__ */ (0, import_jsx_runtime21.jsx)("div", { className: "absolute left-3 top-1/2 transform -translate-y-1/2", children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
2934
- "button",
2935
- {
2936
- type: "button",
2937
- className: "w-6 h-6 text-text-800 flex items-center justify-center bg-transparent border-0 p-0 cursor-pointer hover:text-text-600 transition-colors",
2938
- onClick: handleLeftIconClick,
2939
- "aria-label": "Voltar",
2940
- children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(import_phosphor_react10.CaretLeft, {})
2941
- }
2942
- ) }),
2943
2968
  /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
2944
2969
  "input",
2945
2970
  {
2946
- ref,
2971
+ ref: (node) => {
2972
+ if (ref) {
2973
+ if (typeof ref === "function") ref(node);
2974
+ else
2975
+ ref.current = node;
2976
+ }
2977
+ inputElRef.current = node;
2978
+ },
2947
2979
  id: inputId,
2948
2980
  type: "text",
2949
- className: `w-full py-0 px-4 pl-10 ${showClearButton ? "pr-10" : "pr-4"} font-normal text-text-900 focus:outline-primary-950 border rounded-full bg-background focus:bg-primary-50 border-border-300 focus:border-2 focus:border-primary-950 h-10 placeholder:text-text-600 ${getInputStateClasses(disabled, readOnly)} ${className}`,
2981
+ className: `w-full py-0 px-4 pr-10 font-normal text-text-900 focus:outline-primary-950 border rounded-full bg-background focus:bg-primary-50 border-border-300 focus:border-2 focus:border-primary-950 h-10 placeholder:text-text-600 ${getInputStateClasses(disabled, readOnly)} ${className}`,
2950
2982
  value,
2951
2983
  onChange: handleInputChange,
2984
+ onKeyDown: handleKeyDown,
2952
2985
  disabled,
2953
2986
  readOnly,
2954
2987
  placeholder,
2955
2988
  "aria-expanded": showDropdown ? "true" : void 0,
2956
2989
  "aria-haspopup": options.length > 0 ? "listbox" : void 0,
2990
+ "aria-controls": showDropdown ? dropdownId : void 0,
2991
+ "aria-autocomplete": "list",
2957
2992
  role: options.length > 0 ? "combobox" : void 0,
2958
2993
  ...props
2959
2994
  }
@@ -2967,11 +3002,22 @@ var Search = (0, import_react14.forwardRef)(
2967
3002
  "aria-label": "Limpar busca",
2968
3003
  children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)("span", { className: "w-6 h-6 text-text-800 flex items-center justify-center hover:text-text-600 transition-colors", children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(import_phosphor_react10.X, {}) })
2969
3004
  }
3005
+ ) }),
3006
+ showSearchIcon && /* @__PURE__ */ (0, import_jsx_runtime21.jsx)("div", { className: "absolute right-3 top-1/2 transform -translate-y-1/2", children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
3007
+ "button",
3008
+ {
3009
+ type: "button",
3010
+ className: "p-0 border-0 bg-transparent cursor-pointer",
3011
+ onMouseDown: handleSearchIconClick,
3012
+ "aria-label": "Buscar",
3013
+ children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)("span", { className: "w-6 h-6 text-text-800 flex items-center justify-center hover:text-text-600 transition-colors", children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(import_phosphor_react10.MagnifyingGlass, {}) })
3014
+ }
2970
3015
  ) })
2971
3016
  ] }),
2972
3017
  showDropdown && /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(DropdownMenu_default, { open: showDropdown, onOpenChange: setDropdownOpen, children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
2973
3018
  DropdownMenuContent,
2974
3019
  {
3020
+ id: dropdownId,
2975
3021
  className: "w-full mt-1",
2976
3022
  style: { maxHeight: dropdownMaxHeight },
2977
3023
  align: "start",