@acusti/dropdown 0.22.0 → 0.24.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/README.md +1 -1
- package/dist/Dropdown.d.ts +2 -2
- package/dist/Dropdown.js +420 -319
- package/dist/Dropdown.js.flow +44 -44
- package/dist/helpers.d.ts +47 -32
- package/dist/helpers.js +35 -36
- package/dist/helpers.js.flow +37 -37
- package/dist/styles.d.ts +1 -1
- package/dist/styles.js +1 -1
- package/dist/styles.js.flow +2 -2
- package/package.json +27 -9
package/dist/Dropdown.js.flow
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Flowtype definitions for Dropdown
|
|
3
3
|
* Generated by Flowgen from a Typescript Definition
|
|
4
|
-
* Flowgen v1.20.
|
|
4
|
+
* Flowgen v1.20.1
|
|
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
|
-
|
|
34
|
-
|
|
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
|
+
keepOpenOnSubmit?: boolean,
|
|
34
|
+
label?: string,
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Only usable in conjunction with {isSearchable: true}; used as search input’s name
|
|
38
|
+
*/
|
|
39
|
+
name?: string,
|
|
40
|
+
onClick?: (event: SyntheticMouseEvent<HTMLElement>) => mixed,
|
|
41
|
+
onMouseDown?: (event: SyntheticMouseEvent<HTMLElement>) => mixed,
|
|
42
|
+
onMouseUp?: (event: SyntheticMouseEvent<HTMLElement>) => mixed,
|
|
43
|
+
onSubmitItem?: (payload: Item) => void,
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Only usable in conjunction with {isSearchable: true}; used as search input’s placeholder
|
|
47
|
+
*/
|
|
48
|
+
placeholder?: string,
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Only usable in conjunction with {isSearchable: true}; used as search input’s tabIndex
|
|
52
|
+
*/
|
|
53
|
+
tabIndex?: number,
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Used as search input’s value if props.isSearchable === true
|
|
57
|
+
* Used to determine if value has changed to avoid triggering onSubmitItem if not
|
|
58
|
+
*/
|
|
59
|
+
value?: string,
|
|
60
60
|
|};
|
|
61
61
|
declare var Dropdown: React.StatelessFunctionalComponent<Props>;
|
|
62
62
|
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
|
@@ -1,50 +1,50 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Flowtype definitions for helpers
|
|
3
3
|
* Generated by Flowgen from a Typescript Definition
|
|
4
|
-
* Flowgen v1.20.
|
|
4
|
+
* Flowgen v1.20.1
|
|
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
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Flowtype definitions for styles
|
|
3
3
|
* Generated by Flowgen from a Typescript Definition
|
|
4
|
-
* Flowgen v1.20.
|
|
4
|
+
* Flowgen v1.20.1
|
|
5
5
|
* @flow
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
declare export var ROOT_CLASS_NAME:
|
|
8
|
+
declare export var ROOT_CLASS_NAME: 'uktdropdown';
|
|
9
9
|
declare export var ROOT_SELECTOR: string;
|
|
10
10
|
declare export var BODY_CLASS_NAME: string;
|
|
11
11
|
declare export var LABEL_CLASS_NAME: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acusti/dropdown",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
|
+
"description": "React component that renders a dropdown with a trigger and supports searching, keyboard access, and more",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"react-component",
|
|
8
|
+
"dropdown",
|
|
9
|
+
"menu",
|
|
10
|
+
"combobox",
|
|
11
|
+
"search",
|
|
12
|
+
"typeahead",
|
|
13
|
+
"a11y",
|
|
14
|
+
"accessibility",
|
|
15
|
+
"key-navigation",
|
|
16
|
+
"ssr",
|
|
17
|
+
"typescript",
|
|
18
|
+
"ts",
|
|
19
|
+
"flow"
|
|
20
|
+
],
|
|
4
21
|
"type": "module",
|
|
5
22
|
"sideEffects": false,
|
|
6
23
|
"exports": "./dist/Dropdown.js",
|
|
@@ -12,7 +29,8 @@
|
|
|
12
29
|
],
|
|
13
30
|
"repository": {
|
|
14
31
|
"type": "git",
|
|
15
|
-
"url": "
|
|
32
|
+
"url": "https://github.com/acusti/uikit.git",
|
|
33
|
+
"directory": "packages/dropdown"
|
|
16
34
|
},
|
|
17
35
|
"author": "andrew patton <andrew@acusti.ca> (https://www.acusti.ca)",
|
|
18
36
|
"license": "Unlicense",
|
|
@@ -21,15 +39,15 @@
|
|
|
21
39
|
},
|
|
22
40
|
"homepage": "https://github.com/acusti/uikit/tree/main/packages/dropdown#readme",
|
|
23
41
|
"devDependencies": {
|
|
24
|
-
"@types/react": "^18.0.
|
|
25
|
-
"typescript": "^4.
|
|
42
|
+
"@types/react": "^18.0.25",
|
|
43
|
+
"typescript": "^4.9.3"
|
|
26
44
|
},
|
|
27
45
|
"dependencies": {
|
|
28
|
-
"@acusti/input-text": "^0.
|
|
29
|
-
"@acusti/matchmaking": "^0.
|
|
30
|
-
"@acusti/styling": "^0.
|
|
31
|
-
"@acusti/use-is-out-of-bounds": "^0.
|
|
32
|
-
"clsx": "^1.
|
|
46
|
+
"@acusti/input-text": "^1.0.1",
|
|
47
|
+
"@acusti/matchmaking": "^0.4.0",
|
|
48
|
+
"@acusti/styling": "^0.6.0",
|
|
49
|
+
"@acusti/use-is-out-of-bounds": "^0.6.0",
|
|
50
|
+
"clsx": "^1.2.1"
|
|
33
51
|
},
|
|
34
52
|
"peerDependencies": {
|
|
35
53
|
"react": "^16.8 || ^17 || ^18",
|