@fluentui/react-combobox 9.5.34 → 9.5.36
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/CHANGELOG.md +28 -9
- package/dist/index.d.ts +11 -0
- package/lib/components/Combobox/useCombobox.js +22 -126
- package/lib/components/Combobox/useCombobox.js.map +1 -1
- package/lib/components/Combobox/useInputTriggerSlot.js +103 -0
- package/lib/components/Combobox/useInputTriggerSlot.js.map +1 -0
- package/lib/components/Dropdown/useButtonTriggerSlot.js +69 -0
- package/lib/components/Dropdown/useButtonTriggerSlot.js.map +1 -0
- package/lib/components/Dropdown/useDropdown.js +18 -76
- package/lib/components/Dropdown/useDropdown.js.map +1 -1
- package/lib/components/Option/useOptionStyles.styles.js +31 -5
- package/lib/components/Option/useOptionStyles.styles.js.map +1 -1
- package/lib/utils/ComboboxBase.types.js.map +1 -1
- package/lib/utils/useComboboxBaseState.js +2 -1
- package/lib/utils/useComboboxBaseState.js.map +1 -1
- package/lib/utils/useListboxSlot.js +37 -0
- package/lib/utils/useListboxSlot.js.map +1 -0
- package/lib/utils/useTriggerSlot.js +75 -0
- package/lib/utils/useTriggerSlot.js.map +1 -0
- package/lib-commonjs/components/Combobox/useCombobox.js +22 -126
- package/lib-commonjs/components/Combobox/useCombobox.js.map +1 -1
- package/lib-commonjs/components/Combobox/useInputTriggerSlot.js +110 -0
- package/lib-commonjs/components/Combobox/useInputTriggerSlot.js.map +1 -0
- package/lib-commonjs/components/Dropdown/useButtonTriggerSlot.js +76 -0
- package/lib-commonjs/components/Dropdown/useButtonTriggerSlot.js.map +1 -0
- package/lib-commonjs/components/Dropdown/useDropdown.js +17 -75
- package/lib-commonjs/components/Dropdown/useDropdown.js.map +1 -1
- package/lib-commonjs/components/Option/useOptionStyles.styles.js +85 -5
- package/lib-commonjs/components/Option/useOptionStyles.styles.js.map +1 -1
- package/lib-commonjs/utils/useComboboxBaseState.js +2 -1
- package/lib-commonjs/utils/useComboboxBaseState.js.map +1 -1
- package/lib-commonjs/utils/useListboxSlot.js +46 -0
- package/lib-commonjs/utils/useListboxSlot.js.map +1 -0
- package/lib-commonjs/utils/useTriggerSlot.js +83 -0
- package/lib-commonjs/utils/useTriggerSlot.js.map +1 -0
- package/package.json +2 -2
- package/lib/utils/useTriggerListboxSlots.js +0 -133
- package/lib/utils/useTriggerListboxSlots.js.map +0 -1
- package/lib-commonjs/utils/useTriggerListboxSlots.js +0 -140
- package/lib-commonjs/utils/useTriggerListboxSlots.js.map +0 -1
|
@@ -11,16 +11,14 @@ Object.defineProperty(exports, "useCombobox_unstable", {
|
|
|
11
11
|
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
|
12
12
|
const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
|
|
13
13
|
const _reactfield = require("@fluentui/react-field");
|
|
14
|
-
const _keyboardkeys = require("@fluentui/keyboard-keys");
|
|
15
14
|
const _reacticons = require("@fluentui/react-icons");
|
|
16
15
|
const _reactutilities = require("@fluentui/react-utilities");
|
|
17
|
-
const _dropdownKeyActions = require("../../utils/dropdownKeyActions");
|
|
18
16
|
const _useComboboxBaseState = require("../../utils/useComboboxBaseState");
|
|
19
17
|
const _useComboboxPositioning = require("../../utils/useComboboxPositioning");
|
|
20
|
-
const _useTriggerListboxSlots = require("../../utils/useTriggerListboxSlots");
|
|
21
18
|
const _Listbox = require("../Listbox/Listbox");
|
|
19
|
+
const _useListboxSlot = require("../../utils/useListboxSlot");
|
|
20
|
+
const _useInputTriggerSlot = require("./useInputTriggerSlot");
|
|
22
21
|
const useCombobox_unstable = (props, ref)=>{
|
|
23
|
-
var _props_input;
|
|
24
22
|
// Merge props from surrounding <Field>, if any
|
|
25
23
|
props = (0, _reactfield.useFieldControlProps_unstable)(props, {
|
|
26
24
|
supportsLabelFor: true,
|
|
@@ -31,9 +29,9 @@ const useCombobox_unstable = (props, ref)=>{
|
|
|
31
29
|
...props,
|
|
32
30
|
editable: true
|
|
33
31
|
});
|
|
34
|
-
const {
|
|
32
|
+
const { open, selectOption, setOpen, setValue, value } = baseState;
|
|
35
33
|
const [comboboxPopupRef, comboboxTargetRef] = (0, _useComboboxPositioning.useComboboxPositioning)(props);
|
|
36
|
-
const { disabled, freeform, inlinePopup
|
|
34
|
+
const { disabled, freeform, inlinePopup } = props;
|
|
37
35
|
const comboId = (0, _reactutilities.useId)('combobox-');
|
|
38
36
|
const { primary: triggerNativeProps, root: rootNativeProps } = (0, _reactutilities.getPartitionedNativeProps)({
|
|
39
37
|
props,
|
|
@@ -43,48 +41,11 @@ const useCombobox_unstable = (props, ref)=>{
|
|
|
43
41
|
'size'
|
|
44
42
|
]
|
|
45
43
|
});
|
|
46
|
-
|
|
47
|
-
const triggerRef = _react.useRef(null);
|
|
48
|
-
// NVDA and JAWS have bugs that suppress reading the input value text when aria-activedescendant is set
|
|
49
|
-
// To prevent this, we clear the HTML attribute (but save the state) when a user presses left/right arrows
|
|
50
|
-
// ref: https://github.com/microsoft/fluentui/issues/26359#issuecomment-1397759888
|
|
51
|
-
const [hideActiveDescendant, setHideActiveDescendant] = _react.useState(false);
|
|
52
|
-
// save the typing vs. navigating options state, as the space key should behave differently in each case
|
|
53
|
-
// we do not want to update the combobox when this changes, just save the value between renders
|
|
54
|
-
const isTyping = _react.useRef(false);
|
|
55
|
-
// set active option and selection based on typing
|
|
56
|
-
const getOptionFromInput = (inputValue)=>{
|
|
57
|
-
const searchString = inputValue === null || inputValue === void 0 ? void 0 : inputValue.trim().toLowerCase();
|
|
58
|
-
if (!searchString || searchString.length === 0) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const matcher = (optionText)=>optionText.toLowerCase().indexOf(searchString) === 0;
|
|
62
|
-
const matches = getOptionsMatchingText(matcher);
|
|
63
|
-
// return first matching option after the current active option, looping back to the top
|
|
64
|
-
if (matches.length > 1 && activeOption) {
|
|
65
|
-
const startIndex = getIndexOfId(activeOption.id);
|
|
66
|
-
const nextMatch = matches.find((option)=>getIndexOfId(option.id) >= startIndex);
|
|
67
|
-
return nextMatch !== null && nextMatch !== void 0 ? nextMatch : matches[0];
|
|
68
|
-
}
|
|
69
|
-
var _matches_;
|
|
70
|
-
return (_matches_ = matches[0]) !== null && _matches_ !== void 0 ? _matches_ : undefined;
|
|
71
|
-
};
|
|
72
|
-
/* Handle typed input */ // reset any typed value when an option is selected
|
|
44
|
+
// reset any typed value when an option is selected
|
|
73
45
|
baseState.selectOption = (ev, option)=>{
|
|
74
46
|
setValue(undefined);
|
|
75
47
|
selectOption(ev, option);
|
|
76
48
|
};
|
|
77
|
-
const onTriggerBlur = (ev)=>{
|
|
78
|
-
// handle selection and updating value if freeform is false
|
|
79
|
-
if (!baseState.open && !freeform) {
|
|
80
|
-
// select matching option, if the value fully matches
|
|
81
|
-
if (value && activeOption && value.trim().toLowerCase() === (activeOption === null || activeOption === void 0 ? void 0 : activeOption.text.toLowerCase())) {
|
|
82
|
-
baseState.selectOption(ev, activeOption);
|
|
83
|
-
}
|
|
84
|
-
// reset typed value when the input loses focus while collapsed, unless freeform is true
|
|
85
|
-
setValue(undefined);
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
49
|
baseState.setOpen = (ev, newState)=>{
|
|
89
50
|
if (disabled) {
|
|
90
51
|
return;
|
|
@@ -94,53 +55,27 @@ const useCombobox_unstable = (props, ref)=>{
|
|
|
94
55
|
}
|
|
95
56
|
setOpen(ev, newState);
|
|
96
57
|
};
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const matchingOption = getOptionFromInput(inputValue);
|
|
104
|
-
setActiveOption(matchingOption);
|
|
105
|
-
setFocusVisible(true);
|
|
106
|
-
// clear selection for single-select if the input value no longer matches the selection
|
|
107
|
-
if (!multiselect && selectedOptions.length === 1 && (inputValue.length < 1 || !matchingOption)) {
|
|
108
|
-
clearSelection(ev);
|
|
58
|
+
const triggerRef = _react.useRef(null);
|
|
59
|
+
const listbox = (0, _useListboxSlot.useListboxSlot)(props.listbox, comboboxPopupRef, {
|
|
60
|
+
state: baseState,
|
|
61
|
+
triggerRef,
|
|
62
|
+
defaultProps: {
|
|
63
|
+
children: props.children
|
|
109
64
|
}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
65
|
+
});
|
|
66
|
+
var _props_input;
|
|
67
|
+
const triggerSlot = (0, _useInputTriggerSlot.useInputTriggerSlot)((_props_input = props.input) !== null && _props_input !== void 0 ? _props_input : {}, (0, _reactutilities.useMergedRefs)(triggerRef, ref), {
|
|
68
|
+
state: baseState,
|
|
69
|
+
freeform,
|
|
115
70
|
defaultProps: {
|
|
116
|
-
ref: (0, _reactutilities.useMergedRefs)((_props_input = props.input) === null || _props_input === void 0 ? void 0 : _props_input.ref, triggerRef),
|
|
117
71
|
type: 'text',
|
|
118
72
|
value: value !== null && value !== void 0 ? value : '',
|
|
119
73
|
...triggerNativeProps
|
|
120
|
-
}
|
|
121
|
-
elementType: 'input'
|
|
74
|
+
}
|
|
122
75
|
});
|
|
123
|
-
const resolvedPropsOnKeyDown = triggerSlot.onKeyDown;
|
|
124
|
-
triggerSlot.onChange = (0, _reactutilities.mergeCallbacks)(triggerSlot.onChange, onTriggerChange);
|
|
125
|
-
triggerSlot.onBlur = (0, _reactutilities.mergeCallbacks)(triggerSlot.onBlur, onTriggerBlur); // only resolve listbox slot if needed
|
|
126
|
-
listboxSlot = open || hasFocus ? _reactutilities.slot.optional(props.listbox, {
|
|
127
|
-
renderByDefault: true,
|
|
128
|
-
defaultProps: {
|
|
129
|
-
children: props.children
|
|
130
|
-
},
|
|
131
|
-
elementType: _Listbox.Listbox
|
|
132
|
-
}) : undefined;
|
|
133
|
-
[triggerSlot, listboxSlot] = (0, _useTriggerListboxSlots.useTriggerListboxSlots)(props, baseState, ref, triggerSlot, listboxSlot);
|
|
134
|
-
const listboxRef = (0, _reactutilities.useMergedRefs)(listboxSlot === null || listboxSlot === void 0 ? void 0 : listboxSlot.ref, comboboxPopupRef);
|
|
135
|
-
if (hideActiveDescendant) {
|
|
136
|
-
triggerSlot['aria-activedescendant'] = undefined;
|
|
137
|
-
}
|
|
138
|
-
if (listboxSlot) {
|
|
139
|
-
listboxSlot.ref = listboxRef;
|
|
140
|
-
}
|
|
141
76
|
const rootSlot = _reactutilities.slot.always(props.root, {
|
|
142
77
|
defaultProps: {
|
|
143
|
-
'aria-owns': !inlinePopup ?
|
|
78
|
+
'aria-owns': !inlinePopup && open ? listbox === null || listbox === void 0 ? void 0 : listbox.id : undefined,
|
|
144
79
|
...rootNativeProps
|
|
145
80
|
},
|
|
146
81
|
elementType: 'div'
|
|
@@ -155,7 +90,7 @@ const useCombobox_unstable = (props, ref)=>{
|
|
|
155
90
|
},
|
|
156
91
|
root: rootSlot,
|
|
157
92
|
input: triggerSlot,
|
|
158
|
-
listbox:
|
|
93
|
+
listbox: open ? listbox : undefined,
|
|
159
94
|
expandIcon: _reactutilities.slot.optional(props.expandIcon, {
|
|
160
95
|
renderByDefault: true,
|
|
161
96
|
defaultProps: {
|
|
@@ -167,54 +102,15 @@ const useCombobox_unstable = (props, ref)=>{
|
|
|
167
102
|
}),
|
|
168
103
|
...baseState
|
|
169
104
|
};
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
state.input.onKeyDown = (0, _reactutilities.useEventCallback)((ev)=>{
|
|
173
|
-
if (!open && (0, _dropdownKeyActions.getDropdownActionFromKey)(ev) === 'Type') {
|
|
174
|
-
baseState.setOpen(ev, true);
|
|
175
|
-
}
|
|
176
|
-
// clear activedescendant when moving the text insertion cursor
|
|
177
|
-
if (ev.key === _keyboardkeys.ArrowLeft || ev.key === _keyboardkeys.ArrowRight) {
|
|
178
|
-
setHideActiveDescendant(true);
|
|
179
|
-
} else {
|
|
180
|
-
setHideActiveDescendant(false);
|
|
181
|
-
}
|
|
182
|
-
// update typing state to true if the user is typing
|
|
183
|
-
const action = (0, _dropdownKeyActions.getDropdownActionFromKey)(ev, {
|
|
184
|
-
open,
|
|
185
|
-
multiselect
|
|
186
|
-
});
|
|
187
|
-
if (action === 'Type') {
|
|
188
|
-
isTyping.current = true;
|
|
189
|
-
} else if (action === 'Open' && ev.key !== ' ' || action === 'Next' || action === 'Previous' || action === 'First' || action === 'Last' || action === 'PageUp' || action === 'PageDown') {
|
|
190
|
-
isTyping.current = false;
|
|
191
|
-
}
|
|
192
|
-
// allow space to insert a character if freeform & the last action was typing, or if the popup is closed
|
|
193
|
-
if (freeform && (isTyping.current || !open) && ev.key === ' ') {
|
|
194
|
-
resolvedPropsOnKeyDown === null || resolvedPropsOnKeyDown === void 0 ? void 0 : resolvedPropsOnKeyDown(ev);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
// if we're not allowing space to type, continue with default behavior
|
|
198
|
-
defaultOnTriggerKeyDown === null || defaultOnTriggerKeyDown === void 0 ? void 0 : defaultOnTriggerKeyDown(ev);
|
|
199
|
-
});
|
|
200
|
-
/* handle open/close + focus change when clicking expandIcon */ const { onMouseDown: onIconMouseDown, onClick: onIconClick } = state.expandIcon || {};
|
|
201
|
-
const onExpandIconMouseDown = (0, _reactutilities.useEventCallback)((0, _reactutilities.mergeCallbacks)(onIconMouseDown, ()=>{
|
|
202
|
-
// do not dismiss on blur when closing via clicking the icon
|
|
203
|
-
if (open) {
|
|
204
|
-
baseState.ignoreNextBlur.current = true;
|
|
205
|
-
}
|
|
206
|
-
}));
|
|
207
|
-
const onExpandIconClick = (0, _reactutilities.useEventCallback)((0, _reactutilities.mergeCallbacks)(onIconClick, (event)=>{
|
|
105
|
+
/* handle open/close + focus change when clicking expandIcon */ const { onMouseDown: onIconMouseDown } = state.expandIcon || {};
|
|
106
|
+
const onExpandIconMouseDown = (0, _reactutilities.useEventCallback)((0, _reactutilities.mergeCallbacks)(onIconMouseDown, (event)=>{
|
|
208
107
|
var _triggerRef_current;
|
|
209
|
-
|
|
108
|
+
event.preventDefault();
|
|
210
109
|
state.setOpen(event, !state.open);
|
|
211
110
|
(_triggerRef_current = triggerRef.current) === null || _triggerRef_current === void 0 ? void 0 : _triggerRef_current.focus();
|
|
212
|
-
// set focus visible=false, since this can only be done with the mouse/pointer
|
|
213
|
-
setFocusVisible(false);
|
|
214
111
|
}));
|
|
215
112
|
if (state.expandIcon) {
|
|
216
113
|
state.expandIcon.onMouseDown = onExpandIconMouseDown;
|
|
217
|
-
state.expandIcon.onClick = onExpandIconClick;
|
|
218
114
|
// If there is no explicit aria-label, calculate default accName attribute for expandIcon button,
|
|
219
115
|
// using the following steps:
|
|
220
116
|
// 1. If there is an aria-label, it is "Open [aria-label]"
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useCombobox.js"],"sourcesContent":["import * as React from 'react';\nimport { useFieldControlProps_unstable } from '@fluentui/react-field';\nimport { ArrowLeft, ArrowRight } from '@fluentui/keyboard-keys';\nimport { ChevronDownRegular as ChevronDownIcon } from '@fluentui/react-icons';\nimport { getPartitionedNativeProps, mergeCallbacks, useEventCallback, useId, useMergedRefs, slot } from '@fluentui/react-utilities';\nimport { getDropdownActionFromKey } from '../../utils/dropdownKeyActions';\nimport { useComboboxBaseState } from '../../utils/useComboboxBaseState';\nimport { useComboboxPositioning } from '../../utils/useComboboxPositioning';\nimport { useTriggerListboxSlots } from '../../utils/useTriggerListboxSlots';\nimport { Listbox } from '../Listbox/Listbox';\n/**\n * Create the state required to render Combobox.\n *\n * The returned state can be modified with hooks such as useComboboxStyles_unstable,\n * before being passed to renderCombobox_unstable.\n *\n * @param props - props from this instance of Combobox\n * @param ref - reference to root HTMLElement of Combobox\n */ export const useCombobox_unstable = (props, ref)=>{\n var _props_input;\n // Merge props from surrounding <Field>, if any\n props = useFieldControlProps_unstable(props, {\n supportsLabelFor: true,\n supportsRequired: true,\n supportsSize: true\n });\n const baseState = useComboboxBaseState({\n ...props,\n editable: true\n });\n const { activeOption, clearSelection, getIndexOfId, getOptionsMatchingText, hasFocus, open, selectOption, selectedOptions, setActiveOption, setFocusVisible, setOpen, setValue, value } = baseState;\n const [comboboxPopupRef, comboboxTargetRef] = useComboboxPositioning(props);\n const { disabled, freeform, inlinePopup, multiselect } = props;\n const comboId = useId('combobox-');\n const { primary: triggerNativeProps, root: rootNativeProps } = getPartitionedNativeProps({\n props,\n primarySlotTagName: 'input',\n excludedPropNames: [\n 'children',\n 'size'\n ]\n });\n const rootRef = React.useRef(null);\n const triggerRef = React.useRef(null);\n // NVDA and JAWS have bugs that suppress reading the input value text when aria-activedescendant is set\n // To prevent this, we clear the HTML attribute (but save the state) when a user presses left/right arrows\n // ref: https://github.com/microsoft/fluentui/issues/26359#issuecomment-1397759888\n const [hideActiveDescendant, setHideActiveDescendant] = React.useState(false);\n // save the typing vs. navigating options state, as the space key should behave differently in each case\n // we do not want to update the combobox when this changes, just save the value between renders\n const isTyping = React.useRef(false);\n // set active option and selection based on typing\n const getOptionFromInput = (inputValue)=>{\n const searchString = inputValue === null || inputValue === void 0 ? void 0 : inputValue.trim().toLowerCase();\n if (!searchString || searchString.length === 0) {\n return;\n }\n const matcher = (optionText)=>optionText.toLowerCase().indexOf(searchString) === 0;\n const matches = getOptionsMatchingText(matcher);\n // return first matching option after the current active option, looping back to the top\n if (matches.length > 1 && activeOption) {\n const startIndex = getIndexOfId(activeOption.id);\n const nextMatch = matches.find((option)=>getIndexOfId(option.id) >= startIndex);\n return nextMatch !== null && nextMatch !== void 0 ? nextMatch : matches[0];\n }\n var _matches_;\n return (_matches_ = matches[0]) !== null && _matches_ !== void 0 ? _matches_ : undefined;\n };\n /* Handle typed input */ // reset any typed value when an option is selected\n baseState.selectOption = (ev, option)=>{\n setValue(undefined);\n selectOption(ev, option);\n };\n const onTriggerBlur = (ev)=>{\n // handle selection and updating value if freeform is false\n if (!baseState.open && !freeform) {\n // select matching option, if the value fully matches\n if (value && activeOption && value.trim().toLowerCase() === (activeOption === null || activeOption === void 0 ? void 0 : activeOption.text.toLowerCase())) {\n baseState.selectOption(ev, activeOption);\n }\n // reset typed value when the input loses focus while collapsed, unless freeform is true\n setValue(undefined);\n }\n };\n baseState.setOpen = (ev, newState)=>{\n if (disabled) {\n return;\n }\n if (!newState && !freeform) {\n setValue(undefined);\n }\n setOpen(ev, newState);\n };\n // update value and active option based on input\n const onTriggerChange = (ev)=>{\n const inputValue = ev.target.value;\n // update uncontrolled value\n baseState.setValue(inputValue);\n // handle updating active option based on input\n const matchingOption = getOptionFromInput(inputValue);\n setActiveOption(matchingOption);\n setFocusVisible(true);\n // clear selection for single-select if the input value no longer matches the selection\n if (!multiselect && selectedOptions.length === 1 && (inputValue.length < 1 || !matchingOption)) {\n clearSelection(ev);\n }\n };\n // resolve input and listbox slot props\n let triggerSlot;\n let listboxSlot;\n triggerSlot = slot.always(props.input, {\n defaultProps: {\n ref: useMergedRefs((_props_input = props.input) === null || _props_input === void 0 ? void 0 : _props_input.ref, triggerRef),\n type: 'text',\n value: value !== null && value !== void 0 ? value : '',\n ...triggerNativeProps\n },\n elementType: 'input'\n });\n const resolvedPropsOnKeyDown = triggerSlot.onKeyDown;\n triggerSlot.onChange = mergeCallbacks(triggerSlot.onChange, onTriggerChange);\n triggerSlot.onBlur = mergeCallbacks(triggerSlot.onBlur, onTriggerBlur); // only resolve listbox slot if needed\n listboxSlot = open || hasFocus ? slot.optional(props.listbox, {\n renderByDefault: true,\n defaultProps: {\n children: props.children\n },\n elementType: Listbox\n }) : undefined;\n [triggerSlot, listboxSlot] = useTriggerListboxSlots(props, baseState, ref, triggerSlot, listboxSlot);\n const listboxRef = useMergedRefs(listboxSlot === null || listboxSlot === void 0 ? void 0 : listboxSlot.ref, comboboxPopupRef);\n if (hideActiveDescendant) {\n triggerSlot['aria-activedescendant'] = undefined;\n }\n if (listboxSlot) {\n listboxSlot.ref = listboxRef;\n }\n const rootSlot = slot.always(props.root, {\n defaultProps: {\n 'aria-owns': !inlinePopup ? listboxSlot === null || listboxSlot === void 0 ? void 0 : listboxSlot.id : undefined,\n ...rootNativeProps\n },\n elementType: 'div'\n });\n rootSlot.ref = useMergedRefs(rootSlot.ref, comboboxTargetRef);\n const state = {\n components: {\n root: 'div',\n input: 'input',\n expandIcon: 'span',\n listbox: Listbox\n },\n root: rootSlot,\n input: triggerSlot,\n listbox: listboxSlot,\n expandIcon: slot.optional(props.expandIcon, {\n renderByDefault: true,\n defaultProps: {\n 'aria-expanded': open,\n children: /*#__PURE__*/ React.createElement(ChevronDownIcon, null),\n role: 'button'\n },\n elementType: 'span'\n }),\n ...baseState\n };\n state.root.ref = useMergedRefs(state.root.ref, rootRef);\n /* Set input.onKeyDown here, so we can override the default behavior for spacebar */ const defaultOnTriggerKeyDown = state.input.onKeyDown;\n state.input.onKeyDown = useEventCallback((ev)=>{\n if (!open && getDropdownActionFromKey(ev) === 'Type') {\n baseState.setOpen(ev, true);\n }\n // clear activedescendant when moving the text insertion cursor\n if (ev.key === ArrowLeft || ev.key === ArrowRight) {\n setHideActiveDescendant(true);\n } else {\n setHideActiveDescendant(false);\n }\n // update typing state to true if the user is typing\n const action = getDropdownActionFromKey(ev, {\n open,\n multiselect\n });\n if (action === 'Type') {\n isTyping.current = true;\n } else if (action === 'Open' && ev.key !== ' ' || action === 'Next' || action === 'Previous' || action === 'First' || action === 'Last' || action === 'PageUp' || action === 'PageDown') {\n isTyping.current = false;\n }\n // allow space to insert a character if freeform & the last action was typing, or if the popup is closed\n if (freeform && (isTyping.current || !open) && ev.key === ' ') {\n resolvedPropsOnKeyDown === null || resolvedPropsOnKeyDown === void 0 ? void 0 : resolvedPropsOnKeyDown(ev);\n return;\n }\n // if we're not allowing space to type, continue with default behavior\n defaultOnTriggerKeyDown === null || defaultOnTriggerKeyDown === void 0 ? void 0 : defaultOnTriggerKeyDown(ev);\n });\n /* handle open/close + focus change when clicking expandIcon */ const { onMouseDown: onIconMouseDown, onClick: onIconClick } = state.expandIcon || {};\n const onExpandIconMouseDown = useEventCallback(mergeCallbacks(onIconMouseDown, ()=>{\n // do not dismiss on blur when closing via clicking the icon\n if (open) {\n baseState.ignoreNextBlur.current = true;\n }\n }));\n const onExpandIconClick = useEventCallback(mergeCallbacks(onIconClick, (event)=>{\n var _triggerRef_current;\n // open and set focus\n state.setOpen(event, !state.open);\n (_triggerRef_current = triggerRef.current) === null || _triggerRef_current === void 0 ? void 0 : _triggerRef_current.focus();\n // set focus visible=false, since this can only be done with the mouse/pointer\n setFocusVisible(false);\n }));\n if (state.expandIcon) {\n state.expandIcon.onMouseDown = onExpandIconMouseDown;\n state.expandIcon.onClick = onExpandIconClick;\n // If there is no explicit aria-label, calculate default accName attribute for expandIcon button,\n // using the following steps:\n // 1. If there is an aria-label, it is \"Open [aria-label]\"\n // 2. If there is an aria-labelledby, it is \"Open [aria-labelledby target]\" (using aria-labelledby + ids)\n // 3. If there is no aria-label/ledby attr, it falls back to \"Open\"\n // We can't fall back to a label/htmlFor name because of https://github.com/w3c/accname/issues/179\n const hasExpandLabel = state.expandIcon['aria-label'] || state.expandIcon['aria-labelledby'];\n const defaultOpenString = 'Open'; // this is english-only since it is the fallback\n if (!hasExpandLabel) {\n if (props['aria-labelledby']) {\n var _state_expandIcon_id;\n const chevronId = (_state_expandIcon_id = state.expandIcon.id) !== null && _state_expandIcon_id !== void 0 ? _state_expandIcon_id : `${comboId}-chevron`;\n const chevronLabelledBy = `${chevronId} ${state.input['aria-labelledby']}`;\n state.expandIcon['aria-label'] = defaultOpenString;\n state.expandIcon.id = chevronId;\n state.expandIcon['aria-labelledby'] = chevronLabelledBy;\n } else if (props['aria-label']) {\n state.expandIcon['aria-label'] = `${defaultOpenString} ${props['aria-label']}`;\n } else {\n state.expandIcon['aria-label'] = defaultOpenString;\n }\n }\n }\n return state;\n};\n"],"names":["useCombobox_unstable","props","ref","_props_input","useFieldControlProps_unstable","supportsLabelFor","supportsRequired","supportsSize","baseState","useComboboxBaseState","editable","activeOption","clearSelection","getIndexOfId","getOptionsMatchingText","hasFocus","open","selectOption","selectedOptions","setActiveOption","setFocusVisible","setOpen","setValue","value","comboboxPopupRef","comboboxTargetRef","useComboboxPositioning","disabled","freeform","inlinePopup","multiselect","comboId","useId","primary","triggerNativeProps","root","rootNativeProps","getPartitionedNativeProps","primarySlotTagName","excludedPropNames","rootRef","React","useRef","triggerRef","hideActiveDescendant","setHideActiveDescendant","useState","isTyping","getOptionFromInput","inputValue","searchString","trim","toLowerCase","length","matcher","optionText","indexOf","matches","startIndex","id","nextMatch","find","option","_matches_","undefined","ev","onTriggerBlur","text","newState","onTriggerChange","target","matchingOption","triggerSlot","listboxSlot","slot","always","input","defaultProps","useMergedRefs","type","elementType","resolvedPropsOnKeyDown","onKeyDown","onChange","mergeCallbacks","onBlur","optional","listbox","renderByDefault","children","Listbox","useTriggerListboxSlots","listboxRef","rootSlot","state","components","expandIcon","createElement","ChevronDownIcon","role","defaultOnTriggerKeyDown","useEventCallback","getDropdownActionFromKey","key","ArrowLeft","ArrowRight","action","current","onMouseDown","onIconMouseDown","onClick","onIconClick","onExpandIconMouseDown","ignoreNextBlur","onExpandIconClick","event","_triggerRef_current","focus","hasExpandLabel","defaultOpenString","_state_expandIcon_id","chevronId","chevronLabelledBy"],"mappings":";;;;+BAkBiBA;;;eAAAA;;;;iEAlBM;4BACuB;8BACR;4BACgB;gCACkD;oCAC/D;sCACJ;wCACE;wCACA;yBACf;AASb,MAAMA,uBAAuB,CAACC,OAAOC;IAC5C,IAAIC;IACJ,+CAA+C;IAC/CF,QAAQG,IAAAA,yCAA6B,EAACH,OAAO;QACzCI,kBAAkB;QAClBC,kBAAkB;QAClBC,cAAc;IAClB;IACA,MAAMC,YAAYC,IAAAA,0CAAoB,EAAC;QACnC,GAAGR,KAAK;QACRS,UAAU;IACd;IACA,MAAM,EAAEC,YAAY,EAAEC,cAAc,EAAEC,YAAY,EAAEC,sBAAsB,EAAEC,QAAQ,EAAEC,IAAI,EAAEC,YAAY,EAAEC,eAAe,EAAEC,eAAe,EAAEC,eAAe,EAAEC,OAAO,EAAEC,QAAQ,EAAEC,KAAK,EAAE,GAAGf;IAC1L,M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AACD,CAAAA,uBAAuB1B,MAAME,UAAU,CAACrC,EAAE,AAAD,MAAO,QAAQ6D,yBAAyB,KAAK,IAAIA,uBAAuB,CAAC,EAAEzF,QAAQ,QAAQ,CAAC;gBACxJ,MAAM2F,oBAAoB,CAAC,EAAED,UAAU,CAAC,EAAE3B,MAAMlB,KAAK,CAAC,kBAAkB,CAAC,CAAC;gBAC1EkB,MAAME,UAAU,CAAC,aAAa,GAAGuB;gBACjCzB,MAAME,UAAU,CAACrC,EAAE,GAAG8D;gBACtB3B,MAAME,UAAU,CAAC,kBAAkB,GAAG0B;YAC1C,OAAO,IAAIzH,KAAK,CAAC,aAAa,EAAE;gBAC5B6F,MAAME,UAAU,CAAC,aAAa,GAAG,CAAC,EAAEuB,kBAAkB,CAAC,EAAEtH,KAAK,CAAC,aAAa,CAAC,CAAC;YAClF,OAAO;gBACH6F,MAAME,UAAU,CAAC,aAAa,GAAGuB;YACrC;QACJ;IACJ;IACA,OAAOzB;AACX"}
|
|
1
|
+
{"version":3,"sources":["useCombobox.js"],"sourcesContent":["import * as React from 'react';\nimport { useFieldControlProps_unstable } from '@fluentui/react-field';\nimport { ChevronDownRegular as ChevronDownIcon } from '@fluentui/react-icons';\nimport { getPartitionedNativeProps, mergeCallbacks, useEventCallback, useId, useMergedRefs, slot } from '@fluentui/react-utilities';\nimport { useComboboxBaseState } from '../../utils/useComboboxBaseState';\nimport { useComboboxPositioning } from '../../utils/useComboboxPositioning';\nimport { Listbox } from '../Listbox/Listbox';\nimport { useListboxSlot } from '../../utils/useListboxSlot';\nimport { useInputTriggerSlot } from './useInputTriggerSlot';\n/**\n * Create the state required to render Combobox.\n *\n * The returned state can be modified with hooks such as useComboboxStyles_unstable,\n * before being passed to renderCombobox_unstable.\n *\n * @param props - props from this instance of Combobox\n * @param ref - reference to root HTMLElement of Combobox\n */ export const useCombobox_unstable = (props, ref)=>{\n // Merge props from surrounding <Field>, if any\n props = useFieldControlProps_unstable(props, {\n supportsLabelFor: true,\n supportsRequired: true,\n supportsSize: true\n });\n const baseState = useComboboxBaseState({\n ...props,\n editable: true\n });\n const { open, selectOption, setOpen, setValue, value } = baseState;\n const [comboboxPopupRef, comboboxTargetRef] = useComboboxPositioning(props);\n const { disabled, freeform, inlinePopup } = props;\n const comboId = useId('combobox-');\n const { primary: triggerNativeProps, root: rootNativeProps } = getPartitionedNativeProps({\n props,\n primarySlotTagName: 'input',\n excludedPropNames: [\n 'children',\n 'size'\n ]\n });\n // reset any typed value when an option is selected\n baseState.selectOption = (ev, option)=>{\n setValue(undefined);\n selectOption(ev, option);\n };\n baseState.setOpen = (ev, newState)=>{\n if (disabled) {\n return;\n }\n if (!newState && !freeform) {\n setValue(undefined);\n }\n setOpen(ev, newState);\n };\n const triggerRef = React.useRef(null);\n const listbox = useListboxSlot(props.listbox, comboboxPopupRef, {\n state: baseState,\n triggerRef,\n defaultProps: {\n children: props.children\n }\n });\n var _props_input;\n const triggerSlot = useInputTriggerSlot((_props_input = props.input) !== null && _props_input !== void 0 ? _props_input : {}, useMergedRefs(triggerRef, ref), {\n state: baseState,\n freeform,\n defaultProps: {\n type: 'text',\n value: value !== null && value !== void 0 ? value : '',\n ...triggerNativeProps\n }\n });\n const rootSlot = slot.always(props.root, {\n defaultProps: {\n 'aria-owns': !inlinePopup && open ? listbox === null || listbox === void 0 ? void 0 : listbox.id : undefined,\n ...rootNativeProps\n },\n elementType: 'div'\n });\n rootSlot.ref = useMergedRefs(rootSlot.ref, comboboxTargetRef);\n const state = {\n components: {\n root: 'div',\n input: 'input',\n expandIcon: 'span',\n listbox: Listbox\n },\n root: rootSlot,\n input: triggerSlot,\n listbox: open ? listbox : undefined,\n expandIcon: slot.optional(props.expandIcon, {\n renderByDefault: true,\n defaultProps: {\n 'aria-expanded': open,\n children: /*#__PURE__*/ React.createElement(ChevronDownIcon, null),\n role: 'button'\n },\n elementType: 'span'\n }),\n ...baseState\n };\n /* handle open/close + focus change when clicking expandIcon */ const { onMouseDown: onIconMouseDown } = state.expandIcon || {};\n const onExpandIconMouseDown = useEventCallback(mergeCallbacks(onIconMouseDown, (event)=>{\n var _triggerRef_current;\n event.preventDefault();\n state.setOpen(event, !state.open);\n (_triggerRef_current = triggerRef.current) === null || _triggerRef_current === void 0 ? void 0 : _triggerRef_current.focus();\n }));\n if (state.expandIcon) {\n state.expandIcon.onMouseDown = onExpandIconMouseDown;\n // If there is no explicit aria-label, calculate default accName attribute for expandIcon button,\n // using the following steps:\n // 1. If there is an aria-label, it is \"Open [aria-label]\"\n // 2. If there is an aria-labelledby, it is \"Open [aria-labelledby target]\" (using aria-labelledby + ids)\n // 3. If there is no aria-label/ledby attr, it falls back to \"Open\"\n // We can't fall back to a label/htmlFor name because of https://github.com/w3c/accname/issues/179\n const hasExpandLabel = state.expandIcon['aria-label'] || state.expandIcon['aria-labelledby'];\n const defaultOpenString = 'Open'; // this is english-only since it is the fallback\n if (!hasExpandLabel) {\n if (props['aria-labelledby']) {\n var _state_expandIcon_id;\n const chevronId = (_state_expandIcon_id = state.expandIcon.id) !== null && _state_expandIcon_id !== void 0 ? _state_expandIcon_id : `${comboId}-chevron`;\n const chevronLabelledBy = `${chevronId} ${state.input['aria-labelledby']}`;\n state.expandIcon['aria-label'] = defaultOpenString;\n state.expandIcon.id = chevronId;\n state.expandIcon['aria-labelledby'] = chevronLabelledBy;\n } else if (props['aria-label']) {\n state.expandIcon['aria-label'] = `${defaultOpenString} ${props['aria-label']}`;\n } else {\n state.expandIcon['aria-label'] = defaultOpenString;\n }\n }\n }\n return state;\n};\n"],"names":["useCombobox_unstable","props","ref","useFieldControlProps_unstable","supportsLabelFor","supportsRequired","supportsSize","baseState","useComboboxBaseState","editable","open","selectOption","setOpen","setValue","value","comboboxPopupRef","comboboxTargetRef","useComboboxPositioning","disabled","freeform","inlinePopup","comboId","useId","primary","triggerNativeProps","root","rootNativeProps","getPartitionedNativeProps","primarySlotTagName","excludedPropNames","ev","option","undefined","newState","triggerRef","React","useRef","listbox","useListboxSlot","state","defaultProps","children","_props_input","triggerSlot","useInputTriggerSlot","input","useMergedRefs","type","rootSlot","slot","always","id","elementType","components","expandIcon","Listbox","optional","renderByDefault","createElement","ChevronDownIcon","role","onMouseDown","onIconMouseDown","onExpandIconMouseDown","useEventCallback","mergeCallbacks","event","_triggerRef_current","preventDefault","current","focus","hasExpandLabel","defaultOpenString","_state_expandIcon_id","chevronId","chevronLabelledBy"],"mappings":";;;;+BAiBiBA;;;eAAAA;;;;iEAjBM;4BACuB;4BACQ;gCACkD;sCACnE;wCACE;yBACf;gCACO;qCACK;AASzB,MAAMA,uBAAuB,CAACC,OAAOC;IAC5C,+CAA+C;IAC/CD,QAAQE,IAAAA,yCAA6B,EAACF,OAAO;QACzCG,kBAAkB;QAClBC,kBAAkB;QAClBC,cAAc;IAClB;IACA,MAAMC,YAAYC,IAAAA,0CAAoB,EAAC;QACnC,GAAGP,KAAK;QACRQ,UAAU;IACd;IACA,MAAM,EAAEC,IAAI,EAAEC,YAAY,EAAEC,OAAO,EAAEC,QAAQ,EAAEC,KAAK,EAAE,GAAGP;IACzD,MAAM,CAACQ,kBAAkBC,kBAAkB,GAAGC,IAAAA,8CAAsB,EAAChB;IACrE,MAAM,EAAEiB,QAAQ,EAAEC,QAAQ,EAAEC,WAAW,EAAE,GAAGnB;IAC5C,MAAMoB,UAAUC,IAAAA,qBAAK,EAAC;IACtB,MAAM,EAAEC,SAASC,kBAAkB,EAAEC,MAAMC,eAAe,EAAE,GAAGC,IAAAA,yCAAyB,EAAC;QACrF1B;QACA2B,oBAAoB;QACpBC,mBAAmB;YACf;YACA;SACH;IACL;IACA,mDAAmD;IACnDtB,UAAUI,YAAY,GAAG,CAACmB,IAAIC;QAC1BlB,SAASmB;QACTrB,aAAamB,IAAIC;IACrB;IACAxB,UAAUK,OAAO,GAAG,CAACkB,IAAIG;QACrB,IAAIf,UAAU;YACV;QACJ;QACA,IAAI,CAACe,YAAY,CAACd,UAAU;YACxBN,SAASmB;QACb;QACApB,QAAQkB,IAAIG;IACh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AEvE,KAAK,CAAC,aAAa,CAAC,CAAC;YAClF,OAAO;gBACHsC,MAAMe,UAAU,CAAC,aAAa,GAAGkB;YACrC;QACJ;IACJ;IACA,OAAOjC;AACX"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "useInputTriggerSlot", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return useInputTriggerSlot;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
|
12
|
+
const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
|
|
13
|
+
const _reactutilities = require("@fluentui/react-utilities");
|
|
14
|
+
const _keyboardkeys = require("@fluentui/keyboard-keys");
|
|
15
|
+
const _useTriggerSlot = require("../../utils/useTriggerSlot");
|
|
16
|
+
const _dropdownKeyActions = require("../../utils/dropdownKeyActions");
|
|
17
|
+
function useInputTriggerSlot(triggerFromProps, ref, options) {
|
|
18
|
+
const { state: { open, value, activeOption, selectOption, setValue, setActiveOption, setFocusVisible, multiselect, selectedOptions, clearSelection, getOptionsMatchingText, getIndexOfId, setOpen }, freeform, defaultProps } = options;
|
|
19
|
+
const onBlur = (ev)=>{
|
|
20
|
+
// handle selection and updating value if freeform is false
|
|
21
|
+
if (!open && !freeform) {
|
|
22
|
+
// select matching option, if the value fully matches
|
|
23
|
+
if (value && activeOption && value.trim().toLowerCase() === (activeOption === null || activeOption === void 0 ? void 0 : activeOption.text.toLowerCase())) {
|
|
24
|
+
selectOption(ev, activeOption);
|
|
25
|
+
}
|
|
26
|
+
// reset typed value when the input loses focus while collapsed, unless freeform is true
|
|
27
|
+
setValue(undefined);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const getOptionFromInput = (inputValue)=>{
|
|
31
|
+
const searchString = inputValue === null || inputValue === void 0 ? void 0 : inputValue.trim().toLowerCase();
|
|
32
|
+
if (!searchString || searchString.length === 0) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const matcher = (optionText)=>optionText.toLowerCase().indexOf(searchString) === 0;
|
|
36
|
+
const matches = getOptionsMatchingText(matcher);
|
|
37
|
+
// return first matching option after the current active option, looping back to the top
|
|
38
|
+
if (matches.length > 1 && activeOption) {
|
|
39
|
+
const startIndex = getIndexOfId(activeOption.id);
|
|
40
|
+
const nextMatch = matches.find((option)=>getIndexOfId(option.id) >= startIndex);
|
|
41
|
+
return nextMatch !== null && nextMatch !== void 0 ? nextMatch : matches[0];
|
|
42
|
+
}
|
|
43
|
+
var _matches_;
|
|
44
|
+
return (_matches_ = matches[0]) !== null && _matches_ !== void 0 ? _matches_ : undefined;
|
|
45
|
+
};
|
|
46
|
+
// update value and active option based on input
|
|
47
|
+
const onChange = (ev)=>{
|
|
48
|
+
const inputValue = ev.target.value;
|
|
49
|
+
// update uncontrolled value
|
|
50
|
+
setValue(inputValue);
|
|
51
|
+
// handle updating active option based on input
|
|
52
|
+
const matchingOption = getOptionFromInput(inputValue);
|
|
53
|
+
setActiveOption(matchingOption);
|
|
54
|
+
setFocusVisible(true);
|
|
55
|
+
// clear selection for single-select if the input value no longer matches the selection
|
|
56
|
+
if (!multiselect && selectedOptions.length === 1 && (inputValue.length < 1 || !matchingOption)) {
|
|
57
|
+
clearSelection(ev);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const trigger = (0, _useTriggerSlot.useTriggerSlot)(triggerFromProps, ref, {
|
|
61
|
+
state: options.state,
|
|
62
|
+
defaultProps,
|
|
63
|
+
elementType: 'input'
|
|
64
|
+
});
|
|
65
|
+
trigger.onChange = (0, _reactutilities.mergeCallbacks)(trigger.onChange, onChange);
|
|
66
|
+
trigger.onBlur = (0, _reactutilities.mergeCallbacks)(trigger.onBlur, onBlur);
|
|
67
|
+
// NVDA and JAWS have bugs that suppress reading the input value text when aria-activedescendant is set
|
|
68
|
+
// To prevent this, we clear the HTML attribute (but save the state) when a user presses left/right arrows
|
|
69
|
+
// ref: https://github.com/microsoft/fluentui/issues/26359#issuecomment-1397759888
|
|
70
|
+
const [hideActiveDescendant, setHideActiveDescendant] = _react.useState(false);
|
|
71
|
+
// save the typing vs. navigating options state, as the space key should behave differently in each case
|
|
72
|
+
// we do not want to update the combobox when this changes, just save the value between renders
|
|
73
|
+
const isTyping = _react.useRef(false);
|
|
74
|
+
/**
|
|
75
|
+
* Freeform combobox should not select
|
|
76
|
+
*/ const defaultOnKeyDown = trigger.onKeyDown;
|
|
77
|
+
const onKeyDown = (0, _reactutilities.useEventCallback)((ev)=>{
|
|
78
|
+
if (!open && (0, _dropdownKeyActions.getDropdownActionFromKey)(ev) === 'Type') {
|
|
79
|
+
setOpen(ev, true);
|
|
80
|
+
}
|
|
81
|
+
// clear activedescendant when moving the text insertion cursor
|
|
82
|
+
if (ev.key === _keyboardkeys.ArrowLeft || ev.key === _keyboardkeys.ArrowRight) {
|
|
83
|
+
setHideActiveDescendant(true);
|
|
84
|
+
} else {
|
|
85
|
+
setHideActiveDescendant(false);
|
|
86
|
+
}
|
|
87
|
+
// update typing state to true if the user is typing
|
|
88
|
+
const action = (0, _dropdownKeyActions.getDropdownActionFromKey)(ev, {
|
|
89
|
+
open,
|
|
90
|
+
multiselect
|
|
91
|
+
});
|
|
92
|
+
if (action === 'Type') {
|
|
93
|
+
isTyping.current = true;
|
|
94
|
+
} else if (action === 'Open' && ev.key !== ' ' || action === 'Next' || action === 'Previous' || action === 'First' || action === 'Last' || action === 'PageUp' || action === 'PageDown') {
|
|
95
|
+
isTyping.current = false;
|
|
96
|
+
}
|
|
97
|
+
// allow space to insert a character if freeform & the last action was typing, or if the popup is closed
|
|
98
|
+
if (freeform && (isTyping.current || !open) && ev.key === ' ') {
|
|
99
|
+
var _triggerFromProps_onKeyDown;
|
|
100
|
+
triggerFromProps === null || triggerFromProps === void 0 ? void 0 : (_triggerFromProps_onKeyDown = triggerFromProps.onKeyDown) === null || _triggerFromProps_onKeyDown === void 0 ? void 0 : _triggerFromProps_onKeyDown.call(triggerFromProps, ev);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
defaultOnKeyDown === null || defaultOnKeyDown === void 0 ? void 0 : defaultOnKeyDown(ev);
|
|
104
|
+
});
|
|
105
|
+
trigger.onKeyDown = onKeyDown;
|
|
106
|
+
if (hideActiveDescendant) {
|
|
107
|
+
trigger['aria-activedescendant'] = undefined;
|
|
108
|
+
}
|
|
109
|
+
return trigger;
|
|
110
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useInputTriggerSlot.js"],"sourcesContent":["import * as React from 'react';\nimport { mergeCallbacks, useEventCallback } from '@fluentui/react-utilities';\nimport { ArrowLeft, ArrowRight } from '@fluentui/keyboard-keys';\nimport { useTriggerSlot } from '../../utils/useTriggerSlot';\nimport { getDropdownActionFromKey } from '../../utils/dropdownKeyActions';\n/*\n * useInputTriggerSlot returns a tuple of trigger/listbox shorthand,\n * with the semantics and event handlers needed for the Combobox and Dropdown components.\n * The element type of the ref should always match the element type used in the trigger shorthand.\n */ export function useInputTriggerSlot(triggerFromProps, ref, options) {\n const { state: { open, value, activeOption, selectOption, setValue, setActiveOption, setFocusVisible, multiselect, selectedOptions, clearSelection, getOptionsMatchingText, getIndexOfId, setOpen }, freeform, defaultProps } = options;\n const onBlur = (ev)=>{\n // handle selection and updating value if freeform is false\n if (!open && !freeform) {\n // select matching option, if the value fully matches\n if (value && activeOption && value.trim().toLowerCase() === (activeOption === null || activeOption === void 0 ? void 0 : activeOption.text.toLowerCase())) {\n selectOption(ev, activeOption);\n }\n // reset typed value when the input loses focus while collapsed, unless freeform is true\n setValue(undefined);\n }\n };\n const getOptionFromInput = (inputValue)=>{\n const searchString = inputValue === null || inputValue === void 0 ? void 0 : inputValue.trim().toLowerCase();\n if (!searchString || searchString.length === 0) {\n return;\n }\n const matcher = (optionText)=>optionText.toLowerCase().indexOf(searchString) === 0;\n const matches = getOptionsMatchingText(matcher);\n // return first matching option after the current active option, looping back to the top\n if (matches.length > 1 && activeOption) {\n const startIndex = getIndexOfId(activeOption.id);\n const nextMatch = matches.find((option)=>getIndexOfId(option.id) >= startIndex);\n return nextMatch !== null && nextMatch !== void 0 ? nextMatch : matches[0];\n }\n var _matches_;\n return (_matches_ = matches[0]) !== null && _matches_ !== void 0 ? _matches_ : undefined;\n };\n // update value and active option based on input\n const onChange = (ev)=>{\n const inputValue = ev.target.value;\n // update uncontrolled value\n setValue(inputValue);\n // handle updating active option based on input\n const matchingOption = getOptionFromInput(inputValue);\n setActiveOption(matchingOption);\n setFocusVisible(true);\n // clear selection for single-select if the input value no longer matches the selection\n if (!multiselect && selectedOptions.length === 1 && (inputValue.length < 1 || !matchingOption)) {\n clearSelection(ev);\n }\n };\n const trigger = useTriggerSlot(triggerFromProps, ref, {\n state: options.state,\n defaultProps,\n elementType: 'input'\n });\n trigger.onChange = mergeCallbacks(trigger.onChange, onChange);\n trigger.onBlur = mergeCallbacks(trigger.onBlur, onBlur);\n // NVDA and JAWS have bugs that suppress reading the input value text when aria-activedescendant is set\n // To prevent this, we clear the HTML attribute (but save the state) when a user presses left/right arrows\n // ref: https://github.com/microsoft/fluentui/issues/26359#issuecomment-1397759888\n const [hideActiveDescendant, setHideActiveDescendant] = React.useState(false);\n // save the typing vs. navigating options state, as the space key should behave differently in each case\n // we do not want to update the combobox when this changes, just save the value between renders\n const isTyping = React.useRef(false);\n /**\n * Freeform combobox should not select\n */ const defaultOnKeyDown = trigger.onKeyDown;\n const onKeyDown = useEventCallback((ev)=>{\n if (!open && getDropdownActionFromKey(ev) === 'Type') {\n setOpen(ev, true);\n }\n // clear activedescendant when moving the text insertion cursor\n if (ev.key === ArrowLeft || ev.key === ArrowRight) {\n setHideActiveDescendant(true);\n } else {\n setHideActiveDescendant(false);\n }\n // update typing state to true if the user is typing\n const action = getDropdownActionFromKey(ev, {\n open,\n multiselect\n });\n if (action === 'Type') {\n isTyping.current = true;\n } else if (action === 'Open' && ev.key !== ' ' || action === 'Next' || action === 'Previous' || action === 'First' || action === 'Last' || action === 'PageUp' || action === 'PageDown') {\n isTyping.current = false;\n }\n // allow space to insert a character if freeform & the last action was typing, or if the popup is closed\n if (freeform && (isTyping.current || !open) && ev.key === ' ') {\n var _triggerFromProps_onKeyDown;\n triggerFromProps === null || triggerFromProps === void 0 ? void 0 : (_triggerFromProps_onKeyDown = triggerFromProps.onKeyDown) === null || _triggerFromProps_onKeyDown === void 0 ? void 0 : _triggerFromProps_onKeyDown.call(triggerFromProps, ev);\n return;\n }\n defaultOnKeyDown === null || defaultOnKeyDown === void 0 ? void 0 : defaultOnKeyDown(ev);\n });\n trigger.onKeyDown = onKeyDown;\n if (hideActiveDescendant) {\n trigger['aria-activedescendant'] = undefined;\n }\n return trigger;\n}\n"],"names":["useInputTriggerSlot","triggerFromProps","ref","options","state","open","value","activeOption","selectOption","setValue","setActiveOption","setFocusVisible","multiselect","selectedOptions","clearSelection","getOptionsMatchingText","getIndexOfId","setOpen","freeform","defaultProps","onBlur","ev","trim","toLowerCase","text","undefined","getOptionFromInput","inputValue","searchString","length","matcher","optionText","indexOf","matches","startIndex","id","nextMatch","find","option","_matches_","onChange","target","matchingOption","trigger","useTriggerSlot","elementType","mergeCallbacks","hideActiveDescendant","setHideActiveDescendant","React","useState","isTyping","useRef","defaultOnKeyDown","onKeyDown","useEventCallback","getDropdownActionFromKey","key","ArrowLeft","ArrowRight","action","current","_triggerFromProps_onKeyDown","call"],"mappings":";;;;+BASoBA;;;eAAAA;;;;iEATG;gCAC0B;8BACX;gCACP;oCACU;AAK9B,SAASA,oBAAoBC,gBAAgB,EAAEC,GAAG,EAAEC,OAAO;IAClE,MAAM,EAAEC,OAAO,EAAEC,IAAI,EAAEC,KAAK,EAAEC,YAAY,EAAEC,YAAY,EAAEC,QAAQ,EAAEC,eAAe,EAAEC,eAAe,EAAEC,WA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WAAW,WAAWA,WAAW,UAAUA,WAAW,YAAYA,WAAW,YAAY;YACrLT,SAASU,OAAO,GAAG;QACvB;QACA,wGAAwG;QACxG,IAAI3C,YAAaiC,CAAAA,SAASU,OAAO,IAAI,CAACxD,IAAG,KAAMgB,GAAGoC,GAAG,KAAK,KAAK;YAC3D,IAAIK;YACJ7D,qBAAqB,QAAQA,qBAAqB,KAAK,IAAI,KAAK,IAAI,AAAC6D,CAAAA,8BAA8B7D,iBAAiBqD,SAAS,AAAD,MAAO,QAAQQ,gCAAgC,KAAK,IAAI,KAAK,IAAIA,4BAA4BC,IAAI,CAAC9D,kBAAkBoB;YAChP;QACJ;QACAgC,qBAAqB,QAAQA,qBAAqB,KAAK,IAAI,KAAK,IAAIA,iBAAiBhC;IACzF;IACAsB,QAAQW,SAAS,GAAGA;IACpB,IAAIP,sBAAsB;QACtBJ,OAAO,CAAC,wBAAwB,GAAGlB;IACvC;IACA,OAAOkB;AACX"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "useButtonTriggerSlot", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return useButtonTriggerSlot;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
|
12
|
+
const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
|
|
13
|
+
const _reactutilities = require("@fluentui/react-utilities");
|
|
14
|
+
const _useTriggerSlot = require("../../utils/useTriggerSlot");
|
|
15
|
+
const _dropdownKeyActions = require("../../utils/dropdownKeyActions");
|
|
16
|
+
function useButtonTriggerSlot(triggerFromProps, ref, options) {
|
|
17
|
+
const { state: { open, activeOption, setOpen, getOptionsMatchingText, getIndexOfId, setActiveOption, setFocusVisible }, defaultProps } = options;
|
|
18
|
+
// jump to matching option based on typing
|
|
19
|
+
const searchString = _react.useRef('');
|
|
20
|
+
const [setKeyTimeout, clearKeyTimeout] = (0, _reactutilities.useTimeout)();
|
|
21
|
+
const getNextMatchingOption = ()=>{
|
|
22
|
+
// first check for matches for the full searchString
|
|
23
|
+
let matcher = (optionText)=>optionText.toLowerCase().indexOf(searchString.current) === 0;
|
|
24
|
+
let matches = getOptionsMatchingText(matcher);
|
|
25
|
+
let startIndex = activeOption ? getIndexOfId(activeOption.id) : 0;
|
|
26
|
+
// if the dropdown is already open and the searchstring is a single character,
|
|
27
|
+
// then look after the current activeOption for letters
|
|
28
|
+
// this is so slowly typing the same letter will cycle through matches
|
|
29
|
+
if (open && searchString.current.length === 1) {
|
|
30
|
+
startIndex++;
|
|
31
|
+
}
|
|
32
|
+
// if there are no direct matches, check if the search is all the same letter, e.g. "aaa"
|
|
33
|
+
if (!matches.length) {
|
|
34
|
+
const letters = searchString.current.split('');
|
|
35
|
+
const allSameLetter = letters.length && letters.every((letter)=>letter === letters[0]);
|
|
36
|
+
// if the search is all the same letter, cycle through options starting with that letter
|
|
37
|
+
if (allSameLetter) {
|
|
38
|
+
startIndex++;
|
|
39
|
+
matcher = (optionText)=>optionText.toLowerCase().indexOf(letters[0]) === 0;
|
|
40
|
+
matches = getOptionsMatchingText(matcher);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// if there is an active option and multiple matches,
|
|
44
|
+
// return first matching option after the current active option, looping back to the top
|
|
45
|
+
if (matches.length > 1 && activeOption) {
|
|
46
|
+
const nextMatch = matches.find((option)=>getIndexOfId(option.id) >= startIndex);
|
|
47
|
+
return nextMatch !== null && nextMatch !== void 0 ? nextMatch : matches[0];
|
|
48
|
+
}
|
|
49
|
+
var _matches_;
|
|
50
|
+
return (_matches_ = matches[0]) !== null && _matches_ !== void 0 ? _matches_ : undefined;
|
|
51
|
+
};
|
|
52
|
+
const onTriggerKeyDown = (ev)=>{
|
|
53
|
+
// clear timeout, if it exists
|
|
54
|
+
clearKeyTimeout();
|
|
55
|
+
// if the key was a char key, update search string
|
|
56
|
+
if ((0, _dropdownKeyActions.getDropdownActionFromKey)(ev) === 'Type') {
|
|
57
|
+
// update search string
|
|
58
|
+
searchString.current += ev.key.toLowerCase();
|
|
59
|
+
setKeyTimeout(()=>{
|
|
60
|
+
searchString.current = '';
|
|
61
|
+
}, 500);
|
|
62
|
+
// update state
|
|
63
|
+
!open && setOpen(ev, true);
|
|
64
|
+
const nextOption = getNextMatchingOption();
|
|
65
|
+
setActiveOption(nextOption);
|
|
66
|
+
setFocusVisible(true);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const trigger = (0, _useTriggerSlot.useTriggerSlot)(triggerFromProps, ref, {
|
|
70
|
+
state: options.state,
|
|
71
|
+
defaultProps,
|
|
72
|
+
elementType: 'button'
|
|
73
|
+
});
|
|
74
|
+
trigger.onKeyDown = (0, _reactutilities.mergeCallbacks)(onTriggerKeyDown, trigger.onKeyDown);
|
|
75
|
+
return trigger;
|
|
76
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useButtonTriggerSlot.js"],"sourcesContent":["import * as React from 'react';\nimport { useTimeout, mergeCallbacks } from '@fluentui/react-utilities';\nimport { useTriggerSlot } from '../../utils/useTriggerSlot';\nimport { getDropdownActionFromKey } from '../../utils/dropdownKeyActions';\n/*\n * useButtonTriggerSlot returns a tuple of trigger/listbox shorthand,\n * with the semantics and event handlers needed for the Combobox and Dropdown components.\n * The element type of the ref should always match the element type used in the trigger shorthand.\n */ export function useButtonTriggerSlot(triggerFromProps, ref, options) {\n const { state: { open, activeOption, setOpen, getOptionsMatchingText, getIndexOfId, setActiveOption, setFocusVisible }, defaultProps } = options;\n // jump to matching option based on typing\n const searchString = React.useRef('');\n const [setKeyTimeout, clearKeyTimeout] = useTimeout();\n const getNextMatchingOption = ()=>{\n // first check for matches for the full searchString\n let matcher = (optionText)=>optionText.toLowerCase().indexOf(searchString.current) === 0;\n let matches = getOptionsMatchingText(matcher);\n let startIndex = activeOption ? getIndexOfId(activeOption.id) : 0;\n // if the dropdown is already open and the searchstring is a single character,\n // then look after the current activeOption for letters\n // this is so slowly typing the same letter will cycle through matches\n if (open && searchString.current.length === 1) {\n startIndex++;\n }\n // if there are no direct matches, check if the search is all the same letter, e.g. \"aaa\"\n if (!matches.length) {\n const letters = searchString.current.split('');\n const allSameLetter = letters.length && letters.every((letter)=>letter === letters[0]);\n // if the search is all the same letter, cycle through options starting with that letter\n if (allSameLetter) {\n startIndex++;\n matcher = (optionText)=>optionText.toLowerCase().indexOf(letters[0]) === 0;\n matches = getOptionsMatchingText(matcher);\n }\n }\n // if there is an active option and multiple matches,\n // return first matching option after the current active option, looping back to the top\n if (matches.length > 1 && activeOption) {\n const nextMatch = matches.find((option)=>getIndexOfId(option.id) >= startIndex);\n return nextMatch !== null && nextMatch !== void 0 ? nextMatch : matches[0];\n }\n var _matches_;\n return (_matches_ = matches[0]) !== null && _matches_ !== void 0 ? _matches_ : undefined;\n };\n const onTriggerKeyDown = (ev)=>{\n // clear timeout, if it exists\n clearKeyTimeout();\n // if the key was a char key, update search string\n if (getDropdownActionFromKey(ev) === 'Type') {\n // update search string\n searchString.current += ev.key.toLowerCase();\n setKeyTimeout(()=>{\n searchString.current = '';\n }, 500);\n // update state\n !open && setOpen(ev, true);\n const nextOption = getNextMatchingOption();\n setActiveOption(nextOption);\n setFocusVisible(true);\n }\n };\n const trigger = useTriggerSlot(triggerFromProps, ref, {\n state: options.state,\n defaultProps,\n elementType: 'button'\n });\n trigger.onKeyDown = mergeCallbacks(onTriggerKeyDown, trigger.onKeyDown);\n return trigger;\n}\n"],"names":["useButtonTriggerSlot","triggerFromProps","ref","options","state","open","activeOption","setOpen","getOptionsMatchingText","getIndexOfId","setActiveOption","setFocusVisible","defaultProps","searchString","React","useRef","setKeyTimeout","clearKeyTimeout","useTimeout","getNextMatchingOption","matcher","optionText","toLowerCase","indexOf","current","matches","startIndex","id","length","letters","split","allSameLetter","every","letter","nextMatch","find","option","_matches_","undefined","onTriggerKeyDown","ev","getDropdownActionFromKey","key","nextOption","trigger","useTriggerSlot","elementType","onKeyDown","mergeCallbacks"],"mappings":";;;;+BAQoBA;;;eAAAA;;;;iEARG;gCACoB;gCACZ;oCACU;AAK9B,SAASA,qBAAqBC,gBAAgB,EAAEC,GAAG,EAAEC,OAAO;IACnE,MAAM,EAAEC,OAAO,EAAEC,IAAI,EAAEC,YAAY,EAAEC,OAAO,EAAEC,sBAAsB,EAAEC,YAAY,EAAEC,eAAe,EAAEC,eAAe,EAAE,EAAEC,YAAY,EAAE,GAAGT;IACzI,0CAA0C;IAC1C,MAAMU,eAAeC,OAAMC,MAAM,CAAC;IAClC,MAAM,CAACC,eAAeC,gBAAgB,GAAGC,IAAAA,0BAAU;IACnD,MAAMC,wBAAwB;QAC1B,oDAAoD;QACpD,IAAIC,UAAU,CAACC,aAAaA,WAAWC,WAAW,GAAGC,OAAO,CAACV,aAAaW,OAAO,MAAM;QACvF,IAAIC,UAAUjB,uBAAuBY;QACrC,IAAIM,aAAapB,eAAeG,aAAaH,aAAaqB,EAAE,IAAI;QAChE,8EAA8E;QAC9E,uDAAuD;QACvD,sEAAsE;QACtE,IAAItB,QAAQQ,aAAaW,OAAO,CAACI,MAAM,KAAK,GAAG;YAC3CF;QACJ;QACA,yFAAyF;QACzF,IAAI,CAACD,QAAQG,MAAM,EAAE;YACjB,MAAMC,UAAUhB,aAAaW,OAAO,CAACM,KAAK,CAAC;YAC3C,MAAMC,gBAAgBF,QAAQD,MAAM,IAAIC,QAAQG,KAAK,CAAC,CAACC,SAASA,WAAWJ,OAAO,CAAC,EAAE;YACrF,wFAAwF;YACxF,IAAIE,eAAe;gBACfL;gBACAN,UAAU,CAACC,aAAaA,WAAWC,WAAW,GAAGC,OAAO,CAACM,OAAO,CAAC,EAAE,MAAM;gBACzEJ,UAAUjB,uBAAuBY;YACrC;QACJ;QACA,qDAAqD;QACrD,wFAAwF;QACxF,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