@economic/taco 1.14.2 → 1.14.3
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/esm/packages/taco/src/primitives/Collection/components/Root.js +38 -49
- package/dist/esm/packages/taco/src/primitives/Collection/components/Root.js.map +1 -1
- package/dist/esm/packages/taco/src/primitives/Listbox2/components/Root.js +1 -1
- package/dist/esm/packages/taco/src/primitives/Listbox2/components/Root.js.map +1 -1
- package/dist/primitives/Collection/components/Root.d.ts +2 -2
- package/dist/taco.cjs.development.js +39 -50
- package/dist/taco.cjs.development.js.map +1 -1
- package/dist/taco.cjs.production.min.js +1 -1
- package/dist/taco.cjs.production.min.js.map +1 -1
- package/package.json +2 -2
@@ -3,98 +3,87 @@ import { createCustomKeyboardEvent } from '../../../utils/input.js';
|
|
3
3
|
import { useMergedRef } from '../../../hooks/useMergedRef.js';
|
4
4
|
import { isAriaDirectionKey } from '../../../utils/aria.js';
|
5
5
|
|
6
|
-
const
|
6
|
+
const getOptionsFromCollection = (collection, selector) => collection.querySelectorAll(selector);
|
7
7
|
// we use javascript to set attributes (rather than cloning children and adding them)
|
8
8
|
// so that we can support nesting (e.g. groups) - child elements that aren't options.
|
9
9
|
// without doing this we would have to unwrap and flatten all groups
|
10
10
|
const Root = /*#__PURE__*/React__default.forwardRef(function CollectionRoot(props, ref) {
|
11
11
|
const {
|
12
|
-
|
12
|
+
querySelector,
|
13
13
|
tabIndex = 0,
|
14
14
|
...otherProps
|
15
15
|
} = props;
|
16
16
|
const internalRef = useMergedRef(ref);
|
17
17
|
const [activeIndex, setActiveIndex] = React__default.useState();
|
18
|
-
const
|
18
|
+
const lastLengthRef = React__default.useRef(0);
|
19
|
+
const setActiveOption = (index, collection, option) => {
|
19
20
|
var _collection$querySele;
|
20
21
|
(_collection$querySele = collection.querySelector(`[aria-current]`)) === null || _collection$querySele === void 0 ? void 0 : _collection$querySele.removeAttribute('aria-current');
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
option.scrollIntoView({
|
27
|
-
block: 'nearest'
|
28
|
-
});
|
29
|
-
}
|
30
|
-
}
|
22
|
+
option.setAttribute('aria-current', 'true');
|
23
|
+
option.scrollIntoView({
|
24
|
+
block: 'nearest'
|
25
|
+
});
|
26
|
+
setActiveIndex(index);
|
31
27
|
};
|
32
28
|
const setActiveIndexByElement = React__default.useCallback(option => {
|
33
29
|
if (internalRef.current) {
|
34
|
-
if (option.matches(
|
35
|
-
const options =
|
30
|
+
if (option.matches(querySelector)) {
|
31
|
+
const options = getOptionsFromCollection(internalRef.current, querySelector);
|
36
32
|
const nextActiveIndex = Array.from(options).indexOf(option);
|
37
33
|
if (nextActiveIndex > -1) {
|
38
|
-
|
34
|
+
setActiveOption(nextActiveIndex, internalRef.current, option);
|
39
35
|
}
|
40
36
|
}
|
41
37
|
}
|
42
|
-
}, [internalRef.current]);
|
38
|
+
}, [internalRef.current, querySelector]);
|
43
39
|
React__default.useEffect(() => {
|
44
40
|
if (internalRef.current) {
|
45
41
|
internalRef.current.setActiveIndex = setActiveIndexByElement;
|
46
42
|
}
|
47
43
|
}, [internalRef.current]);
|
48
|
-
// set/remove appropriate aria-current attributes
|
49
|
-
// we do this in a hook because we expose setActiveIndex on the ref
|
50
|
-
React__default.useEffect(() => {
|
51
|
-
if (internalRef.current) {
|
52
|
-
toggleCurrent(internalRef.current);
|
53
|
-
}
|
54
|
-
}, [activeIndex]);
|
55
|
-
// set the initial active index on mount
|
56
44
|
React__default.useEffect(() => {
|
57
45
|
if (internalRef.current) {
|
58
|
-
const
|
59
|
-
if (
|
60
|
-
const
|
61
|
-
if (
|
62
|
-
|
63
|
-
|
64
|
-
|
46
|
+
const options = getOptionsFromCollection(internalRef.current, querySelector);
|
47
|
+
if (options.length && options.length !== lastLengthRef.current) {
|
48
|
+
const selected = internalRef.current.querySelectorAll(`[aria-selected]`);
|
49
|
+
if (selected.length === 1) {
|
50
|
+
if (options) {
|
51
|
+
const firstSelected = selected.item(0);
|
52
|
+
const selectedIndex = Array.from(options).indexOf(firstSelected);
|
53
|
+
if (selectedIndex > -1) {
|
54
|
+
setActiveOption(selectedIndex, internalRef.current, firstSelected);
|
55
|
+
}
|
65
56
|
}
|
57
|
+
} else {
|
58
|
+
// multiple selected or none selected should go to 0
|
59
|
+
setActiveOption(0, internalRef.current, options.item(0));
|
66
60
|
}
|
67
|
-
} else {
|
68
|
-
// multiple selected or none selected should go to 0
|
69
|
-
setActiveIndex(0);
|
70
|
-
}
|
71
|
-
}
|
72
|
-
}, []);
|
73
|
-
// if children length changes, make sure we still have a valid active index (e.g. searching/filtering or adding a new item)
|
74
|
-
React__default.useEffect(() => {
|
75
|
-
if (internalRef.current) {
|
76
|
-
// make sure index and dom are synced
|
77
|
-
toggleCurrent(internalRef.current);
|
78
|
-
if (activeIndex !== undefined && activeIndex > React__default.Children.count(props.children)) {
|
79
|
-
setActiveIndex(0);
|
80
61
|
}
|
62
|
+
lastLengthRef.current = options.length;
|
81
63
|
}
|
82
64
|
}, [props.children]);
|
83
65
|
const handleClick = event => {
|
84
|
-
|
66
|
+
const option = event.target;
|
67
|
+
if (option.matches(querySelector)) {
|
68
|
+
const options = getOptionsFromCollection(event.currentTarget, querySelector);
|
69
|
+
const nextActiveIndex = Array.from(options).indexOf(option);
|
70
|
+
if (nextActiveIndex > -1) {
|
71
|
+
setActiveOption(nextActiveIndex, event.currentTarget, option);
|
72
|
+
}
|
73
|
+
}
|
85
74
|
};
|
86
75
|
const handleKeyDown = event => {
|
87
76
|
// this stops the event dispatched to the option rebounding back and starting an infinite loop
|
88
77
|
if (event.target !== event.currentTarget) {
|
89
78
|
return;
|
90
79
|
}
|
91
|
-
const options =
|
80
|
+
const options = getOptionsFromCollection(event.currentTarget, querySelector);
|
92
81
|
if (options) {
|
93
82
|
if (isAriaDirectionKey(event)) {
|
94
83
|
const nextActiveIndex = getNextEnabledItem(event, options, activeIndex);
|
95
84
|
if (nextActiveIndex !== undefined && nextActiveIndex !== activeIndex) {
|
96
85
|
event.preventDefault();
|
97
|
-
|
86
|
+
setActiveOption(nextActiveIndex, event.currentTarget, options.item(nextActiveIndex));
|
98
87
|
}
|
99
88
|
} else if (activeIndex !== undefined) {
|
100
89
|
// forward events onto the underlying option - this lets consumers place onKeyDown handlers on their own components
|
@@ -106,7 +95,7 @@ const Root = /*#__PURE__*/React__default.forwardRef(function CollectionRoot(prop
|
|
106
95
|
onClick: handleClick,
|
107
96
|
onKeyDown: handleKeyDown,
|
108
97
|
ref: internalRef,
|
109
|
-
tabIndex: tabIndex
|
98
|
+
tabIndex: tabIndex
|
110
99
|
}));
|
111
100
|
});
|
112
101
|
const getNextIndexFromKeycode = (event, length, activeIndex) => {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Root.js","sources":["../../../../../../../../src/primitives/Collection/components/Root.tsx"],"sourcesContent":["import React from 'react';\nimport { useMergedRef } from '../../../hooks/useMergedRef';\nimport { isAriaDirectionKey } from '../../../utils/aria';\nimport { createCustomKeyboardEvent } from '../../../utils/input';\n\n/* This component provides a keyboard navigable collection primitive for use in lists\n * It is unlikely you need to edit this component\n */\n\nexport type CollectionProps = React.HTMLAttributes<HTMLDivElement> & {\n selector: string;\n};\n\nexport type CollectionRef = HTMLDivElement & {\n setActiveIndex: (option: HTMLDivElement) => void;\n};\n\nconst getOptions = (collection: HTMLDivElement, selector: string): NodeListOf<Element> => collection.querySelectorAll(selector);\n\n// we use javascript to set attributes (rather than cloning children and adding them)\n// so that we can support nesting (e.g. groups) - child elements that aren't options.\n// without doing this we would have to unwrap and flatten all groups\nexport const Root = React.forwardRef<CollectionRef, CollectionProps>(function CollectionRoot(props, ref) {\n const { selector, tabIndex = 0, ...otherProps } = props;\n const internalRef = useMergedRef<CollectionRef>(ref);\n const [activeIndex, setActiveIndex] = React.useState<undefined | number>();\n\n const toggleCurrent = (collection: HTMLDivElement) => {\n collection.querySelector(`[aria-current]`)?.removeAttribute('aria-current');\n\n if (activeIndex !== undefined) {\n const option = getOptions(collection, selector)?.item(activeIndex);\n\n if (option) {\n option.setAttribute('aria-current', 'true');\n option.scrollIntoView({ block: 'nearest' });\n }\n }\n };\n\n const setActiveIndexByElement = React.useCallback(\n (option: HTMLDivElement) => {\n if (internalRef.current) {\n if (option.matches(selector)) {\n const options = getOptions(internalRef.current, selector);\n const nextActiveIndex = Array.from(options).indexOf(option);\n\n if (nextActiveIndex > -1) {\n setActiveIndex(nextActiveIndex);\n }\n }\n }\n },\n [internalRef.current]\n );\n\n React.useEffect(() => {\n if (internalRef.current) {\n internalRef.current.setActiveIndex = setActiveIndexByElement;\n }\n }, [internalRef.current]);\n\n // set/remove appropriate aria-current attributes\n // we do this in a hook because we expose setActiveIndex on the ref\n React.useEffect(() => {\n if (internalRef.current) {\n toggleCurrent(internalRef.current);\n }\n }, [activeIndex]);\n\n // set the initial active index on mount\n React.useEffect(() => {\n if (internalRef.current) {\n const selected = internalRef.current.querySelectorAll(`[aria-selected]`);\n\n if (selected.length === 1) {\n const options = getOptions(internalRef.current, selector);\n\n if (options) {\n const selectedIndex = Array.from(options).indexOf(selected.item(0));\n\n if (selectedIndex > -1) {\n setActiveIndex(selectedIndex);\n }\n }\n } else {\n // multiple selected or none selected should go to 0\n setActiveIndex(0);\n }\n }\n }, []);\n\n // if children length changes, make sure we still have a valid active index (e.g. searching/filtering or adding a new item)\n React.useEffect(() => {\n if (internalRef.current) {\n // make sure index and dom are synced\n toggleCurrent(internalRef.current);\n\n if (activeIndex !== undefined && activeIndex > React.Children.count(props.children)) {\n setActiveIndex(0);\n }\n }\n }, [props.children]);\n\n const handleClick = (event: React.MouseEvent<HTMLDivElement>) => {\n setActiveIndexByElement(event.target as HTMLDivElement);\n };\n\n const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {\n // this stops the event dispatched to the option rebounding back and starting an infinite loop\n if (event.target !== event.currentTarget) {\n return;\n }\n\n const options = getOptions(event.currentTarget, selector);\n\n if (options) {\n if (isAriaDirectionKey(event)) {\n const nextActiveIndex = getNextEnabledItem(event, options, activeIndex);\n\n if (nextActiveIndex !== undefined && nextActiveIndex !== activeIndex) {\n event.preventDefault();\n setActiveIndex(nextActiveIndex);\n }\n } else if (activeIndex !== undefined) {\n // forward events onto the underlying option - this lets consumers place onKeyDown handlers on their own components\n options\n .item(activeIndex)\n .dispatchEvent(createCustomKeyboardEvent(event as React.KeyboardEvent<HTMLInputElement>));\n }\n }\n };\n\n return <div {...otherProps} onClick={handleClick} onKeyDown={handleKeyDown} ref={internalRef} tabIndex={tabIndex ?? 0} />;\n});\n\nexport const getNextIndexFromKeycode = (\n event: React.KeyboardEvent,\n length: number,\n activeIndex: number | undefined\n): number | undefined => {\n switch (event.key) {\n case 'ArrowUp':\n return activeIndex === undefined ? length - 1 : activeIndex > 0 ? activeIndex - 1 : activeIndex;\n\n case 'ArrowDown':\n return activeIndex === undefined ? 0 : activeIndex < length - 1 ? activeIndex + 1 : activeIndex;\n\n case 'Home':\n return 0;\n\n case 'End':\n return length - 1;\n\n default:\n return;\n }\n};\n\nexport const getNextEnabledItem = (\n event: React.KeyboardEvent<HTMLElement>,\n options: NodeListOf<Element>,\n activeIndex: number | undefined,\n recurse = true\n): number | undefined => {\n const nextIndex = getNextIndexFromKeycode(event, options.length, activeIndex);\n\n if (nextIndex !== undefined) {\n if (nextIndex === activeIndex) {\n return activeIndex;\n } else if (options.item(nextIndex) && isSkippableItem(options.item(nextIndex))) {\n // check in the other direction if the first or last item is disabled,\n // but prevent infinite loops if all elements are disabled by disabling recursion\n if (recurse) {\n if (nextIndex === 0) {\n return getNextEnabledItem(\n new KeyboardEvent(event.type, { ...(event as any), key: 'ArrowDown' }) as any,\n options,\n nextIndex,\n false\n );\n } else if (nextIndex === options.length - 1) {\n return getNextEnabledItem(\n new KeyboardEvent(event.type, { ...(event as any), key: 'ArrowUp' }) as any,\n options,\n nextIndex,\n false\n );\n }\n }\n\n return getNextEnabledItem(event, options, nextIndex, recurse);\n }\n }\n\n return nextIndex;\n};\n\nconst isSkippableItem = (element: Element) => {\n return (\n element.getAttribute('role') === 'presentation' ||\n !!element.hasAttribute('disabled') ||\n !!element.getAttribute('aria-disabled') ||\n !!element.getAttribute('aria-hidden')\n );\n};\n"],"names":["getOptions","collection","selector","querySelectorAll","Root","React","forwardRef","CollectionRoot","props","ref","tabIndex","otherProps","internalRef","useMergedRef","activeIndex","setActiveIndex","useState","toggleCurrent","querySelector","removeAttribute","undefined","option","item","setAttribute","scrollIntoView","block","setActiveIndexByElement","useCallback","current","matches","options","nextActiveIndex","Array","from","indexOf","useEffect","selected","length","selectedIndex","Children","count","children","handleClick","event","target","handleKeyDown","currentTarget","isAriaDirectionKey","getNextEnabledItem","preventDefault","dispatchEvent","createCustomKeyboardEvent","onClick","onKeyDown","getNextIndexFromKeycode","key","recurse","nextIndex","isSkippableItem","KeyboardEvent","type","element","getAttribute","hasAttribute"],"mappings":";;;;;AAiBA,MAAMA,UAAU,GAAG,CAACC,UAA0B,EAAEC,QAAgB,KAA0BD,UAAU,CAACE,gBAAgB,CAACD,QAAQ,CAAC;AAE/H;AACA;AACA;MACaE,IAAI,gBAAGC,cAAK,CAACC,UAAU,CAAiC,SAASC,cAAc,CAACC,KAAK,EAAEC,GAAG;EACnG,MAAM;IAAEP,QAAQ;IAAEQ,QAAQ,GAAG,CAAC;IAAE,GAAGC;GAAY,GAAGH,KAAK;EACvD,MAAMI,WAAW,GAAGC,YAAY,CAAgBJ,GAAG,CAAC;EACpD,MAAM,CAACK,WAAW,EAAEC,cAAc,CAAC,GAAGV,cAAK,CAACW,QAAQ,EAAsB;EAE1E,MAAMC,aAAa,GAAIhB,UAA0B;;IAC7C,yBAAAA,UAAU,CAACiB,aAAa,iBAAiB,CAAC,0DAA1C,sBAA4CC,eAAe,CAAC,cAAc,CAAC;IAE3E,IAAIL,WAAW,KAAKM,SAAS,EAAE;MAAA;MAC3B,MAAMC,MAAM,kBAAGrB,UAAU,CAACC,UAAU,EAAEC,QAAQ,CAAC,gD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AIC,eAAe,CAAC5B,OAAO,CAACR,IAAI,CAACmC,SAAS,CAAC,CAAC,EAAE;;;MAG5E,IAAID,OAAO,EAAE;QACT,IAAIC,SAAS,KAAK,CAAC,EAAE;UACjB,OAAOT,kBAAkB,CACrB,IAAIW,aAAa,CAAChB,KAAK,CAACiB,IAAI,EAAE;YAAE,GAAIjB,KAAa;YAAEY,GAAG,EAAE;WAAa,CAAQ,EAC7EzB,OAAO,EACP2B,SAAS,EACT,KAAK,CACR;SACJ,MAAM,IAAIA,SAAS,KAAK3B,OAAO,CAACO,MAAM,GAAG,CAAC,EAAE;UACzC,OAAOW,kBAAkB,CACrB,IAAIW,aAAa,CAAChB,KAAK,CAACiB,IAAI,EAAE;YAAE,GAAIjB,KAAa;YAAEY,GAAG,EAAE;WAAW,CAAQ,EAC3EzB,OAAO,EACP2B,SAAS,EACT,KAAK,CACR;;;MAIT,OAAOT,kBAAkB,CAACL,KAAK,EAAEb,OAAO,EAAE2B,SAAS,EAAED,OAAO,CAAC;;;EAIrE,OAAOC,SAAS;AACpB;AAEA,MAAMC,eAAe,GAAIG,OAAgB;EACrC,OACIA,OAAO,CAACC,YAAY,CAAC,MAAM,CAAC,KAAK,cAAc,IAC/C,CAAC,CAACD,OAAO,CAACE,YAAY,CAAC,UAAU,CAAC,IAClC,CAAC,CAACF,OAAO,CAACC,YAAY,CAAC,eAAe,CAAC,IACvC,CAAC,CAACD,OAAO,CAACC,YAAY,CAAC,aAAa,CAAC;AAE7C,CAAC;;;;"}
|
1
|
+
{"version":3,"file":"Root.js","sources":["../../../../../../../../src/primitives/Collection/components/Root.tsx"],"sourcesContent":["import React from 'react';\nimport { useMergedRef } from '../../../hooks/useMergedRef';\nimport { isAriaDirectionKey } from '../../../utils/aria';\nimport { createCustomKeyboardEvent } from '../../../utils/input';\n\n/* This component provides a keyboard navigable collection primitive for use in lists\n * It is unlikely you need to edit this component\n */\n\nexport type CollectionProps = React.HTMLAttributes<HTMLDivElement> & {\n querySelector: string;\n};\n\nexport type CollectionRef = HTMLDivElement & {\n setActiveIndex: (option: HTMLDivElement) => void;\n};\n\nconst getOptionsFromCollection = (collection: HTMLDivElement, selector: string): NodeListOf<Element> =>\n collection.querySelectorAll(selector);\n\n// we use javascript to set attributes (rather than cloning children and adding them)\n// so that we can support nesting (e.g. groups) - child elements that aren't options.\n// without doing this we would have to unwrap and flatten all groups\nexport const Root = React.forwardRef<CollectionRef, CollectionProps>(function CollectionRoot(props, ref) {\n const { querySelector, tabIndex = 0, ...otherProps } = props;\n const internalRef = useMergedRef<CollectionRef>(ref);\n const [activeIndex, setActiveIndex] = React.useState<number | undefined>();\n const lastLengthRef = React.useRef(0);\n\n const setActiveOption = (index: number, collection: HTMLDivElement, option: Element) => {\n collection.querySelector(`[aria-current]`)?.removeAttribute('aria-current');\n option.setAttribute('aria-current', 'true');\n option.scrollIntoView({ block: 'nearest' });\n setActiveIndex(index);\n };\n\n const setActiveIndexByElement = React.useCallback(\n (option: HTMLDivElement) => {\n if (internalRef.current) {\n if (option.matches(querySelector)) {\n const options = getOptionsFromCollection(internalRef.current, querySelector);\n const nextActiveIndex = Array.from(options).indexOf(option);\n\n if (nextActiveIndex > -1) {\n setActiveOption(nextActiveIndex, internalRef.current, option);\n }\n }\n }\n },\n [internalRef.current, querySelector]\n );\n\n React.useEffect(() => {\n if (internalRef.current) {\n internalRef.current.setActiveIndex = setActiveIndexByElement;\n }\n }, [internalRef.current]);\n\n React.useEffect(() => {\n if (internalRef.current) {\n const options = getOptionsFromCollection(internalRef.current, querySelector);\n\n if (options.length && options.length !== lastLengthRef.current) {\n const selected = internalRef.current.querySelectorAll(`[aria-selected]`);\n\n if (selected.length === 1) {\n if (options) {\n const firstSelected = selected.item(0);\n const selectedIndex = Array.from(options).indexOf(firstSelected);\n\n if (selectedIndex > -1) {\n setActiveOption(selectedIndex, internalRef.current, firstSelected);\n }\n }\n } else {\n // multiple selected or none selected should go to 0\n setActiveOption(0, internalRef.current, options.item(0));\n }\n }\n\n lastLengthRef.current = options.length;\n }\n }, [props.children]);\n\n const handleClick = (event: React.MouseEvent<HTMLDivElement>) => {\n const option = event.target as HTMLElement;\n\n if (option.matches(querySelector)) {\n const options = getOptionsFromCollection(event.currentTarget, querySelector);\n const nextActiveIndex = Array.from(options).indexOf(option);\n\n if (nextActiveIndex > -1) {\n setActiveOption(nextActiveIndex, event.currentTarget, option);\n }\n }\n };\n\n const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {\n // this stops the event dispatched to the option rebounding back and starting an infinite loop\n if (event.target !== event.currentTarget) {\n return;\n }\n\n const options = getOptionsFromCollection(event.currentTarget, querySelector);\n\n if (options) {\n if (isAriaDirectionKey(event)) {\n const nextActiveIndex = getNextEnabledItem(event, options, activeIndex);\n\n if (nextActiveIndex !== undefined && nextActiveIndex !== activeIndex) {\n event.preventDefault();\n setActiveOption(nextActiveIndex, event.currentTarget, options.item(nextActiveIndex));\n }\n } else if (activeIndex !== undefined) {\n // forward events onto the underlying option - this lets consumers place onKeyDown handlers on their own components\n options\n .item(activeIndex)\n .dispatchEvent(createCustomKeyboardEvent(event as React.KeyboardEvent<HTMLInputElement>));\n }\n }\n };\n\n return <div {...otherProps} onClick={handleClick} onKeyDown={handleKeyDown} ref={internalRef} tabIndex={tabIndex} />;\n});\n\nexport const getNextIndexFromKeycode = (\n event: React.KeyboardEvent,\n length: number,\n activeIndex: number | undefined\n): number | undefined => {\n switch (event.key) {\n case 'ArrowUp':\n return activeIndex === undefined ? length - 1 : activeIndex > 0 ? activeIndex - 1 : activeIndex;\n\n case 'ArrowDown':\n return activeIndex === undefined ? 0 : activeIndex < length - 1 ? activeIndex + 1 : activeIndex;\n\n case 'Home':\n return 0;\n\n case 'End':\n return length - 1;\n\n default:\n return;\n }\n};\n\nexport const getNextEnabledItem = (\n event: React.KeyboardEvent<HTMLElement>,\n options: NodeListOf<Element>,\n activeIndex: number | undefined,\n recurse = true\n): number | undefined => {\n const nextIndex = getNextIndexFromKeycode(event, options.length, activeIndex);\n\n if (nextIndex !== undefined) {\n if (nextIndex === activeIndex) {\n return activeIndex;\n } else if (options.item(nextIndex) && isSkippableItem(options.item(nextIndex))) {\n // check in the other direction if the first or last item is disabled,\n // but prevent infinite loops if all elements are disabled by disabling recursion\n if (recurse) {\n if (nextIndex === 0) {\n return getNextEnabledItem(\n new KeyboardEvent(event.type, { ...(event as any), key: 'ArrowDown' }) as any,\n options,\n nextIndex,\n false\n );\n } else if (nextIndex === options.length - 1) {\n return getNextEnabledItem(\n new KeyboardEvent(event.type, { ...(event as any), key: 'ArrowUp' }) as any,\n options,\n nextIndex,\n false\n );\n }\n }\n\n return getNextEnabledItem(event, options, nextIndex, recurse);\n }\n }\n\n return nextIndex;\n};\n\nconst isSkippableItem = (element: Element) => {\n return (\n element.getAttribute('role') === 'presentation' ||\n !!element.hasAttribute('disabled') ||\n !!element.getAttribute('aria-disabled') ||\n !!element.getAttribute('aria-hidden')\n );\n};\n"],"names":["getOptionsFromCollection","collection","selector","querySelectorAll","Root","React","forwardRef","CollectionRoot","props","ref","querySelector","tabIndex","otherProps","internalRef","useMergedRef","activeIndex","setActiveIndex","useState","lastLengthRef","useRef","setActiveOption","index","option","removeAttribute","setAttribute","scrollIntoView","block","setActiveIndexByElement","useCallback","current","matches","options","nextActiveIndex","Array","from","indexOf","useEffect","length","selected","firstSelected","item","selectedIndex","children","handleClick","event","target","currentTarget","handleKeyDown","isAriaDirectionKey","getNextEnabledItem","undefined","preventDefault","dispatchEvent","createCustomKeyboardEvent","onClick","onKeyDown","getNextIndexFromKeycode","key","recurse","nextIndex","isSkippableItem","KeyboardEvent","type","element","getAttribute","hasAttribute"],"mappings":";;;;;AAiBA,MAAMA,wBAAwB,GAAG,CAACC,UAA0B,EAAEC,QAAgB,KAC1ED,UAAU,CAACE,gBAAgB,CAACD,QAAQ,CAAC;AAEzC;AACA;AACA;MACaE,IAAI,gBAAGC,cAAK,CAACC,UAAU,CAAiC,SAASC,cAAc,CAACC,KAAK,EAAEC,GAAG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V,kBAAkB,CAACL,KAAK,EAAEb,OAAO,EAAE4B,SAAS,EAAED,OAAO,CAAC;;;EAIrE,OAAOC,SAAS;AACpB;AAEA,MAAMC,eAAe,GAAIG,OAAgB;EACrC,OACIA,OAAO,CAACC,YAAY,CAAC,MAAM,CAAC,KAAK,cAAc,IAC/C,CAAC,CAACD,OAAO,CAACE,YAAY,CAAC,UAAU,CAAC,IAClC,CAAC,CAACF,OAAO,CAACC,YAAY,CAAC,eAAe,CAAC,IACvC,CAAC,CAACD,OAAO,CAACC,YAAY,CAAC,aAAa,CAAC;AAE7C,CAAC;;;;"}
|
@@ -33,7 +33,7 @@ const Root = /*#__PURE__*/React__default.forwardRef(function Listbox2(props, ref
|
|
33
33
|
id: id,
|
34
34
|
ref: ref,
|
35
35
|
role: "listbox",
|
36
|
-
|
36
|
+
querySelector: customSelector ? `${DEFAULT_SELECTOR}, ${customSelector}` : DEFAULT_SELECTOR
|
37
37
|
}), children)));
|
38
38
|
});
|
39
39
|
const createListboxValueSetter = (multiple, setValue) => nextValue => {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Root.js","sources":["../../../../../../../../src/primitives/Listbox2/components/Root.tsx"],"sourcesContent":["import React from 'react';\nimport { useId } from '../../../hooks/useId';\nimport * as CollectionPrimitive from '../../Collection/Collection';\nimport { Listbox2Value } from '../types';\nimport { Listbox2Context } from './Context';\n\nexport type Listbox2Props = React.HTMLAttributes<HTMLDivElement> & {\n customSelector?: string;\n disabled?: boolean;\n multiple?: boolean;\n readOnly?: boolean;\n setValue: (value: Listbox2Value) => void;\n value?: Listbox2Value;\n};\n\nconst DEFAULT_SELECTOR = '[role=\"option\"]';\n\nexport const Root = React.forwardRef<CollectionPrimitive.CollectionRef, Listbox2Props>(function Listbox2(props, ref) {\n const {\n children,\n customSelector,\n disabled = false,\n id: nativeId,\n multiple,\n readOnly = false,\n setValue,\n title,\n value,\n ...otherProps\n } = props;\n const id = useId(nativeId);\n\n const context = React.useMemo(\n () => ({\n disabled,\n readOnly,\n setValue,\n value,\n }),\n [disabled, readOnly, value]\n );\n\n return (\n <Listbox2Context.Provider value={context}>\n <div data-taco=\"listbox2\">\n <CollectionPrimitive.Root\n {...otherProps}\n aria-multiselectable={multiple ? true : undefined}\n id={id}\n ref={ref}\n role=\"listbox\"\n
|
1
|
+
{"version":3,"file":"Root.js","sources":["../../../../../../../../src/primitives/Listbox2/components/Root.tsx"],"sourcesContent":["import React from 'react';\nimport { useId } from '../../../hooks/useId';\nimport * as CollectionPrimitive from '../../Collection/Collection';\nimport { Listbox2Value } from '../types';\nimport { Listbox2Context } from './Context';\n\nexport type Listbox2Props = React.HTMLAttributes<HTMLDivElement> & {\n customSelector?: string;\n disabled?: boolean;\n multiple?: boolean;\n readOnly?: boolean;\n setValue: (value: Listbox2Value) => void;\n value?: Listbox2Value;\n};\n\nconst DEFAULT_SELECTOR = '[role=\"option\"]';\n\nexport const Root = React.forwardRef<CollectionPrimitive.CollectionRef, Listbox2Props>(function Listbox2(props, ref) {\n const {\n children,\n customSelector,\n disabled = false,\n id: nativeId,\n multiple,\n readOnly = false,\n setValue,\n title,\n value,\n ...otherProps\n } = props;\n const id = useId(nativeId);\n\n const context = React.useMemo(\n () => ({\n disabled,\n readOnly,\n setValue,\n value,\n }),\n [disabled, readOnly, value]\n );\n\n return (\n <Listbox2Context.Provider value={context}>\n <div data-taco=\"listbox2\">\n <CollectionPrimitive.Root\n {...otherProps}\n aria-multiselectable={multiple ? true : undefined}\n id={id}\n ref={ref}\n role=\"listbox\"\n querySelector={customSelector ? `${DEFAULT_SELECTOR}, ${customSelector}` : DEFAULT_SELECTOR}>\n {children}\n </CollectionPrimitive.Root>\n </div>\n </Listbox2Context.Provider>\n );\n});\n\nexport const createListboxValueSetter =\n (multiple: boolean, setValue: React.Dispatch<React.SetStateAction<Listbox2Value | undefined>>) =>\n (nextValue: Listbox2Value) => {\n setValue(value => {\n if (Array.isArray(nextValue)) {\n return nextValue;\n }\n\n if (multiple) {\n if (value === undefined) {\n return [nextValue];\n } else if (Array.isArray(value)) {\n if (value.includes(nextValue)) {\n return value.filter(v => v !== nextValue);\n }\n\n return [...value, nextValue];\n } else if (value === nextValue) {\n return [];\n }\n\n return [value, nextValue];\n }\n\n return nextValue;\n });\n };\n"],"names":["DEFAULT_SELECTOR","Root","React","forwardRef","Listbox2","props","ref","children","customSelector","disabled","id","nativeId","multiple","readOnly","setValue","title","value","otherProps","useId","context","useMemo","Listbox2Context","Provider","CollectionPrimitive","undefined","role","querySelector","createListboxValueSetter","nextValue","Array","isArray","includes","filter","v"],"mappings":";;;;;AAeA,MAAMA,gBAAgB,GAAG,iBAAiB;MAE7BC,IAAI,gBAAGC,cAAK,CAACC,UAAU,CAAmD,SAASC,QAAQ,CAACC,KAAK,EAAEC,GAAG;EAC/G,MAAM;IACFC,QAAQ;IACRC,cAAc;IACdC,QAAQ,GAAG,KAAK;IAChBC,EAAE,EAAEC,QAAQ;IACZC,QAAQ;IACRC,QAAQ,GAAG,KAAK;IAChBC,QAAQ;IACRC,KAAK;IACLC,KAAK;IACL,GAAGC;GACN,GAAGZ,KAAK;EACT,MAAMK,EAAE,GAAGQ,KAAK,CAACP,QAAQ,CAAC;EAE1B,MAAMQ,OAAO,GAAGjB,cAAK,CAACkB,OAAO,CACzB,OAAO;IACHX,QAAQ;IACRI,QAAQ;IACRC,QAAQ;IACRE;GACH,CAAC,EACF,CAACP,QAAQ,EAAEI,QAAQ,EAAEG,KAAK,CAAC,CAC9B;EAED,oBACId,6BAACmB,eAAe,CAACC,QAAQ;IAACN,KAAK,EAAEG;kBAC7BjB;iBAAe;kBACXA,6BAACqB,MAAwB,oBACjBN,UAAU;4BACQL,QAAQ,GAAG,IAAI,GAAGY,SAAS;IACjDd,EAAE,EAAEA,EAAE;IACNJ,GAAG,EAAEA,GAAG;IACRmB,IAAI,EAAC,SAAS;IACdC,aAAa,EAAElB,cAAc,MAAMR,qBAAqBQ,gBAAgB,GAAGR;MAC1EO,QAAQ,CACc,CACzB,CACiB;AAEnC,CAAC;MAEYoB,wBAAwB,GACjC,CAACf,QAAiB,EAAEE,QAAyE,KAC5Fc,SAAwB;EACrBd,QAAQ,CAACE,KAAK;IACV,IAAIa,KAAK,CAACC,OAAO,CAACF,SAAS,CAAC,EAAE;MAC1B,OAAOA,SAAS;;IAGpB,IAAIhB,QAAQ,EAAE;MACV,IAAII,KAAK,KAAKQ,SAAS,EAAE;QACrB,OAAO,CAACI,SAAS,CAAC;OACrB,MAAM,IAAIC,KAAK,CAACC,OAAO,CAACd,KAAK,CAAC,EAAE;QAC7B,IAAIA,KAAK,CAACe,QAAQ,CAACH,SAAS,CAAC,EAAE;UAC3B,OAAOZ,KAAK,CAACgB,MAAM,CAACC,CAAC,IAAIA,CAAC,KAAKL,SAAS,CAAC;;QAG7C,OAAO,CAAC,GAAGZ,KAAK,EAAEY,SAAS,CAAC;OAC/B,MAAM,IAAIZ,KAAK,KAAKY,SAAS,EAAE;QAC5B,OAAO,EAAE;;MAGb,OAAO,CAACZ,KAAK,EAAEY,SAAS,CAAC;;IAG7B,OAAOA,SAAS;GACnB,CAAC;AACN;;;;"}
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import React from 'react';
|
2
2
|
export declare type CollectionProps = React.HTMLAttributes<HTMLDivElement> & {
|
3
|
-
|
3
|
+
querySelector: string;
|
4
4
|
};
|
5
5
|
export declare type CollectionRef = HTMLDivElement & {
|
6
6
|
setActiveIndex: (option: HTMLDivElement) => void;
|
7
7
|
};
|
8
8
|
export declare const Root: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLDivElement> & {
|
9
|
-
|
9
|
+
querySelector: string;
|
10
10
|
} & React.RefAttributes<CollectionRef>>;
|
11
11
|
export declare const getNextIndexFromKeycode: (event: React.KeyboardEvent, length: number, activeIndex: number | undefined) => number | undefined;
|
12
12
|
export declare const getNextEnabledItem: (event: React.KeyboardEvent<HTMLElement>, options: NodeListOf<Element>, activeIndex: number | undefined, recurse?: boolean) => number | undefined;
|
@@ -7288,98 +7288,87 @@ const isAriaDirectionKey = event => {
|
|
7288
7288
|
return false;
|
7289
7289
|
};
|
7290
7290
|
|
7291
|
-
const
|
7291
|
+
const getOptionsFromCollection = (collection, selector) => collection.querySelectorAll(selector);
|
7292
7292
|
// we use javascript to set attributes (rather than cloning children and adding them)
|
7293
7293
|
// so that we can support nesting (e.g. groups) - child elements that aren't options.
|
7294
7294
|
// without doing this we would have to unwrap and flatten all groups
|
7295
7295
|
const Root = /*#__PURE__*/React__default.forwardRef(function CollectionRoot(props, ref) {
|
7296
7296
|
const {
|
7297
|
-
|
7297
|
+
querySelector,
|
7298
7298
|
tabIndex = 0,
|
7299
7299
|
...otherProps
|
7300
7300
|
} = props;
|
7301
7301
|
const internalRef = useMergedRef(ref);
|
7302
7302
|
const [activeIndex, setActiveIndex] = React__default.useState();
|
7303
|
-
const
|
7303
|
+
const lastLengthRef = React__default.useRef(0);
|
7304
|
+
const setActiveOption = (index, collection, option) => {
|
7304
7305
|
var _collection$querySele;
|
7305
7306
|
(_collection$querySele = collection.querySelector(`[aria-current]`)) === null || _collection$querySele === void 0 ? void 0 : _collection$querySele.removeAttribute('aria-current');
|
7306
|
-
|
7307
|
-
|
7308
|
-
|
7309
|
-
|
7310
|
-
|
7311
|
-
option.scrollIntoView({
|
7312
|
-
block: 'nearest'
|
7313
|
-
});
|
7314
|
-
}
|
7315
|
-
}
|
7307
|
+
option.setAttribute('aria-current', 'true');
|
7308
|
+
option.scrollIntoView({
|
7309
|
+
block: 'nearest'
|
7310
|
+
});
|
7311
|
+
setActiveIndex(index);
|
7316
7312
|
};
|
7317
7313
|
const setActiveIndexByElement = React__default.useCallback(option => {
|
7318
7314
|
if (internalRef.current) {
|
7319
|
-
if (option.matches(
|
7320
|
-
const options =
|
7315
|
+
if (option.matches(querySelector)) {
|
7316
|
+
const options = getOptionsFromCollection(internalRef.current, querySelector);
|
7321
7317
|
const nextActiveIndex = Array.from(options).indexOf(option);
|
7322
7318
|
if (nextActiveIndex > -1) {
|
7323
|
-
|
7319
|
+
setActiveOption(nextActiveIndex, internalRef.current, option);
|
7324
7320
|
}
|
7325
7321
|
}
|
7326
7322
|
}
|
7327
|
-
}, [internalRef.current]);
|
7323
|
+
}, [internalRef.current, querySelector]);
|
7328
7324
|
React__default.useEffect(() => {
|
7329
7325
|
if (internalRef.current) {
|
7330
7326
|
internalRef.current.setActiveIndex = setActiveIndexByElement;
|
7331
7327
|
}
|
7332
7328
|
}, [internalRef.current]);
|
7333
|
-
// set/remove appropriate aria-current attributes
|
7334
|
-
// we do this in a hook because we expose setActiveIndex on the ref
|
7335
7329
|
React__default.useEffect(() => {
|
7336
7330
|
if (internalRef.current) {
|
7337
|
-
|
7338
|
-
|
7339
|
-
|
7340
|
-
|
7341
|
-
|
7342
|
-
|
7343
|
-
|
7344
|
-
|
7345
|
-
|
7346
|
-
|
7347
|
-
const selectedIndex = Array.from(options).indexOf(selected.item(0));
|
7348
|
-
if (selectedIndex > -1) {
|
7349
|
-
setActiveIndex(selectedIndex);
|
7331
|
+
const options = getOptionsFromCollection(internalRef.current, querySelector);
|
7332
|
+
if (options.length && options.length !== lastLengthRef.current) {
|
7333
|
+
const selected = internalRef.current.querySelectorAll(`[aria-selected]`);
|
7334
|
+
if (selected.length === 1) {
|
7335
|
+
if (options) {
|
7336
|
+
const firstSelected = selected.item(0);
|
7337
|
+
const selectedIndex = Array.from(options).indexOf(firstSelected);
|
7338
|
+
if (selectedIndex > -1) {
|
7339
|
+
setActiveOption(selectedIndex, internalRef.current, firstSelected);
|
7340
|
+
}
|
7350
7341
|
}
|
7342
|
+
} else {
|
7343
|
+
// multiple selected or none selected should go to 0
|
7344
|
+
setActiveOption(0, internalRef.current, options.item(0));
|
7351
7345
|
}
|
7352
|
-
} else {
|
7353
|
-
// multiple selected or none selected should go to 0
|
7354
|
-
setActiveIndex(0);
|
7355
|
-
}
|
7356
|
-
}
|
7357
|
-
}, []);
|
7358
|
-
// if children length changes, make sure we still have a valid active index (e.g. searching/filtering or adding a new item)
|
7359
|
-
React__default.useEffect(() => {
|
7360
|
-
if (internalRef.current) {
|
7361
|
-
// make sure index and dom are synced
|
7362
|
-
toggleCurrent(internalRef.current);
|
7363
|
-
if (activeIndex !== undefined && activeIndex > React__default.Children.count(props.children)) {
|
7364
|
-
setActiveIndex(0);
|
7365
7346
|
}
|
7347
|
+
lastLengthRef.current = options.length;
|
7366
7348
|
}
|
7367
7349
|
}, [props.children]);
|
7368
7350
|
const handleClick = event => {
|
7369
|
-
|
7351
|
+
const option = event.target;
|
7352
|
+
if (option.matches(querySelector)) {
|
7353
|
+
const options = getOptionsFromCollection(event.currentTarget, querySelector);
|
7354
|
+
const nextActiveIndex = Array.from(options).indexOf(option);
|
7355
|
+
if (nextActiveIndex > -1) {
|
7356
|
+
setActiveOption(nextActiveIndex, event.currentTarget, option);
|
7357
|
+
}
|
7358
|
+
}
|
7370
7359
|
};
|
7371
7360
|
const handleKeyDown = event => {
|
7372
7361
|
// this stops the event dispatched to the option rebounding back and starting an infinite loop
|
7373
7362
|
if (event.target !== event.currentTarget) {
|
7374
7363
|
return;
|
7375
7364
|
}
|
7376
|
-
const options =
|
7365
|
+
const options = getOptionsFromCollection(event.currentTarget, querySelector);
|
7377
7366
|
if (options) {
|
7378
7367
|
if (isAriaDirectionKey(event)) {
|
7379
7368
|
const nextActiveIndex = getNextEnabledItem$1(event, options, activeIndex);
|
7380
7369
|
if (nextActiveIndex !== undefined && nextActiveIndex !== activeIndex) {
|
7381
7370
|
event.preventDefault();
|
7382
|
-
|
7371
|
+
setActiveOption(nextActiveIndex, event.currentTarget, options.item(nextActiveIndex));
|
7383
7372
|
}
|
7384
7373
|
} else if (activeIndex !== undefined) {
|
7385
7374
|
// forward events onto the underlying option - this lets consumers place onKeyDown handlers on their own components
|
@@ -7391,7 +7380,7 @@ const Root = /*#__PURE__*/React__default.forwardRef(function CollectionRoot(prop
|
|
7391
7380
|
onClick: handleClick,
|
7392
7381
|
onKeyDown: handleKeyDown,
|
7393
7382
|
ref: internalRef,
|
7394
|
-
tabIndex: tabIndex
|
7383
|
+
tabIndex: tabIndex
|
7395
7384
|
}));
|
7396
7385
|
});
|
7397
7386
|
const getNextIndexFromKeycode = (event, length, activeIndex) => {
|
@@ -7471,7 +7460,7 @@ const Root$1 = /*#__PURE__*/React__default.forwardRef(function Listbox2(props, r
|
|
7471
7460
|
id: id,
|
7472
7461
|
ref: ref,
|
7473
7462
|
role: "listbox",
|
7474
|
-
|
7463
|
+
querySelector: customSelector ? `${DEFAULT_SELECTOR}, ${customSelector}` : DEFAULT_SELECTOR
|
7475
7464
|
}), children)));
|
7476
7465
|
});
|
7477
7466
|
const createListboxValueSetter = (multiple, setValue) => nextValue => {
|