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.
@@ -1,5 +1,5 @@
1
1
  // src/components/Search/Search.tsx
2
- import { CaretLeft, X as X2 } from "phosphor-react";
2
+ import { X as X2, MagnifyingGlass } from "phosphor-react";
3
3
  import {
4
4
  forwardRef as forwardRef3,
5
5
  useState as useState3,
@@ -1086,11 +1086,15 @@ var Search = forwardRef3(
1086
1086
  value,
1087
1087
  onChange,
1088
1088
  placeholder = "Buscar...",
1089
+ onKeyDown: userOnKeyDown,
1089
1090
  ...props
1090
1091
  }, ref) => {
1091
1092
  const [dropdownOpen, setDropdownOpen] = useState3(false);
1093
+ const [forceClose, setForceClose] = useState3(false);
1094
+ const justSelectedRef = useRef2(false);
1092
1095
  const dropdownStore = useRef2(createDropdownStore()).current;
1093
1096
  const dropdownRef = useRef2(null);
1097
+ const inputElRef = useRef2(null);
1094
1098
  const filteredOptions = useMemo(() => {
1095
1099
  if (!options.length) {
1096
1100
  return [];
@@ -1098,24 +1102,35 @@ var Search = forwardRef3(
1098
1102
  const filtered = filterOptions(options, value || "");
1099
1103
  return filtered;
1100
1104
  }, [options, value]);
1101
- const showDropdown = controlledShowDropdown ?? (dropdownOpen && value && String(value).length > 0);
1105
+ const showDropdown = !forceClose && (controlledShowDropdown ?? (dropdownOpen && value && String(value).length > 0));
1106
+ const setOpenAndNotify = (open) => {
1107
+ setDropdownOpen(open);
1108
+ dropdownStore.setState({ open });
1109
+ onDropdownChange?.(open);
1110
+ };
1102
1111
  useEffect5(() => {
1112
+ if (justSelectedRef.current) {
1113
+ justSelectedRef.current = false;
1114
+ return;
1115
+ }
1116
+ if (forceClose) {
1117
+ setOpenAndNotify(false);
1118
+ return;
1119
+ }
1103
1120
  const shouldShow = Boolean(value && String(value).length > 0);
1104
- setDropdownOpen(shouldShow);
1105
- dropdownStore.setState({ open: shouldShow });
1106
- onDropdownChange?.(shouldShow);
1107
- }, [value, onDropdownChange, dropdownStore]);
1121
+ setOpenAndNotify(shouldShow);
1122
+ }, [value, forceClose, onDropdownChange, dropdownStore]);
1108
1123
  const handleSelectOption = (option) => {
1124
+ justSelectedRef.current = true;
1125
+ setForceClose(true);
1109
1126
  onSelect?.(option);
1110
- setDropdownOpen(false);
1111
- dropdownStore.setState({ open: false });
1127
+ setOpenAndNotify(false);
1112
1128
  updateInputValue(option, ref, onChange);
1113
1129
  };
1114
1130
  useEffect5(() => {
1115
1131
  const handleClickOutside = (event) => {
1116
1132
  if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
1117
- setDropdownOpen(false);
1118
- dropdownStore.setState({ open: false });
1133
+ setOpenAndNotify(false);
1119
1134
  }
1120
1135
  };
1121
1136
  if (showDropdown) {
@@ -1124,9 +1139,10 @@ var Search = forwardRef3(
1124
1139
  return () => {
1125
1140
  document.removeEventListener("mousedown", handleClickOutside);
1126
1141
  };
1127
- }, [showDropdown, dropdownStore]);
1142
+ }, [showDropdown, dropdownStore, onDropdownChange]);
1128
1143
  const generatedId = useId2();
1129
1144
  const inputId = id ?? `search-${generatedId}`;
1145
+ const dropdownId = `${inputId}-dropdown`;
1130
1146
  const handleClear = () => {
1131
1147
  if (onClear) {
1132
1148
  onClear();
@@ -1139,21 +1155,40 @@ var Search = forwardRef3(
1139
1155
  e.stopPropagation();
1140
1156
  handleClear();
1141
1157
  };
1142
- const handleLeftIconClick = () => {
1143
- if (ref && "current" in ref && ref.current) {
1144
- ref.current.blur();
1145
- }
1158
+ const handleSearchIconClick = (e) => {
1159
+ e.preventDefault();
1160
+ e.stopPropagation();
1161
+ setTimeout(() => {
1162
+ inputElRef.current?.focus();
1163
+ }, 0);
1146
1164
  };
1147
1165
  const handleInputChange = (e) => {
1166
+ setForceClose(false);
1148
1167
  onChange?.(e);
1149
1168
  onSearch?.(e.target.value);
1150
1169
  };
1170
+ const handleKeyDown = (e) => {
1171
+ userOnKeyDown?.(e);
1172
+ if (e.defaultPrevented) return;
1173
+ if (e.key === "Enter") {
1174
+ e.preventDefault();
1175
+ if (showDropdown && filteredOptions.length > 0) {
1176
+ handleSelectOption(filteredOptions[0]);
1177
+ } else if (value) {
1178
+ onSearch?.(String(value));
1179
+ setForceClose(true);
1180
+ setOpenAndNotify(false);
1181
+ }
1182
+ }
1183
+ };
1151
1184
  const getInputStateClasses = (disabled2, readOnly2) => {
1152
1185
  if (disabled2) return "cursor-not-allowed opacity-40";
1153
1186
  if (readOnly2) return "cursor-default focus:outline-none !text-text-900";
1154
1187
  return "hover:border-border-400";
1155
1188
  };
1156
- const showClearButton = value && !disabled && !readOnly;
1189
+ const hasValue = String(value ?? "").length > 0;
1190
+ const showClearButton = hasValue && !disabled && !readOnly;
1191
+ const showSearchIcon = !hasValue && !disabled && !readOnly;
1157
1192
  return /* @__PURE__ */ jsxs5(
1158
1193
  "div",
1159
1194
  {
@@ -1161,30 +1196,30 @@ var Search = forwardRef3(
1161
1196
  className: `w-full max-w-lg md:w-[488px] ${containerClassName}`,
1162
1197
  children: [
1163
1198
  /* @__PURE__ */ jsxs5("div", { className: "relative flex items-center", children: [
1164
- /* @__PURE__ */ jsx7("div", { className: "absolute left-3 top-1/2 transform -translate-y-1/2", children: /* @__PURE__ */ jsx7(
1165
- "button",
1166
- {
1167
- type: "button",
1168
- 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",
1169
- onClick: handleLeftIconClick,
1170
- "aria-label": "Voltar",
1171
- children: /* @__PURE__ */ jsx7(CaretLeft, {})
1172
- }
1173
- ) }),
1174
1199
  /* @__PURE__ */ jsx7(
1175
1200
  "input",
1176
1201
  {
1177
- ref,
1202
+ ref: (node) => {
1203
+ if (ref) {
1204
+ if (typeof ref === "function") ref(node);
1205
+ else
1206
+ ref.current = node;
1207
+ }
1208
+ inputElRef.current = node;
1209
+ },
1178
1210
  id: inputId,
1179
1211
  type: "text",
1180
- 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}`,
1212
+ 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}`,
1181
1213
  value,
1182
1214
  onChange: handleInputChange,
1215
+ onKeyDown: handleKeyDown,
1183
1216
  disabled,
1184
1217
  readOnly,
1185
1218
  placeholder,
1186
1219
  "aria-expanded": showDropdown ? "true" : void 0,
1187
1220
  "aria-haspopup": options.length > 0 ? "listbox" : void 0,
1221
+ "aria-controls": showDropdown ? dropdownId : void 0,
1222
+ "aria-autocomplete": "list",
1188
1223
  role: options.length > 0 ? "combobox" : void 0,
1189
1224
  ...props
1190
1225
  }
@@ -1198,11 +1233,22 @@ var Search = forwardRef3(
1198
1233
  "aria-label": "Limpar busca",
1199
1234
  children: /* @__PURE__ */ jsx7("span", { className: "w-6 h-6 text-text-800 flex items-center justify-center hover:text-text-600 transition-colors", children: /* @__PURE__ */ jsx7(X2, {}) })
1200
1235
  }
1236
+ ) }),
1237
+ showSearchIcon && /* @__PURE__ */ jsx7("div", { className: "absolute right-3 top-1/2 transform -translate-y-1/2", children: /* @__PURE__ */ jsx7(
1238
+ "button",
1239
+ {
1240
+ type: "button",
1241
+ className: "p-0 border-0 bg-transparent cursor-pointer",
1242
+ onMouseDown: handleSearchIconClick,
1243
+ "aria-label": "Buscar",
1244
+ children: /* @__PURE__ */ jsx7("span", { className: "w-6 h-6 text-text-800 flex items-center justify-center hover:text-text-600 transition-colors", children: /* @__PURE__ */ jsx7(MagnifyingGlass, {}) })
1245
+ }
1201
1246
  ) })
1202
1247
  ] }),
1203
1248
  showDropdown && /* @__PURE__ */ jsx7(DropdownMenu_default, { open: showDropdown, onOpenChange: setDropdownOpen, children: /* @__PURE__ */ jsx7(
1204
1249
  DropdownMenuContent,
1205
1250
  {
1251
+ id: dropdownId,
1206
1252
  className: "w-full mt-1",
1207
1253
  style: { maxHeight: dropdownMaxHeight },
1208
1254
  align: "start",