@acusti/dropdown 0.46.0 → 0.47.0

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/Dropdown.js CHANGED
@@ -1,487 +1,971 @@
1
- /* eslint-disable jsx-a11y/click-events-have-key-events, jsx-a11y/mouse-events-have-key-events, jsx-a11y/no-static-element-interactions */
2
- import { Style } from '@acusti/styling';
3
- import useIsOutOfBounds from '@acusti/use-is-out-of-bounds';
4
- import useKeyboardEvents, { isEventTargetUsingKeyEvent, } from '@acusti/use-keyboard-events';
5
- import clsx from 'clsx';
6
- import * as React from 'react';
7
- import { getActiveItemElement, getItemElements, ITEM_SELECTOR, setActiveItem, } from './helpers.js';
8
- import { BODY_CLASS_NAME, BODY_MAX_HEIGHT_VAR, BODY_MAX_WIDTH_VAR, BODY_SELECTOR, LABEL_CLASS_NAME, LABEL_TEXT_CLASS_NAME, ROOT_CLASS_NAME, STYLES, TRIGGER_CLASS_NAME, } from './styles.js';
9
- const { Children, Fragment, useCallback, useEffect, useMemo, useRef, useState } = React;
10
- const noop = () => { }; // eslint-disable-line @typescript-eslint/no-empty-function
11
- const CHILDREN_ERROR = '@acusti/dropdown requires either 1 child (the dropdown body) or 2 children: the dropdown trigger and the dropdown body.';
12
- const TEXT_INPUT_SELECTOR = 'input:not([type=radio]):not([type=checkbox]):not([type=range]),textarea';
13
- export default function Dropdown({ allowCreate, allowEmpty = true, children, className, disabled, hasItems = true, isOpenOnMount, isSearchable, keepOpenOnSubmit = !hasItems, label, name, onClick, onClose, onMouseDown, onMouseUp, onOpen, onSubmitItem, placeholder, style: styleFromProps, tabIndex, value, }) {
14
- const childrenCount = Children.count(children);
15
- if (childrenCount !== 1 && childrenCount !== 2) {
16
- if (childrenCount === 0) {
17
- throw new Error(CHILDREN_ERROR + ' Received no children.');
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { c } from "react/compiler-runtime";
3
+ import { SYSTEM_UI_FONT, Style } from "@acusti/styling";
4
+ import useIsOutOfBounds from "@acusti/use-is-out-of-bounds";
5
+ import useKeyboardEvents, { isEventTargetUsingKeyEvent } from "@acusti/use-keyboard-events";
6
+ import clsx from "clsx";
7
+ import * as React from "react";
8
+ import { getBestMatch } from "@acusti/matchmaking";
9
+ const ROOT_CLASS_NAME = "uktdropdown";
10
+ const ROOT_SELECTOR = `.${ROOT_CLASS_NAME}`;
11
+ const BODY_CLASS_NAME = `${ROOT_CLASS_NAME}-body`;
12
+ const LABEL_CLASS_NAME = `${ROOT_CLASS_NAME}-label`;
13
+ const LABEL_TEXT_CLASS_NAME = `${ROOT_CLASS_NAME}-label-text`;
14
+ const TRIGGER_CLASS_NAME = `${ROOT_CLASS_NAME}-trigger`;
15
+ const BODY_SELECTOR = `.${BODY_CLASS_NAME}`;
16
+ const LABEL_SELECTOR = `.${LABEL_CLASS_NAME}`;
17
+ const LABEL_TEXT_SELECTOR = `.${LABEL_TEXT_CLASS_NAME}`;
18
+ const TRIGGER_SELECTOR = `.${TRIGGER_CLASS_NAME}`;
19
+ const BODY_MAX_HEIGHT_VAR = "--uktdd-body-max-height";
20
+ const BODY_MAX_WIDTH_VAR = "--uktdd-body-max-width";
21
+ const STYLES = `
22
+ :root {
23
+ --uktdd-font-family: ${SYSTEM_UI_FONT};
24
+ --uktdd-body-bg-color: #fff;
25
+ --uktdd-body-bg-color-hover: rgb(105,162,249);
26
+ --uktdd-body-color-hover: #fff;
27
+ --uktdd-body-buffer: 10px;
28
+ ${BODY_MAX_HEIGHT_VAR}: calc(100vh - var(--uktdd-body-buffer));
29
+ ${BODY_MAX_WIDTH_VAR}: calc(100vw - var(--uktdd-body-buffer));
30
+ --uktdd-body-pad-bottom: 9px;
31
+ --uktdd-body-pad-left: 12px;
32
+ --uktdd-body-pad-right: 12px;
33
+ --uktdd-body-pad-top: 9px;
34
+ --uktdd-label-pad-right: 10px;
35
+ }
36
+ ${ROOT_SELECTOR},
37
+ ${TRIGGER_SELECTOR} {
38
+ font-family: var(--uktdd-font-family);
39
+ }
40
+ ${ROOT_SELECTOR} {
41
+ position: relative;
42
+ display: inline-block;
43
+ }
44
+ ${ROOT_SELECTOR}.disabled {
45
+ pointer-events: none;
46
+ }
47
+ ${ROOT_SELECTOR} > * {
48
+ cursor: default;
49
+ }
50
+ ${LABEL_SELECTOR} {
51
+ display: flex;
52
+ }
53
+ ${LABEL_TEXT_SELECTOR} {
54
+ padding-right: var(--uktdd-label-pad-right);
55
+ }
56
+ ${BODY_SELECTOR} {
57
+ box-sizing: border-box;
58
+ position: absolute;
59
+ top: 100%;
60
+ max-height: var(${BODY_MAX_HEIGHT_VAR});
61
+ min-height: 50px;
62
+ max-width: var(${BODY_MAX_WIDTH_VAR});
63
+ min-width: 100%;
64
+ overflow: auto;
65
+ z-index: 2;
66
+ padding: var(--uktdd-body-pad-top) var(--uktdd-body-pad-right) var(--uktdd-body-pad-bottom) var(--uktdd-body-pad-left);
67
+ background-color: var(--uktdd-body-bg-color);
68
+ box-shadow: 0 8px 18px rgba(0,0,0,0.25);
69
+ }
70
+ ${BODY_SELECTOR}.calculating-position {
71
+ visibility: hidden;
72
+ }
73
+ ${BODY_SELECTOR}.out-of-bounds-bottom:not(.out-of-bounds-top) {
74
+ top: auto;
75
+ bottom: 100%;
76
+ }
77
+ ${BODY_SELECTOR}.out-of-bounds-right:not(.out-of-bounds-left) {
78
+ left: auto;
79
+ right: 0px;
80
+ }
81
+ ${LABEL_SELECTOR} + ${BODY_SELECTOR} {
82
+ left: auto;
83
+ right: 0;
84
+ }
85
+ ${BODY_SELECTOR}.has-items {
86
+ user-select: none;
87
+ }
88
+ ${BODY_SELECTOR} [data-ukt-active] {
89
+ background-color: var(--uktdd-body-bg-color-hover);
90
+ color: var(--uktdd-body-color-hover);
91
+ }
92
+ `;
93
+ const ITEM_SELECTOR = `[data-ukt-item], [data-ukt-value]`;
94
+ const getItemElements = (dropdownElement) => {
95
+ if (!dropdownElement) return null;
96
+ const bodyElement = dropdownElement.querySelector(BODY_SELECTOR);
97
+ if (!bodyElement) return null;
98
+ let items = bodyElement.querySelectorAll(ITEM_SELECTOR);
99
+ if (items.length) return items;
100
+ items = bodyElement.children;
101
+ while (items.length === 1) {
102
+ if (items[0].children == null) break;
103
+ items = items[0].children;
104
+ }
105
+ if (items.length === 1) {
106
+ items = bodyElement.children;
107
+ }
108
+ return items;
109
+ };
110
+ const getActiveItemElement = (dropdownElement) => {
111
+ if (!dropdownElement) return null;
112
+ return dropdownElement.querySelector("[data-ukt-active]");
113
+ };
114
+ const clearItemElementsState = (itemElements) => {
115
+ itemElements.forEach((itemElement) => {
116
+ if (itemElement.hasAttribute("data-ukt-active")) {
117
+ delete itemElement.dataset.uktActive;
118
+ }
119
+ });
120
+ };
121
+ const setActiveItem = ({
122
+ dropdownElement,
123
+ element,
124
+ index,
125
+ indexAddend,
126
+ isExactMatch,
127
+ text
128
+ }) => {
129
+ const items = getItemElements(dropdownElement);
130
+ if (!items) return;
131
+ const itemElements = Array.from(items);
132
+ if (!itemElements.length) return;
133
+ const lastIndex = itemElements.length - 1;
134
+ const currentActiveIndex = itemElements.findIndex((itemElement) => itemElement.hasAttribute("data-ukt-active"));
135
+ let nextActiveIndex = currentActiveIndex;
136
+ if (typeof index === "number") {
137
+ nextActiveIndex = index < 0 ? itemElements.length + index : index;
138
+ }
139
+ if (element) {
140
+ nextActiveIndex = itemElements.findIndex((itemElement) => itemElement === element);
141
+ } else if (typeof indexAddend === "number") {
142
+ if (currentActiveIndex === -1 && indexAddend === -1) {
143
+ nextActiveIndex = lastIndex;
144
+ } else {
145
+ nextActiveIndex += indexAddend;
146
+ }
147
+ if (nextActiveIndex < 0) {
148
+ nextActiveIndex = 0;
149
+ } else if (nextActiveIndex > lastIndex) {
150
+ nextActiveIndex = lastIndex;
151
+ }
152
+ } else if (typeof text === "string") {
153
+ if (!text) {
154
+ clearItemElementsState(itemElements);
155
+ return;
156
+ }
157
+ const itemTexts = itemElements.map((itemElement) => itemElement.innerText);
158
+ if (isExactMatch) {
159
+ const textToCompare = text.toLowerCase();
160
+ nextActiveIndex = itemTexts.findIndex((itemText) => itemText.toLowerCase().startsWith(textToCompare));
161
+ if (nextActiveIndex === -1) {
162
+ clearItemElementsState(itemElements);
163
+ }
164
+ } else {
165
+ const bestMatch = getBestMatch({
166
+ items: itemTexts,
167
+ text
168
+ });
169
+ nextActiveIndex = itemTexts.findIndex((itemText) => itemText === bestMatch);
170
+ }
171
+ }
172
+ if (nextActiveIndex === -1 || nextActiveIndex === currentActiveIndex) return;
173
+ clearItemElementsState(itemElements);
174
+ const nextActiveItem = items[nextActiveIndex];
175
+ if (nextActiveItem != null) {
176
+ nextActiveItem.setAttribute("data-ukt-active", "");
177
+ let {
178
+ parentElement
179
+ } = nextActiveItem;
180
+ let scrollableParent = null;
181
+ while (!scrollableParent && parentElement && parentElement !== dropdownElement) {
182
+ const isScrollable = parentElement.scrollHeight > parentElement.clientHeight + 15;
183
+ if (isScrollable) {
184
+ scrollableParent = parentElement;
185
+ } else {
186
+ parentElement = parentElement.parentElement;
187
+ }
188
+ }
189
+ if (scrollableParent) {
190
+ const parentRect = scrollableParent.getBoundingClientRect();
191
+ const itemRect = nextActiveItem.getBoundingClientRect();
192
+ const isAboveTop = itemRect.top < parentRect.top;
193
+ const isBelowBottom = itemRect.bottom > parentRect.bottom;
194
+ if (isAboveTop || isBelowBottom) {
195
+ let {
196
+ scrollTop
197
+ } = scrollableParent;
198
+ if (isAboveTop) {
199
+ scrollTop -= parentRect.top - itemRect.top;
200
+ } else {
201
+ scrollTop += itemRect.bottom - parentRect.bottom;
18
202
  }
19
- console.error(`${CHILDREN_ERROR} Received ${childrenCount} children.`);
203
+ scrollableParent.scrollTop = scrollTop;
204
+ }
20
205
  }
21
- let trigger;
22
- if (childrenCount > 1) {
23
- trigger = children[0];
206
+ }
207
+ };
208
+ const {
209
+ Children,
210
+ Fragment,
211
+ useEffect,
212
+ useRef,
213
+ useState
214
+ } = React;
215
+ const noop = () => {
216
+ };
217
+ const CHILDREN_ERROR = "@acusti/dropdown requires either 1 child (the dropdown body) or 2 children: the dropdown trigger and the dropdown body.";
218
+ const TEXT_INPUT_SELECTOR = "input:not([type=radio]):not([type=checkbox]):not([type=range]),textarea";
219
+ function Dropdown(t0) {
220
+ const $ = c(97);
221
+ const {
222
+ allowCreate,
223
+ allowEmpty: t1,
224
+ children,
225
+ className,
226
+ disabled,
227
+ hasItems: t2,
228
+ isOpenOnMount,
229
+ isSearchable,
230
+ keepOpenOnSubmit: t3,
231
+ label,
232
+ name,
233
+ onClick,
234
+ onClose,
235
+ onMouseDown,
236
+ onMouseUp,
237
+ onOpen,
238
+ onSubmitItem,
239
+ placeholder,
240
+ style: styleFromProps,
241
+ tabIndex,
242
+ value
243
+ } = t0;
244
+ const allowEmpty = t1 === void 0 ? true : t1;
245
+ const hasItems = t2 === void 0 ? true : t2;
246
+ const keepOpenOnSubmit = t3 === void 0 ? !hasItems : t3;
247
+ const childrenCount = Children.count(children);
248
+ if (childrenCount !== 1 && childrenCount !== 2) {
249
+ if (childrenCount === 0) {
250
+ throw new Error(CHILDREN_ERROR + " Received no children.");
24
251
  }
25
- const isTriggerFromProps = React.isValidElement(trigger);
26
- const [isOpen, setIsOpen] = useState(isOpenOnMount !== null && isOpenOnMount !== void 0 ? isOpenOnMount : false);
27
- const [isOpening, setIsOpening] = useState(!isOpenOnMount);
28
- const [dropdownBodyElement, setDropdownBodyElement] = useState(null);
29
- const dropdownElementRef = useRef(null);
30
- const inputElementRef = useRef(null);
31
- const closingTimerRef = useRef(null);
32
- const isOpeningTimerRef = useRef(null);
33
- const currentInputMethodRef = useRef('mouse');
34
- const clearEnteredCharactersTimerRef = useRef(null);
35
- const enteredCharactersRef = useRef('');
36
- const mouseDownPositionRef = useRef(null);
37
- const outOfBounds = useIsOutOfBounds(dropdownBodyElement);
38
- const setDropdownOpenRef = useRef(() => setIsOpen(true));
39
- const allowCreateRef = useRef(allowCreate);
40
- const allowEmptyRef = useRef(allowEmpty);
41
- const hasItemsRef = useRef(hasItems);
42
- const isOpenRef = useRef(isOpen);
43
- const isOpeningRef = useRef(isOpening);
44
- const keepOpenOnSubmitRef = useRef(keepOpenOnSubmit);
45
- const onCloseRef = useRef(onClose);
46
- const onOpenRef = useRef(onOpen);
47
- const onSubmitItemRef = useRef(onSubmitItem);
48
- const valueRef = useRef(value);
49
- useEffect(() => {
50
- allowCreateRef.current = allowCreate;
51
- allowEmptyRef.current = allowEmpty;
52
- hasItemsRef.current = hasItems;
53
- isOpenRef.current = isOpen;
54
- isOpeningRef.current = isOpening;
55
- keepOpenOnSubmitRef.current = keepOpenOnSubmit;
56
- onCloseRef.current = onClose;
57
- onOpenRef.current = onOpen;
58
- onSubmitItemRef.current = onSubmitItem;
59
- valueRef.current = value;
60
- }, [
61
- allowCreate,
62
- allowEmpty,
63
- hasItems,
64
- isOpen,
65
- isOpening,
66
- keepOpenOnSubmit,
67
- onClose,
68
- onOpen,
69
- onSubmitItem,
70
- value,
71
- ]);
72
- const isMountedRef = useRef(false);
73
- useEffect(() => {
74
- if (!isMountedRef.current) {
75
- isMountedRef.current = true;
76
- // If isOpenOnMount, trigger onOpen right away
77
- if (isOpenRef.current && onOpenRef.current) {
78
- onOpenRef.current();
79
- }
80
- return;
252
+ console.error(`${CHILDREN_ERROR} Received ${childrenCount} children.`);
253
+ }
254
+ let trigger;
255
+ if (childrenCount > 1) {
256
+ trigger = children[0];
257
+ }
258
+ let t4;
259
+ if ($[0] !== trigger) {
260
+ t4 = React.isValidElement(trigger);
261
+ $[0] = trigger;
262
+ $[1] = t4;
263
+ } else {
264
+ t4 = $[1];
265
+ }
266
+ const isTriggerFromProps = t4;
267
+ const [isOpen, setIsOpen] = useState(isOpenOnMount ?? false);
268
+ const [isOpening, setIsOpening] = useState(!isOpenOnMount);
269
+ const [dropdownBodyElement, setDropdownBodyElement] = useState(null);
270
+ const dropdownElementRef = useRef(null);
271
+ const inputElementRef = useRef(null);
272
+ const closingTimerRef = useRef(null);
273
+ const isOpeningTimerRef = useRef(null);
274
+ const currentInputMethodRef = useRef("mouse");
275
+ const clearEnteredCharactersTimerRef = useRef(null);
276
+ const enteredCharactersRef = useRef("");
277
+ const mouseDownPositionRef = useRef(null);
278
+ const outOfBounds = useIsOutOfBounds(dropdownBodyElement);
279
+ const allowCreateRef = useRef(allowCreate);
280
+ const allowEmptyRef = useRef(allowEmpty);
281
+ const hasItemsRef = useRef(hasItems);
282
+ const isOpenRef = useRef(isOpen);
283
+ const isOpeningRef = useRef(isOpening);
284
+ const keepOpenOnSubmitRef = useRef(keepOpenOnSubmit);
285
+ const onCloseRef = useRef(onClose);
286
+ const onOpenRef = useRef(onOpen);
287
+ const onSubmitItemRef = useRef(onSubmitItem);
288
+ const valueRef = useRef(value);
289
+ let t5;
290
+ let t6;
291
+ if ($[2] !== allowCreate || $[3] !== allowEmpty || $[4] !== hasItems || $[5] !== isOpen || $[6] !== isOpening || $[7] !== keepOpenOnSubmit || $[8] !== onClose || $[9] !== onOpen || $[10] !== onSubmitItem || $[11] !== value) {
292
+ t5 = () => {
293
+ allowCreateRef.current = allowCreate;
294
+ allowEmptyRef.current = allowEmpty;
295
+ hasItemsRef.current = hasItems;
296
+ isOpenRef.current = isOpen;
297
+ isOpeningRef.current = isOpening;
298
+ keepOpenOnSubmitRef.current = keepOpenOnSubmit;
299
+ onCloseRef.current = onClose;
300
+ onOpenRef.current = onOpen;
301
+ onSubmitItemRef.current = onSubmitItem;
302
+ valueRef.current = value;
303
+ };
304
+ t6 = [allowCreate, allowEmpty, hasItems, isOpen, isOpening, keepOpenOnSubmit, onClose, onOpen, onSubmitItem, value];
305
+ $[2] = allowCreate;
306
+ $[3] = allowEmpty;
307
+ $[4] = hasItems;
308
+ $[5] = isOpen;
309
+ $[6] = isOpening;
310
+ $[7] = keepOpenOnSubmit;
311
+ $[8] = onClose;
312
+ $[9] = onOpen;
313
+ $[10] = onSubmitItem;
314
+ $[11] = value;
315
+ $[12] = t5;
316
+ $[13] = t6;
317
+ } else {
318
+ t5 = $[12];
319
+ t6 = $[13];
320
+ }
321
+ useEffect(t5, t6);
322
+ const isMountedRef = useRef(false);
323
+ let t7;
324
+ let t8;
325
+ if ($[14] !== isOpen) {
326
+ t7 = () => {
327
+ if (!isMountedRef.current) {
328
+ isMountedRef.current = true;
329
+ if (isOpenRef.current && onOpenRef.current) {
330
+ onOpenRef.current();
331
+ }
332
+ return;
333
+ }
334
+ if (isOpen && onOpenRef.current) {
335
+ onOpenRef.current();
336
+ } else {
337
+ if (!isOpen && onCloseRef.current) {
338
+ onCloseRef.current();
339
+ }
340
+ }
341
+ };
342
+ t8 = [isOpen];
343
+ $[14] = isOpen;
344
+ $[15] = t7;
345
+ $[16] = t8;
346
+ } else {
347
+ t7 = $[15];
348
+ t8 = $[16];
349
+ }
350
+ useEffect(t7, t8);
351
+ let t9;
352
+ if ($[17] !== setIsOpen || $[18] !== setIsOpening) {
353
+ t9 = () => {
354
+ setIsOpen(false);
355
+ setIsOpening(false);
356
+ mouseDownPositionRef.current = null;
357
+ if (closingTimerRef.current) {
358
+ clearTimeout(closingTimerRef.current);
359
+ closingTimerRef.current = null;
360
+ }
361
+ };
362
+ $[17] = setIsOpen;
363
+ $[18] = setIsOpening;
364
+ $[19] = t9;
365
+ } else {
366
+ t9 = $[19];
367
+ }
368
+ const closeDropdown = t9;
369
+ let t10;
370
+ if ($[20] !== closeDropdown) {
371
+ t10 = (event) => {
372
+ var _a;
373
+ const eventTarget = event.target;
374
+ if (isOpenRef.current && !keepOpenOnSubmitRef.current) {
375
+ const keepOpen = eventTarget.closest("[data-ukt-keep-open]");
376
+ if (!(keepOpen == null ? void 0 : keepOpen.dataset.uktKeepOpen) || keepOpen.dataset.uktKeepOpen === "false") {
377
+ closingTimerRef.current = setTimeout(closeDropdown, 90);
378
+ }
379
+ }
380
+ if (!hasItemsRef.current) {
381
+ return;
382
+ }
383
+ const element = getActiveItemElement(dropdownElementRef.current);
384
+ if (!element && !allowCreateRef.current) {
385
+ if (!allowEmptyRef.current) {
386
+ return;
387
+ }
388
+ if ((_a = inputElementRef.current) == null ? void 0 : _a.value) {
389
+ return;
81
390
  }
82
- if (isOpen && onOpenRef.current) {
83
- onOpenRef.current();
391
+ }
392
+ let itemLabel = (element == null ? void 0 : element.innerText) ?? "";
393
+ if (inputElementRef.current) {
394
+ if (!element) {
395
+ itemLabel = inputElementRef.current.value;
396
+ } else {
397
+ inputElementRef.current.value = itemLabel;
84
398
  }
85
- else if (!isOpen && onCloseRef.current) {
86
- onCloseRef.current();
399
+ if (inputElementRef.current === inputElementRef.current.ownerDocument.activeElement) {
400
+ inputElementRef.current.blur();
87
401
  }
88
- }, [isOpen]);
89
- const closeDropdown = useCallback(() => {
90
- setIsOpen(false);
402
+ }
403
+ const nextValue = (element == null ? void 0 : element.dataset.uktValue) ?? itemLabel;
404
+ if (valueRef.current && valueRef.current === nextValue) {
405
+ return;
406
+ }
407
+ if (onSubmitItemRef.current) {
408
+ onSubmitItemRef.current({
409
+ element,
410
+ event,
411
+ label: itemLabel,
412
+ value: nextValue
413
+ });
414
+ }
415
+ };
416
+ $[20] = closeDropdown;
417
+ $[21] = t10;
418
+ } else {
419
+ t10 = $[21];
420
+ }
421
+ const handleSubmitItem = t10;
422
+ let t11;
423
+ if ($[22] !== setIsOpening) {
424
+ t11 = (t122) => {
425
+ const {
426
+ clientX,
427
+ clientY
428
+ } = t122;
429
+ currentInputMethodRef.current = "mouse";
430
+ const initialPosition = mouseDownPositionRef.current;
431
+ if (!initialPosition) {
432
+ return;
433
+ }
434
+ if (Math.abs(initialPosition.clientX - clientX) < 12 && Math.abs(initialPosition.clientY - clientY) < 12) {
435
+ return;
436
+ }
437
+ setIsOpening(false);
438
+ };
439
+ $[22] = setIsOpening;
440
+ $[23] = t11;
441
+ } else {
442
+ t11 = $[23];
443
+ }
444
+ const handleMouseMove = t11;
445
+ let t12;
446
+ if ($[24] === Symbol.for("react.memo_cache_sentinel")) {
447
+ t12 = (event_0) => {
448
+ if (!hasItemsRef.current) {
449
+ return;
450
+ }
451
+ if (currentInputMethodRef.current !== "mouse") {
452
+ return;
453
+ }
454
+ const dropdownElement = dropdownElementRef.current;
455
+ if (!dropdownElement) {
456
+ return;
457
+ }
458
+ const itemElements = getItemElements(dropdownElement);
459
+ if (!itemElements) {
460
+ return;
461
+ }
462
+ const eventTarget_0 = event_0.target;
463
+ const item = eventTarget_0.closest(ITEM_SELECTOR);
464
+ const element_0 = item ?? eventTarget_0;
465
+ for (const itemElement of itemElements) {
466
+ if (itemElement === element_0) {
467
+ setActiveItem({
468
+ dropdownElement,
469
+ element: element_0
470
+ });
471
+ return;
472
+ }
473
+ }
474
+ };
475
+ $[24] = t12;
476
+ } else {
477
+ t12 = $[24];
478
+ }
479
+ const handleMouseOver = t12;
480
+ let t13;
481
+ if ($[25] === Symbol.for("react.memo_cache_sentinel")) {
482
+ t13 = (event_1) => {
483
+ if (!hasItemsRef.current) {
484
+ return;
485
+ }
486
+ const activeItem = getActiveItemElement(dropdownElementRef.current);
487
+ if (!activeItem) {
488
+ return;
489
+ }
490
+ const eventRelatedTarget = event_1.relatedTarget;
491
+ if (activeItem !== event_1.target || activeItem.contains(eventRelatedTarget)) {
492
+ return;
493
+ }
494
+ delete activeItem.dataset.uktActive;
495
+ };
496
+ $[25] = t13;
497
+ } else {
498
+ t13 = $[25];
499
+ }
500
+ const handleMouseOut = t13;
501
+ let t14;
502
+ if ($[26] !== onMouseDown || $[27] !== setIsOpen || $[28] !== setIsOpening) {
503
+ t14 = (event_2) => {
504
+ if (onMouseDown) {
505
+ onMouseDown(event_2);
506
+ }
507
+ if (isOpenRef.current) {
508
+ return;
509
+ }
510
+ setIsOpen(true);
511
+ setIsOpening(true);
512
+ mouseDownPositionRef.current = {
513
+ clientX: event_2.clientX,
514
+ clientY: event_2.clientY
515
+ };
516
+ isOpeningTimerRef.current = setTimeout(() => {
91
517
  setIsOpening(false);
92
- mouseDownPositionRef.current = null;
93
- if (closingTimerRef.current) {
94
- clearTimeout(closingTimerRef.current);
95
- closingTimerRef.current = null;
518
+ isOpeningTimerRef.current = null;
519
+ }, 1e3);
520
+ };
521
+ $[26] = onMouseDown;
522
+ $[27] = setIsOpen;
523
+ $[28] = setIsOpening;
524
+ $[29] = t14;
525
+ } else {
526
+ t14 = $[29];
527
+ }
528
+ const handleMouseDown = t14;
529
+ let t15;
530
+ if ($[30] !== closeDropdown || $[31] !== handleSubmitItem || $[32] !== onMouseUp) {
531
+ t15 = (event_3) => {
532
+ if (onMouseUp) {
533
+ onMouseUp(event_3);
534
+ }
535
+ if (isOpeningRef.current || !isOpenRef.current || closingTimerRef.current) {
536
+ return;
537
+ }
538
+ const eventTarget_1 = event_3.target;
539
+ if (!eventTarget_1.closest(BODY_SELECTOR)) {
540
+ if (!isOpeningRef.current && inputElementRef.current !== eventTarget_1.ownerDocument.activeElement) {
541
+ closeDropdown();
542
+ }
543
+ return;
544
+ }
545
+ if (!hasItemsRef.current) {
546
+ return;
547
+ }
548
+ handleSubmitItem(event_3);
549
+ };
550
+ $[30] = closeDropdown;
551
+ $[31] = handleSubmitItem;
552
+ $[32] = onMouseUp;
553
+ $[33] = t15;
554
+ } else {
555
+ t15 = $[33];
556
+ }
557
+ const handleMouseUp = t15;
558
+ let t16;
559
+ if ($[34] !== closeDropdown || $[35] !== handleSubmitItem || $[36] !== setIsOpen) {
560
+ t16 = (event_4) => {
561
+ const {
562
+ altKey,
563
+ ctrlKey,
564
+ key,
565
+ metaKey
566
+ } = event_4;
567
+ const eventTarget_2 = event_4.target;
568
+ const dropdownElement_0 = dropdownElementRef.current;
569
+ if (!dropdownElement_0) {
570
+ return;
571
+ }
572
+ const onEventHandled = () => {
573
+ event_4.stopPropagation();
574
+ event_4.preventDefault();
575
+ currentInputMethodRef.current = "keyboard";
576
+ };
577
+ const isEventTargetingDropdown = dropdownElement_0.contains(eventTarget_2);
578
+ if (!isOpenRef.current) {
579
+ if (!isEventTargetingDropdown) {
580
+ return;
581
+ }
582
+ if (key === " " || key === "Enter" || hasItemsRef.current && (key === "ArrowUp" || key === "ArrowDown")) {
583
+ onEventHandled();
584
+ setIsOpen(true);
96
585
  }
97
- }, []);
98
- const handleSubmitItem = useCallback((event) => {
99
- var _a, _b, _c;
100
- const eventTarget = event.target;
101
- if (isOpenRef.current && !keepOpenOnSubmitRef.current) {
102
- const keepOpen = eventTarget.closest('[data-ukt-keep-open]');
103
- // Don’t close dropdown if event occurs w/in data-ukt-keep-open element
104
- if (!(keepOpen === null || keepOpen === void 0 ? void 0 : keepOpen.dataset.uktKeepOpen) ||
105
- keepOpen.dataset.uktKeepOpen === 'false') {
106
- // A short timeout before closing is better UX when user selects an item so dropdown
107
- // doesn’t close before expected. It also enables using <Link />s in the dropdown body.
108
- closingTimerRef.current = setTimeout(closeDropdown, 90);
109
- }
586
+ return;
587
+ }
588
+ const isTargetUsingKeyEvents = isEventTargetUsingKeyEvent(event_4);
589
+ if (hasItemsRef.current && !isTargetUsingKeyEvents) {
590
+ let isEditingCharacters = !ctrlKey && !metaKey && /^[A-Za-z0-9]$/.test(key);
591
+ if (!isEditingCharacters && enteredCharactersRef.current) {
592
+ isEditingCharacters = key === " " || key === "Backspace";
110
593
  }
111
- if (!hasItemsRef.current)
112
- return;
113
- const element = getActiveItemElement(dropdownElementRef.current);
114
- if (!element && !allowCreateRef.current) {
115
- // If not allowEmpty, don’t allow submitting an empty item
116
- if (!allowEmptyRef.current)
117
- return;
118
- // If we have an input element as trigger & the user didn’t clear the text, do nothing
119
- if ((_a = inputElementRef.current) === null || _a === void 0 ? void 0 : _a.value)
120
- return;
594
+ if (isEditingCharacters) {
595
+ onEventHandled();
596
+ if (key === "Backspace") {
597
+ enteredCharactersRef.current = enteredCharactersRef.current.slice(0, -1);
598
+ } else {
599
+ enteredCharactersRef.current = enteredCharactersRef.current + key;
600
+ }
601
+ setActiveItem({
602
+ dropdownElement: dropdownElement_0,
603
+ isExactMatch: allowCreateRef.current,
604
+ text: enteredCharactersRef.current
605
+ });
606
+ if (clearEnteredCharactersTimerRef.current) {
607
+ clearTimeout(clearEnteredCharactersTimerRef.current);
608
+ }
609
+ clearEnteredCharactersTimerRef.current = setTimeout(() => {
610
+ enteredCharactersRef.current = "";
611
+ clearEnteredCharactersTimerRef.current = null;
612
+ }, 1500);
613
+ return;
121
614
  }
122
- let itemLabel = (_b = element === null || element === void 0 ? void 0 : element.innerText) !== null && _b !== void 0 ? _b : '';
123
- if (inputElementRef.current) {
124
- if (!element) {
125
- itemLabel = inputElementRef.current.value;
126
- }
127
- else {
128
- inputElementRef.current.value = itemLabel;
129
- }
130
- if (inputElementRef.current ===
131
- inputElementRef.current.ownerDocument.activeElement) {
132
- inputElementRef.current.blur();
133
- }
615
+ }
616
+ if (key === "Enter" || key === " " && !inputElementRef.current) {
617
+ onEventHandled();
618
+ handleSubmitItem(event_4);
619
+ return;
620
+ }
621
+ if (key === "Escape" || isEventTargetingDropdown && key === " " && !hasItemsRef.current) {
622
+ if (hasItemsRef.current || !isTargetUsingKeyEvents) {
623
+ closeDropdown();
624
+ }
625
+ return;
626
+ }
627
+ if (hasItemsRef.current) {
628
+ if (key === "ArrowUp") {
629
+ onEventHandled();
630
+ if (altKey || metaKey) {
631
+ setActiveItem({
632
+ dropdownElement: dropdownElement_0,
633
+ index: 0
634
+ });
635
+ } else {
636
+ setActiveItem({
637
+ dropdownElement: dropdownElement_0,
638
+ indexAddend: -1
639
+ });
640
+ }
641
+ return;
134
642
  }
135
- const nextValue = (_c = element === null || element === void 0 ? void 0 : element.dataset.uktValue) !== null && _c !== void 0 ? _c : itemLabel;
136
- // If parent is controlling Dropdown via props.value and nextValue is the same, do nothing
137
- if (valueRef.current && valueRef.current === nextValue)
138
- return;
139
- if (onSubmitItemRef.current) {
140
- onSubmitItemRef.current({
141
- element,
142
- event,
143
- label: itemLabel,
144
- value: nextValue,
643
+ if (key === "ArrowDown") {
644
+ onEventHandled();
645
+ if (altKey || metaKey) {
646
+ setActiveItem({
647
+ dropdownElement: dropdownElement_0,
648
+ index: -1
145
649
  });
650
+ } else {
651
+ setActiveItem({
652
+ dropdownElement: dropdownElement_0,
653
+ indexAddend: 1
654
+ });
655
+ }
656
+ return;
146
657
  }
147
- }, [closeDropdown]);
148
- const handleMouseMove = useCallback(({ clientX, clientY }) => {
149
- currentInputMethodRef.current = 'mouse';
150
- const initialPosition = mouseDownPositionRef.current;
151
- if (!initialPosition)
152
- return;
153
- if (Math.abs(initialPosition.clientX - clientX) < 12 &&
154
- Math.abs(initialPosition.clientY - clientY) < 12) {
155
- return;
658
+ }
659
+ };
660
+ $[34] = closeDropdown;
661
+ $[35] = handleSubmitItem;
662
+ $[36] = setIsOpen;
663
+ $[37] = t16;
664
+ } else {
665
+ t16 = $[37];
666
+ }
667
+ const handleKeyDown = t16;
668
+ let t17;
669
+ if ($[38] !== handleKeyDown) {
670
+ t17 = {
671
+ ignoreUsedKeyboardEvents: false,
672
+ onKeyDown: handleKeyDown
673
+ };
674
+ $[38] = handleKeyDown;
675
+ $[39] = t17;
676
+ } else {
677
+ t17 = $[39];
678
+ }
679
+ useKeyboardEvents(t17);
680
+ const cleanupEventListenersRef = useRef(noop);
681
+ let t18;
682
+ if ($[40] !== closeDropdown || $[41] !== isOpenOnMount || $[42] !== isTriggerFromProps || $[43] !== setIsOpen || $[44] !== setIsOpening) {
683
+ t18 = (ref) => {
684
+ dropdownElementRef.current = ref;
685
+ if (!ref) {
686
+ cleanupEventListenersRef.current();
687
+ cleanupEventListenersRef.current = noop;
688
+ return;
689
+ }
690
+ const {
691
+ ownerDocument
692
+ } = ref;
693
+ let inputElement = inputElementRef.current;
694
+ if (isTriggerFromProps && !inputElement && ref.firstElementChild) {
695
+ if (ref.firstElementChild.matches(TEXT_INPUT_SELECTOR)) {
696
+ inputElement = ref.firstElementChild;
697
+ } else {
698
+ inputElement = ref.firstElementChild.querySelector(TEXT_INPUT_SELECTOR);
156
699
  }
157
- setIsOpening(false);
158
- }, []);
159
- const handleMouseOver = useCallback((event) => {
160
- if (!hasItemsRef.current)
161
- return;
162
- // If user isn’t currently using the mouse to navigate the dropdown, do nothing
163
- if (currentInputMethodRef.current !== 'mouse')
164
- return;
165
- // Ensure we have the dropdown root HTMLElement
166
- const dropdownElement = dropdownElementRef.current;
167
- if (!dropdownElement)
168
- return;
169
- const itemElements = getItemElements(dropdownElement);
170
- if (!itemElements)
171
- return;
172
- const eventTarget = event.target;
173
- const item = eventTarget.closest(ITEM_SELECTOR);
174
- const element = item !== null && item !== void 0 ? item : eventTarget;
175
- for (const itemElement of itemElements) {
176
- if (itemElement === element) {
177
- setActiveItem({ dropdownElement, element });
178
- return;
179
- }
700
+ inputElementRef.current = inputElement;
701
+ }
702
+ const handleGlobalMouseDown = (t192) => {
703
+ const {
704
+ target
705
+ } = t192;
706
+ const eventTarget_3 = target;
707
+ if (dropdownElementRef.current && !dropdownElementRef.current.contains(eventTarget_3)) {
708
+ closeDropdown();
180
709
  }
181
- }, []);
182
- const handleMouseOut = useCallback((event) => {
183
- if (!hasItemsRef.current)
184
- return;
185
- const activeItem = getActiveItemElement(dropdownElementRef.current);
186
- if (!activeItem)
187
- return;
188
- const eventRelatedTarget = event.relatedTarget;
189
- if (activeItem !== event.target || activeItem.contains(eventRelatedTarget)) {
190
- return;
710
+ };
711
+ const handleGlobalMouseUp = (t202) => {
712
+ var _a;
713
+ const {
714
+ target: target_0
715
+ } = t202;
716
+ if (!isOpenRef.current || closingTimerRef.current) {
717
+ return;
191
718
  }
192
- // If user moused out of activeItem (not into a descendant), it’s no longer active
193
- delete activeItem.dataset.uktActive;
194
- }, []);
195
- const handleMouseDown = useCallback((event) => {
196
- if (onMouseDown)
197
- onMouseDown(event);
198
- if (isOpenRef.current)
199
- return;
200
- setIsOpen(true);
201
- setIsOpening(true);
202
- mouseDownPositionRef.current = {
203
- clientX: event.clientX,
204
- clientY: event.clientY,
205
- };
206
- isOpeningTimerRef.current = setTimeout(() => {
207
- setIsOpening(false);
719
+ if (isOpeningRef.current) {
720
+ setIsOpening(false);
721
+ if (isOpeningTimerRef.current) {
722
+ clearTimeout(isOpeningTimerRef.current);
208
723
  isOpeningTimerRef.current = null;
209
- }, 1000);
210
- }, [onMouseDown]);
211
- const handleMouseUp = useCallback((event) => {
212
- if (onMouseUp)
213
- onMouseUp(event);
214
- // If dropdown is still opening or isn’t open or is closing, do nothing
215
- if (isOpeningRef.current || !isOpenRef.current || closingTimerRef.current) {
216
- return;
724
+ }
725
+ return;
217
726
  }
218
- const eventTarget = event.target;
219
- // If click was outside dropdown body, don’t trigger submit
220
- if (!eventTarget.closest(BODY_SELECTOR)) {
221
- // Don’t close dropdown if isOpening or search input is focused
222
- if (!isOpeningRef.current &&
223
- inputElementRef.current !== eventTarget.ownerDocument.activeElement) {
224
- closeDropdown();
225
- }
226
- return;
727
+ const eventTarget_4 = target_0;
728
+ if (!((_a = dropdownElementRef.current) == null ? void 0 : _a.contains(eventTarget_4))) {
729
+ closeDropdown();
227
730
  }
228
- // If dropdown has no items and click was within dropdown body, do nothing
229
- if (!hasItemsRef.current)
230
- return;
231
- handleSubmitItem(event);
232
- }, [closeDropdown, handleSubmitItem, onMouseUp]);
233
- const handleKeyDown = useCallback((event) => {
234
- const { altKey, ctrlKey, key, metaKey } = event;
235
- const eventTarget = event.target;
236
- const dropdownElement = dropdownElementRef.current;
237
- if (!dropdownElement)
238
- return;
239
- const onEventHandled = () => {
240
- event.stopPropagation();
241
- event.preventDefault();
242
- currentInputMethodRef.current = 'keyboard';
243
- };
244
- const isEventTargetingDropdown = dropdownElement.contains(eventTarget);
731
+ };
732
+ const handleGlobalFocusIn = (t212) => {
733
+ const {
734
+ target: target_1
735
+ } = t212;
245
736
  if (!isOpenRef.current) {
246
- // If dropdown is closed, don’t handle key events if event target isn’t within dropdown
247
- if (!isEventTargetingDropdown)
248
- return;
249
- // Open the dropdown on spacebar, enter, or if isSearchable and user hits the ↑/↓ arrows
250
- if (key === ' ' ||
251
- key === 'Enter' ||
252
- (hasItemsRef.current && (key === 'ArrowUp' || key === 'ArrowDown'))) {
253
- onEventHandled();
254
- setIsOpen(true);
255
- }
256
- return;
737
+ return;
257
738
  }
258
- const isTargetUsingKeyEvents = isEventTargetUsingKeyEvent(event);
259
- // If dropdown isOpen + hasItems & eventTargetNotUsingKeyEvents, handle characters
260
- if (hasItemsRef.current && !isTargetUsingKeyEvents) {
261
- let isEditingCharacters = !ctrlKey && !metaKey && /^[A-Za-z0-9]$/.test(key);
262
- // User could also be editing characters if there are already characters entered
263
- // and they are hitting delete or spacebar
264
- if (!isEditingCharacters && enteredCharactersRef.current) {
265
- isEditingCharacters = key === ' ' || key === 'Backspace';
266
- }
267
- if (isEditingCharacters) {
268
- onEventHandled();
269
- if (key === 'Backspace') {
270
- enteredCharactersRef.current = enteredCharactersRef.current.slice(0, -1);
271
- }
272
- else {
273
- enteredCharactersRef.current += key;
274
- }
275
- setActiveItem({
276
- dropdownElement,
277
- // If props.allowCreate, only override the input’s value with an
278
- // exact text match so user can enter a value not in items
279
- isExactMatch: allowCreateRef.current,
280
- text: enteredCharactersRef.current,
281
- });
282
- if (clearEnteredCharactersTimerRef.current) {
283
- clearTimeout(clearEnteredCharactersTimerRef.current);
284
- }
285
- clearEnteredCharactersTimerRef.current = setTimeout(() => {
286
- enteredCharactersRef.current = '';
287
- clearEnteredCharactersTimerRef.current = null;
288
- }, 1500);
289
- return;
290
- }
739
+ const eventTarget_5 = target_1;
740
+ if (!dropdownElementRef.current || dropdownElementRef.current.contains(eventTarget_5) || eventTarget_5.contains(dropdownElementRef.current)) {
741
+ return;
291
742
  }
292
- // If dropdown isOpen, handle submitting the value
293
- if (key === 'Enter' || (key === ' ' && !inputElementRef.current)) {
294
- onEventHandled();
295
- handleSubmitItem(event);
296
- return;
743
+ closeDropdown();
744
+ };
745
+ document.addEventListener("focusin", handleGlobalFocusIn);
746
+ document.addEventListener("mousedown", handleGlobalMouseDown);
747
+ document.addEventListener("mouseup", handleGlobalMouseUp);
748
+ if (ownerDocument !== document) {
749
+ ownerDocument.addEventListener("focusin", handleGlobalFocusIn);
750
+ ownerDocument.addEventListener("mousedown", handleGlobalMouseDown);
751
+ ownerDocument.addEventListener("mouseup", handleGlobalMouseUp);
752
+ }
753
+ if (isOpenOnMount) {
754
+ ref.focus();
755
+ }
756
+ const handleInput = (event_5) => {
757
+ const dropdownElement_1 = dropdownElementRef.current;
758
+ if (!dropdownElement_1) {
759
+ return;
297
760
  }
298
- // If dropdown isOpen, handle closing it on escape or spacebar if !hasItems
299
- if (key === 'Escape' ||
300
- (isEventTargetingDropdown && key === ' ' && !hasItemsRef.current)) {
301
- // Close dropdown if hasItems or event target not using key events
302
- if (hasItemsRef.current || !isTargetUsingKeyEvents) {
303
- closeDropdown();
304
- }
305
- return;
306
- }
307
- // Handle ↑/↓ arrows
308
- if (hasItemsRef.current) {
309
- if (key === 'ArrowUp') {
310
- onEventHandled();
311
- if (altKey || metaKey) {
312
- setActiveItem({ dropdownElement, index: 0 });
313
- }
314
- else {
315
- setActiveItem({ dropdownElement, indexAddend: -1 });
316
- }
317
- return;
318
- }
319
- if (key === 'ArrowDown') {
320
- onEventHandled();
321
- if (altKey || metaKey) {
322
- // Using a negative index counts back from the end
323
- setActiveItem({ dropdownElement, index: -1 });
324
- }
325
- else {
326
- setActiveItem({ dropdownElement, indexAddend: 1 });
327
- }
328
- return;
329
- }
330
- }
331
- }, [closeDropdown, handleSubmitItem]);
332
- useKeyboardEvents({ ignoreUsedKeyboardEvents: false, onKeyDown: handleKeyDown });
333
- const cleanupEventListenersRef = useRef(noop);
334
- const handleRef = useCallback((ref) => {
335
- dropdownElementRef.current = ref;
336
- if (!ref) {
337
- // If component was unmounted, cleanup handlers
338
- cleanupEventListenersRef.current();
339
- cleanupEventListenersRef.current = noop;
340
- return;
761
+ if (!isOpenRef.current) {
762
+ setIsOpen(true);
341
763
  }
342
- const { ownerDocument } = ref;
343
- let inputElement = inputElementRef.current;
344
- // Check if trigger from props is a textual input or textarea element
345
- if (isTriggerFromProps && !inputElement && ref.firstElementChild) {
346
- if (ref.firstElementChild.matches(TEXT_INPUT_SELECTOR)) {
347
- inputElement = ref.firstElementChild;
348
- }
349
- else {
350
- inputElement =
351
- ref.firstElementChild.querySelector(TEXT_INPUT_SELECTOR);
352
- }
353
- inputElementRef.current = inputElement;
764
+ const input = event_5.target;
765
+ const isDeleting = enteredCharactersRef.current.length > input.value.length;
766
+ enteredCharactersRef.current = input.value;
767
+ if (isDeleting && input.value.length && getActiveItemElement(dropdownElement_1)) {
768
+ return;
354
769
  }
355
- const handleGlobalMouseDown = ({ target }) => {
356
- const eventTarget = target;
357
- if (dropdownElementRef.current &&
358
- !dropdownElementRef.current.contains(eventTarget)) {
359
- // Close dropdown on an outside click
360
- closeDropdown();
361
- }
362
- };
363
- const handleGlobalMouseUp = ({ target }) => {
364
- var _a;
365
- if (!isOpenRef.current || closingTimerRef.current)
366
- return;
367
- // If still isOpening (gets set false 1s after open triggers), set it to false onMouseUp
368
- if (isOpeningRef.current) {
369
- setIsOpening(false);
370
- if (isOpeningTimerRef.current) {
371
- clearTimeout(isOpeningTimerRef.current);
372
- isOpeningTimerRef.current = null;
373
- }
374
- return;
375
- }
376
- const eventTarget = target;
377
- // Only handle mouseup events from outside the dropdown here
378
- if (!((_a = dropdownElementRef.current) === null || _a === void 0 ? void 0 : _a.contains(eventTarget))) {
379
- closeDropdown();
380
- }
381
- };
382
- // Close dropdown if any element is focused outside of this dropdown
383
- const handleGlobalFocusIn = ({ target }) => {
384
- if (!isOpenRef.current)
385
- return;
386
- const eventTarget = target;
387
- // If focused element is a descendant or a parent of the dropdown, do nothing
388
- if (!dropdownElementRef.current ||
389
- dropdownElementRef.current.contains(eventTarget) ||
390
- eventTarget.contains(dropdownElementRef.current)) {
391
- return;
392
- }
393
- closeDropdown();
394
- };
395
- document.addEventListener('focusin', handleGlobalFocusIn);
396
- document.addEventListener('mousedown', handleGlobalMouseDown);
397
- document.addEventListener('mouseup', handleGlobalMouseUp);
770
+ setActiveItem({
771
+ dropdownElement: dropdownElement_1,
772
+ isExactMatch: allowCreateRef.current,
773
+ text: enteredCharactersRef.current
774
+ });
775
+ };
776
+ if (inputElement) {
777
+ inputElement.addEventListener("input", handleInput);
778
+ }
779
+ cleanupEventListenersRef.current = () => {
780
+ document.removeEventListener("focusin", handleGlobalFocusIn);
781
+ document.removeEventListener("mousedown", handleGlobalMouseDown);
782
+ document.removeEventListener("mouseup", handleGlobalMouseUp);
398
783
  if (ownerDocument !== document) {
399
- ownerDocument.addEventListener('focusin', handleGlobalFocusIn);
400
- ownerDocument.addEventListener('mousedown', handleGlobalMouseDown);
401
- ownerDocument.addEventListener('mouseup', handleGlobalMouseUp);
784
+ ownerDocument.removeEventListener("focusin", handleGlobalFocusIn);
785
+ ownerDocument.removeEventListener("mousedown", handleGlobalMouseDown);
786
+ ownerDocument.removeEventListener("mouseup", handleGlobalMouseUp);
402
787
  }
403
- // If dropdown should be open on mount, focus it
404
- if (isOpenOnMount) {
405
- ref.focus();
406
- }
407
- const handleInput = (event) => {
408
- const dropdownElement = dropdownElementRef.current;
409
- if (!dropdownElement)
410
- return;
411
- if (!isOpenRef.current)
412
- setIsOpen(true);
413
- const input = event.target;
414
- const isDeleting = enteredCharactersRef.current.length > input.value.length;
415
- enteredCharactersRef.current = input.value;
416
- // When deleting text, if there’s already an active item and
417
- // input isn’t empty, preserve the active item, else update it
418
- if (isDeleting &&
419
- input.value.length &&
420
- getActiveItemElement(dropdownElement)) {
421
- return;
422
- }
423
- setActiveItem({
424
- dropdownElement,
425
- // If props.allowCreate, only override the input’s value with an
426
- // exact text match so user can enter a value not in items
427
- isExactMatch: allowCreateRef.current,
428
- text: enteredCharactersRef.current,
429
- });
430
- };
431
788
  if (inputElement) {
432
- inputElement.addEventListener('input', handleInput);
433
- }
434
- cleanupEventListenersRef.current = () => {
435
- document.removeEventListener('focusin', handleGlobalFocusIn);
436
- document.removeEventListener('mousedown', handleGlobalMouseDown);
437
- document.removeEventListener('mouseup', handleGlobalMouseUp);
438
- if (ownerDocument !== document) {
439
- ownerDocument.removeEventListener('focusin', handleGlobalFocusIn);
440
- ownerDocument.removeEventListener('mousedown', handleGlobalMouseDown);
441
- ownerDocument.removeEventListener('mouseup', handleGlobalMouseUp);
442
- }
443
- if (inputElement) {
444
- inputElement.removeEventListener('input', handleInput);
445
- }
446
- };
447
- }, [closeDropdown, isOpenOnMount, isTriggerFromProps]);
448
- if (!isTriggerFromProps) {
449
- if (isSearchable) {
450
- trigger = (React.createElement("input", { autoComplete: "off", className: TRIGGER_CLASS_NAME, defaultValue: value !== null && value !== void 0 ? value : '', disabled: disabled, name: name, onFocus: setDropdownOpenRef.current, placeholder: placeholder, ref: inputElementRef, tabIndex: tabIndex, type: "text" }));
451
- }
452
- else {
453
- trigger = (React.createElement("button", { className: TRIGGER_CLASS_NAME, tabIndex: 0 }, trigger));
789
+ inputElement.removeEventListener("input", handleInput);
454
790
  }
791
+ };
792
+ };
793
+ $[40] = closeDropdown;
794
+ $[41] = isOpenOnMount;
795
+ $[42] = isTriggerFromProps;
796
+ $[43] = setIsOpen;
797
+ $[44] = setIsOpening;
798
+ $[45] = t18;
799
+ } else {
800
+ t18 = $[45];
801
+ }
802
+ const handleRef = t18;
803
+ if (!isTriggerFromProps) {
804
+ if (isSearchable) {
805
+ const t192 = value ?? "";
806
+ let t202;
807
+ if ($[46] !== setIsOpen) {
808
+ t202 = () => setIsOpen(true);
809
+ $[46] = setIsOpen;
810
+ $[47] = t202;
811
+ } else {
812
+ t202 = $[47];
813
+ }
814
+ let t212;
815
+ if ($[48] !== disabled || $[49] !== name || $[50] !== placeholder || $[51] !== t192 || $[52] !== t202 || $[53] !== tabIndex) {
816
+ t212 = /* @__PURE__ */ jsx("input", { autoComplete: "off", className: TRIGGER_CLASS_NAME, defaultValue: t192, disabled, name, onFocus: t202, placeholder, ref: inputElementRef, tabIndex, type: "text" });
817
+ $[48] = disabled;
818
+ $[49] = name;
819
+ $[50] = placeholder;
820
+ $[51] = t192;
821
+ $[52] = t202;
822
+ $[53] = tabIndex;
823
+ $[54] = t212;
824
+ } else {
825
+ t212 = $[54];
826
+ }
827
+ trigger = t212;
828
+ } else {
829
+ let t192;
830
+ if ($[55] !== trigger) {
831
+ t192 = /* @__PURE__ */ jsx("button", { className: TRIGGER_CLASS_NAME, tabIndex: 0, children: trigger });
832
+ $[55] = trigger;
833
+ $[56] = t192;
834
+ } else {
835
+ t192 = $[56];
836
+ }
837
+ trigger = t192;
455
838
  }
456
- if (label) {
457
- trigger = (React.createElement("label", { className: LABEL_CLASS_NAME },
458
- React.createElement("div", { className: LABEL_TEXT_CLASS_NAME }, label),
459
- trigger));
839
+ }
840
+ if (label) {
841
+ let t192;
842
+ if ($[57] !== label) {
843
+ t192 = /* @__PURE__ */ jsx("div", { className: LABEL_TEXT_CLASS_NAME, children: label });
844
+ $[57] = label;
845
+ $[58] = t192;
846
+ } else {
847
+ t192 = $[58];
460
848
  }
461
- const style = useMemo(() => (Object.assign(Object.assign(Object.assign({}, styleFromProps), (outOfBounds.maxHeight != null && outOfBounds.maxHeight > 0
462
- ? {
463
- [BODY_MAX_HEIGHT_VAR]: `calc(${outOfBounds.maxHeight}px - var(--uktdd-body-buffer))`,
464
- }
465
- : null)), (outOfBounds.maxWidth != null && outOfBounds.maxWidth > 0
466
- ? {
467
- [BODY_MAX_WIDTH_VAR]: `calc(${outOfBounds.maxWidth}px - var(--uktdd-body-buffer))`,
468
- }
469
- : null))), [outOfBounds.maxHeight, outOfBounds.maxWidth, styleFromProps]);
470
- return (React.createElement(Fragment, null,
471
- React.createElement(Style, { href: "@acusti/dropdown/Dropdown" }, STYLES),
472
- React.createElement("div", { className: clsx(ROOT_CLASS_NAME, className, {
473
- disabled,
474
- 'is-open': isOpen,
475
- 'is-searchable': isSearchable,
476
- }), onClick: onClick, onMouseDown: handleMouseDown, onMouseMove: handleMouseMove, onMouseOut: handleMouseOut, onMouseOver: handleMouseOver, onMouseUp: handleMouseUp, ref: handleRef, style: style },
477
- trigger,
478
- isOpen ? (React.createElement("div", { className: clsx(BODY_CLASS_NAME, {
479
- 'calculating-position': !outOfBounds.hasLayout,
480
- 'has-items': hasItems,
481
- 'out-of-bounds-bottom': outOfBounds.bottom && !outOfBounds.top,
482
- 'out-of-bounds-left': outOfBounds.left && !outOfBounds.right,
483
- 'out-of-bounds-right': outOfBounds.right && !outOfBounds.left,
484
- 'out-of-bounds-top': outOfBounds.top && !outOfBounds.bottom,
485
- }), ref: setDropdownBodyElement }, childrenCount > 1 ? children[1] : children)) : null)));
849
+ let t202;
850
+ if ($[59] !== t192 || $[60] !== trigger) {
851
+ t202 = /* @__PURE__ */ jsxs("label", { className: LABEL_CLASS_NAME, children: [
852
+ t192,
853
+ trigger
854
+ ] });
855
+ $[59] = t192;
856
+ $[60] = trigger;
857
+ $[61] = t202;
858
+ } else {
859
+ t202 = $[61];
860
+ }
861
+ trigger = t202;
862
+ }
863
+ let t19;
864
+ if ($[62] !== outOfBounds.maxHeight) {
865
+ t19 = outOfBounds.maxHeight != null && outOfBounds.maxHeight > 0 ? {
866
+ [BODY_MAX_HEIGHT_VAR]: `calc(${outOfBounds.maxHeight}px - var(--uktdd-body-buffer))`
867
+ } : null;
868
+ $[62] = outOfBounds.maxHeight;
869
+ $[63] = t19;
870
+ } else {
871
+ t19 = $[63];
872
+ }
873
+ let t20;
874
+ if ($[64] !== outOfBounds.maxWidth) {
875
+ t20 = outOfBounds.maxWidth != null && outOfBounds.maxWidth > 0 ? {
876
+ [BODY_MAX_WIDTH_VAR]: `calc(${outOfBounds.maxWidth}px - var(--uktdd-body-buffer))`
877
+ } : null;
878
+ $[64] = outOfBounds.maxWidth;
879
+ $[65] = t20;
880
+ } else {
881
+ t20 = $[65];
882
+ }
883
+ let t21;
884
+ if ($[66] !== styleFromProps || $[67] !== t19 || $[68] !== t20) {
885
+ t21 = {
886
+ ...styleFromProps,
887
+ ...t19,
888
+ ...t20
889
+ };
890
+ $[66] = styleFromProps;
891
+ $[67] = t19;
892
+ $[68] = t20;
893
+ $[69] = t21;
894
+ } else {
895
+ t21 = $[69];
896
+ }
897
+ const style = t21;
898
+ let t22;
899
+ if ($[70] === Symbol.for("react.memo_cache_sentinel")) {
900
+ t22 = /* @__PURE__ */ jsx(Style, { href: "@acusti/dropdown/Dropdown", children: STYLES });
901
+ $[70] = t22;
902
+ } else {
903
+ t22 = $[70];
904
+ }
905
+ let t23;
906
+ if ($[71] !== className || $[72] !== disabled || $[73] !== isOpen || $[74] !== isSearchable) {
907
+ t23 = clsx(ROOT_CLASS_NAME, className, {
908
+ disabled,
909
+ "is-open": isOpen,
910
+ "is-searchable": isSearchable
911
+ });
912
+ $[71] = className;
913
+ $[72] = disabled;
914
+ $[73] = isOpen;
915
+ $[74] = isSearchable;
916
+ $[75] = t23;
917
+ } else {
918
+ t23 = $[75];
919
+ }
920
+ let t24;
921
+ if ($[76] !== children || $[77] !== childrenCount || $[78] !== hasItems || $[79] !== isOpen || $[80] !== outOfBounds.bottom || $[81] !== outOfBounds.hasLayout || $[82] !== outOfBounds.left || $[83] !== outOfBounds.right || $[84] !== outOfBounds.top || $[85] !== setDropdownBodyElement) {
922
+ t24 = isOpen ? /* @__PURE__ */ jsx("div", { className: clsx(BODY_CLASS_NAME, {
923
+ "calculating-position": !outOfBounds.hasLayout,
924
+ "has-items": hasItems,
925
+ "out-of-bounds-bottom": outOfBounds.bottom && !outOfBounds.top,
926
+ "out-of-bounds-left": outOfBounds.left && !outOfBounds.right,
927
+ "out-of-bounds-right": outOfBounds.right && !outOfBounds.left,
928
+ "out-of-bounds-top": outOfBounds.top && !outOfBounds.bottom
929
+ }), ref: setDropdownBodyElement, children: childrenCount > 1 ? children[1] : children }) : null;
930
+ $[76] = children;
931
+ $[77] = childrenCount;
932
+ $[78] = hasItems;
933
+ $[79] = isOpen;
934
+ $[80] = outOfBounds.bottom;
935
+ $[81] = outOfBounds.hasLayout;
936
+ $[82] = outOfBounds.left;
937
+ $[83] = outOfBounds.right;
938
+ $[84] = outOfBounds.top;
939
+ $[85] = setDropdownBodyElement;
940
+ $[86] = t24;
941
+ } else {
942
+ t24 = $[86];
943
+ }
944
+ let t25;
945
+ if ($[87] !== handleMouseDown || $[88] !== handleMouseMove || $[89] !== handleMouseUp || $[90] !== handleRef || $[91] !== onClick || $[92] !== style || $[93] !== t23 || $[94] !== t24 || $[95] !== trigger) {
946
+ t25 = /* @__PURE__ */ jsxs(Fragment, { children: [
947
+ t22,
948
+ /* @__PURE__ */ jsxs("div", { className: t23, onClick, onMouseDown: handleMouseDown, onMouseMove: handleMouseMove, onMouseOut: handleMouseOut, onMouseOver: handleMouseOver, onMouseUp: handleMouseUp, ref: handleRef, style, children: [
949
+ trigger,
950
+ t24
951
+ ] })
952
+ ] });
953
+ $[87] = handleMouseDown;
954
+ $[88] = handleMouseMove;
955
+ $[89] = handleMouseUp;
956
+ $[90] = handleRef;
957
+ $[91] = onClick;
958
+ $[92] = style;
959
+ $[93] = t23;
960
+ $[94] = t24;
961
+ $[95] = trigger;
962
+ $[96] = t25;
963
+ } else {
964
+ t25 = $[96];
965
+ }
966
+ return t25;
486
967
  }
487
- //# sourceMappingURL=Dropdown.js.map
968
+ export {
969
+ Dropdown as default
970
+ };
971
+ //# sourceMappingURL=Dropdown.js.map