@acusti/dropdown 0.15.0 → 0.16.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 +349 -269
- package/dist/Dropdown.js.flow +39 -39
- package/dist/helpers.d.ts +47 -32
- package/dist/helpers.js +35 -36
- package/dist/helpers.js.flow +36 -36
- package/dist/styles.d.ts +1 -1
- package/dist/styles.js +1 -1
- package/dist/styles.js.flow +1 -1
- package/package.json +6 -6
- package/src/Dropdown.tsx +2 -0
package/dist/Dropdown.js
CHANGED
|
@@ -3,12 +3,41 @@ import { Style } from '@acusti/styling';
|
|
|
3
3
|
import useIsOutOfBounds from '@acusti/use-is-out-of-bounds';
|
|
4
4
|
import classnames from 'classnames';
|
|
5
5
|
import * as React from 'react';
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import {
|
|
7
|
+
BODY_CLASS_NAME,
|
|
8
|
+
BODY_SELECTOR,
|
|
9
|
+
LABEL_CLASS_NAME,
|
|
10
|
+
LABEL_TEXT_CLASS_NAME,
|
|
11
|
+
ROOT_CLASS_NAME,
|
|
12
|
+
STYLES,
|
|
13
|
+
TRIGGER_CLASS_NAME,
|
|
14
|
+
} from './styles.js';
|
|
15
|
+
import {
|
|
16
|
+
getActiveItemElement,
|
|
17
|
+
getItemElements,
|
|
18
|
+
ITEM_SELECTOR,
|
|
19
|
+
KEY_EVENT_ELEMENTS,
|
|
20
|
+
setActiveItem,
|
|
21
|
+
} from './helpers.js';
|
|
8
22
|
const { Children, Fragment, useCallback, useLayoutEffect, useRef, useState } = React;
|
|
9
|
-
const noop = () => {
|
|
10
|
-
const CHILDREN_ERROR =
|
|
11
|
-
|
|
23
|
+
const noop = () => {};
|
|
24
|
+
const CHILDREN_ERROR =
|
|
25
|
+
'@acusti/dropdown requires either 1 child (the dropdown body) or 2 children: the dropdown trigger and the dropdown body.';
|
|
26
|
+
const Dropdown = ({
|
|
27
|
+
allowEmpty = true,
|
|
28
|
+
children,
|
|
29
|
+
className,
|
|
30
|
+
disabled,
|
|
31
|
+
hasItems = true,
|
|
32
|
+
isOpenOnMount,
|
|
33
|
+
isSearchable,
|
|
34
|
+
label,
|
|
35
|
+
name,
|
|
36
|
+
onSubmitItem,
|
|
37
|
+
placeholder,
|
|
38
|
+
tabIndex,
|
|
39
|
+
value,
|
|
40
|
+
}) => {
|
|
12
41
|
const childrenCount = Children.count(children);
|
|
13
42
|
if (childrenCount !== 1 && childrenCount !== 2) {
|
|
14
43
|
if (childrenCount === 0) {
|
|
@@ -70,30 +99,39 @@ const Dropdown = ({ allowEmpty = true, children, className, disabled, hasItems =
|
|
|
70
99
|
// doesn’t close before expected. It also enables using <Link />s in the dropdown body.
|
|
71
100
|
closingTimerRef.current = setTimeout(closeDropdown, 90);
|
|
72
101
|
}
|
|
73
|
-
if (!hasItemsRef.current)
|
|
74
|
-
return;
|
|
102
|
+
if (!hasItemsRef.current) return;
|
|
75
103
|
const nextElement = getActiveItemElement(dropdownElementRef.current);
|
|
76
104
|
if (!nextElement) {
|
|
77
105
|
// If not allowEmpty, don’t allow submitting an empty item
|
|
78
|
-
if (!allowEmptyRef.current)
|
|
79
|
-
return;
|
|
106
|
+
if (!allowEmptyRef.current) return;
|
|
80
107
|
// If we have an input element as trigger & the user didn’t clear the text, do nothing
|
|
81
|
-
if (
|
|
108
|
+
if (
|
|
109
|
+
(_a = inputElementRef.current) === null || _a === void 0
|
|
110
|
+
? void 0
|
|
111
|
+
: _a.value
|
|
112
|
+
)
|
|
82
113
|
return;
|
|
83
114
|
}
|
|
84
|
-
const label =
|
|
85
|
-
|
|
115
|
+
const label =
|
|
116
|
+
(nextElement === null || nextElement === void 0
|
|
117
|
+
? void 0
|
|
118
|
+
: nextElement.innerText) || '';
|
|
119
|
+
const nextValue =
|
|
120
|
+
(nextElement === null || nextElement === void 0
|
|
121
|
+
? void 0
|
|
122
|
+
: nextElement.dataset.uktValue) || label;
|
|
86
123
|
const nextItem = { element: nextElement, value: nextValue };
|
|
87
124
|
if (inputElementRef.current) {
|
|
88
125
|
inputElementRef.current.value = label;
|
|
89
|
-
if (
|
|
90
|
-
inputElementRef.current
|
|
126
|
+
if (
|
|
127
|
+
inputElementRef.current ===
|
|
128
|
+
inputElementRef.current.ownerDocument.activeElement
|
|
129
|
+
) {
|
|
91
130
|
inputElementRef.current.blur();
|
|
92
131
|
}
|
|
93
132
|
}
|
|
94
133
|
// If parent is controlling Dropdown via props.value and nextValue is the same, do nothing
|
|
95
|
-
if (valueRef.current && valueRef.current === nextValue)
|
|
96
|
-
return;
|
|
134
|
+
if (valueRef.current && valueRef.current === nextValue) return;
|
|
97
135
|
if (onSubmitItemRef.current) {
|
|
98
136
|
onSubmitItemRef.current(nextItem);
|
|
99
137
|
}
|
|
@@ -101,27 +139,24 @@ const Dropdown = ({ allowEmpty = true, children, className, disabled, hasItems =
|
|
|
101
139
|
const handleMouseMove = useCallback(({ clientX, clientY }) => {
|
|
102
140
|
currentInputMethodRef.current = 'mouse';
|
|
103
141
|
const initialPosition = mouseDownPositionRef.current;
|
|
104
|
-
if (!initialPosition)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
Math.abs(initialPosition.clientY - clientY) < 12
|
|
142
|
+
if (!initialPosition) return;
|
|
143
|
+
if (
|
|
144
|
+
Math.abs(initialPosition.clientX - clientX) < 12 &&
|
|
145
|
+
Math.abs(initialPosition.clientY - clientY) < 12
|
|
146
|
+
) {
|
|
108
147
|
return;
|
|
109
148
|
}
|
|
110
149
|
setIsOpening(false);
|
|
111
150
|
}, []);
|
|
112
151
|
const handleMouseOver = useCallback((event) => {
|
|
113
|
-
if (!hasItemsRef.current)
|
|
114
|
-
return;
|
|
152
|
+
if (!hasItemsRef.current) return;
|
|
115
153
|
// If user isn’t currently using the mouse to navigate the dropdown, do nothing
|
|
116
|
-
if (currentInputMethodRef.current !== 'mouse')
|
|
117
|
-
return;
|
|
154
|
+
if (currentInputMethodRef.current !== 'mouse') return;
|
|
118
155
|
// Ensure we have the dropdown root HTMLElement
|
|
119
156
|
const dropdownElement = dropdownElementRef.current;
|
|
120
|
-
if (!dropdownElement)
|
|
121
|
-
return;
|
|
157
|
+
if (!dropdownElement) return;
|
|
122
158
|
const itemElements = getItemElements(dropdownElement);
|
|
123
|
-
if (!itemElements)
|
|
124
|
-
return;
|
|
159
|
+
if (!itemElements) return;
|
|
125
160
|
const eventTarget = event.target;
|
|
126
161
|
const item = eventTarget.closest(ITEM_SELECTOR);
|
|
127
162
|
const element = item || eventTarget;
|
|
@@ -136,282 +171,327 @@ const Dropdown = ({ allowEmpty = true, children, className, disabled, hasItems =
|
|
|
136
171
|
}
|
|
137
172
|
}, []);
|
|
138
173
|
const cleanupEventListenersRef = useRef(noop);
|
|
139
|
-
const handleRef = useCallback(
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
const { ownerDocument } = ref;
|
|
148
|
-
let inputElement = inputElementRef.current;
|
|
149
|
-
// Check if trigger from props is an input or textarea element
|
|
150
|
-
if (isTriggerFromProps && !inputElement && ref.firstElementChild) {
|
|
151
|
-
inputElement = ref.firstElementChild.querySelector('input:not([type=radio]):not([type=checkbox]):not([type=range]),textarea');
|
|
152
|
-
inputElementRef.current = inputElement;
|
|
153
|
-
}
|
|
154
|
-
const handleMouseDown = ({ clientX, clientY, target }) => {
|
|
155
|
-
const eventTarget = target;
|
|
156
|
-
if (dropdownElementRef.current &&
|
|
157
|
-
!dropdownElementRef.current.contains(eventTarget)) {
|
|
158
|
-
// Close dropdown on an outside click
|
|
159
|
-
closeDropdown();
|
|
174
|
+
const handleRef = useCallback(
|
|
175
|
+
(ref) => {
|
|
176
|
+
dropdownElementRef.current = ref;
|
|
177
|
+
if (!ref) {
|
|
178
|
+
// If component was unmounted, cleanup handlers
|
|
179
|
+
cleanupEventListenersRef.current();
|
|
180
|
+
cleanupEventListenersRef.current = noop;
|
|
160
181
|
return;
|
|
161
182
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}, 1000);
|
|
171
|
-
};
|
|
172
|
-
const handleMouseUp = ({ target }) => {
|
|
173
|
-
if (!isOpenRef.current || closingTimerRef.current)
|
|
174
|
-
return;
|
|
175
|
-
// If still isOpening (gets set false 1s after open triggers), set it to false onMouseUp
|
|
176
|
-
if (isOpeningRef.current) {
|
|
177
|
-
setIsOpening(false);
|
|
178
|
-
if (isOpeningTimerRef.current) {
|
|
179
|
-
clearTimeout(isOpeningTimerRef.current);
|
|
180
|
-
isOpeningTimerRef.current = null;
|
|
181
|
-
}
|
|
182
|
-
return;
|
|
183
|
+
const { ownerDocument } = ref;
|
|
184
|
+
let inputElement = inputElementRef.current;
|
|
185
|
+
// Check if trigger from props is an input or textarea element
|
|
186
|
+
if (isTriggerFromProps && !inputElement && ref.firstElementChild) {
|
|
187
|
+
inputElement = ref.firstElementChild.querySelector(
|
|
188
|
+
'input:not([type=radio]):not([type=checkbox]):not([type=range]),textarea',
|
|
189
|
+
);
|
|
190
|
+
inputElementRef.current = inputElement;
|
|
183
191
|
}
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
else if (!inputElementRef.current ||
|
|
193
|
-
(dropdownElementRef.current &&
|
|
194
|
-
dropdownElementRef.current.contains(ownerDocument.activeElement))) {
|
|
195
|
-
// If dropdown is searchable and ref is still focused, this won’t be invoked
|
|
196
|
-
closeDropdown();
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
const handleKeyDown = (event) => {
|
|
200
|
-
const { altKey, ctrlKey, key, metaKey } = event;
|
|
201
|
-
const eventTarget = event.target;
|
|
202
|
-
const dropdownElement = dropdownElementRef.current;
|
|
203
|
-
if (!dropdownElement)
|
|
204
|
-
return;
|
|
205
|
-
const onEventHandled = () => {
|
|
206
|
-
event.stopPropagation();
|
|
207
|
-
event.preventDefault();
|
|
208
|
-
currentInputMethodRef.current = 'keyboard';
|
|
209
|
-
};
|
|
210
|
-
const isEventTargetingDropdown = dropdownElement.contains(eventTarget);
|
|
211
|
-
if (!isOpenRef.current) {
|
|
212
|
-
// If dropdown is closed, don’t handle key events if event target isn’t within dropdown
|
|
213
|
-
if (!isEventTargetingDropdown)
|
|
214
|
-
return;
|
|
215
|
-
// Open the dropdown on spacebar, enter, or if isSearchable and user hits the up/down arrows
|
|
216
|
-
if (key === ' ' ||
|
|
217
|
-
key === 'Enter' ||
|
|
218
|
-
(hasItemsRef.current &&
|
|
219
|
-
(key === 'ArrowUp' || key === 'ArrowDown'))) {
|
|
220
|
-
onEventHandled();
|
|
221
|
-
setIsOpen(true);
|
|
192
|
+
const handleMouseDown = ({ clientX, clientY, target }) => {
|
|
193
|
+
const eventTarget = target;
|
|
194
|
+
if (
|
|
195
|
+
dropdownElementRef.current &&
|
|
196
|
+
!dropdownElementRef.current.contains(eventTarget)
|
|
197
|
+
) {
|
|
198
|
+
// Close dropdown on an outside click
|
|
199
|
+
closeDropdown();
|
|
222
200
|
return;
|
|
223
201
|
}
|
|
224
|
-
return;
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if (
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
enteredCharactersRef.current += key;
|
|
242
|
-
}
|
|
243
|
-
setActiveItem({
|
|
244
|
-
dropdownElement,
|
|
245
|
-
// If input element came from props, only override the input’s value
|
|
246
|
-
// with an exact text match so user can enter a value not in items
|
|
247
|
-
isExactMatch: isTriggerFromPropsRef.current,
|
|
248
|
-
text: enteredCharactersRef.current,
|
|
249
|
-
});
|
|
250
|
-
if (clearEnteredCharactersTimerRef.current) {
|
|
251
|
-
clearTimeout(clearEnteredCharactersTimerRef.current);
|
|
202
|
+
if (isOpenRef.current) return;
|
|
203
|
+
setIsOpen(true);
|
|
204
|
+
setIsOpening(true);
|
|
205
|
+
mouseDownPositionRef.current = { clientX, clientY };
|
|
206
|
+
isOpeningTimerRef.current = setTimeout(() => {
|
|
207
|
+
setIsOpening(false);
|
|
208
|
+
isOpeningTimerRef.current = null;
|
|
209
|
+
}, 1000);
|
|
210
|
+
};
|
|
211
|
+
const handleMouseUp = ({ target }) => {
|
|
212
|
+
if (!isOpenRef.current || closingTimerRef.current) return;
|
|
213
|
+
// If still isOpening (gets set false 1s after open triggers), set it to false onMouseUp
|
|
214
|
+
if (isOpeningRef.current) {
|
|
215
|
+
setIsOpening(false);
|
|
216
|
+
if (isOpeningTimerRef.current) {
|
|
217
|
+
clearTimeout(isOpeningTimerRef.current);
|
|
218
|
+
isOpeningTimerRef.current = null;
|
|
252
219
|
}
|
|
253
|
-
clearEnteredCharactersTimerRef.current = setTimeout(() => {
|
|
254
|
-
enteredCharactersRef.current = '';
|
|
255
|
-
clearEnteredCharactersTimerRef.current = null;
|
|
256
|
-
}, 1500);
|
|
257
220
|
return;
|
|
258
221
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
(
|
|
272
|
-
|
|
222
|
+
const isTargetInBody = target.closest(BODY_SELECTOR);
|
|
223
|
+
// If mouseup is on dropdown body and there are no items, don’t close the dropdown
|
|
224
|
+
if (!hasItemsRef.current && isTargetInBody) return;
|
|
225
|
+
// If mouseup is on an item, trigger submit item, else close the dropdown
|
|
226
|
+
if (isTargetInBody) {
|
|
227
|
+
handleSubmitItem();
|
|
228
|
+
} else if (
|
|
229
|
+
!inputElementRef.current ||
|
|
230
|
+
(dropdownElementRef.current &&
|
|
231
|
+
dropdownElementRef.current.contains(ownerDocument.activeElement))
|
|
232
|
+
) {
|
|
233
|
+
// If dropdown is searchable and ref is still focused, this won’t be invoked
|
|
234
|
+
closeDropdown();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const handleKeyDown = (event) => {
|
|
238
|
+
const { altKey, ctrlKey, key, metaKey } = event;
|
|
239
|
+
const eventTarget = event.target;
|
|
240
|
+
const dropdownElement = dropdownElementRef.current;
|
|
241
|
+
if (!dropdownElement) return;
|
|
242
|
+
const onEventHandled = () => {
|
|
243
|
+
event.stopPropagation();
|
|
244
|
+
event.preventDefault();
|
|
245
|
+
currentInputMethodRef.current = 'keyboard';
|
|
246
|
+
};
|
|
247
|
+
const isEventTargetingDropdown = dropdownElement.contains(eventTarget);
|
|
248
|
+
if (!isOpenRef.current) {
|
|
249
|
+
// If dropdown is closed, don’t handle key events if event target isn’t within dropdown
|
|
250
|
+
if (!isEventTargetingDropdown) return;
|
|
251
|
+
// Open the dropdown on spacebar, enter, or if isSearchable and user hits the up/down arrows
|
|
252
|
+
if (
|
|
253
|
+
key === ' ' ||
|
|
254
|
+
key === 'Enter' ||
|
|
255
|
+
(hasItemsRef.current &&
|
|
256
|
+
(key === 'ArrowUp' || key === 'ArrowDown'))
|
|
257
|
+
) {
|
|
258
|
+
onEventHandled();
|
|
259
|
+
setIsOpen(true);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
273
262
|
return;
|
|
274
263
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
setActiveItem({
|
|
284
|
-
dropdownElement,
|
|
285
|
-
index: 0,
|
|
286
|
-
});
|
|
264
|
+
// If dropdown isOpen, hasItems, and not isSearchable, handle entering characters
|
|
265
|
+
if (hasItemsRef.current && !inputElementRef.current) {
|
|
266
|
+
let isEditingCharacters =
|
|
267
|
+
!ctrlKey && !metaKey && /^[A-Za-z0-9]$/.test(key);
|
|
268
|
+
// User could also be editing characters if there are already characters entered
|
|
269
|
+
// and they are hitting delete or spacebar
|
|
270
|
+
if (!isEditingCharacters && enteredCharactersRef.current) {
|
|
271
|
+
isEditingCharacters = key === ' ' || key === 'Backspace';
|
|
287
272
|
}
|
|
288
|
-
|
|
273
|
+
if (isEditingCharacters) {
|
|
274
|
+
onEventHandled();
|
|
275
|
+
if (key === 'Backspace') {
|
|
276
|
+
enteredCharactersRef.current =
|
|
277
|
+
enteredCharactersRef.current.slice(0, -1);
|
|
278
|
+
} else {
|
|
279
|
+
enteredCharactersRef.current += key;
|
|
280
|
+
}
|
|
289
281
|
setActiveItem({
|
|
290
282
|
dropdownElement,
|
|
291
|
-
|
|
283
|
+
// If input element came from props, only override the input’s value
|
|
284
|
+
// with an exact text match so user can enter a value not in items
|
|
285
|
+
isExactMatch: isTriggerFromPropsRef.current,
|
|
286
|
+
text: enteredCharactersRef.current,
|
|
292
287
|
});
|
|
288
|
+
if (clearEnteredCharactersTimerRef.current) {
|
|
289
|
+
clearTimeout(clearEnteredCharactersTimerRef.current);
|
|
290
|
+
}
|
|
291
|
+
clearEnteredCharactersTimerRef.current = setTimeout(() => {
|
|
292
|
+
enteredCharactersRef.current = '';
|
|
293
|
+
clearEnteredCharactersTimerRef.current = null;
|
|
294
|
+
}, 1500);
|
|
295
|
+
return;
|
|
293
296
|
}
|
|
294
|
-
return;
|
|
295
297
|
}
|
|
296
|
-
|
|
298
|
+
// If dropdown isOpen, handle submitting the value
|
|
299
|
+
if (key === 'Enter' || (key === ' ' && !inputElementRef.current)) {
|
|
297
300
|
onEventHandled();
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
301
|
+
handleSubmitItem();
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
// If dropdown isOpen, handle closing it on escape or spacebar if !hasItems
|
|
305
|
+
if (
|
|
306
|
+
key === 'Escape' ||
|
|
307
|
+
(isEventTargetingDropdown && key === ' ' && !hasItemsRef.current)
|
|
308
|
+
) {
|
|
309
|
+
// If there are no items & event target element uses key events, don’t close it
|
|
310
|
+
if (
|
|
311
|
+
!hasItemsRef.current &&
|
|
312
|
+
(eventTarget.isContentEditable ||
|
|
313
|
+
KEY_EVENT_ELEMENTS.has(eventTarget.tagName))
|
|
314
|
+
) {
|
|
315
|
+
return;
|
|
304
316
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
317
|
+
closeDropdown();
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
// Handle ↑/↓ arrows
|
|
321
|
+
if (hasItemsRef.current) {
|
|
322
|
+
if (key === 'ArrowUp') {
|
|
323
|
+
onEventHandled();
|
|
324
|
+
if (altKey || metaKey) {
|
|
325
|
+
setActiveItem({
|
|
326
|
+
dropdownElement,
|
|
327
|
+
index: 0,
|
|
328
|
+
});
|
|
329
|
+
} else {
|
|
330
|
+
setActiveItem({
|
|
331
|
+
dropdownElement,
|
|
332
|
+
indexAddend: -1,
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (key === 'ArrowDown') {
|
|
338
|
+
onEventHandled();
|
|
339
|
+
if (altKey || metaKey) {
|
|
340
|
+
// Using a negative index counts back from the end
|
|
341
|
+
setActiveItem({
|
|
342
|
+
dropdownElement,
|
|
343
|
+
index: -1,
|
|
344
|
+
});
|
|
345
|
+
} else {
|
|
346
|
+
setActiveItem({
|
|
347
|
+
dropdownElement,
|
|
348
|
+
indexAddend: 1,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
return;
|
|
310
352
|
}
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
// Close dropdown if any element is focused outside of this dropdown
|
|
356
|
+
const handleFocusIn = (event) => {
|
|
357
|
+
if (!isOpenRef.current) return;
|
|
358
|
+
const eventTarget = event.target;
|
|
359
|
+
// If focused element is a descendant or a parent of the dropdown, do nothing
|
|
360
|
+
if (
|
|
361
|
+
!dropdownElementRef.current ||
|
|
362
|
+
dropdownElementRef.current.contains(eventTarget) ||
|
|
363
|
+
eventTarget.contains(dropdownElementRef.current)
|
|
364
|
+
) {
|
|
311
365
|
return;
|
|
312
366
|
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
const eventTarget = event.target;
|
|
320
|
-
// If focused element is a descendant or a parent of the dropdown, do nothing
|
|
321
|
-
if (!dropdownElementRef.current ||
|
|
322
|
-
dropdownElementRef.current.contains(eventTarget) ||
|
|
323
|
-
eventTarget.contains(dropdownElementRef.current)) {
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
326
|
-
closeDropdown();
|
|
327
|
-
};
|
|
328
|
-
document.addEventListener('focusin', handleFocusIn);
|
|
329
|
-
document.addEventListener('keydown', handleKeyDown);
|
|
330
|
-
document.addEventListener('mousedown', handleMouseDown);
|
|
331
|
-
document.addEventListener('mouseup', handleMouseUp);
|
|
332
|
-
if (ownerDocument !== document) {
|
|
333
|
-
ownerDocument.addEventListener('focusin', handleFocusIn);
|
|
334
|
-
ownerDocument.addEventListener('keydown', handleKeyDown);
|
|
335
|
-
ownerDocument.addEventListener('mousedown', handleMouseDown);
|
|
336
|
-
ownerDocument.addEventListener('mouseup', handleMouseUp);
|
|
337
|
-
}
|
|
338
|
-
// If dropdown should be open on mount, focus it
|
|
339
|
-
if (isOpenOnMount) {
|
|
340
|
-
ref.focus();
|
|
341
|
-
}
|
|
342
|
-
const handleInput = (event) => {
|
|
343
|
-
const dropdownElement = dropdownElementRef.current;
|
|
344
|
-
if (!dropdownElement)
|
|
345
|
-
return;
|
|
346
|
-
if (!isOpenRef.current)
|
|
347
|
-
setIsOpen(true);
|
|
348
|
-
const input = event.target;
|
|
349
|
-
const isDeleting = enteredCharactersRef.current.length > input.value.length;
|
|
350
|
-
enteredCharactersRef.current = input.value;
|
|
351
|
-
// Don’t set a new active item if user is deleting text unless text is now empty
|
|
352
|
-
if (isDeleting && input.value.length)
|
|
353
|
-
return;
|
|
354
|
-
setActiveItem({
|
|
355
|
-
dropdownElement,
|
|
356
|
-
// If input element came from props, only override the input’s value
|
|
357
|
-
// with an exact text match so user can enter a value not in items
|
|
358
|
-
isExactMatch: isTriggerFromPropsRef.current,
|
|
359
|
-
text: enteredCharactersRef.current,
|
|
360
|
-
});
|
|
361
|
-
};
|
|
362
|
-
if (inputElement) {
|
|
363
|
-
inputElement.addEventListener('input', handleInput);
|
|
364
|
-
}
|
|
365
|
-
cleanupEventListenersRef.current = () => {
|
|
366
|
-
document.removeEventListener('focusin', handleFocusIn);
|
|
367
|
-
document.removeEventListener('keydown', handleKeyDown);
|
|
368
|
-
document.removeEventListener('mousedown', handleMouseDown);
|
|
369
|
-
document.removeEventListener('mouseup', handleMouseUp);
|
|
367
|
+
closeDropdown();
|
|
368
|
+
};
|
|
369
|
+
document.addEventListener('focusin', handleFocusIn);
|
|
370
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
371
|
+
document.addEventListener('mousedown', handleMouseDown);
|
|
372
|
+
document.addEventListener('mouseup', handleMouseUp);
|
|
370
373
|
if (ownerDocument !== document) {
|
|
371
|
-
ownerDocument.
|
|
372
|
-
ownerDocument.
|
|
373
|
-
ownerDocument.
|
|
374
|
-
ownerDocument.
|
|
374
|
+
ownerDocument.addEventListener('focusin', handleFocusIn);
|
|
375
|
+
ownerDocument.addEventListener('keydown', handleKeyDown);
|
|
376
|
+
ownerDocument.addEventListener('mousedown', handleMouseDown);
|
|
377
|
+
ownerDocument.addEventListener('mouseup', handleMouseUp);
|
|
378
|
+
}
|
|
379
|
+
// If dropdown should be open on mount, focus it
|
|
380
|
+
if (isOpenOnMount) {
|
|
381
|
+
ref.focus();
|
|
375
382
|
}
|
|
383
|
+
const handleInput = (event) => {
|
|
384
|
+
const dropdownElement = dropdownElementRef.current;
|
|
385
|
+
if (!dropdownElement) return;
|
|
386
|
+
if (!isOpenRef.current) setIsOpen(true);
|
|
387
|
+
const input = event.target;
|
|
388
|
+
const isDeleting =
|
|
389
|
+
enteredCharactersRef.current.length > input.value.length;
|
|
390
|
+
enteredCharactersRef.current = input.value;
|
|
391
|
+
// Don’t set a new active item if user is deleting text unless text is now empty
|
|
392
|
+
if (isDeleting && input.value.length) return;
|
|
393
|
+
setActiveItem({
|
|
394
|
+
dropdownElement,
|
|
395
|
+
// If input element came from props, only override the input’s value
|
|
396
|
+
// with an exact text match so user can enter a value not in items
|
|
397
|
+
isExactMatch: isTriggerFromPropsRef.current,
|
|
398
|
+
text: enteredCharactersRef.current,
|
|
399
|
+
});
|
|
400
|
+
};
|
|
376
401
|
if (inputElement) {
|
|
377
|
-
inputElement.
|
|
402
|
+
inputElement.addEventListener('input', handleInput);
|
|
378
403
|
}
|
|
379
|
-
|
|
380
|
-
|
|
404
|
+
cleanupEventListenersRef.current = () => {
|
|
405
|
+
document.removeEventListener('focusin', handleFocusIn);
|
|
406
|
+
document.removeEventListener('keydown', handleKeyDown);
|
|
407
|
+
document.removeEventListener('mousedown', handleMouseDown);
|
|
408
|
+
document.removeEventListener('mouseup', handleMouseUp);
|
|
409
|
+
if (ownerDocument !== document) {
|
|
410
|
+
ownerDocument.removeEventListener('focusin', handleFocusIn);
|
|
411
|
+
ownerDocument.removeEventListener('keydown', handleKeyDown);
|
|
412
|
+
ownerDocument.removeEventListener('mousedown', handleMouseDown);
|
|
413
|
+
ownerDocument.removeEventListener('mouseup', handleMouseUp);
|
|
414
|
+
}
|
|
415
|
+
if (inputElement) {
|
|
416
|
+
inputElement.removeEventListener('input', handleInput);
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
},
|
|
420
|
+
[closeDropdown, handleSubmitItem, isOpenOnMount, isTriggerFromProps],
|
|
421
|
+
);
|
|
381
422
|
const handleTriggerFocus = useCallback(() => {
|
|
382
423
|
setIsOpen(true);
|
|
383
424
|
}, []);
|
|
384
425
|
if (!isTriggerFromProps) {
|
|
385
426
|
if (isSearchable) {
|
|
386
|
-
trigger =
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
427
|
+
trigger = React.createElement(InputText, {
|
|
428
|
+
className: TRIGGER_CLASS_NAME,
|
|
429
|
+
disabled: disabled,
|
|
430
|
+
initialValue: value || '',
|
|
431
|
+
name: name,
|
|
432
|
+
onFocus: handleTriggerFocus,
|
|
433
|
+
placeholder: placeholder,
|
|
434
|
+
ref: inputElementRef,
|
|
435
|
+
selectTextOnFocus: true,
|
|
436
|
+
tabIndex: tabIndex,
|
|
437
|
+
type: 'text',
|
|
438
|
+
});
|
|
439
|
+
} else {
|
|
440
|
+
trigger = React.createElement(
|
|
441
|
+
'button',
|
|
442
|
+
{ className: TRIGGER_CLASS_NAME, tabIndex: 0 },
|
|
443
|
+
trigger,
|
|
444
|
+
);
|
|
390
445
|
}
|
|
391
446
|
}
|
|
392
447
|
if (label) {
|
|
393
|
-
trigger =
|
|
394
|
-
|
|
395
|
-
|
|
448
|
+
trigger = React.createElement(
|
|
449
|
+
'label',
|
|
450
|
+
{ className: LABEL_CLASS_NAME },
|
|
451
|
+
React.createElement('div', { className: LABEL_TEXT_CLASS_NAME }, label),
|
|
452
|
+
trigger,
|
|
453
|
+
);
|
|
396
454
|
}
|
|
397
|
-
return
|
|
455
|
+
return React.createElement(
|
|
456
|
+
Fragment,
|
|
457
|
+
null,
|
|
398
458
|
React.createElement(Style, null, STYLES),
|
|
399
|
-
React.createElement(
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
459
|
+
React.createElement(
|
|
460
|
+
'div',
|
|
461
|
+
{
|
|
462
|
+
className: classnames(ROOT_CLASS_NAME, className, {
|
|
463
|
+
disabled,
|
|
464
|
+
'is-open': isOpen,
|
|
465
|
+
'is-searchable': isSearchable,
|
|
466
|
+
}),
|
|
467
|
+
onMouseMove: handleMouseMove,
|
|
468
|
+
onMouseOver: handleMouseOver,
|
|
469
|
+
ref: handleRef,
|
|
470
|
+
tabIndex:
|
|
471
|
+
isSearchable || inputElementRef.current || !isTriggerFromProps
|
|
472
|
+
? undefined
|
|
473
|
+
: 0,
|
|
474
|
+
},
|
|
406
475
|
trigger,
|
|
407
|
-
isOpen
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
476
|
+
isOpen
|
|
477
|
+
? React.createElement(
|
|
478
|
+
'div',
|
|
479
|
+
{
|
|
480
|
+
className: classnames(BODY_CLASS_NAME, {
|
|
481
|
+
'calculating-position': !outOfBounds.hasLayout,
|
|
482
|
+
'has-items': hasItems,
|
|
483
|
+
'out-of-bounds-bottom': outOfBounds.bottom,
|
|
484
|
+
'out-of-bounds-left': outOfBounds.left,
|
|
485
|
+
'out-of-bounds-right': outOfBounds.right,
|
|
486
|
+
'out-of-bounds-top': outOfBounds.top,
|
|
487
|
+
}),
|
|
488
|
+
ref: setDropdownBodyElement,
|
|
489
|
+
},
|
|
490
|
+
children[1] || children[0] || children,
|
|
491
|
+
)
|
|
492
|
+
: null,
|
|
493
|
+
),
|
|
494
|
+
);
|
|
415
495
|
};
|
|
416
496
|
export default Dropdown;
|
|
417
|
-
//# sourceMappingURL=Dropdown.js.map
|
|
497
|
+
//# sourceMappingURL=Dropdown.js.map
|
package/dist/Dropdown.js.flow
CHANGED
|
@@ -5,54 +5,54 @@
|
|
|
5
5
|
* @flow
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import * as React from
|
|
8
|
+
import * as React from 'react';
|
|
9
9
|
export type Item = {|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
element: HTMLElement | null,
|
|
11
|
+
value: string,
|
|
12
12
|
|};
|
|
13
13
|
export type Props = {|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Boolean indicating if the user can submit an empty value (i.e. clear the value); defaults to true
|
|
16
|
+
*/
|
|
17
|
+
allowEmpty?: boolean,
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Can take a single React element (e.g. ReactChild) or exactly two renderable children
|
|
21
|
+
*/
|
|
22
|
+
children: React.Element<any> | [React.Node, React.Node],
|
|
23
|
+
className?: string,
|
|
24
|
+
disabled?: boolean,
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Group identifier string links dropdowns together into a menu (like macOS top menubar)
|
|
28
|
+
*/
|
|
29
|
+
group?: string,
|
|
30
|
+
hasItems?: boolean,
|
|
31
|
+
isOpenOnMount?: boolean,
|
|
32
|
+
isSearchable?: boolean,
|
|
33
|
+
label?: string,
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Only usable in conjunction with {isSearchable: true}; used as search input’s name
|
|
37
|
+
*/
|
|
38
|
+
name?: string,
|
|
39
|
+
onSubmitItem?: (payload: Item) => void,
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Only usable in conjunction with {isSearchable: true}; used as search input’s placeholder
|
|
43
|
+
*/
|
|
44
|
+
placeholder?: string,
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Only usable in conjunction with {isSearchable: true}; used as search input’s tabIndex
|
|
48
|
+
*/
|
|
49
|
+
tabIndex?: number,
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Used as search input’s value if props.isSearchable === true
|
|
53
|
+
* Used to determine if value has changed to avoid triggering onSubmitItem if not
|
|
54
|
+
*/
|
|
55
|
+
value?: string,
|
|
56
56
|
|};
|
|
57
57
|
declare var Dropdown: React.StatelessFunctionalComponent<Props>;
|
|
58
58
|
declare export default typeof Dropdown;
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,33 +1,48 @@
|
|
|
1
|
-
export declare const ITEM_SELECTOR =
|
|
1
|
+
export declare const ITEM_SELECTOR = '[data-ukt-item], [data-ukt-value]';
|
|
2
2
|
export declare const KEY_EVENT_ELEMENTS: Set<string>;
|
|
3
|
-
export declare const getItemElements: (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
3
|
+
export declare const getItemElements: (
|
|
4
|
+
dropdownElement: HTMLElement | null,
|
|
5
|
+
) => NodeListOf<Element> | HTMLCollection | null;
|
|
6
|
+
export declare const getActiveItemElement: (
|
|
7
|
+
dropdownElement: HTMLElement | null,
|
|
8
|
+
) => HTMLElement | null;
|
|
9
|
+
export declare const setActiveItem: ({
|
|
10
|
+
dropdownElement,
|
|
11
|
+
element,
|
|
12
|
+
index,
|
|
13
|
+
indexAddend,
|
|
14
|
+
isExactMatch,
|
|
15
|
+
text,
|
|
16
|
+
}:
|
|
17
|
+
| {
|
|
18
|
+
dropdownElement: HTMLElement;
|
|
19
|
+
element: HTMLElement;
|
|
20
|
+
index?: null | undefined;
|
|
21
|
+
indexAddend?: null | undefined;
|
|
22
|
+
isExactMatch?: null | undefined;
|
|
23
|
+
text?: null | undefined;
|
|
24
|
+
}
|
|
25
|
+
| {
|
|
26
|
+
dropdownElement: HTMLElement;
|
|
27
|
+
element?: null | undefined;
|
|
28
|
+
index: number;
|
|
29
|
+
indexAddend?: null | undefined;
|
|
30
|
+
isExactMatch?: null | undefined;
|
|
31
|
+
text?: null | undefined;
|
|
32
|
+
}
|
|
33
|
+
| {
|
|
34
|
+
dropdownElement: HTMLElement;
|
|
35
|
+
element?: null | undefined;
|
|
36
|
+
index?: null | undefined;
|
|
37
|
+
indexAddend: number;
|
|
38
|
+
isExactMatch?: null | undefined;
|
|
39
|
+
text?: null | undefined;
|
|
40
|
+
}
|
|
41
|
+
| {
|
|
42
|
+
dropdownElement: HTMLElement;
|
|
43
|
+
element?: null | undefined;
|
|
44
|
+
index?: null | undefined;
|
|
45
|
+
indexAddend?: null | undefined;
|
|
46
|
+
isExactMatch?: boolean | undefined;
|
|
47
|
+
text: string;
|
|
48
|
+
}) => void;
|
package/dist/helpers.js
CHANGED
|
@@ -3,20 +3,16 @@ import { BODY_SELECTOR } from './styles.js';
|
|
|
3
3
|
export const ITEM_SELECTOR = `[data-ukt-item], [data-ukt-value]`;
|
|
4
4
|
export const KEY_EVENT_ELEMENTS = new Set(['INPUT', 'TEXTAREA']);
|
|
5
5
|
export const getItemElements = (dropdownElement) => {
|
|
6
|
-
if (!dropdownElement)
|
|
7
|
-
return null;
|
|
6
|
+
if (!dropdownElement) return null;
|
|
8
7
|
const bodyElement = dropdownElement.querySelector(BODY_SELECTOR);
|
|
9
|
-
if (!bodyElement)
|
|
10
|
-
return null;
|
|
8
|
+
if (!bodyElement) return null;
|
|
11
9
|
let items = bodyElement.querySelectorAll(ITEM_SELECTOR);
|
|
12
|
-
if (items.length)
|
|
13
|
-
return items;
|
|
10
|
+
if (items.length) return items;
|
|
14
11
|
// If no items found via [data-ukt-item] or [data-ukt-value] selector,
|
|
15
12
|
// use first instance of multiple children found
|
|
16
13
|
items = bodyElement.children;
|
|
17
14
|
while (items.length === 1) {
|
|
18
|
-
if (!items[0].children)
|
|
19
|
-
break;
|
|
15
|
+
if (!items[0].children) break;
|
|
20
16
|
items = items[0].children;
|
|
21
17
|
}
|
|
22
18
|
// If unable to find an element with more than one child, treat direct child as items
|
|
@@ -26,8 +22,7 @@ export const getItemElements = (dropdownElement) => {
|
|
|
26
22
|
return items;
|
|
27
23
|
};
|
|
28
24
|
export const getActiveItemElement = (dropdownElement) => {
|
|
29
|
-
if (!dropdownElement)
|
|
30
|
-
return null;
|
|
25
|
+
if (!dropdownElement) return null;
|
|
31
26
|
return dropdownElement.querySelector('[data-ukt-active]');
|
|
32
27
|
};
|
|
33
28
|
const clearItemElementsState = (itemElements) => {
|
|
@@ -37,40 +32,45 @@ const clearItemElementsState = (itemElements) => {
|
|
|
37
32
|
}
|
|
38
33
|
});
|
|
39
34
|
};
|
|
40
|
-
export const setActiveItem = ({
|
|
35
|
+
export const setActiveItem = ({
|
|
36
|
+
dropdownElement,
|
|
37
|
+
element,
|
|
38
|
+
index,
|
|
39
|
+
indexAddend,
|
|
40
|
+
isExactMatch,
|
|
41
|
+
text,
|
|
42
|
+
}) => {
|
|
41
43
|
const items = getItemElements(dropdownElement);
|
|
42
|
-
if (!items)
|
|
43
|
-
return;
|
|
44
|
+
if (!items) return;
|
|
44
45
|
const itemElements = Array.from(items);
|
|
45
|
-
if (!itemElements.length)
|
|
46
|
-
return;
|
|
46
|
+
if (!itemElements.length) return;
|
|
47
47
|
const lastIndex = itemElements.length - 1;
|
|
48
|
-
const currentActiveIndex = itemElements.findIndex((itemElement) =>
|
|
48
|
+
const currentActiveIndex = itemElements.findIndex((itemElement) =>
|
|
49
|
+
itemElement.hasAttribute('data-ukt-active'),
|
|
50
|
+
);
|
|
49
51
|
let nextActiveIndex = currentActiveIndex;
|
|
50
52
|
if (typeof index === 'number') {
|
|
51
53
|
// Negative index means count back from the end
|
|
52
54
|
nextActiveIndex = index < 0 ? itemElements.length + index : index;
|
|
53
55
|
}
|
|
54
56
|
if (element) {
|
|
55
|
-
nextActiveIndex = itemElements.findIndex(
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
nextActiveIndex = itemElements.findIndex(
|
|
58
|
+
(itemElement) => itemElement === element,
|
|
59
|
+
);
|
|
60
|
+
} else if (typeof indexAddend === 'number') {
|
|
58
61
|
// If there’s no currentActiveIndex and we are handling -1, start at lastIndex
|
|
59
62
|
if (currentActiveIndex === -1 && indexAddend === -1) {
|
|
60
63
|
nextActiveIndex = lastIndex;
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
64
|
+
} else {
|
|
63
65
|
nextActiveIndex += indexAddend;
|
|
64
66
|
}
|
|
65
67
|
// Keep it within the bounds of the items list
|
|
66
68
|
if (nextActiveIndex < 0) {
|
|
67
69
|
nextActiveIndex = 0;
|
|
68
|
-
}
|
|
69
|
-
else if (nextActiveIndex > lastIndex) {
|
|
70
|
+
} else if (nextActiveIndex > lastIndex) {
|
|
70
71
|
nextActiveIndex = lastIndex;
|
|
71
72
|
}
|
|
72
|
-
}
|
|
73
|
-
else if (typeof text === 'string') {
|
|
73
|
+
} else if (typeof text === 'string') {
|
|
74
74
|
// If text is empty, clear existing active items and early return
|
|
75
75
|
if (!text) {
|
|
76
76
|
clearItemElementsState(itemElements);
|
|
@@ -79,19 +79,19 @@ export const setActiveItem = ({ dropdownElement, element, index, indexAddend, is
|
|
|
79
79
|
const itemTexts = itemElements.map((itemElement) => itemElement.innerText);
|
|
80
80
|
if (isExactMatch) {
|
|
81
81
|
const textToCompare = text.toLowerCase();
|
|
82
|
-
nextActiveIndex = itemTexts.findIndex((itemText) =>
|
|
82
|
+
nextActiveIndex = itemTexts.findIndex((itemText) =>
|
|
83
|
+
itemText.toLowerCase().startsWith(textToCompare),
|
|
84
|
+
);
|
|
83
85
|
// If isExactMatch is required and no exact match was found, clear active items
|
|
84
86
|
if (nextActiveIndex === -1) {
|
|
85
87
|
clearItemElementsState(itemElements);
|
|
86
88
|
}
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
+
} else {
|
|
89
90
|
const bestMatch = getBestMatch({ items: itemTexts, text });
|
|
90
91
|
nextActiveIndex = itemTexts.findIndex((text) => text === bestMatch);
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
|
-
if (nextActiveIndex === -1 || nextActiveIndex === currentActiveIndex)
|
|
94
|
-
return;
|
|
94
|
+
if (nextActiveIndex === -1 || nextActiveIndex === currentActiveIndex) return;
|
|
95
95
|
// Clear any existing active dropdown body item state
|
|
96
96
|
clearItemElementsState(itemElements);
|
|
97
97
|
const nextActiveItem = items[nextActiveIndex];
|
|
@@ -101,11 +101,11 @@ export const setActiveItem = ({ dropdownElement, element, index, indexAddend, is
|
|
|
101
101
|
let { parentElement } = nextActiveItem;
|
|
102
102
|
let scrollableParent = null;
|
|
103
103
|
while (!scrollableParent && parentElement && parentElement !== dropdownElement) {
|
|
104
|
-
const isScrollable =
|
|
104
|
+
const isScrollable =
|
|
105
|
+
parentElement.scrollHeight > parentElement.clientHeight + 15;
|
|
105
106
|
if (isScrollable) {
|
|
106
107
|
scrollableParent = parentElement;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
108
|
+
} else {
|
|
109
109
|
parentElement = parentElement.parentElement;
|
|
110
110
|
}
|
|
111
111
|
}
|
|
@@ -119,8 +119,7 @@ export const setActiveItem = ({ dropdownElement, element, index, indexAddend, is
|
|
|
119
119
|
// Item isn’t fully visible; adjust scrollTop to put item within closest edge
|
|
120
120
|
if (isAboveTop) {
|
|
121
121
|
scrollTop -= parentRect.top - itemRect.top;
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
122
|
+
} else {
|
|
124
123
|
scrollTop += itemRect.bottom - parentRect.bottom;
|
|
125
124
|
}
|
|
126
125
|
scrollableParent.scrollTop = scrollTop;
|
|
@@ -128,4 +127,4 @@ export const setActiveItem = ({ dropdownElement, element, index, indexAddend, is
|
|
|
128
127
|
}
|
|
129
128
|
}
|
|
130
129
|
};
|
|
131
|
-
//# sourceMappingURL=helpers.js.map
|
|
130
|
+
//# sourceMappingURL=helpers.js.map
|
package/dist/helpers.js.flow
CHANGED
|
@@ -5,46 +5,46 @@
|
|
|
5
5
|
* @flow
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
declare export var ITEM_SELECTOR:
|
|
8
|
+
declare export var ITEM_SELECTOR: '[data-ukt-item], [data-ukt-value]';
|
|
9
9
|
declare export var KEY_EVENT_ELEMENTS: Set<string>;
|
|
10
10
|
declare export var getItemElements: (
|
|
11
|
-
|
|
11
|
+
dropdownElement: HTMLElement | null,
|
|
12
12
|
) => NodeListOf<Element> | HTMLCollection | null;
|
|
13
13
|
declare export var getActiveItemElement: (
|
|
14
|
-
|
|
14
|
+
dropdownElement: HTMLElement | null,
|
|
15
15
|
) => HTMLElement | null;
|
|
16
16
|
declare export var setActiveItem: (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
17
|
+
x:
|
|
18
|
+
| {|
|
|
19
|
+
dropdownElement: HTMLElement,
|
|
20
|
+
element: HTMLElement,
|
|
21
|
+
index?: null | void,
|
|
22
|
+
indexAddend?: null | void,
|
|
23
|
+
isExactMatch?: null | void,
|
|
24
|
+
text?: null | void,
|
|
25
|
+
|}
|
|
26
|
+
| {|
|
|
27
|
+
dropdownElement: HTMLElement,
|
|
28
|
+
element?: null | void,
|
|
29
|
+
index: number,
|
|
30
|
+
indexAddend?: null | void,
|
|
31
|
+
isExactMatch?: null | void,
|
|
32
|
+
text?: null | void,
|
|
33
|
+
|}
|
|
34
|
+
| {|
|
|
35
|
+
dropdownElement: HTMLElement,
|
|
36
|
+
element?: null | void,
|
|
37
|
+
index?: null | void,
|
|
38
|
+
indexAddend: number,
|
|
39
|
+
isExactMatch?: null | void,
|
|
40
|
+
text?: null | void,
|
|
41
|
+
|}
|
|
42
|
+
| {|
|
|
43
|
+
dropdownElement: HTMLElement,
|
|
44
|
+
element?: null | void,
|
|
45
|
+
index?: null | void,
|
|
46
|
+
indexAddend?: null | void,
|
|
47
|
+
isExactMatch?: boolean | void,
|
|
48
|
+
text: string,
|
|
49
|
+
|},
|
|
50
50
|
) => void;
|
package/dist/styles.d.ts
CHANGED
package/dist/styles.js
CHANGED
package/dist/styles.js.flow
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acusti/dropdown",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"exports": "./dist/Dropdown.js",
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
"typescript": "^4.4.3"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@acusti/input-text": "^0.
|
|
29
|
+
"@acusti/input-text": "^0.9.0",
|
|
30
30
|
"@acusti/matchmaking": "^0.3.0",
|
|
31
|
-
"@acusti/styling": "^0.
|
|
32
|
-
"@acusti/use-is-out-of-bounds": "^0.
|
|
31
|
+
"@acusti/styling": "^0.5.0",
|
|
32
|
+
"@acusti/use-is-out-of-bounds": "^0.5.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"classnames": "^2",
|
|
36
|
-
"react": "^16.8 || ^17",
|
|
37
|
-
"react-dom": "^16.8 || ^17"
|
|
36
|
+
"react": "^16.8 || ^17 || ^18",
|
|
37
|
+
"react-dom": "^16.8 || ^17 || ^18"
|
|
38
38
|
}
|
|
39
39
|
}
|
package/src/Dropdown.tsx
CHANGED