@adam-milo/ui 1.0.19 → 1.0.21

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/index14.js CHANGED
@@ -1,186 +1,297 @@
1
- import { jsxs as O, jsx as b } from "react/jsx-runtime";
2
- import { forwardRef as B, useRef as _, useState as H, useCallback as I, useEffect as k } from "react";
3
- import { cn as x } from "./index17.js";
4
- import { Chip as G } from "./index13.js";
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { forwardRef, useRef, useState, useCallback, useEffect } from "react";
3
+ import { cn } from "./index17.js";
4
+ import { Chip } from "./index13.js";
5
5
  /* empty css */
6
- import { Icon as J } from "@adam-milo/icons";
7
- const Q = (t, c, n) => {
8
- if (!t) return 0.5 * c;
9
- const l = t.match(/^([\d.]+)(px|rem|em)?/);
10
- if (!l) return 0.5 * c;
11
- const s = parseFloat(l[1]);
12
- switch (l[2] || "px") {
6
+ import { devWarn } from "./index31.js";
7
+ import { Icon } from "@adam-milo/icons";
8
+ const parseGapValue = (gapString, rootFontSize, containerFontSize) => {
9
+ if (!gapString) return 0.5 * rootFontSize;
10
+ const match = gapString.match(/^([\d.]+)(px|rem|em)?/);
11
+ if (!match) return 0.5 * rootFontSize;
12
+ const value = parseFloat(match[1]);
13
+ const unit = match[2] || "px";
14
+ switch (unit) {
13
15
  case "px":
14
- return s;
16
+ return value;
15
17
  case "rem":
16
- return s * c;
18
+ return value * rootFontSize;
17
19
  case "em":
18
- return s * n;
20
+ return value * containerFontSize;
19
21
  default:
20
- return 0.5 * c;
22
+ return 0.5 * rootFontSize;
21
23
  }
22
- }, X = (t, c, n, l, s) => {
23
- let w = 0, a = 0;
24
- for (let i = 0; i < t.length; i++) {
25
- const D = t[i], E = c.get(D.value) || 0;
26
- if (E === 0) {
27
- a++;
24
+ };
25
+ const calculateFittingChips = (items, chipWidths, containerWidth, gap, overflowChipWidth) => {
26
+ let totalWidth = 0;
27
+ let count = 0;
28
+ for (let i = 0; i < items.length; i++) {
29
+ const item = items[i];
30
+ const chipWidth = chipWidths.get(item.value) || 0;
31
+ if (chipWidth === 0) {
32
+ count++;
28
33
  break;
29
34
  }
30
- const $ = i > 0 ? l : 0, C = w + $ + E;
31
- if (i < t.length - 1) {
32
- if (C + l + s > n && a > 0) break;
33
- } else if (C > n && a > 0) break;
34
- w = C, a++;
35
+ const gapBeforeThisChip = i > 0 ? gap : 0;
36
+ const widthWithThisChip = totalWidth + gapBeforeThisChip + chipWidth;
37
+ const hasMoreItems = i < items.length - 1;
38
+ if (hasMoreItems) {
39
+ const widthWithOverflow = widthWithThisChip + gap + overflowChipWidth;
40
+ if (widthWithOverflow > containerWidth && count > 0) break;
41
+ } else {
42
+ if (widthWithThisChip > containerWidth && count > 0) break;
43
+ }
44
+ totalWidth = widthWithThisChip;
45
+ count++;
35
46
  }
36
- return Math.max(1, a);
37
- }, Y = (t, c) => {
38
- const n = _();
39
- return k(() => () => {
40
- n.current && clearTimeout(n.current);
41
- }, []), I(
42
- (...s) => {
43
- n.current && clearTimeout(n.current), n.current = setTimeout(() => t(...s), c);
47
+ return Math.max(1, count);
48
+ };
49
+ const useDebounce = (callback, delay) => {
50
+ const timeoutRef = useRef();
51
+ useEffect(() => {
52
+ return () => {
53
+ if (timeoutRef.current) {
54
+ clearTimeout(timeoutRef.current);
55
+ }
56
+ };
57
+ }, []);
58
+ const debouncedFn = useCallback(
59
+ (...args) => {
60
+ if (timeoutRef.current) clearTimeout(timeoutRef.current);
61
+ timeoutRef.current = setTimeout(() => callback(...args), delay);
44
62
  },
45
- [t, c]
63
+ [callback, delay]
46
64
  );
47
- }, Z = (t, c, n, l, s) => {
48
- const [w, a] = H(t.length), i = I(() => {
49
- if (s !== void 0) {
50
- a(Math.min(s, t.length));
65
+ return debouncedFn;
66
+ };
67
+ const useChipOverflow = (items, containerRef, chipRefs, chipWidths, maxVisible) => {
68
+ const [visibleCount, setVisibleCount] = useState(items.length);
69
+ const calculateVisibleChips = useCallback(() => {
70
+ if (maxVisible !== void 0) {
71
+ setVisibleCount(Math.min(maxVisible, items.length));
51
72
  return;
52
73
  }
53
- const f = c.current;
54
- if (!f) return;
55
- const m = f.clientWidth, p = parseFloat(getComputedStyle(document.documentElement).fontSize) || 16, T = window.getComputedStyle(f), M = T.gap, W = parseFloat(T.fontSize) || 16, S = Q(M, p, W), N = 3.75 * p;
56
- for (let v = 0; v < t.length; v++) {
57
- const g = t[v], A = n.current[v];
58
- A && l.current.set(g.value, A.offsetWidth);
74
+ const container = containerRef.current;
75
+ if (!container) return;
76
+ const containerWidth = container.clientWidth;
77
+ const rootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize) || 16;
78
+ const computedStyle = window.getComputedStyle(container);
79
+ const gapString = computedStyle.gap;
80
+ const containerFontSize = parseFloat(computedStyle.fontSize) || 16;
81
+ const gap = parseGapValue(gapString, rootFontSize, containerFontSize);
82
+ const OVERFLOW_CHIP_WIDTH_REM = 3.75;
83
+ const overflowChipWidth = OVERFLOW_CHIP_WIDTH_REM * rootFontSize;
84
+ for (let i = 0; i < items.length; i++) {
85
+ const item = items[i];
86
+ const chip = chipRefs.current[i];
87
+ if (chip) {
88
+ chipWidths.current.set(item.value, chip.offsetWidth);
89
+ }
59
90
  }
60
- const y = X(
61
- t,
62
- l.current,
63
- m,
64
- S,
65
- N
91
+ const count = calculateFittingChips(
92
+ items,
93
+ chipWidths.current,
94
+ containerWidth,
95
+ gap,
96
+ overflowChipWidth
66
97
  );
67
- a(y);
68
- }, [t, s, c, n, l]), D = Y(i, 100);
69
- k(() => {
70
- n.current.length > t.length && (n.current = n.current.slice(0, t.length));
71
- const f = new Set(t.map((m) => m.value));
72
- l.current.forEach((m, p) => {
73
- f.has(p) || l.current.delete(p);
74
- }), s === void 0 && (t.some((p) => !l.current.has(p.value)) ? a(t.length) : setTimeout(i, 0));
75
- }, [t, s, i, n, l]), k(() => {
76
- const f = c.current;
77
- if (!f) return;
78
- const m = new ResizeObserver(() => {
79
- D();
98
+ setVisibleCount(count);
99
+ }, [items, maxVisible, containerRef, chipRefs, chipWidths]);
100
+ const debouncedCalculate = useDebounce(calculateVisibleChips, 100);
101
+ useEffect(() => {
102
+ if (chipRefs.current.length > items.length) {
103
+ chipRefs.current = chipRefs.current.slice(0, items.length);
104
+ }
105
+ const currentValues = new Set(items.map((item) => item.value));
106
+ chipWidths.current.forEach((_, value) => {
107
+ if (!currentValues.has(value)) {
108
+ chipWidths.current.delete(value);
109
+ }
80
110
  });
81
- return m.observe(f), setTimeout(i, 0), () => {
82
- m.disconnect();
111
+ if (maxVisible === void 0) {
112
+ const hasUnmeasuredItems = items.some((item) => !chipWidths.current.has(item.value));
113
+ if (hasUnmeasuredItems) {
114
+ setVisibleCount(items.length);
115
+ } else {
116
+ setTimeout(calculateVisibleChips, 0);
117
+ }
118
+ }
119
+ }, [items, maxVisible, calculateVisibleChips, chipRefs, chipWidths]);
120
+ useEffect(() => {
121
+ const container = containerRef.current;
122
+ if (!container) return;
123
+ const resizeObserver = new ResizeObserver(() => {
124
+ debouncedCalculate();
125
+ });
126
+ resizeObserver.observe(container);
127
+ setTimeout(calculateVisibleChips, 0);
128
+ return () => {
129
+ resizeObserver.disconnect();
83
130
  };
84
- }, [i, D, c]);
85
- const E = t.slice(0, w), $ = t.slice(w), C = $.length > 0;
86
- return { visibleCount: w, visibleItems: E, hiddenItems: $, hasOverflow: C };
87
- }, ee = B(
131
+ }, [calculateVisibleChips, debouncedCalculate, containerRef]);
132
+ const visibleItems = items.slice(0, visibleCount);
133
+ const hiddenItems = items.slice(visibleCount);
134
+ const hasOverflow = hiddenItems.length > 0;
135
+ return { visibleCount, visibleItems, hiddenItems, hasOverflow };
136
+ };
137
+ const ChipGroup = forwardRef(
88
138
  ({
89
- items: t,
90
- selectedValue: c,
91
- onSelectionChange: n,
92
- removable: l = !1,
93
- onRemove: s,
94
- variant: w = "primary",
95
- disabled: a = !1,
96
- maxVisible: i,
97
- className: D,
98
- "data-testid": E,
99
- "data-cy": $,
100
- ...C
101
- }, f) => {
102
- const p = (t && Array.isArray(t) ? t : []).filter((e) => e != null && typeof e == "object"), T = _(null), M = _([]), W = _(/* @__PURE__ */ new Map()), S = _(null), [K, N] = H(!1), [y, v] = H(!1), g = _([]), { visibleItems: A, hiddenItems: d, hasOverflow: V } = Z(
103
- p,
104
- T,
105
- M,
106
- W,
107
- i
108
- ), F = I(
109
- (e, o, r) => {
110
- a || o || n && n(c === e ? null : e, r);
139
+ items,
140
+ selectedValue,
141
+ onSelectionChange,
142
+ removable = false,
143
+ onRemove,
144
+ variant = "primary",
145
+ disabled = false,
146
+ maxVisible,
147
+ className,
148
+ "data-testid": dataTestId,
149
+ "data-cy": dataCy,
150
+ ...props
151
+ }, ref) => {
152
+ const normalizedItems = items && Array.isArray(items) ? items : [];
153
+ const validItems = normalizedItems.filter((item) => {
154
+ return item !== null && item !== void 0 && typeof item === "object";
155
+ });
156
+ const containerRef = useRef(null);
157
+ const chipRefs = useRef([]);
158
+ const chipWidths = useRef(/* @__PURE__ */ new Map());
159
+ const dropdownRef = useRef(null);
160
+ const [showTooltip, setShowTooltip] = useState(false);
161
+ const [showDropdown, setShowDropdown] = useState(false);
162
+ const dropdownItemRefs = useRef([]);
163
+ const { visibleItems, hiddenItems, hasOverflow } = useChipOverflow(
164
+ validItems,
165
+ containerRef,
166
+ chipRefs,
167
+ chipWidths,
168
+ maxVisible
169
+ );
170
+ const handleChipClick = useCallback(
171
+ (value, itemDisabled, event) => {
172
+ if (disabled || itemDisabled) return;
173
+ if (onSelectionChange) {
174
+ const newValue = selectedValue === value ? null : value;
175
+ onSelectionChange(newValue, event);
176
+ }
111
177
  },
112
- [a, c, n]
113
- ), j = I(
114
- (e, o) => {
115
- e.stopPropagation(), !a && s && s(o, e);
178
+ [disabled, selectedValue, onSelectionChange]
179
+ );
180
+ const handleRemove = useCallback(
181
+ (e, value) => {
182
+ e.stopPropagation();
183
+ if (disabled) return;
184
+ if (onRemove) {
185
+ onRemove(value, e);
186
+ }
116
187
  },
117
- [a, s]
118
- ), U = I(
119
- (e, o, r) => {
120
- a || r || (e.key === "Enter" || e.key === " ") && (e.preventDefault(), F(o, r, e));
188
+ [disabled, onRemove]
189
+ );
190
+ const handleKeyDown = useCallback(
191
+ (e, value, itemDisabled) => {
192
+ if (disabled || itemDisabled) return;
193
+ if (e.key === "Enter" || e.key === " ") {
194
+ e.preventDefault();
195
+ handleChipClick(value, itemDisabled, e);
196
+ }
121
197
  },
122
- [a, F]
123
- ), q = I((e) => {
124
- e.stopPropagation(), v((o) => !o), N(!1);
125
- }, []), R = I(
126
- (e, o) => {
127
- var r, u, z, h;
198
+ [disabled, handleChipClick]
199
+ );
200
+ const handleOverflowClick = useCallback((e) => {
201
+ e.stopPropagation();
202
+ setShowDropdown((prev) => !prev);
203
+ setShowTooltip(false);
204
+ }, []);
205
+ const handleDropdownKeyDown = useCallback(
206
+ (e, index) => {
207
+ var _a, _b, _c, _d;
128
208
  switch (e.key) {
129
209
  case "ArrowDown":
130
- e.preventDefault(), o < d.length - 1 && ((r = g.current[o + 1]) == null || r.focus());
210
+ e.preventDefault();
211
+ if (index < hiddenItems.length - 1) {
212
+ (_a = dropdownItemRefs.current[index + 1]) == null ? void 0 : _a.focus();
213
+ }
131
214
  break;
132
215
  case "ArrowUp":
133
- e.preventDefault(), o > 0 && ((u = g.current[o - 1]) == null || u.focus());
216
+ e.preventDefault();
217
+ if (index > 0) {
218
+ (_b = dropdownItemRefs.current[index - 1]) == null ? void 0 : _b.focus();
219
+ }
134
220
  break;
135
221
  case "Home":
136
- e.preventDefault(), (z = g.current[0]) == null || z.focus();
222
+ e.preventDefault();
223
+ (_c = dropdownItemRefs.current[0]) == null ? void 0 : _c.focus();
137
224
  break;
138
225
  case "End":
139
- e.preventDefault(), (h = g.current[d.length - 1]) == null || h.focus();
226
+ e.preventDefault();
227
+ (_d = dropdownItemRefs.current[hiddenItems.length - 1]) == null ? void 0 : _d.focus();
140
228
  break;
141
229
  }
142
230
  },
143
- [d.length]
144
- ), L = _(), P = _();
145
- return k(() => {
146
- L.current = (e) => {
147
- S.current && !S.current.contains(e.target) && v(!1);
148
- }, P.current = (e) => {
149
- e.key === "Escape" && v(!1);
231
+ [hiddenItems.length]
232
+ );
233
+ const handleClickOutsideRef = useRef();
234
+ const handleEscapeRef = useRef();
235
+ useEffect(() => {
236
+ handleClickOutsideRef.current = (event) => {
237
+ if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
238
+ setShowDropdown(false);
239
+ }
150
240
  };
151
- }), k(() => {
152
- if (!y) return;
153
- const e = (r) => {
154
- var u;
155
- return (u = L.current) == null ? void 0 : u.call(L, r);
156
- }, o = (r) => {
157
- var u;
158
- return (u = P.current) == null ? void 0 : u.call(P, r);
241
+ handleEscapeRef.current = (event) => {
242
+ if (event.key === "Escape") {
243
+ setShowDropdown(false);
244
+ }
245
+ };
246
+ });
247
+ useEffect(() => {
248
+ if (!showDropdown) return;
249
+ const handleClickOutside = (e) => {
250
+ var _a;
251
+ return (_a = handleClickOutsideRef.current) == null ? void 0 : _a.call(handleClickOutsideRef, e);
252
+ };
253
+ const handleEscape = (e) => {
254
+ var _a;
255
+ return (_a = handleEscapeRef.current) == null ? void 0 : _a.call(handleEscapeRef, e);
159
256
  };
160
- return document.addEventListener("mousedown", e), document.addEventListener("keydown", o), () => {
161
- document.removeEventListener("mousedown", e), document.removeEventListener("keydown", o);
257
+ document.addEventListener("mousedown", handleClickOutside);
258
+ document.addEventListener("keydown", handleEscape);
259
+ return () => {
260
+ document.removeEventListener("mousedown", handleClickOutside);
261
+ document.removeEventListener("keydown", handleEscape);
162
262
  };
163
- }, [y]), k(() => {
164
- if (process.env.NODE_ENV === "development" && p.length > 0) {
165
- const e = p.map((r) => r.value), o = new Set(e);
166
- e.length !== o.size && console.warn(
167
- "[ChipGroup] Duplicate values detected in items array. Each item should have a unique value."
168
- );
263
+ }, [showDropdown]);
264
+ useEffect(() => {
265
+ if (process.env.NODE_ENV === "development" && validItems.length > 0) {
266
+ const values = validItems.map((item) => item.value);
267
+ const uniqueValues = new Set(values);
268
+ if (values.length !== uniqueValues.size) {
269
+ devWarn(
270
+ "[ChipGroup] Duplicate values detected in items array. Each item should have a unique value."
271
+ );
272
+ }
273
+ }
274
+ }, [validItems]);
275
+ useEffect(() => {
276
+ if (dropdownItemRefs.current.length > hiddenItems.length) {
277
+ dropdownItemRefs.current = dropdownItemRefs.current.slice(0, hiddenItems.length);
169
278
  }
170
- }, [p]), k(() => {
171
- g.current.length > d.length && (g.current = g.current.slice(0, d.length));
172
- }, [d]), !t || !Array.isArray(t) ? null : /* @__PURE__ */ O(
279
+ }, [hiddenItems]);
280
+ if (!items || !Array.isArray(items)) {
281
+ return null;
282
+ }
283
+ return /* @__PURE__ */ jsxs(
173
284
  "div",
174
285
  {
175
- ref: f,
176
- className: x("chip-group", a && "chip-group--disabled", D),
286
+ ref,
287
+ className: cn("chip-group", disabled && "chip-group--disabled", className),
177
288
  role: "group",
178
289
  "aria-label": "Chip group",
179
- "data-testid": E || "chip-group",
180
- "data-cy": $ || "chip-group",
181
- ...C,
290
+ "data-testid": dataTestId || "chip-group",
291
+ "data-cy": dataCy || "chip-group",
292
+ ...props,
182
293
  children: [
183
- V && /* @__PURE__ */ O(
294
+ hasOverflow && /* @__PURE__ */ jsxs(
184
295
  "div",
185
296
  {
186
297
  className: "sr-only",
@@ -190,141 +301,169 @@ const Q = (t, c, n) => {
190
301
  "data-testid": "chip-group-live-region",
191
302
  "data-cy": "chip-group-live-region",
192
303
  children: [
193
- d.length,
304
+ hiddenItems.length,
194
305
  " additional ",
195
- d.length === 1 ? "chip" : "chips",
306
+ hiddenItems.length === 1 ? "chip" : "chips",
196
307
  " hidden"
197
308
  ]
198
309
  }
199
310
  ),
200
- /* @__PURE__ */ O("div", { ref: T, className: "chip-group__container", children: [
201
- A.map((e) => {
202
- const o = c === e.value, r = e.variant || w, u = a || e.disabled || !1, z = p.findIndex((h) => h.value === e.value);
203
- return /* @__PURE__ */ b(
311
+ /* @__PURE__ */ jsxs("div", { ref: containerRef, className: "chip-group__container", children: [
312
+ visibleItems.map((item) => {
313
+ const isSelected = selectedValue === item.value;
314
+ const itemVariant = item.variant || variant;
315
+ const itemDisabled = disabled || item.disabled || false;
316
+ const itemIndexInFullArray = validItems.findIndex((i) => i.value === item.value);
317
+ return /* @__PURE__ */ jsx(
204
318
  "div",
205
319
  {
206
- ref: (h) => {
207
- h ? M.current[z] = h : M.current[z] = null;
320
+ ref: (el) => {
321
+ if (el) {
322
+ chipRefs.current[itemIndexInFullArray] = el;
323
+ } else {
324
+ chipRefs.current[itemIndexInFullArray] = null;
325
+ }
208
326
  },
209
327
  className: "chip-group__item-wrapper",
210
- "data-cy": `chip-group-item-wrapper-${e.value}`,
211
- "data-testid": `chip-group-item-wrapper-${e.value}`,
212
- children: /* @__PURE__ */ O(
328
+ "data-cy": `chip-group-item-wrapper-${item.value}`,
329
+ "data-testid": `chip-group-item-wrapper-${item.value}`,
330
+ children: /* @__PURE__ */ jsxs(
213
331
  "div",
214
332
  {
215
333
  className: "chip-group__item",
216
- onClick: (h) => F(e.value, u, h),
217
- onKeyDown: (h) => U(h, e.value, u),
218
- tabIndex: u ? -1 : 0,
334
+ onClick: (e) => handleChipClick(item.value, itemDisabled, e),
335
+ onKeyDown: (e) => handleKeyDown(e, item.value, itemDisabled),
336
+ tabIndex: itemDisabled ? -1 : 0,
219
337
  role: "button",
220
- "aria-pressed": o ? "true" : "false",
221
- "aria-disabled": u ? "true" : "false",
222
- "data-cy": `chip-group-item-${e.value}`,
223
- "data-testid": `chip-group-item-${e.value}`,
338
+ "aria-pressed": isSelected ? "true" : "false",
339
+ "aria-disabled": itemDisabled ? "true" : "false",
340
+ "data-cy": `chip-group-item-${item.value}`,
341
+ "data-testid": `chip-group-item-${item.value}`,
224
342
  children: [
225
- /* @__PURE__ */ b(
226
- G,
343
+ /* @__PURE__ */ jsx(
344
+ Chip,
227
345
  {
228
- variant: r,
229
- selected: o,
230
- disabled: u,
231
- "data-cy": `chip-${e.value}`,
232
- "data-testid": `chip-${e.value}`,
233
- children: e.label
346
+ variant: itemVariant,
347
+ selected: isSelected,
348
+ disabled: itemDisabled,
349
+ "data-cy": `chip-${item.value}`,
350
+ "data-testid": `chip-${item.value}`,
351
+ children: item.label
234
352
  }
235
353
  ),
236
- l && !a && !u && /* @__PURE__ */ b(
354
+ removable && !disabled && !itemDisabled && /* @__PURE__ */ jsx(
237
355
  "button",
238
356
  {
239
357
  className: "chip-group__remove",
240
- onClick: (h) => j(h, e.value),
241
- "aria-label": `Remove ${e.label}`,
242
- "data-cy": `chip-remove-${e.value}`,
243
- "data-testid": `chip-remove-${e.value}`,
358
+ onClick: (e) => handleRemove(e, item.value),
359
+ "aria-label": `Remove ${item.label}`,
360
+ "data-cy": `chip-remove-${item.value}`,
361
+ "data-testid": `chip-remove-${item.value}`,
244
362
  type: "button",
245
- children: /* @__PURE__ */ b(J, { name: "Cross2Icon", size: "sm", decorative: !0 })
363
+ children: /* @__PURE__ */ jsx(Icon, { name: "Cross2Icon", size: "sm", decorative: true })
246
364
  }
247
365
  )
248
366
  ]
249
367
  }
250
368
  )
251
369
  },
252
- e.value
370
+ item.value
253
371
  );
254
372
  }),
255
- V && /* @__PURE__ */ O(
373
+ hasOverflow && /* @__PURE__ */ jsxs(
256
374
  "div",
257
375
  {
258
- ref: S,
376
+ ref: dropdownRef,
259
377
  className: "chip-group__overflow",
260
378
  role: "button",
261
379
  tabIndex: 0,
262
- "aria-expanded": y ? "true" : "false",
380
+ "aria-expanded": showDropdown ? "true" : "false",
263
381
  "aria-haspopup": "menu",
264
- "aria-label": `Show ${d.length} more ${d.length === 1 ? "chip" : "chips"}`,
265
- onMouseEnter: () => !y && N(!0),
266
- onMouseLeave: () => N(!1),
267
- onClick: q,
382
+ "aria-label": `Show ${hiddenItems.length} more ${hiddenItems.length === 1 ? "chip" : "chips"}`,
383
+ onMouseEnter: () => !showDropdown && setShowTooltip(true),
384
+ onMouseLeave: () => setShowTooltip(false),
385
+ onClick: handleOverflowClick,
268
386
  onKeyDown: (e) => {
269
- (e.key === "Enter" || e.key === " ") && (e.preventDefault(), e.stopPropagation(), v(!y), N(!1));
387
+ if (e.key === "Enter" || e.key === " ") {
388
+ e.preventDefault();
389
+ e.stopPropagation();
390
+ setShowDropdown(!showDropdown);
391
+ setShowTooltip(false);
392
+ }
270
393
  },
271
394
  "data-cy": "chip-group-overflow",
272
395
  "data-testid": "chip-group-overflow",
273
396
  children: [
274
- /* @__PURE__ */ O(G, { variant: w, "data-cy": "chip-overflow", "data-testid": "chip-overflow", children: [
397
+ /* @__PURE__ */ jsxs(Chip, { variant, "data-cy": "chip-overflow", "data-testid": "chip-overflow", children: [
275
398
  "+",
276
- d.length
399
+ hiddenItems.length
277
400
  ] }),
278
- K && !y && /* @__PURE__ */ b(
401
+ showTooltip && !showDropdown && /* @__PURE__ */ jsx(
279
402
  "div",
280
403
  {
281
404
  className: "chip-group__tooltip chip-group__tooltip--hover",
282
405
  role: "tooltip",
283
406
  "data-cy": "chip-group-tooltip",
284
407
  "data-testid": "chip-group-tooltip",
285
- children: d.map((e) => /* @__PURE__ */ b(
408
+ children: hiddenItems.map((item) => /* @__PURE__ */ jsx(
286
409
  "div",
287
410
  {
288
411
  className: "chip-group__tooltip-item",
289
- "data-cy": `chip-group-tooltip-item-${e.value}`,
290
- "data-testid": `chip-group-tooltip-item-${e.value}`,
291
- children: e.label
412
+ "data-cy": `chip-group-tooltip-item-${item.value}`,
413
+ "data-testid": `chip-group-tooltip-item-${item.value}`,
414
+ children: item.label
292
415
  },
293
- e.value
416
+ item.value
294
417
  ))
295
418
  }
296
419
  ),
297
- y && /* @__PURE__ */ b(
420
+ showDropdown && /* @__PURE__ */ jsx(
298
421
  "div",
299
422
  {
300
423
  className: "chip-group__dropdown",
301
424
  role: "menu",
302
425
  "data-cy": "chip-group-dropdown",
303
426
  "data-testid": "chip-group-dropdown",
304
- children: d.map((e, o) => /* @__PURE__ */ b(
427
+ children: hiddenItems.map((item, index) => /* @__PURE__ */ jsx(
305
428
  "div",
306
429
  {
307
- ref: (r) => {
308
- r ? g.current[o] = r : g.current[o] = null;
430
+ ref: (el) => {
431
+ if (el) {
432
+ dropdownItemRefs.current[index] = el;
433
+ } else {
434
+ dropdownItemRefs.current[index] = null;
435
+ }
309
436
  },
310
- className: x(
437
+ className: cn(
311
438
  "chip-group__dropdown-item",
312
- e.disabled && "chip-group__dropdown-item--disabled"
439
+ item.disabled && "chip-group__dropdown-item--disabled"
313
440
  ),
314
441
  role: "menuitem",
315
- tabIndex: e.disabled ? -1 : 0,
316
- "aria-disabled": e.disabled ? "true" : "false",
317
- onClick: (r) => {
318
- r.stopPropagation(), e.disabled || (F(e.value, !1, r), v(!1));
442
+ tabIndex: item.disabled ? -1 : 0,
443
+ "aria-disabled": item.disabled ? "true" : "false",
444
+ onClick: (e) => {
445
+ e.stopPropagation();
446
+ if (!item.disabled) {
447
+ handleChipClick(item.value, false, e);
448
+ setShowDropdown(false);
449
+ }
319
450
  },
320
- onKeyDown: (r) => {
321
- R(r, o), (r.key === "Enter" || r.key === " ") && (r.preventDefault(), r.stopPropagation(), e.disabled || (F(e.value, !1, r), v(!1)));
451
+ onKeyDown: (e) => {
452
+ handleDropdownKeyDown(e, index);
453
+ if (e.key === "Enter" || e.key === " ") {
454
+ e.preventDefault();
455
+ e.stopPropagation();
456
+ if (!item.disabled) {
457
+ handleChipClick(item.value, false, e);
458
+ setShowDropdown(false);
459
+ }
460
+ }
322
461
  },
323
- "data-cy": `chip-group-dropdown-item-${e.value}`,
324
- "data-testid": `chip-group-dropdown-item-${e.value}`,
325
- children: e.label
462
+ "data-cy": `chip-group-dropdown-item-${item.value}`,
463
+ "data-testid": `chip-group-dropdown-item-${item.value}`,
464
+ children: item.label
326
465
  },
327
- e.value
466
+ item.value
328
467
  ))
329
468
  }
330
469
  )
@@ -337,7 +476,7 @@ const Q = (t, c, n) => {
337
476
  );
338
477
  }
339
478
  );
340
- ee.displayName = "ChipGroup";
479
+ ChipGroup.displayName = "ChipGroup";
341
480
  export {
342
- ee as ChipGroup
481
+ ChipGroup
343
482
  };