forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseDropdown.tsx
169 lines (147 loc) · 5.78 KB
/
useDropdown.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import * as React from 'react';
import { ChevronDownRegular as ChevronDownIcon } from '@fluentui/react-icons';
import { getPartitionedNativeProps, mergeCallbacks, resolveShorthand, useTimeout } from '@fluentui/react-utilities';
import { getDropdownActionFromKey } from '../../utils/dropdownKeyActions';
import { useComboboxBaseState } from '../../utils/useComboboxBaseState';
import { useComboboxPopup } from '../../utils/useComboboxPopup';
import { useTriggerListboxSlots } from '../../utils/useTriggerListboxSlots';
import { Listbox } from '../Listbox/Listbox';
import type { Slot } from '@fluentui/react-utilities';
import type { OptionValue } from '../../utils/OptionCollection.types';
import type { DropdownProps, DropdownState } from './Dropdown.types';
import { useMergedRefs } from '@fluentui/react-utilities';
/**
* Create the state required to render Dropdown.
*
* The returned state can be modified with hooks such as useDropdownStyles_unstable,
* before being passed to renderDropdown_unstable.
*
* @param props - props from this instance of Dropdown
* @param ref - reference to root HTMLElement of Dropdown
*/
export const useDropdown_unstable = (props: DropdownProps, ref: React.Ref<HTMLButtonElement>): DropdownState => {
const baseState = useComboboxBaseState(props);
const {
activeOption,
getIndexOfId,
getOptionsMatchingText,
open,
setActiveOption,
setFocusVisible,
setOpen,
} = baseState;
const { primary: triggerNativeProps, root: rootNativeProps } = getPartitionedNativeProps({
props,
primarySlotTagName: 'button',
excludedPropNames: ['children'],
});
// set listbox popup width based off the root/trigger width
const rootRef = React.useRef<HTMLDivElement>(null);
const [popupWidth, setPopupWidth] = React.useState<string>();
React.useEffect(() => {
const width = open ? `${rootRef.current?.clientWidth}px` : undefined;
setPopupWidth(width);
}, [open]);
// jump to matching option based on typing
const searchString = React.useRef('');
const [setKeyTimeout, clearKeyTimeout] = useTimeout();
const getNextMatchingOption = (): OptionValue | undefined => {
// first check for matches for the full searchString
let matcher = (optionText: string) => optionText.toLowerCase().indexOf(searchString.current) === 0;
let matches = getOptionsMatchingText(matcher);
let startIndex = activeOption ? getIndexOfId(activeOption.id) : 0;
// if the dropdown is already open and the searchstring is a single character,
// then look after the current activeOption for letters
// this is so slowly typing the same letter will cycle through matches
if (open && searchString.current.length === 1) {
startIndex++;
}
// if there are no direct matches, check if the search is all the same letter, e.g. "aaa"
if (!matches.length) {
const letters = searchString.current.split('');
const allSameLetter = letters.length && letters.every(letter => letter === letters[0]);
// if the search is all the same letter, cycle through options starting with that letter
if (allSameLetter) {
startIndex++;
matcher = (optionText: string) => optionText.toLowerCase().indexOf(letters[0]) === 0;
matches = getOptionsMatchingText(matcher);
}
}
// if there is an active option and multiple matches,
// return first matching option after the current active option, looping back to the top
if (matches.length > 1 && activeOption) {
const nextMatch = matches.find(option => getIndexOfId(option.id) >= startIndex);
return nextMatch ?? matches[0];
}
return matches[0] ?? undefined;
};
const onTriggerKeyDown = (ev: React.KeyboardEvent<HTMLButtonElement>) => {
// clear timeout, if it exists
clearKeyTimeout();
// if the key was a char key, update search string
if (getDropdownActionFromKey(ev) === 'Type') {
// update search string
searchString.current += ev.key.toLowerCase();
setKeyTimeout(() => {
searchString.current = '';
}, 500);
// update state
!open && setOpen(ev, true);
const nextOption = getNextMatchingOption();
setActiveOption(nextOption);
setFocusVisible(true);
}
};
// resolve button and listbox slot props
let triggerSlot: Slot<'button'>;
let listboxSlot: Slot<typeof Listbox> | undefined;
triggerSlot = resolveShorthand(props.button, {
required: true,
defaultProps: {
type: 'button',
children: baseState.value || props.placeholder,
...triggerNativeProps,
},
});
triggerSlot.onKeyDown = mergeCallbacks(onTriggerKeyDown, triggerSlot.onKeyDown);
listboxSlot =
baseState.open || baseState.hasFocus
? resolveShorthand(props.listbox, {
required: true,
defaultProps: {
children: props.children,
style: { width: popupWidth },
},
})
: undefined;
[triggerSlot, listboxSlot] = useComboboxPopup(props, triggerSlot, listboxSlot);
[triggerSlot, listboxSlot] = useTriggerListboxSlots(props, baseState, ref, triggerSlot, listboxSlot);
const state: DropdownState = {
components: {
root: 'div',
button: 'button',
expandIcon: 'span',
listbox: Listbox,
},
root: resolveShorthand(props.root, {
required: true,
defaultProps: {
'aria-owns': !props.inlinePopup ? listboxSlot?.id : undefined,
children: props.children,
...rootNativeProps,
},
}),
button: triggerSlot,
listbox: listboxSlot,
expandIcon: resolveShorthand(props.expandIcon, {
required: true,
defaultProps: {
children: <ChevronDownIcon />,
},
}),
placeholderVisible: !baseState.value && !!props.placeholder,
...baseState,
};
state.root.ref = useMergedRefs(state.root.ref, rootRef);
return state;
};