@fluentui-copilot/react-prompt-listbox 0.8.1-hotfix.1 → 0.8.1-hotfix.2

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 (77) hide show
  1. package/CHANGELOG.json +4 -4
  2. package/CHANGELOG.md +5 -5
  3. package/lib/PromptListbox.js +1 -0
  4. package/lib/PromptOption.js +1 -0
  5. package/lib/components/PromptListbox/PromptListbox.js +7 -5
  6. package/lib/components/PromptListbox/PromptListbox.types.js +2 -1
  7. package/lib/components/PromptListbox/index.js +1 -0
  8. package/lib/components/PromptListbox/renderPromptListbox.js +21 -17
  9. package/lib/components/PromptListbox/usePromptListbox.js +67 -53
  10. package/lib/components/PromptListbox/usePromptListboxContextValues.js +30 -22
  11. package/lib/components/PromptListbox/usePromptListboxStyles.styles.raw.js +25 -21
  12. package/lib/components/PromptOption/PromptOption.js +6 -4
  13. package/lib/components/PromptOption/PromptOption.types.js +2 -1
  14. package/lib/components/PromptOption/index.js +1 -0
  15. package/lib/components/PromptOption/renderPromptOption.js +6 -4
  16. package/lib/components/PromptOption/usePromptOption.js +84 -84
  17. package/lib/components/PromptOption/usePromptOptionStyles.styles.raw.js +60 -54
  18. package/lib/components/motion/PromptListboxMotion.js +50 -52
  19. package/lib/components/utils/OptionCollection.types.js +2 -1
  20. package/lib/components/utils/PromptListboxFunctionality.types.js +2 -1
  21. package/lib/components/utils/Selection.types.js +2 -1
  22. package/lib/components/utils/dropdownKeyActions.js +60 -49
  23. package/lib/components/utils/useListboxPositioning.js +24 -22
  24. package/lib/components/utils/useOptionCollection.js +32 -30
  25. package/lib/components/utils/usePromptListboxFunctionality.js +123 -121
  26. package/lib/components/utils/usePromptListboxFunctionalityV2.js +124 -109
  27. package/lib/components/utils/useSelection.js +49 -57
  28. package/lib/components/utils/useTriggerKeyDown.js +133 -124
  29. package/lib/index.js +1 -0
  30. package/lib/plugins/CursorPositionPlugin.js +39 -39
  31. package/lib-commonjs/PromptListbox.js +1 -0
  32. package/lib-commonjs/PromptOption.js +1 -0
  33. package/lib-commonjs/components/PromptListbox/PromptListbox.js +1 -1
  34. package/lib-commonjs/components/PromptListbox/PromptListbox.js.map +1 -1
  35. package/lib-commonjs/components/PromptListbox/PromptListbox.types.js +1 -0
  36. package/lib-commonjs/components/PromptListbox/index.js +1 -0
  37. package/lib-commonjs/components/PromptListbox/renderPromptListbox.js +1 -1
  38. package/lib-commonjs/components/PromptListbox/renderPromptListbox.js.map +1 -1
  39. package/lib-commonjs/components/PromptListbox/usePromptListbox.js +1 -1
  40. package/lib-commonjs/components/PromptListbox/usePromptListbox.js.map +1 -1
  41. package/lib-commonjs/components/PromptListbox/usePromptListboxContextValues.js +1 -1
  42. package/lib-commonjs/components/PromptListbox/usePromptListboxContextValues.js.map +1 -1
  43. package/lib-commonjs/components/PromptListbox/usePromptListboxStyles.styles.raw.js +1 -1
  44. package/lib-commonjs/components/PromptListbox/usePromptListboxStyles.styles.raw.js.map +1 -1
  45. package/lib-commonjs/components/PromptOption/PromptOption.js +1 -1
  46. package/lib-commonjs/components/PromptOption/PromptOption.js.map +1 -1
  47. package/lib-commonjs/components/PromptOption/PromptOption.types.js +1 -0
  48. package/lib-commonjs/components/PromptOption/index.js +1 -0
  49. package/lib-commonjs/components/PromptOption/renderPromptOption.js +1 -1
  50. package/lib-commonjs/components/PromptOption/renderPromptOption.js.map +1 -1
  51. package/lib-commonjs/components/PromptOption/usePromptOption.js +1 -1
  52. package/lib-commonjs/components/PromptOption/usePromptOption.js.map +1 -1
  53. package/lib-commonjs/components/PromptOption/usePromptOptionStyles.styles.raw.js +1 -1
  54. package/lib-commonjs/components/PromptOption/usePromptOptionStyles.styles.raw.js.map +1 -1
  55. package/lib-commonjs/components/motion/PromptListboxMotion.js +1 -1
  56. package/lib-commonjs/components/motion/PromptListboxMotion.js.map +1 -1
  57. package/lib-commonjs/components/utils/OptionCollection.types.js +1 -0
  58. package/lib-commonjs/components/utils/PromptListboxFunctionality.types.js +1 -0
  59. package/lib-commonjs/components/utils/Selection.types.js +1 -0
  60. package/lib-commonjs/components/utils/dropdownKeyActions.js +1 -1
  61. package/lib-commonjs/components/utils/dropdownKeyActions.js.map +1 -1
  62. package/lib-commonjs/components/utils/useListboxPositioning.js +1 -1
  63. package/lib-commonjs/components/utils/useListboxPositioning.js.map +1 -1
  64. package/lib-commonjs/components/utils/useOptionCollection.js +1 -1
  65. package/lib-commonjs/components/utils/useOptionCollection.js.map +1 -1
  66. package/lib-commonjs/components/utils/usePromptListboxFunctionality.js +1 -1
  67. package/lib-commonjs/components/utils/usePromptListboxFunctionality.js.map +1 -1
  68. package/lib-commonjs/components/utils/usePromptListboxFunctionalityV2.js +1 -1
  69. package/lib-commonjs/components/utils/usePromptListboxFunctionalityV2.js.map +1 -1
  70. package/lib-commonjs/components/utils/useSelection.js +1 -1
  71. package/lib-commonjs/components/utils/useSelection.js.map +1 -1
  72. package/lib-commonjs/components/utils/useTriggerKeyDown.js +1 -1
  73. package/lib-commonjs/components/utils/useTriggerKeyDown.js.map +1 -1
  74. package/lib-commonjs/index.js +1 -0
  75. package/lib-commonjs/plugins/CursorPositionPlugin.js +1 -1
  76. package/lib-commonjs/plugins/CursorPositionPlugin.js.map +1 -1
  77. package/package.json +10 -10
@@ -11,131 +11,133 @@ import { promptOptionClassNames } from '../PromptOption';
11
11
  import { PromptListboxMotion } from '../motion/PromptListboxMotion';
12
12
  /**
13
13
  * @deprecated -- use usePromptListboxFunctionality from usePromptListboxFunctionalityV2.tsx instead
14
- */ export function usePromptListboxFunctionality(params) {
15
- const { positioning, onOpenChange, onSelectionModeChange, listboxProps, fluid = false, allowArrowUpNavigation = false } = params;
16
- const { listboxRef: activeDescendantListboxRef, activeParentRef, controller: activeDescendantController } = useActiveDescendant({
17
- matchOption: (el)=>el.classList.contains(promptOptionClassNames.root)
14
+ */
15
+ export function usePromptListboxFunctionality(params) {
16
+ const {
17
+ positioning,
18
+ onOpenChange,
19
+ onSelectionModeChange,
20
+ listboxProps,
21
+ fluid = false,
22
+ allowArrowUpNavigation = false
23
+ } = params;
24
+ const {
25
+ listboxRef: activeDescendantListboxRef,
26
+ activeParentRef,
27
+ controller: activeDescendantController
28
+ } = useActiveDescendant({
29
+ matchOption: el => el.classList.contains(promptOptionClassNames.root)
30
+ });
31
+ // useMergedRefs to normalize the ref into a React.RefObject type
32
+ const triggerRef = useMergedRefs(activeParentRef);
33
+ const selectionState = useSelection(listboxProps !== null && listboxProps !== void 0 ? listboxProps : {});
34
+ const {
35
+ selectOption
36
+ } = selectionState;
37
+ const optionCollection = useOptionCollection();
38
+ const {
39
+ getOptionById
40
+ } = optionCollection;
41
+ const [cursorPosition, setCursorPosition] = React.useState('end');
42
+ const [isInSelectionMode, setIsInSelectionMode] = React.useState(false);
43
+ const [open, setOpen] = useControllableState({
44
+ state: params.open,
45
+ defaultState: params.defaultOpen,
46
+ initialState: false
47
+ });
48
+ const setSelectionMode = React.useCallback(selectionMode => {
49
+ if (selectionMode === false) {
50
+ activeDescendantController.blur();
51
+ setHideActiveDescendant(true);
52
+ } else {
53
+ setHideActiveDescendant(false);
54
+ }
55
+ setIsInSelectionMode(selectionMode);
56
+ onSelectionModeChange === null || onSelectionModeChange === void 0 ? void 0 : onSelectionModeChange(selectionMode);
57
+ }, [activeDescendantController, onSelectionModeChange]);
58
+ const onBlur = event => {
59
+ setOpen(false);
60
+ onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(event, {
61
+ event,
62
+ type: 'focus',
63
+ open: false
18
64
  });
19
- // useMergedRefs to normalize the ref into a React.RefObject type
20
- const triggerRef = useMergedRefs(activeParentRef);
21
- const selectionState = useSelection(listboxProps !== null && listboxProps !== void 0 ? listboxProps : {});
22
- const { selectOption } = selectionState;
23
- const optionCollection = useOptionCollection();
24
- const { getOptionById } = optionCollection;
25
- const [cursorPosition, setCursorPosition] = React.useState('end');
26
- const [isInSelectionMode, setIsInSelectionMode] = React.useState(false);
27
- const [open, setOpen] = useControllableState({
28
- state: params.open,
29
- defaultState: params.defaultOpen,
30
- initialState: false
31
- });
32
- const setSelectionMode = React.useCallback((selectionMode)=>{
33
- if (selectionMode === false) {
34
- activeDescendantController.blur();
35
- setHideActiveDescendant(true);
36
- } else {
37
- setHideActiveDescendant(false);
38
- }
39
- setIsInSelectionMode(selectionMode);
40
- onSelectionModeChange === null || onSelectionModeChange === void 0 ? void 0 : onSelectionModeChange(selectionMode);
41
- }, [
42
- activeDescendantController,
43
- onSelectionModeChange
44
- ]);
45
- const onBlur = (event)=>{
46
- setOpen(false);
47
- onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(event, {
48
- event,
49
- type: 'focus',
50
- open: false
51
- });
52
- setSelectionMode(false);
53
- };
54
- const onFocus = (event)=>{
55
- if (event.target === event.currentTarget) {
56
- setOpen(true);
57
- onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(event, {
58
- event,
59
- type: 'focus',
60
- open: true
61
- });
62
- }
63
- };
64
- const cursorPositionPlugin = /*#__PURE__*/ React.createElement(CursorPositionPlugin, {
65
- setCursorPosition: setCursorPosition
66
- });
67
- const onListboxBlur = React.useCallback(()=>{
68
- setSelectionMode(false);
69
- onSelectionModeChange === null || onSelectionModeChange === void 0 ? void 0 : onSelectionModeChange(false);
70
- }, [
71
- onSelectionModeChange,
72
- setSelectionMode
73
- ]);
74
- // handle combobox keyboard interaction
75
- const onKeyDown = useTriggerKeydown({
76
- ...optionCollection,
77
- allowArrowUpNavigation,
78
- activeDescendantController,
79
- getOptionById,
80
- onBlur: onListboxBlur,
81
- selectOption,
82
- cursorPosition,
83
- open,
84
- multiselect: false,
85
- isInSelectionMode,
86
- setSelectionMode
87
- });
88
- // NVDA and JAWS have bugs that suppress reading the input value text when aria-activedescendant is set
89
- // To prevent this, we clear the HTML attribute (but save the state) when a user presses left/right arrows
90
- // ref: https://github.com/microsoft/fluentui/issues/26359#issuecomment-1397759888
91
- const [hideActiveDescendant, setHideActiveDescendant] = React.useState(false);
92
- React.useEffect(()=>{
93
- if (hideActiveDescendant) {
94
- var _triggerRef_current;
95
- (_triggerRef_current = triggerRef.current) === null || _triggerRef_current === void 0 ? void 0 : _triggerRef_current.removeAttribute('aria-activedescendant');
96
- }
65
+ setSelectionMode(false);
66
+ };
67
+ const onFocus = event => {
68
+ if (event.target === event.currentTarget) {
69
+ setOpen(true);
70
+ onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(event, {
71
+ event,
72
+ type: 'focus',
73
+ open: true
74
+ });
75
+ }
76
+ };
77
+ const cursorPositionPlugin = /*#__PURE__*/React.createElement(CursorPositionPlugin, {
78
+ setCursorPosition: setCursorPosition
79
+ });
80
+ const onListboxBlur = React.useCallback(() => {
81
+ setSelectionMode(false);
82
+ onSelectionModeChange === null || onSelectionModeChange === void 0 ? void 0 : onSelectionModeChange(false);
83
+ }, [onSelectionModeChange, setSelectionMode]);
84
+ // handle combobox keyboard interaction
85
+ const onKeyDown = useTriggerKeydown({
86
+ ...optionCollection,
87
+ allowArrowUpNavigation,
88
+ activeDescendantController,
89
+ getOptionById,
90
+ onBlur: onListboxBlur,
91
+ selectOption,
92
+ cursorPosition,
93
+ open,
94
+ multiselect: false,
95
+ isInSelectionMode,
96
+ setSelectionMode
97
+ });
98
+ // NVDA and JAWS have bugs that suppress reading the input value text when aria-activedescendant is set
99
+ // To prevent this, we clear the HTML attribute (but save the state) when a user presses left/right arrows
100
+ // ref: https://github.com/microsoft/fluentui/issues/26359#issuecomment-1397759888
101
+ const [hideActiveDescendant, setHideActiveDescendant] = React.useState(false);
102
+ React.useEffect(() => {
103
+ if (hideActiveDescendant) {
104
+ var _triggerRef_current;
105
+ (_triggerRef_current = triggerRef.current) === null || _triggerRef_current === void 0 ? void 0 : _triggerRef_current.removeAttribute('aria-activedescendant');
106
+ }
97
107
  // We only want to run this when the hideActiveDescendant changes, if the triggerRef
98
108
  // is undefined, there's no need to remove theAttribute and we shouldn't be adding
99
109
  // refs as dependencies since it can blow up the number of runs.
100
110
  // eslint-disable-next-line react-compiler/react-compiler
101
111
  // eslint-disable-next-line react-hooks/exhaustive-deps
102
- }, [
103
- hideActiveDescendant
104
- ]);
105
- const [comboboxPopupRef, comboboxTargetRef] = useListboxPositioning({
106
- positioning,
107
- fluid
108
- });
109
- const listboxMergedRef = useMergedRefs(comboboxPopupRef, activeDescendantListboxRef, listboxProps === null || listboxProps === void 0 ? void 0 : listboxProps.ref);
110
- const listbox = React.useMemo(()=>{
111
- return /*#__PURE__*/ React.createElement(PromptListboxMotion, {
112
- visible: open
113
- }, /*#__PURE__*/ React.createElement(PromptListbox, {
114
- open: open,
115
- ...listboxProps,
116
- ...optionCollection,
117
- ...selectionState,
118
- ref: listboxMergedRef,
119
- activeDescendantController: activeDescendantController
120
- }));
121
- }, [
122
- activeDescendantController,
123
- listboxMergedRef,
124
- listboxProps,
125
- open,
126
- optionCollection,
127
- selectionState
128
- ]);
129
- return {
130
- promptListbox: listbox,
131
- triggerProps: {
132
- ref: triggerRef,
133
- onBlur,
134
- onFocus,
135
- onKeyDown,
136
- isInSelectionMode
137
- },
138
- containerRef: comboboxTargetRef,
139
- cursorPositionPlugin
140
- };
112
+ }, [hideActiveDescendant]);
113
+ const [comboboxPopupRef, comboboxTargetRef] = useListboxPositioning({
114
+ positioning,
115
+ fluid
116
+ });
117
+ const listboxMergedRef = useMergedRefs(comboboxPopupRef, activeDescendantListboxRef, listboxProps === null || listboxProps === void 0 ? void 0 : listboxProps.ref);
118
+ const listbox = React.useMemo(() => {
119
+ return /*#__PURE__*/React.createElement(PromptListboxMotion, {
120
+ visible: open
121
+ }, /*#__PURE__*/React.createElement(PromptListbox, {
122
+ open: open,
123
+ ...listboxProps,
124
+ ...optionCollection,
125
+ ...selectionState,
126
+ ref: listboxMergedRef,
127
+ activeDescendantController: activeDescendantController
128
+ }));
129
+ }, [activeDescendantController, listboxMergedRef, listboxProps, open, optionCollection, selectionState]);
130
+ return {
131
+ promptListbox: listbox,
132
+ triggerProps: {
133
+ ref: triggerRef,
134
+ onBlur,
135
+ onFocus,
136
+ onKeyDown,
137
+ isInSelectionMode
138
+ },
139
+ containerRef: comboboxTargetRef,
140
+ cursorPositionPlugin
141
+ };
141
142
  }
143
+ //# sourceMappingURL=usePromptListboxFunctionality.js.map
@@ -6,119 +6,134 @@ import { CursorPositionPlugin } from '../../plugins/CursorPositionPlugin';
6
6
  import { useListboxPositioning } from './useListboxPositioning';
7
7
  import { Portal } from '@fluentui/react-components';
8
8
  export function usePromptListboxFunctionality(params) {
9
- var _params_listboxProps, _params_listboxProps1;
10
- const { fluid = false, allowArrowUpNavigation = false, onSelectionModeChange } = params;
11
- const [open, setOpen] = useControllableState({
12
- state: params.open,
13
- defaultState: params.defaultOpen,
14
- initialState: false
9
+ var _params_listboxProps, _params_listboxProps1;
10
+ const {
11
+ fluid = false,
12
+ allowArrowUpNavigation = false,
13
+ onSelectionModeChange
14
+ } = params;
15
+ const [open, setOpen] = useControllableState({
16
+ state: params.open,
17
+ defaultState: params.defaultOpen,
18
+ initialState: false
19
+ });
20
+ const [cursorPosition, setCursorPosition] = React.useState('end');
21
+ const overrideActionHandler = (e, {
22
+ activeOption,
23
+ action,
24
+ activeDescendantController,
25
+ isInSelectionMode
26
+ }) => {
27
+ var _params_overrideActionHandler;
28
+ const atStart = allowArrowUpNavigation && (cursorPosition === 'start' || cursorPosition === 'start-end');
29
+ const atEnd = cursorPosition === 'end' || cursorPosition === 'start-end';
30
+ let newAction = undefined;
31
+ let newIsInSelection = undefined;
32
+ const firstOption = activeDescendantController.first({
33
+ passive: true
15
34
  });
16
- const [cursorPosition, setCursorPosition] = React.useState('end');
17
- const overrideActionHandler = (e, { activeOption, action, activeDescendantController, isInSelectionMode })=>{
18
- var _params_overrideActionHandler;
19
- const atStart = allowArrowUpNavigation && (cursorPosition === 'start' || cursorPosition === 'start-end');
20
- const atEnd = cursorPosition === 'end' || cursorPosition === 'start-end';
21
- let newAction = undefined;
22
- let newIsInSelection = undefined;
23
- const firstOption = activeDescendantController.first({
24
- passive: true
25
- });
26
- switch(action){
27
- case 'Next':
28
- if (!isInSelectionMode && !atEnd) {
29
- newAction = 'Type';
30
- newIsInSelection = false;
31
- }
32
- break;
33
- case 'Previous':
34
- if (!isInSelectionMode && !atStart) {
35
- newAction = 'Type';
36
- newIsInSelection = false;
37
- } else if (isInSelectionMode && !allowArrowUpNavigation && atEnd && activeOption !== undefined && activeOption.id === firstOption) {
38
- newAction = 'Type';
39
- newIsInSelection = false;
40
- e.preventDefault();
41
- }
42
- break;
43
- case 'First':
44
- case 'Last':
45
- case 'PageDown':
46
- case 'PageUp':
47
- if (!isInSelectionMode) {
48
- newAction = 'Type';
49
- newIsInSelection = false;
50
- }
51
- break;
35
+ switch (action) {
36
+ case 'Next':
37
+ if (!isInSelectionMode && !atEnd) {
38
+ newAction = 'Type';
39
+ newIsInSelection = false;
52
40
  }
53
- const userOverrides = (_params_overrideActionHandler = params.overrideActionHandler) === null || _params_overrideActionHandler === void 0 ? void 0 : _params_overrideActionHandler.call(params, e, {
54
- activeOption,
55
- action,
56
- activeDescendantController,
57
- isInSelectionMode
58
- });
59
- return {
60
- action: newAction,
61
- isInSelectionMode: newIsInSelection,
62
- ...userOverrides
63
- };
64
- };
65
- const baseListboxFunctionality = useInputListboxFunctionality({
66
- ...params,
67
- onSelectionModeChange,
68
- overrideActionHandler
69
- });
70
- const { onBlur: baseOnBlur, onKeyDown: baseOnKeyDown, ref: triggerRef, isInSelectionMode } = baseListboxFunctionality.triggerProps;
71
- const onFocus = (event)=>{
72
- if (event.target === event.currentTarget) {
73
- var _params_onOpenChange;
74
- setOpen(true);
75
- (_params_onOpenChange = params.onOpenChange) === null || _params_onOpenChange === void 0 ? void 0 : _params_onOpenChange.call(params, event, {
76
- event,
77
- type: 'focus',
78
- open: true
79
- });
41
+ break;
42
+ case 'Previous':
43
+ if (!isInSelectionMode && !atStart) {
44
+ newAction = 'Type';
45
+ newIsInSelection = false;
46
+ } else if (isInSelectionMode && !allowArrowUpNavigation && atEnd && activeOption !== undefined && activeOption.id === firstOption) {
47
+ newAction = 'Type';
48
+ newIsInSelection = false;
49
+ e.preventDefault();
80
50
  }
81
- };
82
- const onBlur = useEventCallback(mergeCallbacks(baseOnBlur, (e)=>{
83
- var _params_onOpenChange;
84
- setOpen(false);
85
- (_params_onOpenChange = params.onOpenChange) === null || _params_onOpenChange === void 0 ? void 0 : _params_onOpenChange.call(params, e, {
86
- event: e,
87
- type: 'focus',
88
- open: false
89
- });
90
- }));
91
- const [listboxRef, anchorRef] = useListboxPositioning({
92
- fluid,
93
- positioning: params.positioning
94
- });
95
- const listboxId = useId('prompt-listbox', (_params_listboxProps = params.listboxProps) === null || _params_listboxProps === void 0 ? void 0 : _params_listboxProps.id);
96
- const cursorPositionPlugin = /*#__PURE__*/ React.createElement(CursorPositionPlugin, {
97
- setCursorPosition: setCursorPosition
98
- });
99
- const innerListbox = /*#__PURE__*/ React.createElement(InputListbox, {
100
- ...params.listboxProps,
101
- ...baseListboxFunctionality.inputListboxProps,
102
- id: listboxId,
103
- ref: useMergedRefs(baseListboxFunctionality.inputListboxProps.ref, listboxRef, (_params_listboxProps1 = params.listboxProps) === null || _params_listboxProps1 === void 0 ? void 0 : _params_listboxProps1.ref)
51
+ break;
52
+ case 'First':
53
+ case 'Last':
54
+ case 'PageDown':
55
+ case 'PageUp':
56
+ if (!isInSelectionMode) {
57
+ newAction = 'Type';
58
+ newIsInSelection = false;
59
+ }
60
+ break;
61
+ }
62
+ const userOverrides = (_params_overrideActionHandler = params.overrideActionHandler) === null || _params_overrideActionHandler === void 0 ? void 0 : _params_overrideActionHandler.call(params, e, {
63
+ activeOption,
64
+ action,
65
+ activeDescendantController,
66
+ isInSelectionMode
104
67
  });
105
- const listbox = /*#__PURE__*/ React.createElement(PromptListboxMotion, {
106
- visible: open
107
- }, /*#__PURE__*/ React.createElement("span", {
108
- "aria-owns": open ? listboxId : undefined
109
- }, open && (params.inline ? innerListbox : /*#__PURE__*/ React.createElement(Portal, {
110
- mountNode: params.mountNode
111
- }, innerListbox))));
112
68
  return {
113
- promptListbox: listbox,
114
- cursorPositionPlugin,
115
- triggerProps: {
116
- ref: triggerRef,
117
- isInSelectionMode,
118
- onFocus,
119
- onKeyDown: baseOnKeyDown,
120
- onBlur
121
- },
122
- containerRef: anchorRef
69
+ action: newAction,
70
+ isInSelectionMode: newIsInSelection,
71
+ ...userOverrides
123
72
  };
73
+ };
74
+ const baseListboxFunctionality = useInputListboxFunctionality({
75
+ ...params,
76
+ onSelectionModeChange,
77
+ overrideActionHandler
78
+ });
79
+ const {
80
+ onBlur: baseOnBlur,
81
+ onKeyDown: baseOnKeyDown,
82
+ ref: triggerRef,
83
+ isInSelectionMode
84
+ } = baseListboxFunctionality.triggerProps;
85
+ const onFocus = event => {
86
+ if (event.target === event.currentTarget) {
87
+ var _params_onOpenChange;
88
+ setOpen(true);
89
+ (_params_onOpenChange = params.onOpenChange) === null || _params_onOpenChange === void 0 ? void 0 : _params_onOpenChange.call(params, event, {
90
+ event,
91
+ type: 'focus',
92
+ open: true
93
+ });
94
+ }
95
+ };
96
+ const onBlur = useEventCallback(mergeCallbacks(baseOnBlur, e => {
97
+ var _params_onOpenChange;
98
+ setOpen(false);
99
+ (_params_onOpenChange = params.onOpenChange) === null || _params_onOpenChange === void 0 ? void 0 : _params_onOpenChange.call(params, e, {
100
+ event: e,
101
+ type: 'focus',
102
+ open: false
103
+ });
104
+ }));
105
+ const [listboxRef, anchorRef] = useListboxPositioning({
106
+ fluid,
107
+ positioning: params.positioning
108
+ });
109
+ const listboxId = useId('prompt-listbox', (_params_listboxProps = params.listboxProps) === null || _params_listboxProps === void 0 ? void 0 : _params_listboxProps.id);
110
+ const cursorPositionPlugin = /*#__PURE__*/React.createElement(CursorPositionPlugin, {
111
+ setCursorPosition: setCursorPosition
112
+ });
113
+ const innerListbox = /*#__PURE__*/React.createElement(InputListbox, {
114
+ ...params.listboxProps,
115
+ ...baseListboxFunctionality.inputListboxProps,
116
+ id: listboxId,
117
+ ref: useMergedRefs(baseListboxFunctionality.inputListboxProps.ref, listboxRef, (_params_listboxProps1 = params.listboxProps) === null || _params_listboxProps1 === void 0 ? void 0 : _params_listboxProps1.ref)
118
+ });
119
+ const listbox = /*#__PURE__*/React.createElement(PromptListboxMotion, {
120
+ visible: open
121
+ }, /*#__PURE__*/React.createElement("span", {
122
+ "aria-owns": open ? listboxId : undefined
123
+ }, open && (params.inline ? innerListbox : /*#__PURE__*/React.createElement(Portal, {
124
+ mountNode: params.mountNode
125
+ }, innerListbox))));
126
+ return {
127
+ promptListbox: listbox,
128
+ cursorPositionPlugin,
129
+ triggerProps: {
130
+ ref: triggerRef,
131
+ isInSelectionMode,
132
+ onFocus,
133
+ onKeyDown: baseOnKeyDown,
134
+ onBlur
135
+ },
136
+ containerRef: anchorRef
137
+ };
124
138
  }
139
+ //# sourceMappingURL=usePromptListboxFunctionalityV2.js.map
@@ -1,62 +1,54 @@
1
1
  // Brought from Fluent UI
2
2
  import { useCallback } from 'react';
3
3
  import { useControllableState } from '@fluentui/react-utilities';
4
- export const useSelection = (props)=>{
5
- const { defaultSelectedOptions, multiselect, onOptionSelect } = props;
6
- const [selectedOptions, setSelectedOptions] = useControllableState({
7
- state: props.selectedOptions,
8
- defaultState: defaultSelectedOptions,
9
- initialState: []
4
+ export const useSelection = props => {
5
+ const {
6
+ defaultSelectedOptions,
7
+ multiselect,
8
+ onOptionSelect
9
+ } = props;
10
+ const [selectedOptions, setSelectedOptions] = useControllableState({
11
+ state: props.selectedOptions,
12
+ defaultState: defaultSelectedOptions,
13
+ initialState: []
14
+ });
15
+ const selectOption = useCallback((event, option) => {
16
+ // if the option is disabled, do nothing
17
+ if (option.disabled) {
18
+ return;
19
+ }
20
+ // for single-select, always return the selected option
21
+ let newSelection = [option.value];
22
+ // toggle selected state of the option for multiselect
23
+ if (multiselect) {
24
+ const selectedIndex = selectedOptions.findIndex(o => o === option.value);
25
+ if (selectedIndex > -1) {
26
+ // deselect option
27
+ newSelection = [...selectedOptions.slice(0, selectedIndex), ...selectedOptions.slice(selectedIndex + 1)];
28
+ } else {
29
+ // select option
30
+ newSelection = [...selectedOptions, option.value];
31
+ }
32
+ }
33
+ setSelectedOptions(newSelection);
34
+ onOptionSelect === null || onOptionSelect === void 0 ? void 0 : onOptionSelect(event, {
35
+ optionValue: option.value,
36
+ optionText: option.text,
37
+ selectedOptions: newSelection
10
38
  });
11
- const selectOption = useCallback((event, option)=>{
12
- // if the option is disabled, do nothing
13
- if (option.disabled) {
14
- return;
15
- }
16
- // for single-select, always return the selected option
17
- let newSelection = [
18
- option.value
19
- ];
20
- // toggle selected state of the option for multiselect
21
- if (multiselect) {
22
- const selectedIndex = selectedOptions.findIndex((o)=>o === option.value);
23
- if (selectedIndex > -1) {
24
- // deselect option
25
- newSelection = [
26
- ...selectedOptions.slice(0, selectedIndex),
27
- ...selectedOptions.slice(selectedIndex + 1)
28
- ];
29
- } else {
30
- // select option
31
- newSelection = [
32
- ...selectedOptions,
33
- option.value
34
- ];
35
- }
36
- }
37
- setSelectedOptions(newSelection);
38
- onOptionSelect === null || onOptionSelect === void 0 ? void 0 : onOptionSelect(event, {
39
- optionValue: option.value,
40
- optionText: option.text,
41
- selectedOptions: newSelection
42
- });
43
- }, [
44
- onOptionSelect,
45
- multiselect,
46
- selectedOptions,
47
- setSelectedOptions
48
- ]);
49
- const clearSelection = (event)=>{
50
- setSelectedOptions([]);
51
- onOptionSelect === null || onOptionSelect === void 0 ? void 0 : onOptionSelect(event, {
52
- optionValue: undefined,
53
- optionText: undefined,
54
- selectedOptions: []
55
- });
56
- };
57
- return {
58
- clearSelection,
59
- selectOption,
60
- selectedOptions
61
- };
39
+ }, [onOptionSelect, multiselect, selectedOptions, setSelectedOptions]);
40
+ const clearSelection = event => {
41
+ setSelectedOptions([]);
42
+ onOptionSelect === null || onOptionSelect === void 0 ? void 0 : onOptionSelect(event, {
43
+ optionValue: undefined,
44
+ optionText: undefined,
45
+ selectedOptions: []
46
+ });
47
+ };
48
+ return {
49
+ clearSelection,
50
+ selectOption,
51
+ selectedOptions
52
+ };
62
53
  };
54
+ //# sourceMappingURL=useSelection.js.map