-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathinternal.tsx
239 lines (213 loc) · 7.61 KB
/
internal.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useState, useRef, useEffect } from 'react';
import clsx from 'clsx';
import styles from './styles.css.js';
import Dropdown from '../internal/components/dropdown';
import { useDropdownStatus } from '../internal/components/dropdown-status';
import Filter from './parts/filter';
import Trigger from './parts/trigger';
import { useUniqueId } from '../internal/hooks/use-unique-id';
import { getBaseProps } from '../internal/base-component';
import { SelectProps } from './interfaces';
import { prepareOptions } from '../internal/components/option/utils/prepare-options';
import { useSelect } from './utils/use-select';
import { checkOptionValueField } from './utils/check-option-value-field';
import { useNativeSearch } from './utils/use-native-search';
import { fireNonCancelableEvent } from '../internal/events';
import { useLoadItems } from './utils/use-load-items';
import { useAnnouncement } from './utils/use-announcement';
import { useFormFieldContext } from '../internal/context/form-field-context';
import PlainList, { SelectListProps } from './parts/plain-list';
import VirtualList from './parts/virtual-list';
import DropdownFooter from '../internal/components/dropdown-footer';
import checkControlled from '../internal/hooks/check-controlled';
import { InternalBaseComponentProps } from '../internal/hooks/use-base-component';
import { useMergeRefs } from '../internal/hooks/use-merge-refs';
import { OptionGroup } from '../internal/components/option/interfaces.js';
import { SomeRequired } from '../internal/types.js';
import ScreenreaderOnly from '../internal/components/screenreader-only/index.js';
import { joinStrings } from '../internal/utils/strings/join-strings.js';
export interface InternalSelectProps extends SomeRequired<SelectProps, 'options'>, InternalBaseComponentProps {
__inFilteringToken?: boolean;
}
const InternalSelect = React.forwardRef(
(
{
options,
filteringType = 'none',
filteringPlaceholder,
filteringAriaLabel,
ariaRequired,
placeholder,
disabled,
ariaLabel,
statusType = 'finished',
empty,
loadingText,
finishedText,
errorText,
recoveryText,
noMatch,
triggerVariant = 'label',
selectedAriaLabel,
renderHighlightedAriaLive,
selectedOption,
onBlur,
onFocus,
onLoadItems,
onChange,
virtualScroll,
expandToViewport,
__inFilteringToken,
__internalRootRef = null,
...restProps
}: InternalSelectProps,
externalRef: React.Ref<SelectProps.Ref>
) => {
const baseProps = getBaseProps(restProps);
const formFieldContext = useFormFieldContext(restProps);
const { handleLoadMore, handleRecoveryClick, fireLoadItems } = useLoadItems({
onLoadItems,
options,
statusType,
});
checkControlled('Select', 'selectedOption', selectedOption, 'onChange', onChange);
checkOptionValueField('Select', 'options', options);
const [filteringValue, setFilteringValue] = useState('');
const { filteredOptions, parentMap } = prepareOptions(options, filteringType, filteringValue);
const rootRef = useRef<HTMLDivElement>(null);
const triggerRef = useRef<HTMLButtonElement>(null);
const selfControlId = useUniqueId('trigger');
const controlId = formFieldContext.controlId ?? selfControlId;
const scrollToIndex = useRef<SelectListProps.SelectListRef>(null);
const {
isOpen,
highlightType,
highlightedOption,
highlightedIndex,
getTriggerProps,
getDropdownProps,
getFilterProps,
getMenuProps,
getOptionProps,
highlightOption,
selectOption,
announceSelected,
} = useSelect({
selectedOptions: selectedOption ? [selectedOption] : [],
updateSelectedOption: option => fireNonCancelableEvent(onChange, { selectedOption: option }),
options: filteredOptions,
filteringType,
onBlur,
onFocus,
externalRef,
fireLoadItems,
setFilteringValue,
});
const handleNativeSearch = useNativeSearch({
isEnabled: filteringType === 'none',
options: filteredOptions,
highlightOption: !isOpen ? selectOption : highlightOption,
highlightedOption: !isOpen ? selectedOption : highlightedOption?.option,
});
const selectAriaLabelId = useUniqueId('select-arialabel-');
useEffect(() => {
scrollToIndex.current?.(highlightedIndex);
}, [highlightedIndex]);
const filter = (
<Filter
filteringType={filteringType}
placeholder={filteringPlaceholder}
ariaLabel={filteringAriaLabel}
ariaRequired={ariaRequired}
value={filteringValue}
{...getFilterProps()}
/>
);
const trigger = (
<Trigger
ref={triggerRef}
placeholder={placeholder}
disabled={disabled}
triggerVariant={triggerVariant}
triggerProps={getTriggerProps(disabled)}
selectedOption={selectedOption}
isOpen={isOpen}
inFilteringToken={__inFilteringToken}
{...formFieldContext}
controlId={controlId}
ariaLabelledby={joinStrings(formFieldContext.ariaLabelledby, selectAriaLabelId)}
ariaLabel={ariaLabel}
/>
);
const menuProps = {
...getMenuProps(),
onLoadMore: handleLoadMore,
ariaLabelledby: joinStrings(selectAriaLabelId, controlId),
};
const isEmpty = !options || options.length === 0;
const isNoMatch = filteredOptions && filteredOptions.length === 0;
const dropdownStatus = useDropdownStatus({
statusType,
empty,
loadingText,
finishedText,
errorText,
recoveryText,
isEmpty,
isNoMatch,
noMatch,
onRecoveryClick: handleRecoveryClick,
});
const announcement = useAnnouncement({
announceSelected,
highlightedOption,
getParent: option => parentMap.get(option)?.option as undefined | OptionGroup,
selectedAriaLabel,
renderHighlightedAriaLive,
});
const ListComponent = virtualScroll ? VirtualList : PlainList;
const handleMouseDown = (event: React.MouseEvent) => {
const target = event.target as HTMLElement;
if (target !== document.activeElement) {
// prevent currently focused element from losing it
event.preventDefault();
}
};
const mergedRef = useMergeRefs(rootRef, __internalRootRef);
return (
<div
{...baseProps}
ref={mergedRef}
className={clsx(styles.root, baseProps.className)}
onKeyPress={handleNativeSearch}
>
<Dropdown
{...getDropdownProps()}
open={isOpen}
stretchTriggerHeight={__inFilteringToken}
trigger={trigger}
header={filter}
onMouseDown={handleMouseDown}
footer={dropdownStatus.isSticky ? <DropdownFooter content={dropdownStatus.content} /> : null}
expandToViewport={expandToViewport}
>
<ListComponent
listBottom={!dropdownStatus.isSticky ? <DropdownFooter content={dropdownStatus.content} /> : null}
menuProps={menuProps}
getOptionProps={getOptionProps}
filteredOptions={filteredOptions}
filteringValue={filteringValue}
ref={scrollToIndex}
hasDropdownStatus={dropdownStatus.content !== null}
screenReaderContent={announcement}
highlightType={highlightType}
/>
</Dropdown>
<ScreenreaderOnly id={selectAriaLabelId}>{ariaLabel}</ScreenreaderOnly>
</div>
);
}
);
export default InternalSelect;