@fluentui/react-combobox 9.5.33 → 9.5.35

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.
Files changed (36) hide show
  1. package/CHANGELOG.md +34 -9
  2. package/dist/index.d.ts +11 -0
  3. package/lib/components/Combobox/useCombobox.js +22 -126
  4. package/lib/components/Combobox/useCombobox.js.map +1 -1
  5. package/lib/components/Combobox/useInputTriggerSlot.js +103 -0
  6. package/lib/components/Combobox/useInputTriggerSlot.js.map +1 -0
  7. package/lib/components/Dropdown/useButtonTriggerSlot.js +69 -0
  8. package/lib/components/Dropdown/useButtonTriggerSlot.js.map +1 -0
  9. package/lib/components/Dropdown/useDropdown.js +18 -76
  10. package/lib/components/Dropdown/useDropdown.js.map +1 -1
  11. package/lib/utils/ComboboxBase.types.js.map +1 -1
  12. package/lib/utils/useComboboxBaseState.js +2 -1
  13. package/lib/utils/useComboboxBaseState.js.map +1 -1
  14. package/lib/utils/useListboxSlot.js +37 -0
  15. package/lib/utils/useListboxSlot.js.map +1 -0
  16. package/lib/utils/useTriggerSlot.js +75 -0
  17. package/lib/utils/useTriggerSlot.js.map +1 -0
  18. package/lib-commonjs/components/Combobox/useCombobox.js +22 -126
  19. package/lib-commonjs/components/Combobox/useCombobox.js.map +1 -1
  20. package/lib-commonjs/components/Combobox/useInputTriggerSlot.js +110 -0
  21. package/lib-commonjs/components/Combobox/useInputTriggerSlot.js.map +1 -0
  22. package/lib-commonjs/components/Dropdown/useButtonTriggerSlot.js +76 -0
  23. package/lib-commonjs/components/Dropdown/useButtonTriggerSlot.js.map +1 -0
  24. package/lib-commonjs/components/Dropdown/useDropdown.js +17 -75
  25. package/lib-commonjs/components/Dropdown/useDropdown.js.map +1 -1
  26. package/lib-commonjs/utils/useComboboxBaseState.js +2 -1
  27. package/lib-commonjs/utils/useComboboxBaseState.js.map +1 -1
  28. package/lib-commonjs/utils/useListboxSlot.js +46 -0
  29. package/lib-commonjs/utils/useListboxSlot.js.map +1 -0
  30. package/lib-commonjs/utils/useTriggerSlot.js +83 -0
  31. package/lib-commonjs/utils/useTriggerSlot.js.map +1 -0
  32. package/package.json +8 -8
  33. package/lib/utils/useTriggerListboxSlots.js +0 -133
  34. package/lib/utils/useTriggerListboxSlots.js.map +0 -1
  35. package/lib-commonjs/utils/useTriggerListboxSlots.js +0 -140
  36. package/lib-commonjs/utils/useTriggerListboxSlots.js.map +0 -1
@@ -1,12 +1,12 @@
1
1
  import * as React from 'react';
2
2
  import { useFieldControlProps_unstable } from '@fluentui/react-field';
3
3
  import { ChevronDownRegular as ChevronDownIcon } from '@fluentui/react-icons';
4
- import { getPartitionedNativeProps, mergeCallbacks, useMergedRefs, useTimeout, slot } from '@fluentui/react-utilities';
5
- import { getDropdownActionFromKey } from '../../utils/dropdownKeyActions';
4
+ import { getPartitionedNativeProps, useMergedRefs, slot } from '@fluentui/react-utilities';
6
5
  import { useComboboxBaseState } from '../../utils/useComboboxBaseState';
7
6
  import { useComboboxPositioning } from '../../utils/useComboboxPositioning';
8
- import { useTriggerListboxSlots } from '../../utils/useTriggerListboxSlots';
9
7
  import { Listbox } from '../Listbox/Listbox';
8
+ import { useListboxSlot } from '../../utils/useListboxSlot';
9
+ import { useButtonTriggerSlot } from './useButtonTriggerSlot';
10
10
  /**
11
11
  * Create the state required to render Dropdown.
12
12
  *
@@ -22,7 +22,7 @@ import { Listbox } from '../Listbox/Listbox';
22
22
  supportsSize: true
23
23
  });
24
24
  const baseState = useComboboxBaseState(props);
25
- const { activeOption, getIndexOfId, getOptionsMatchingText, open, setActiveOption, setFocusVisible, setOpen } = baseState;
25
+ const { open } = baseState;
26
26
  const { primary: triggerNativeProps, root: rootNativeProps } = getPartitionedNativeProps({
27
27
  props,
28
28
  primarySlotTagName: 'button',
@@ -31,85 +31,27 @@ import { Listbox } from '../Listbox/Listbox';
31
31
  ]
32
32
  });
33
33
  const [comboboxPopupRef, comboboxTargetRef] = useComboboxPositioning(props);
34
- // jump to matching option based on typing
35
- const searchString = React.useRef('');
36
- const [setKeyTimeout, clearKeyTimeout] = useTimeout();
37
- const getNextMatchingOption = ()=>{
38
- // first check for matches for the full searchString
39
- let matcher = (optionText)=>optionText.toLowerCase().indexOf(searchString.current) === 0;
40
- let matches = getOptionsMatchingText(matcher);
41
- let startIndex = activeOption ? getIndexOfId(activeOption.id) : 0;
42
- // if the dropdown is already open and the searchstring is a single character,
43
- // then look after the current activeOption for letters
44
- // this is so slowly typing the same letter will cycle through matches
45
- if (open && searchString.current.length === 1) {
46
- startIndex++;
47
- }
48
- // if there are no direct matches, check if the search is all the same letter, e.g. "aaa"
49
- if (!matches.length) {
50
- const letters = searchString.current.split('');
51
- const allSameLetter = letters.length && letters.every((letter)=>letter === letters[0]);
52
- // if the search is all the same letter, cycle through options starting with that letter
53
- if (allSameLetter) {
54
- startIndex++;
55
- matcher = (optionText)=>optionText.toLowerCase().indexOf(letters[0]) === 0;
56
- matches = getOptionsMatchingText(matcher);
57
- }
58
- }
59
- // if there is an active option and multiple matches,
60
- // return first matching option after the current active option, looping back to the top
61
- if (matches.length > 1 && activeOption) {
62
- const nextMatch = matches.find((option)=>getIndexOfId(option.id) >= startIndex);
63
- return nextMatch !== null && nextMatch !== void 0 ? nextMatch : matches[0];
64
- }
65
- var _matches_;
66
- return (_matches_ = matches[0]) !== null && _matches_ !== void 0 ? _matches_ : undefined;
67
- };
68
- const onTriggerKeyDown = (ev)=>{
69
- // clear timeout, if it exists
70
- clearKeyTimeout();
71
- // if the key was a char key, update search string
72
- if (getDropdownActionFromKey(ev) === 'Type') {
73
- // update search string
74
- searchString.current += ev.key.toLowerCase();
75
- setKeyTimeout(()=>{
76
- searchString.current = '';
77
- }, 500);
78
- // update state
79
- !open && setOpen(ev, true);
80
- const nextOption = getNextMatchingOption();
81
- setActiveOption(nextOption);
82
- setFocusVisible(true);
34
+ const triggerRef = React.useRef(null);
35
+ const listbox = useListboxSlot(props.listbox, comboboxPopupRef, {
36
+ state: baseState,
37
+ triggerRef,
38
+ defaultProps: {
39
+ children: props.children
83
40
  }
84
- };
85
- // resolve button and listbox slot props
86
- let triggerSlot;
87
- let listboxSlot;
88
- triggerSlot = slot.always(props.button, {
41
+ });
42
+ var _props_button;
43
+ const trigger = useButtonTriggerSlot((_props_button = props.button) !== null && _props_button !== void 0 ? _props_button : {}, useMergedRefs(triggerRef, ref), {
44
+ state: baseState,
89
45
  defaultProps: {
90
46
  type: 'button',
91
47
  tabIndex: 0,
92
48
  children: baseState.value || props.placeholder,
93
49
  ...triggerNativeProps
94
- },
95
- elementType: 'button'
50
+ }
96
51
  });
97
- triggerSlot.onKeyDown = mergeCallbacks(onTriggerKeyDown, triggerSlot.onKeyDown);
98
- listboxSlot = baseState.open || baseState.hasFocus ? slot.optional(props.listbox, {
99
- renderByDefault: true,
100
- defaultProps: {
101
- children: props.children
102
- },
103
- elementType: Listbox
104
- }) : undefined;
105
- [triggerSlot, listboxSlot] = useTriggerListboxSlots(props, baseState, ref, triggerSlot, listboxSlot);
106
- const listboxRef = useMergedRefs(listboxSlot === null || listboxSlot === void 0 ? void 0 : listboxSlot.ref, comboboxPopupRef);
107
- if (listboxSlot) {
108
- listboxSlot.ref = listboxRef;
109
- }
110
52
  const rootSlot = slot.always(props.root, {
111
53
  defaultProps: {
112
- 'aria-owns': !props.inlinePopup ? listboxSlot === null || listboxSlot === void 0 ? void 0 : listboxSlot.id : undefined,
54
+ 'aria-owns': !props.inlinePopup && open ? listbox === null || listbox === void 0 ? void 0 : listbox.id : undefined,
113
55
  children: props.children,
114
56
  ...rootNativeProps
115
57
  },
@@ -124,8 +66,8 @@ import { Listbox } from '../Listbox/Listbox';
124
66
  listbox: Listbox
125
67
  },
126
68
  root: rootSlot,
127
- button: triggerSlot,
128
- listbox: listboxSlot,
69
+ button: trigger,
70
+ listbox: open ? listbox : undefined,
129
71
  expandIcon: slot.optional(props.expandIcon, {
130
72
  renderByDefault: true,
131
73
  defaultProps: {
@@ -1 +1 @@
1
- {"version":3,"sources":["useDropdown.tsx"],"sourcesContent":["import * as React from 'react';\nimport { useFieldControlProps_unstable } from '@fluentui/react-field';\nimport { ChevronDownRegular as ChevronDownIcon } from '@fluentui/react-icons';\nimport { getPartitionedNativeProps, mergeCallbacks, useMergedRefs, useTimeout, 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';\nimport type { Slot } from '@fluentui/react-utilities';\nimport type { OptionValue } from '../../utils/OptionCollection.types';\nimport type { DropdownProps, DropdownState } from './Dropdown.types';\n\n/**\n * Create the state required to render Dropdown.\n *\n * The returned state can be modified with hooks such as useDropdownStyles_unstable,\n * before being passed to renderDropdown_unstable.\n *\n * @param props - props from this instance of Dropdown\n * @param ref - reference to root HTMLElement of Dropdown\n */\nexport const useDropdown_unstable = (props: DropdownProps, ref: React.Ref<HTMLButtonElement>): DropdownState => {\n // Merge props from surrounding <Field>, if any\n props = useFieldControlProps_unstable(props, { supportsLabelFor: true, supportsSize: true });\n\n const baseState = useComboboxBaseState(props);\n const { activeOption, getIndexOfId, getOptionsMatchingText, open, setActiveOption, setFocusVisible, setOpen } =\n baseState;\n\n const { primary: triggerNativeProps, root: rootNativeProps } = getPartitionedNativeProps({\n props,\n primarySlotTagName: 'button',\n excludedPropNames: ['children'],\n });\n\n const [comboboxPopupRef, comboboxTargetRef] = useComboboxPositioning(props);\n\n // jump to matching option based on typing\n const searchString = React.useRef('');\n const [setKeyTimeout, clearKeyTimeout] = useTimeout();\n\n const getNextMatchingOption = (): OptionValue | undefined => {\n // first check for matches for the full searchString\n let matcher = (optionText: string) => optionText.toLowerCase().indexOf(searchString.current) === 0;\n let matches = getOptionsMatchingText(matcher);\n let startIndex = activeOption ? getIndexOfId(activeOption.id) : 0;\n\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\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\n // if the search is all the same letter, cycle through options starting with that letter\n if (allSameLetter) {\n startIndex++;\n matcher = (optionText: string) => optionText.toLowerCase().indexOf(letters[0]) === 0;\n matches = getOptionsMatchingText(matcher);\n }\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 ?? matches[0];\n }\n\n return matches[0] ?? undefined;\n };\n\n const onTriggerKeyDown = (ev: React.KeyboardEvent<HTMLButtonElement>) => {\n // clear timeout, if it exists\n clearKeyTimeout();\n\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\n // update state\n !open && setOpen(ev, true);\n\n const nextOption = getNextMatchingOption();\n setActiveOption(nextOption);\n setFocusVisible(true);\n }\n };\n\n // resolve button and listbox slot props\n let triggerSlot: Slot<'button'>;\n let listboxSlot: Slot<typeof Listbox> | undefined;\n\n triggerSlot = slot.always(props.button, {\n defaultProps: {\n type: 'button',\n tabIndex: 0, // Safari doesn't focus the button on click without this\n children: baseState.value || props.placeholder,\n ...triggerNativeProps,\n },\n elementType: 'button',\n });\n triggerSlot.onKeyDown = mergeCallbacks(onTriggerKeyDown, triggerSlot.onKeyDown);\n listboxSlot =\n baseState.open || baseState.hasFocus\n ? slot.optional(props.listbox, {\n renderByDefault: true,\n defaultProps: { children: props.children },\n elementType: Listbox,\n })\n : undefined;\n [triggerSlot, listboxSlot] = useTriggerListboxSlots(props, baseState, ref, triggerSlot, listboxSlot);\n const listboxRef = useMergedRefs(listboxSlot?.ref, comboboxPopupRef);\n if (listboxSlot) {\n listboxSlot.ref = listboxRef;\n }\n\n const rootSlot = slot.always(props.root, {\n defaultProps: {\n 'aria-owns': !props.inlinePopup ? listboxSlot?.id : undefined,\n children: props.children,\n ...rootNativeProps,\n },\n elementType: 'div',\n });\n rootSlot.ref = useMergedRefs(rootSlot.ref, comboboxTargetRef);\n\n const state: DropdownState = {\n components: { root: 'div', button: 'button', expandIcon: 'span', listbox: Listbox },\n root: rootSlot,\n button: triggerSlot,\n listbox: listboxSlot,\n expandIcon: slot.optional(props.expandIcon, {\n renderByDefault: true,\n defaultProps: {\n children: <ChevronDownIcon />,\n },\n elementType: 'span',\n }),\n placeholderVisible: !baseState.value && !!props.placeholder,\n ...baseState,\n };\n\n return state;\n};\n"],"names":["React","useFieldControlProps_unstable","ChevronDownRegular","ChevronDownIcon","getPartitionedNativeProps","mergeCallbacks","useMergedRefs","useTimeout","slot","getDropdownActionFromKey","useComboboxBaseState","useComboboxPositioning","useTriggerListboxSlots","Listbox","useDropdown_unstable","props","ref","supportsLabelFor","supportsSize","baseState","activeOption","getIndexOfId","getOptionsMatchingText","open","setActiveOption","setFocusVisible","setOpen","primary","triggerNativeProps","root","rootNativeProps","primarySlotTagName","excludedPropNames","comboboxPopupRef","comboboxTargetRef","searchString","useRef","setKeyTimeout","clearKeyTimeout","getNextMatchingOption","matcher","optionText","toLowerCase","indexOf","current","matches","startIndex","id","length","letters","split","allSameLetter","every","letter","nextMatch","find","option","undefined","onTriggerKeyDown","ev","key","nextOption","triggerSlot","listboxSlot","always","button","defaultProps","type","tabIndex","children","value","placeholder","elementType","onKeyDown","hasFocus","optional","listbox","renderByDefault","listboxRef","rootSlot","inlinePopup","state","components","expandIcon","placeholderVisible"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,6BAA6B,QAAQ,wBAAwB;AACtE,SAASC,sBAAsBC,eAAe,QAAQ,wBAAwB;AAC9E,SAASC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qD,kBAAkBI,YAAYW,SAAS;IAC9EV,cACE5C,UAAUI,IAAI,IAAIJ,UAAUuD,QAAQ,GAChClE,KAAKmE,QAAQ,CAAC5D,MAAM6D,OAAO,EAAE;QAC3BC,iBAAiB;QACjBX,cAAc;YAAEG,UAAUtD,MAAMsD,QAAQ;QAAC;QACzCG,aAAa3D;IACf,KACA4C;IACN,CAACK,aAAaC,YAAY,GAAGnD,uBAAuBG,OAAOI,WAAWH,KAAK8C,aAAaC;IACxF,MAAMe,aAAaxE,cAAcyD,wBAAAA,kCAAAA,YAAa/C,GAAG,EAAEiB;IACnD,IAAI8B,aAAa;QACfA,YAAY/C,GAAG,GAAG8D;IACpB;IAEA,MAAMC,WAAWvE,KAAKwD,MAAM,CAACjD,MAAMc,IAAI,EAAE;QACvCqC,cAAc;YACZ,aAAa,CAACnD,MAAMiE,WAAW,GAAGjB,wBAAAA,kCAAAA,YAAahB,EAAE,GAAGU;YACpDY,UAAUtD,MAAMsD,QAAQ;YACxB,GAAGvC,eAAe;QACpB;QACA0C,aAAa;IACf;IACAO,SAAS/D,GAAG,GAAGV,cAAcyE,SAAS/D,GAAG,EAAEkB;IAE3C,MAAM+C,QAAuB;QAC3BC,YAAY;YAAErD,MAAM;YAAOoC,QAAQ;YAAUkB,YAAY;YAAQP,SAAS/D;QAAQ;QAClFgB,MAAMkD;QACNd,QAAQH;QACRc,SAASb;QACToB,YAAY3E,KAAKmE,QAAQ,CAAC5D,MAAMoE,UAAU,EAAE;YAC1CN,iBAAiB;YACjBX,cAAc;gBACZG,wBAAU,oBAAClE;YACb;YACAqE,aAAa;QACf;QACAY,oBAAoB,CAACjE,UAAUmD,KAAK,IAAI,CAAC,CAACvD,MAAMwD,WAAW;QAC3D,GAAGpD,SAAS;IACd;IAEA,OAAO8D;AACT,EAAE"}
1
+ {"version":3,"sources":["useDropdown.tsx"],"sourcesContent":["import * as React from 'react';\nimport { useFieldControlProps_unstable } from '@fluentui/react-field';\nimport { ChevronDownRegular as ChevronDownIcon } from '@fluentui/react-icons';\nimport { getPartitionedNativeProps, useMergedRefs, slot } from '@fluentui/react-utilities';\nimport { useComboboxBaseState } from '../../utils/useComboboxBaseState';\nimport { useComboboxPositioning } from '../../utils/useComboboxPositioning';\nimport { Listbox } from '../Listbox/Listbox';\nimport type { DropdownProps, DropdownState } from './Dropdown.types';\nimport { useListboxSlot } from '../../utils/useListboxSlot';\nimport { useButtonTriggerSlot } from './useButtonTriggerSlot';\n\n/**\n * Create the state required to render Dropdown.\n *\n * The returned state can be modified with hooks such as useDropdownStyles_unstable,\n * before being passed to renderDropdown_unstable.\n *\n * @param props - props from this instance of Dropdown\n * @param ref - reference to root HTMLElement of Dropdown\n */\nexport const useDropdown_unstable = (props: DropdownProps, ref: React.Ref<HTMLButtonElement>): DropdownState => {\n // Merge props from surrounding <Field>, if any\n props = useFieldControlProps_unstable(props, { supportsLabelFor: true, supportsSize: true });\n\n const baseState = useComboboxBaseState(props);\n const { open } = baseState;\n\n const { primary: triggerNativeProps, root: rootNativeProps } = getPartitionedNativeProps({\n props,\n primarySlotTagName: 'button',\n excludedPropNames: ['children'],\n });\n\n const [comboboxPopupRef, comboboxTargetRef] = useComboboxPositioning(props);\n\n const triggerRef = React.useRef<HTMLButtonElement>(null);\n const listbox = useListboxSlot(props.listbox, comboboxPopupRef, {\n state: baseState,\n triggerRef,\n defaultProps: {\n children: props.children,\n },\n });\n\n const trigger = useButtonTriggerSlot(props.button ?? {}, useMergedRefs(triggerRef, ref), {\n state: baseState,\n defaultProps: {\n type: 'button',\n tabIndex: 0,\n children: baseState.value || props.placeholder,\n ...triggerNativeProps,\n },\n });\n\n const rootSlot = slot.always(props.root, {\n defaultProps: {\n 'aria-owns': !props.inlinePopup && open ? listbox?.id : undefined,\n children: props.children,\n ...rootNativeProps,\n },\n elementType: 'div',\n });\n rootSlot.ref = useMergedRefs(rootSlot.ref, comboboxTargetRef);\n\n const state: DropdownState = {\n components: { root: 'div', button: 'button', expandIcon: 'span', listbox: Listbox },\n root: rootSlot,\n button: trigger,\n listbox: open ? listbox : undefined,\n expandIcon: slot.optional(props.expandIcon, {\n renderByDefault: true,\n defaultProps: {\n children: <ChevronDownIcon />,\n },\n elementType: 'span',\n }),\n placeholderVisible: !baseState.value && !!props.placeholder,\n ...baseState,\n };\n\n return state;\n};\n"],"names":["React","useFieldControlProps_unstable","ChevronDownRegular","ChevronDownIcon","getPartitionedNativeProps","useMergedRefs","slot","useComboboxBaseState","useComboboxPositioning","Listbox","useListboxSlot","useButtonTriggerSlot","useDropdown_unstable","props","ref","supportsLabelFor","supportsSize","baseState","open","primary","triggerNativeProps","root","rootNativeProps","primarySlotTagName","excludedPropNames","comboboxPopupRef","comboboxTargetRef","triggerRef","useRef","listbox","state","defaultProps","children","trigger","button","type","tabIndex","value","placeholder","rootSlot","always","inlinePopup","id","undefined","elementType","components","expandIcon","optional","renderByDefault","placeholderVisible"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,6BAA6B,QAAQ,wBAAwB;AACtE,SAASC,sBAAsBC,eAAe,QAAQ,wBAAwB;AAC9E,SAASC,yBAAyB,EAAEC,aAAa,EAAEC,IAAI,QAAQ,4BAA4B;AAC3F,SAASC,oBAAoB,QAAQ,mCAAmC;AACxE,SAASC,sBAAsB,QAAQ,qCAAqC;AAC5E,SAASC,OAAO,QAAQ,qBAAqB;AAE7C,SAASC,cAAc,QAAQ,6BAA6B;AAC5D,SAASC,oBAAoB,QAAQ,yBAAyB;AAE9D;;;;;;;;CAQC,GACD,OAAO,MAAMC,uBAAuB,CAACC,OAAsBC;IACzD,+CAA+C;IAC/CD,QAAQZ,8BAA8BY,OAAO;QAAEE,kBAAkB;QAAMC,cAAc;IAAK;IAE1F,MAAMC,YAAYV,qBAAqBM;IACvC,MAAM,EAAEK,IAAI,EAAE,GAAGD;IAEjB,MAAM,EAAEE,SAASC,kBAAkB,EAAEC,MAAMC,eAAe,EAAE,GAAGlB,0BAA0B;QACvFS;QACAU,oBAAoB;QACp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AACT,EAAE"}
@@ -1 +1 @@
1
- {"version":3,"sources":["ComboboxBase.types.ts"],"sourcesContent":["import * as React from 'react';\nimport type { PositioningShorthand } from '@fluentui/react-positioning';\nimport type { ComboboxContextValue } from '../contexts/ComboboxContext';\nimport type { OptionValue, OptionCollectionState } from '../utils/OptionCollection.types';\nimport { SelectionProps, SelectionState } from '../utils/Selection.types';\nimport { PortalProps } from '@fluentui/react-portal';\n\n/**\n * ComboboxBase Props\n * Shared types between Combobox and Dropdown components\n */\nexport type ComboboxBaseProps = SelectionProps &\n Pick<PortalProps, 'mountNode'> & {\n /**\n * Controls the colors and borders of the combobox trigger.\n * @default 'outline'\n */\n appearance?: 'filled-darker' | 'filled-lighter' | 'outline' | 'underline';\n\n /**\n * The default open state when open is uncontrolled\n */\n defaultOpen?: boolean;\n\n /**\n * The default value displayed in the trigger input or button when the combobox's value is uncontrolled\n */\n defaultValue?: string;\n\n /**\n * Render the combobox's popup inline in the DOM.\n * This has accessibility benefits, particularly for touch screen readers.\n */\n inlinePopup?: boolean;\n\n /**\n * Callback when the open/closed state of the dropdown changes\n */\n onOpenChange?: (e: ComboboxBaseOpenEvents, data: ComboboxBaseOpenChangeData) => void;\n\n /**\n * Sets the open/closed state of the dropdown.\n * Use together with onOpenChange to fully control the dropdown's visibility\n */\n open?: boolean;\n\n /**\n * If set, the placeholder will show when no value is selected\n */\n placeholder?: string;\n\n /**\n * Configure the positioning of the combobox dropdown\n *\n * @defaultvalue below\n */\n positioning?: PositioningShorthand;\n\n /**\n * Controls the size of the combobox faceplate\n * @default 'medium'\n */\n size?: 'small' | 'medium' | 'large';\n\n /**\n * The value displayed by the Combobox.\n * Use this with `onOptionSelect` to directly control the displayed value string\n */\n value?: string;\n };\n\n/**\n * State used in rendering Combobox\n */\nexport type ComboboxBaseState = Required<Pick<ComboboxBaseProps, 'appearance' | 'open' | 'inlinePopup' | 'size'>> &\n Pick<ComboboxBaseProps, 'mountNode' | 'placeholder' | 'value' | 'multiselect'> &\n OptionCollectionState &\n SelectionState & {\n /* Option data for the currently highlighted option (not the selected option) */\n activeOption?: OptionValue;\n\n // Whether the keyboard focus outline style should be visible\n focusVisible: boolean;\n\n // whether the combobox/dropdown currently has focus\n hasFocus: boolean;\n\n /* Whether the next blur event should be ignored, and the combobox/dropdown will not close.*/\n ignoreNextBlur: React.MutableRefObject<boolean>;\n\n setActiveOption: React.Dispatch<React.SetStateAction<OptionValue | undefined>>;\n\n setFocusVisible(focusVisible: boolean): void;\n\n setHasFocus(hasFocus: boolean): void;\n\n setOpen(event: ComboboxBaseOpenEvents, newState: boolean): void;\n\n setValue(newValue: string | undefined): void;\n };\n\n/**\n * Data for the Combobox onOpenChange event.\n */\nexport type ComboboxBaseOpenChangeData = {\n open: boolean;\n};\n\n/* Possible event types for onOpen */\nexport type ComboboxBaseOpenEvents =\n | React.MouseEvent<HTMLElement>\n | React.KeyboardEvent<HTMLElement>\n | React.FocusEvent<HTMLElement>;\n\nexport type ComboboxBaseContextValues = {\n combobox: ComboboxContextValue;\n};\n"],"names":["React"],"mappings":"AAAA,YAAYA,WAAW,QAAQ"}
1
+ {"version":3,"sources":["ComboboxBase.types.ts"],"sourcesContent":["import * as React from 'react';\nimport type { PositioningShorthand } from '@fluentui/react-positioning';\nimport type { ComboboxContextValue } from '../contexts/ComboboxContext';\nimport type { OptionValue, OptionCollectionState } from '../utils/OptionCollection.types';\nimport { SelectionProps, SelectionState } from '../utils/Selection.types';\nimport { PortalProps } from '@fluentui/react-portal';\n\n/**\n * ComboboxBase Props\n * Shared types between Combobox and Dropdown components\n */\nexport type ComboboxBaseProps = SelectionProps &\n Pick<PortalProps, 'mountNode'> & {\n /**\n * Controls the colors and borders of the combobox trigger.\n * @default 'outline'\n */\n appearance?: 'filled-darker' | 'filled-lighter' | 'outline' | 'underline';\n\n /**\n * The default open state when open is uncontrolled\n */\n defaultOpen?: boolean;\n\n /**\n * The default value displayed in the trigger input or button when the combobox's value is uncontrolled\n */\n defaultValue?: string;\n\n /**\n * Render the combobox's popup inline in the DOM.\n * This has accessibility benefits, particularly for touch screen readers.\n */\n inlinePopup?: boolean;\n\n /**\n * Callback when the open/closed state of the dropdown changes\n */\n onOpenChange?: (e: ComboboxBaseOpenEvents, data: ComboboxBaseOpenChangeData) => void;\n\n /**\n * Sets the open/closed state of the dropdown.\n * Use together with onOpenChange to fully control the dropdown's visibility\n */\n open?: boolean;\n\n /**\n * If set, the placeholder will show when no value is selected\n */\n placeholder?: string;\n\n /**\n * Configure the positioning of the combobox dropdown\n *\n * @defaultvalue below\n */\n positioning?: PositioningShorthand;\n\n /**\n * Controls the size of the combobox faceplate\n * @default 'medium'\n */\n size?: 'small' | 'medium' | 'large';\n\n /**\n * The value displayed by the Combobox.\n * Use this with `onOptionSelect` to directly control the displayed value string\n */\n value?: string;\n };\n\n/**\n * State used in rendering Combobox\n */\nexport type ComboboxBaseState = Required<Pick<ComboboxBaseProps, 'appearance' | 'open' | 'inlinePopup' | 'size'>> &\n Pick<ComboboxBaseProps, 'mountNode' | 'placeholder' | 'value' | 'multiselect'> &\n OptionCollectionState &\n SelectionState & {\n /* Option data for the currently highlighted option (not the selected option) */\n activeOption?: OptionValue;\n\n // Whether the keyboard focus outline style should be visible\n focusVisible: boolean;\n\n /**\n * @deprecated - no longer used internally\n * whether the combobox/dropdown currently has focus\n */\n hasFocus: boolean;\n\n /**\n * @deprecated - no longer used internally\n * Whether the next blur event should be ignored, and the combobox/dropdown will not close.\n */\n ignoreNextBlur: React.MutableRefObject<boolean>;\n\n setActiveOption: React.Dispatch<React.SetStateAction<OptionValue | undefined>>;\n\n setFocusVisible(focusVisible: boolean): void;\n\n /**\n * @deprecated - no longer used internally\n */\n setHasFocus(hasFocus: boolean): void;\n\n setOpen(event: ComboboxBaseOpenEvents, newState: boolean): void;\n\n setValue(newValue: string | undefined): void;\n };\n\n/**\n * Data for the Combobox onOpenChange event.\n */\nexport type ComboboxBaseOpenChangeData = {\n open: boolean;\n};\n\n/* Possible event types for onOpen */\nexport type ComboboxBaseOpenEvents =\n | React.MouseEvent<HTMLElement>\n | React.KeyboardEvent<HTMLElement>\n | React.FocusEvent<HTMLElement>;\n\nexport type ComboboxBaseContextValues = {\n combobox: ComboboxContextValue;\n};\n"],"names":["React"],"mappings":"AAAA,YAAYA,WAAW,QAAQ"}
@@ -104,6 +104,7 @@ import { useSelection } from '../utils/useSelection';
104
104
  setOpen,
105
105
  setValue,
106
106
  size,
107
- value
107
+ value,
108
+ multiselect
108
109
  };
109
110
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["useComboboxBaseState.ts"],"sourcesContent":["import * as React from 'react';\nimport { useControllableState, useFirstMount } from '@fluentui/react-utilities';\nimport { useOptionCollection } from '../utils/useOptionCollection';\nimport { OptionValue } from '../utils/OptionCollection.types';\nimport { useSelection } from '../utils/useSelection';\nimport type { ComboboxBaseProps, ComboboxBaseOpenEvents, ComboboxBaseState } from './ComboboxBase.types';\n\n/**\n * State shared between Combobox and Dropdown components\n */\nexport const useComboboxBaseState = (\n props: ComboboxBaseProps & { children?: React.ReactNode; editable?: boolean },\n): ComboboxBaseState => {\n const {\n appearance = 'outline',\n children,\n editable = false,\n inlinePopup = false,\n mountNode = undefined,\n multiselect,\n onOpenChange,\n size = 'medium',\n } = props;\n\n const optionCollection = useOptionCollection();\n const { getOptionAtIndex, getOptionsMatchingValue } = optionCollection;\n\n const [activeOption, setActiveOption] = React.useState<OptionValue | undefined>();\n\n // track whether keyboard focus outline should be shown\n // tabster/keyborg doesn't work here, since the actual keyboard focus target doesn't move\n const [focusVisible, setFocusVisible] = React.useState(false);\n\n // track focused state to conditionally render collapsed listbox\n const [hasFocus, setHasFocus] = React.useState(false);\n\n const ignoreNextBlur = React.useRef(false);\n\n const selectionState = useSelection(props);\n const { selectedOptions } = selectionState;\n\n // calculate value based on props, internal value changes, and selected options\n const isFirstMount = useFirstMount();\n const [controllableValue, setValue] = useControllableState({\n state: props.value,\n initialState: undefined,\n });\n\n const value = React.useMemo(() => {\n // don't compute the value if it is defined through props or setValue,\n if (controllableValue !== undefined) {\n return controllableValue;\n }\n\n // handle defaultValue here, so it is overridden by selection\n if (isFirstMount && props.defaultValue !== undefined) {\n return props.defaultValue;\n }\n\n const selectedOptionsText = getOptionsMatchingValue(optionValue => {\n return selectedOptions.includes(optionValue);\n }).map(option => option.text);\n\n if (multiselect) {\n // editable inputs should not display multiple selected options in the input as text\n return editable ? '' : selectedOptionsText.join(', ');\n }\n\n return selectedOptionsText[0];\n\n // do not change value after isFirstMount changes,\n // we do not want to accidentally override defaultValue on a second render\n // unless another value is intentionally set\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [controllableValue, editable, getOptionsMatchingValue, multiselect, props.defaultValue, selectedOptions]);\n\n // Handle open state, which is shared with options in context\n const [open, setOpenState] = useControllableState({\n state: props.open,\n defaultState: props.defaultOpen,\n initialState: false,\n });\n\n const setOpen = React.useCallback(\n (event: ComboboxBaseOpenEvents, newState: boolean) => {\n onOpenChange?.(event, { open: newState });\n setOpenState(newState);\n },\n [onOpenChange, setOpenState],\n );\n\n // update active option based on change in open state or children\n React.useEffect(() => {\n if (open && !activeOption) {\n // if it is single-select and there is a selected option, start at the selected option\n if (!multiselect && selectedOptions.length > 0) {\n const selectedOption = getOptionsMatchingValue(v => v === selectedOptions[0]).pop();\n selectedOption && setActiveOption(selectedOption);\n }\n // default to starting at the first option\n else {\n setActiveOption(getOptionAtIndex(0));\n }\n } else if (!open) {\n // reset the active option when closing\n setActiveOption(undefined);\n }\n // this should only be run in response to changes in the open state or children\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [open, children]);\n\n return {\n ...optionCollection,\n ...selectionState,\n activeOption,\n appearance,\n focusVisible,\n hasFocus,\n ignoreNextBlur,\n inlinePopup,\n mountNode,\n open,\n setActiveOption,\n setFocusVisible,\n setHasFocus,\n setOpen,\n setValue,\n size,\n value,\n };\n};\n"],"names":["React","useControllableState","useFirstMount","useOptionCollection","useSelection","useComboboxBaseState","props","appearance","children","editable","inlinePopup","mountNode","undefined","multiselect","onOpenChange","size","optionCollection","getOptionAtIndex","getOptionsMatchingValue","activeOption","setActiveOption","useState","focusVisible","setFocusVisible","hasFocus","setHasFocus","ignoreNextBlur","useRef","selectionState","selectedOptions","isFirstMount","controllableValue","setValue","state","value","initialState","useMemo","defaultValue","selectedOptionsText","optionValue","includes","map","option","text","join","open","setOpenState","defaultState","defaultOpen","setOpen","useCallback","event","newState","useEffect","length","selectedOption","v","pop"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,oBAAoB,EAAEC,aAAa,QAAQ,4BAA4B;AAChF,SAASC,mBAAmB,QAAQ,+BAA+B;AAEnE,SAASC,YAAY,QAAQ,wBAAwB;AAGrD;;CAEC,GACD,OAAO,MAAMC,uBAAuB,CAClCC;IAEA,MAAM,EACJC,aAAa,SAAS,EACtBC,QAAQ,EACRC,WAAW,KAAK,EAChBC,cAAc,KAAK,EACnBC,YAAYC,SAAS,EACrBC,WAAW,EACXC,YAAY,EACZC,OAAO,QAAQ,EAChB,GAAGT;IAEJ,MAAMU,mBAAmBb;IACzB,MAAM,EAAEc,gBAAgB,EAAEC,uBAAuB,EAAE,GAAGF;IAEtD,MAAM,CAACG,cAAcC,gBAAgB,GAAGpB,MAAMqB,QAAQ;IAEtD,uDAAuD;IACvD,yFAAyF;IACzF,MAAM,CAACC,cAAcC,gBAAgB,GAAGvB,MAAMqB,QAAQ,CAAC;IAEvD,gEAAgE;IAChE,MAAM,CAACG,UAAUC,YAAY,GAAGzB,MAAMqB,QAAQ,CAAC;IAE/C,MAAMK,iBAAiB1B,MAAM2B,MAAM,CAAC;IAEpC,MAAMC,iBAAiBxB,aAAaE;IACpC,MAAM,EAAEuB,eAAe,EAAE,GAAGD;IAE5B,+EAA+E;IAC/E,MAAME,eAAe5B;IACrB,MAAM,CAAC6B,mBAAmBC,SAAS,GAAG/B,qBAAqB;QACzDgC,OAAO3B,MAAM4B,KAAK;QAClBC,cAAcvB;IAChB;IAEA,MAAMsB,Q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AAC4B,MAAM;YAChB,uCAAuC;YACvCzB,gBAAgBR;QAClB;IACA,+EAA+E;IAC/E,uDAAuD;IACzD,GAAG;QAACiC;QAAMrC;KAAS;IAEnB,OAAO;QACL,GAAGQ,gBAAgB;QACnB,GAAGY,cAAc;QACjBT;QACAZ;QACAe;QACAE;QACAE;QACAhB;QACAC;QACAkC;QACAzB;QACAG;QACAE;QACAwB;QACAjB;QACAjB;QACAmB;IACF;AACF,EAAE"}
1
+ {"version":3,"sources":["useComboboxBaseState.ts"],"sourcesContent":["import * as React from 'react';\nimport { useControllableState, useFirstMount } from '@fluentui/react-utilities';\nimport { useOptionCollection } from '../utils/useOptionCollection';\nimport { OptionValue } from '../utils/OptionCollection.types';\nimport { useSelection } from '../utils/useSelection';\nimport type { ComboboxBaseProps, ComboboxBaseOpenEvents, ComboboxBaseState } from './ComboboxBase.types';\n\n/**\n * State shared between Combobox and Dropdown components\n */\nexport const useComboboxBaseState = (\n props: ComboboxBaseProps & { children?: React.ReactNode; editable?: boolean },\n): ComboboxBaseState => {\n const {\n appearance = 'outline',\n children,\n editable = false,\n inlinePopup = false,\n mountNode = undefined,\n multiselect,\n onOpenChange,\n size = 'medium',\n } = props;\n\n const optionCollection = useOptionCollection();\n const { getOptionAtIndex, getOptionsMatchingValue } = optionCollection;\n\n const [activeOption, setActiveOption] = React.useState<OptionValue | undefined>();\n\n // track whether keyboard focus outline should be shown\n // tabster/keyborg doesn't work here, since the actual keyboard focus target doesn't move\n const [focusVisible, setFocusVisible] = React.useState(false);\n\n // track focused state to conditionally render collapsed listbox\n const [hasFocus, setHasFocus] = React.useState(false);\n\n const ignoreNextBlur = React.useRef(false);\n\n const selectionState = useSelection(props);\n const { selectedOptions } = selectionState;\n\n // calculate value based on props, internal value changes, and selected options\n const isFirstMount = useFirstMount();\n const [controllableValue, setValue] = useControllableState({\n state: props.value,\n initialState: undefined,\n });\n\n const value = React.useMemo(() => {\n // don't compute the value if it is defined through props or setValue,\n if (controllableValue !== undefined) {\n return controllableValue;\n }\n\n // handle defaultValue here, so it is overridden by selection\n if (isFirstMount && props.defaultValue !== undefined) {\n return props.defaultValue;\n }\n\n const selectedOptionsText = getOptionsMatchingValue(optionValue => {\n return selectedOptions.includes(optionValue);\n }).map(option => option.text);\n\n if (multiselect) {\n // editable inputs should not display multiple selected options in the input as text\n return editable ? '' : selectedOptionsText.join(', ');\n }\n\n return selectedOptionsText[0];\n\n // do not change value after isFirstMount changes,\n // we do not want to accidentally override defaultValue on a second render\n // unless another value is intentionally set\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [controllableValue, editable, getOptionsMatchingValue, multiselect, props.defaultValue, selectedOptions]);\n\n // Handle open state, which is shared with options in context\n const [open, setOpenState] = useControllableState({\n state: props.open,\n defaultState: props.defaultOpen,\n initialState: false,\n });\n\n const setOpen = React.useCallback(\n (event: ComboboxBaseOpenEvents, newState: boolean) => {\n onOpenChange?.(event, { open: newState });\n setOpenState(newState);\n },\n [onOpenChange, setOpenState],\n );\n\n // update active option based on change in open state or children\n React.useEffect(() => {\n if (open && !activeOption) {\n // if it is single-select and there is a selected option, start at the selected option\n if (!multiselect && selectedOptions.length > 0) {\n const selectedOption = getOptionsMatchingValue(v => v === selectedOptions[0]).pop();\n selectedOption && setActiveOption(selectedOption);\n }\n // default to starting at the first option\n else {\n setActiveOption(getOptionAtIndex(0));\n }\n } else if (!open) {\n // reset the active option when closing\n setActiveOption(undefined);\n }\n // this should only be run in response to changes in the open state or children\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [open, children]);\n\n return {\n ...optionCollection,\n ...selectionState,\n activeOption,\n appearance,\n focusVisible,\n hasFocus,\n ignoreNextBlur,\n inlinePopup,\n mountNode,\n open,\n setActiveOption,\n setFocusVisible,\n setHasFocus,\n setOpen,\n setValue,\n size,\n value,\n multiselect,\n };\n};\n"],"names":["React","useControllableState","useFirstMount","useOptionCollection","useSelection","useComboboxBaseState","props","appearance","children","editable","inlinePopup","mountNode","undefined","multiselect","onOpenChange","size","optionCollection","getOptionAtIndex","getOptionsMatchingValue","activeOption","setActiveOption","useState","focusVisible","setFocusVisible","hasFocus","setHasFocus","ignoreNextBlur","useRef","selectionState","selectedOptions","isFirstMount","controllableValue","setValue","state","value","initialState","useMemo","defaultValue","selectedOptionsText","optionValue","includes","map","option","text","join","open","setOpenState","defaultState","defaultOpen","setOpen","useCallback","event","newState","useEffect","length","selectedOption","v","pop"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,oBAAoB,EAAEC,aAAa,QAAQ,4BAA4B;AAChF,SAASC,mBAAmB,QAAQ,+BAA+B;AAEnE,SAASC,YAAY,QAAQ,wBAAwB;AAGrD;;CAEC,GACD,OAAO,MAAMC,uBAAuB,CAClCC;IAEA,MAAM,EACJC,aAAa,SAAS,EACtBC,QAAQ,EACRC,WAAW,KAAK,EAChBC,cAAc,KAAK,EACnBC,YAAYC,SAAS,EACrBC,WAAW,EACXC,YAAY,EACZC,OAAO,QAAQ,EAChB,GAAGT;IAEJ,MAAMU,mBAAmBb;IACzB,MAAM,EAAEc,gBAAgB,EAAEC,uBAAuB,EAAE,GAAGF;IAEtD,MAAM,CAACG,cAAcC,gBAAgB,GAAGpB,MAAMqB,QAAQ;IAEtD,uDAAuD;IACvD,yFAAyF;IACzF,MAAM,CAACC,cAAcC,gBAAgB,GAAGvB,MAAMqB,QAAQ,CAAC;IAEvD,gEAAgE;IAChE,MAAM,CAACG,UAAUC,YAAY,GAAGzB,MAAMqB,QAAQ,CAAC;IAE/C,MAAMK,iBAAiB1B,MAAM2B,MAAM,CAAC;IAEpC,MAAMC,iBAAiBxB,aAAaE;IACpC,MAAM,EAAEuB,eAAe,EAAE,GAAGD;IAE5B,+EAA+E;IAC/E,MAAME,eAAe5B;IACrB,MAAM,CAAC6B,mBAAmBC,SAAS,GAAG/B,qBAAqB;QACzDgC,OAAO3B,MAAM4B,KAAK;QAClBC,cAAcvB;IAChB;IAEA,MAAMsB,QAAQlC,MAAMoC,OAAO,C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AuC;YACvCzB,gBAAgBR;QAClB;IACA,+EAA+E;IAC/E,uDAAuD;IACzD,GAAG;QAACiC;QAAMrC;KAAS;IAEnB,OAAO;QACL,GAAGQ,gBAAgB;QACnB,GAAGY,cAAc;QACjBT;QACAZ;QACAe;QACAE;QACAE;QACAhB;QACAC;QACAkC;QACAzB;QACAG;QACAE;QACAwB;QACAjB;QACAjB;QACAmB;QACArB;IACF;AACF,EAAE"}
@@ -0,0 +1,37 @@
1
+ import * as React from 'react';
2
+ import { mergeCallbacks, useId, useEventCallback, slot, isResolvedShorthand, useMergedRefs } from '@fluentui/react-utilities';
3
+ import { Listbox } from '../Listbox';
4
+ /**
5
+ * @returns listbox slot with desired behaviour and props
6
+ */ export function useListboxSlot(listboxSlotFromProp, ref, options) {
7
+ const { state: { multiselect }, triggerRef, defaultProps } = options;
8
+ const listboxId = useId('fluent-listbox', isResolvedShorthand(listboxSlotFromProp) ? listboxSlotFromProp.id : undefined);
9
+ const listboxSlot = slot.optional(listboxSlotFromProp, {
10
+ renderByDefault: true,
11
+ elementType: Listbox,
12
+ defaultProps: {
13
+ id: listboxId,
14
+ multiselect,
15
+ tabIndex: undefined,
16
+ ...defaultProps
17
+ }
18
+ });
19
+ /**
20
+ * Clicking on the listbox should never blur the trigger
21
+ * in a combobox
22
+ */ const onMouseDown = useEventCallback(mergeCallbacks((event)=>{
23
+ event.preventDefault();
24
+ }, listboxSlot === null || listboxSlot === void 0 ? void 0 : listboxSlot.onMouseDown));
25
+ const onClick = useEventCallback(mergeCallbacks((event)=>{
26
+ var _triggerRef_current;
27
+ event.preventDefault();
28
+ (_triggerRef_current = triggerRef.current) === null || _triggerRef_current === void 0 ? void 0 : _triggerRef_current.focus();
29
+ }, listboxSlot === null || listboxSlot === void 0 ? void 0 : listboxSlot.onClick));
30
+ const listboxRef = useMergedRefs(listboxSlot === null || listboxSlot === void 0 ? void 0 : listboxSlot.ref, ref);
31
+ if (listboxSlot) {
32
+ listboxSlot.ref = listboxRef;
33
+ listboxSlot.onMouseDown = onMouseDown;
34
+ listboxSlot.onClick = onClick;
35
+ }
36
+ return listboxSlot;
37
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["useListboxSlot.ts"],"sourcesContent":["import * as React from 'react';\nimport {\n mergeCallbacks,\n useId,\n useEventCallback,\n slot,\n isResolvedShorthand,\n useMergedRefs,\n} from '@fluentui/react-utilities';\nimport type { ExtractSlotProps, Slot, SlotComponentType } from '@fluentui/react-utilities';\nimport type { ComboboxBaseState } from './ComboboxBase.types';\nimport { Listbox } from '../Listbox';\nimport { ListboxProps } from '../Listbox';\n\nexport type UseTriggerSlotState = Pick<ComboboxBaseState, 'multiselect'>;\n\ntype UseListboxSlotOptions = {\n state: ComboboxBaseState;\n triggerRef: React.RefObject<HTMLInputElement> | React.RefObject<HTMLButtonElement>;\n defaultProps?: Partial<ListboxProps>;\n};\n\n/**\n * @returns listbox slot with desired behaviour and props\n */\nexport function useListboxSlot(\n listboxSlotFromProp: Slot<typeof Listbox> | undefined,\n ref: React.Ref<HTMLDivElement>,\n options: UseListboxSlotOptions,\n): SlotComponentType<ExtractSlotProps<Slot<typeof Listbox>>> | undefined {\n const {\n state: { multiselect },\n triggerRef,\n defaultProps,\n } = options;\n\n const listboxId = useId(\n 'fluent-listbox',\n isResolvedShorthand(listboxSlotFromProp) ? listboxSlotFromProp.id : undefined,\n );\n\n const listboxSlot = slot.optional(listboxSlotFromProp, {\n renderByDefault: true,\n elementType: Listbox,\n defaultProps: {\n id: listboxId,\n multiselect,\n tabIndex: undefined,\n ...defaultProps,\n },\n });\n\n /**\n * Clicking on the listbox should never blur the trigger\n * in a combobox\n */\n const onMouseDown = useEventCallback(\n mergeCallbacks((event: React.MouseEvent<HTMLDivElement>) => {\n event.preventDefault();\n }, listboxSlot?.onMouseDown),\n );\n\n const onClick = useEventCallback(\n mergeCallbacks((event: React.MouseEvent<HTMLDivElement>) => {\n event.preventDefault();\n triggerRef.current?.focus();\n }, listboxSlot?.onClick),\n );\n\n const listboxRef = useMergedRefs(listboxSlot?.ref, ref);\n if (listboxSlot) {\n listboxSlot.ref = listboxRef;\n listboxSlot.onMouseDown = onMouseDown;\n listboxSlot.onClick = onClick;\n }\n\n return listboxSlot;\n}\n"],"names":["React","mergeCallbacks","useId","useEventCallback","slot","isResolvedShorthand","useMergedRefs","Listbox","useListboxSlot","listboxSlotFromProp","ref","options","state","multiselect","triggerRef","defaultProps","listboxId","id","undefined","listboxSlot","optional","renderByDefault","elementType","tabIndex","onMouseDown","event","preventDefault","onClick","current","focus","listboxRef"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SACEC,cAAc,EACdC,KAAK,EACLC,gBAAgB,EAChBC,IAAI,EACJC,mBAAmB,EACnBC,aAAa,QACR,4BAA4B;AAGnC,SAASC,OAAO,QAAQ,aAAa;AAWrC;;CAEC,GACD,OAAO,SAASC,eACdC,mBAAqD,EACrDC,GAA8B,EAC9BC,OAA8B;IAE9B,MAAM,EACJC,OAAO,EAAEC,WAAW,EAAE,EACtBC,UAAU,EACVC,YAAY,EACb,GAAGJ;IAEJ,MAAMK,YAAYd,MAChB,kBACAG,oBAAoBI,uBAAuBA,oBAAoBQ,EAAE,GAAGC;IAGtE,MAAMC,cAAcf,KAAKgB,QAAQ,CAACX,qBAAqB;QACrDY,iBAAiB;QACjBC,aAAaf;QACbQ,cAAc;YACZE,IAAID;YACJH;YACAU,UAAUL;YACV,GAAGH,YAAY;QACjB;IACF;IAEA;;;GAGC,GACD,MAAMS,cAAcrB,iBAClBF,eAAe,CAACwB;QACdA,MAAMC,cAAc;IACtB,GAAGP,wBAAAA,kCAAAA,YAAaK,WAAW;IAG7B,MAAMG,UAAUxB,iBACdF,eAAe,CAACwB;YAEdX;QADAW,MAAMC,cAAc;SACpBZ,sBAAAA,WAAWc,OAAO,cAAlBd,0CAAAA,oBAAoBe,KAAK;IAC3B,GAAGV,wBAAAA,kCAAAA,YAAaQ,OAAO;IAGzB,MAAMG,aAAaxB,cAAca,wBAAAA,kCAAAA,YAAaT,GAAG,EAAEA;IACnD,IAAIS,aAAa;QACfA,YAAYT,GAAG,GAAGoB;QAClBX,YAAYK,WAAW,GAAGA;QAC1BL,YAAYQ,OAAO,GAAGA;IACxB;IAEA,OAAOR;AACT"}
@@ -0,0 +1,75 @@
1
+ import * as React from 'react';
2
+ import { mergeCallbacks, slot, useMergedRefs } from '@fluentui/react-utilities';
3
+ import { getDropdownActionFromKey, getIndexFromAction } from '../utils/dropdownKeyActions';
4
+ /**
5
+ * Shared trigger behaviour for combobox and dropdown
6
+ * @returns trigger slot with desired behaviour and props
7
+ */ export function useTriggerSlot(triggerSlotFromProp, ref, options) {
8
+ const { state: { activeOption, getCount, getIndexOfId, getOptionAtIndex, open, selectOption, setActiveOption, setFocusVisible, setOpen, multiselect }, defaultProps, elementType } = options;
9
+ const trigger = slot.always(triggerSlotFromProp, {
10
+ defaultProps: {
11
+ type: 'text',
12
+ 'aria-expanded': open,
13
+ 'aria-activedescendant': open ? activeOption === null || activeOption === void 0 ? void 0 : activeOption.id : undefined,
14
+ role: 'combobox',
15
+ ...typeof defaultProps === 'object' && defaultProps
16
+ },
17
+ elementType
18
+ });
19
+ // handle trigger focus/blur
20
+ const triggerRef = React.useRef(null);
21
+ trigger.ref = useMergedRefs(triggerRef, trigger.ref, ref);
22
+ // the trigger should open/close the popup on click or blur
23
+ trigger.onBlur = mergeCallbacks((event)=>{
24
+ setOpen(event, false);
25
+ }, trigger.onBlur);
26
+ trigger.onClick = mergeCallbacks((event)=>{
27
+ setOpen(event, !open);
28
+ }, trigger.onClick);
29
+ // handle combobox keyboard interaction
30
+ trigger.onKeyDown = mergeCallbacks((event)=>{
31
+ const action = getDropdownActionFromKey(event, {
32
+ open,
33
+ multiselect
34
+ });
35
+ const maxIndex = getCount() - 1;
36
+ const activeIndex = activeOption ? getIndexOfId(activeOption.id) : -1;
37
+ let newIndex = activeIndex;
38
+ switch(action){
39
+ case 'Open':
40
+ event.preventDefault();
41
+ setFocusVisible(true);
42
+ setOpen(event, true);
43
+ break;
44
+ case 'Close':
45
+ // stop propagation for escape key to avoid dismissing any parent popups
46
+ event.stopPropagation();
47
+ event.preventDefault();
48
+ setOpen(event, false);
49
+ break;
50
+ case 'CloseSelect':
51
+ !multiselect && !(activeOption === null || activeOption === void 0 ? void 0 : activeOption.disabled) && setOpen(event, false);
52
+ // fallthrough
53
+ case 'Select':
54
+ activeOption && selectOption(event, activeOption);
55
+ event.preventDefault();
56
+ break;
57
+ case 'Tab':
58
+ !multiselect && activeOption && selectOption(event, activeOption);
59
+ break;
60
+ default:
61
+ newIndex = getIndexFromAction(action, activeIndex, maxIndex);
62
+ }
63
+ if (newIndex !== activeIndex) {
64
+ // prevent default page scroll/keyboard action if the index changed
65
+ event.preventDefault();
66
+ setActiveOption(getOptionAtIndex(newIndex));
67
+ setFocusVisible(true);
68
+ }
69
+ }, trigger.onKeyDown);
70
+ trigger.onMouseOver = mergeCallbacks((event)=>{
71
+ setFocusVisible(false);
72
+ }, trigger.onMouseOver);
73
+ // TODO fix cast
74
+ return trigger;
75
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["useTriggerSlot.ts"],"sourcesContent":["import * as React from 'react';\nimport { mergeCallbacks, slot, useMergedRefs } from '@fluentui/react-utilities';\nimport type { ExtractSlotProps, Slot, SlotComponentType } from '@fluentui/react-utilities';\nimport { getDropdownActionFromKey, getIndexFromAction } from '../utils/dropdownKeyActions';\nimport type { ComboboxBaseState } from './ComboboxBase.types';\n\nexport type UseTriggerSlotState = Pick<\n ComboboxBaseState,\n | 'activeOption'\n | 'getCount'\n | 'getIndexOfId'\n | 'getOptionAtIndex'\n | 'open'\n | 'selectOption'\n | 'setActiveOption'\n | 'setFocusVisible'\n | 'setOpen'\n | 'multiselect'\n | 'value'\n>;\n\ntype UseTriggerSlotOptions = {\n state: UseTriggerSlotState;\n defaultProps: unknown;\n};\n\nexport function useTriggerSlot(\n triggerSlotFromProp: NonNullable<Slot<'button'>>,\n ref: React.Ref<HTMLButtonElement>,\n options: UseTriggerSlotOptions & { elementType: 'button' },\n): SlotComponentType<ExtractSlotProps<Slot<'button'>>>;\n\nexport function useTriggerSlot(\n triggerSlotFromProp: NonNullable<Slot<'input'>>,\n ref: React.Ref<HTMLInputElement>,\n options: UseTriggerSlotOptions & { elementType: 'input' },\n): SlotComponentType<ExtractSlotProps<Slot<'input'>>>;\n\n/**\n * Shared trigger behaviour for combobox and dropdown\n * @returns trigger slot with desired behaviour and props\n */\nexport function useTriggerSlot(\n triggerSlotFromProp: NonNullable<Slot<'input'>> | NonNullable<Slot<'button'>>,\n ref: React.Ref<HTMLButtonElement> | React.Ref<HTMLInputElement>,\n options: UseTriggerSlotOptions & { elementType: 'input' | 'button' },\n): SlotComponentType<ExtractSlotProps<Slot<'button'>>> | SlotComponentType<ExtractSlotProps<Slot<'input'>>> {\n const {\n state: {\n activeOption,\n getCount,\n getIndexOfId,\n getOptionAtIndex,\n open,\n selectOption,\n setActiveOption,\n setFocusVisible,\n setOpen,\n multiselect,\n },\n defaultProps,\n elementType,\n } = options;\n\n const trigger = slot.always(triggerSlotFromProp, {\n defaultProps: {\n type: 'text',\n 'aria-expanded': open,\n 'aria-activedescendant': open ? activeOption?.id : undefined,\n role: 'combobox',\n ...(typeof defaultProps === 'object' && defaultProps),\n },\n elementType,\n });\n\n // handle trigger focus/blur\n const triggerRef = React.useRef<HTMLButtonElement | HTMLInputElement>(null);\n trigger.ref = useMergedRefs(triggerRef, trigger.ref, ref) as React.Ref<HTMLButtonElement & HTMLInputElement>;\n\n // the trigger should open/close the popup on click or blur\n trigger.onBlur = mergeCallbacks((event: React.FocusEvent<HTMLButtonElement> & React.FocusEvent<HTMLInputElement>) => {\n setOpen(event, false);\n }, trigger.onBlur);\n\n trigger.onClick = mergeCallbacks(\n (event: React.MouseEvent<HTMLButtonElement> & React.MouseEvent<HTMLInputElement>) => {\n setOpen(event, !open);\n },\n trigger.onClick,\n );\n\n // handle combobox keyboard interaction\n trigger.onKeyDown = mergeCallbacks(\n (event: React.KeyboardEvent<HTMLButtonElement> & React.KeyboardEvent<HTMLInputElement>) => {\n const action = getDropdownActionFromKey(event, { open, multiselect });\n const maxIndex = getCount() - 1;\n const activeIndex = activeOption ? getIndexOfId(activeOption.id) : -1;\n let newIndex = activeIndex;\n\n switch (action) {\n case 'Open':\n event.preventDefault();\n setFocusVisible(true);\n setOpen(event, true);\n break;\n case 'Close':\n // stop propagation for escape key to avoid dismissing any parent popups\n event.stopPropagation();\n event.preventDefault();\n setOpen(event, false);\n break;\n case 'CloseSelect':\n !multiselect && !activeOption?.disabled && setOpen(event, false);\n // fallthrough\n case 'Select':\n activeOption && selectOption(event, activeOption);\n event.preventDefault();\n break;\n case 'Tab':\n !multiselect && activeOption && selectOption(event, activeOption);\n break;\n default:\n newIndex = getIndexFromAction(action, activeIndex, maxIndex);\n }\n if (newIndex !== activeIndex) {\n // prevent default page scroll/keyboard action if the index changed\n event.preventDefault();\n setActiveOption(getOptionAtIndex(newIndex));\n setFocusVisible(true);\n }\n },\n trigger.onKeyDown,\n );\n\n trigger.onMouseOver = mergeCallbacks(\n (event: React.MouseEvent<HTMLButtonElement> & React.MouseEvent<HTMLInputElement>) => {\n setFocusVisible(false);\n },\n trigger.onMouseOver,\n );\n\n // TODO fix cast\n return trigger as SlotComponentType<ExtractSlotProps<Slot<'input'>>>;\n}\n"],"names":["React","mergeCallbacks","slot","useMergedRefs","getDropdownActionFromKey","getIndexFromAction","useTriggerSlot","triggerSlotFromProp","ref","options","state","activeOption","getCount","getIndexOfId","getOptionAtIndex","open","selectOption","setActiveOption","setFocusVisible","setOpen","multiselect","defaultProps","elementType","trigger","always","type","id","undefined","role","triggerRef","useRef","onBlur","event","onClick","onKeyDown","action","maxIndex","activeIndex","newIndex","preventDefault","stopPropagation","disabled","onMouseOver"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,cAAc,EAAEC,IAAI,EAAEC,aAAa,QAAQ,4BAA4B;AAEhF,SAASC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AAe;gBACrBR,MAAMO,cAAc;gBACpBpB,QAAQa,OAAO;gBACf;YACF,KAAK;gBACH,CAACZ,eAAe,EAACT,yBAAAA,mCAAAA,aAAc8B,QAAQ,KAAItB,QAAQa,OAAO;YAC5D,cAAc;YACd,KAAK;gBACHrB,gBAAgBK,aAAagB,OAAOrB;gBACpCqB,MAAMO,cAAc;gBACpB;YACF,KAAK;gBACH,CAACnB,eAAeT,gBAAgBK,aAAagB,OAAOrB;gBACpD;YACF;gBACE2B,WAAWjC,mBAAmB8B,QAAQE,aAAaD;QACvD;QACA,IAAIE,aAAaD,aAAa;YAC5B,mEAAmE;YACnEL,MAAMO,cAAc;YACpBtB,gBAAgBH,iBAAiBwB;YACjCpB,gBAAgB;QAClB;IACF,GACAK,QAAQW,SAAS;IAGnBX,QAAQmB,WAAW,GAAGzC,eACpB,CAAC+B;QACCd,gBAAgB;IAClB,GACAK,QAAQmB,WAAW;IAGrB,gBAAgB;IAChB,OAAOnB;AACT"}
@@ -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 { activeOption, clearSelection, getIndexOfId, getOptionsMatchingText, hasFocus, open, selectOption, selectedOptions, setActiveOption, setFocusVisible, setOpen, setValue, value } = baseState;
32
+ const { open, selectOption, setOpen, setValue, value } = baseState;
35
33
  const [comboboxPopupRef, comboboxTargetRef] = (0, _useComboboxPositioning.useComboboxPositioning)(props);
36
- const { disabled, freeform, inlinePopup, multiselect } = props;
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
- const rootRef = _react.useRef(null);
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
- // update value and active option based on input
98
- const onTriggerChange = (ev)=>{
99
- const inputValue = ev.target.value;
100
- // update uncontrolled value
101
- baseState.setValue(inputValue);
102
- // handle updating active option based on input
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
- // resolve input and listbox slot props
112
- let triggerSlot;
113
- let listboxSlot;
114
- triggerSlot = _reactutilities.slot.always(props.input, {
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 ? listboxSlot === null || listboxSlot === void 0 ? void 0 : listboxSlot.id : undefined,
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: listboxSlot,
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
- state.root.ref = (0, _reactutilities.useMergedRefs)(state.root.ref, rootRef);
171
- /* Set input.onKeyDown here, so we can override the default behavior for spacebar */ const defaultOnTriggerKeyDown = state.input.onKeyDown;
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
- // open and set focus
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]"