generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathinternal.tsx
More file actions
274 lines (243 loc) · 9.38 KB
/
internal.tsx
File metadata and controls
274 lines (243 loc) · 9.38 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { Ref, useImperativeHandle, useRef } from 'react';
import clsx from 'clsx';
import { useUniqueId, warnOnce } from '@cloudscape-design/component-toolkit/internal';
import { useFormFieldContext } from '../contexts/form-field';
import { useInternalI18n } from '../i18n/context';
import { BaseChangeDetail } from '../input/interfaces';
import AutosuggestInput, { AutosuggestInputRef } from '../internal/components/autosuggest-input';
import { OptionsLoadItemsDetail } from '../internal/components/dropdown/interfaces';
import DropdownFooter from '../internal/components/dropdown-footer';
import { useDropdownStatus } from '../internal/components/dropdown-status';
import {
BaseKeyDetail,
fireCancelableEvent,
fireNonCancelableEvent,
NonCancelableCustomEvent,
} from '../internal/events';
import checkControlled from '../internal/hooks/check-controlled';
import { InternalBaseComponentProps } from '../internal/hooks/use-base-component';
import { checkOptionValueField } from '../select/utils/check-option-value-field';
import { AutosuggestItem, AutosuggestProps } from './interfaces';
import { useAutosuggestLoadMore } from './load-more-controller';
import { useAutosuggestItems } from './options-controller';
import AutosuggestOptionsList from './options-list';
import styles from './styles.css.js';
interface InternalAutosuggestProps extends AutosuggestProps, InternalBaseComponentProps {}
const InternalAutosuggest = React.forwardRef((props: InternalAutosuggestProps, ref: Ref<AutosuggestProps.Ref>) => {
const {
value,
onChange,
onBlur,
onFocus,
onKeyUp,
onLoadItems,
options,
filteringType = 'auto',
statusType = 'finished',
placeholder,
clearAriaLabel,
name,
disabled,
disableBrowserAutocorrect = false,
autoFocus,
readOnly,
ariaLabel,
ariaRequired,
enteredTextLabel,
hideEnteredTextOption,
filteringResultsText,
onKeyDown,
virtualScroll,
expandToViewport,
onSelect,
renderHighlightedAriaLive,
style,
__internalRootRef,
...restProps
} = props;
checkControlled('Autosuggest', 'value', value, 'onChange', onChange);
checkOptionValueField('Autosuggest', 'options', options);
const autosuggestInputRef = useRef<AutosuggestInputRef>(null);
useImperativeHandle(
ref,
() => ({
focus: () => autosuggestInputRef.current?.focus(),
select: () => autosuggestInputRef.current?.select(),
}),
[]
);
const i18n = useInternalI18n('autosuggest');
const errorIconAriaLabel = i18n('errorIconAriaLabel', restProps.errorIconAriaLabel);
const selectedAriaLabel = i18n('selectedAriaLabel', restProps.selectedAriaLabel);
const recoveryText = i18n('recoveryText', restProps.recoveryText);
if (restProps.recoveryText && !onLoadItems) {
warnOnce('Autosuggest', '`onLoadItems` must be provided for `recoveryText` to be displayed.');
}
const [autosuggestItemsState, autosuggestItemsHandlers] = useAutosuggestItems({
options: options || [],
filterValue: value,
filterText: value,
filteringType,
enteredTextLabel,
hideEnteredTextLabel: hideEnteredTextOption,
onSelectItem: (option: AutosuggestItem) => {
const value = option.value || '';
fireNonCancelableEvent(onChange, { value });
fireNonCancelableEvent(onSelect, {
value,
selectedOption: option.type !== 'use-entered' ? option.option : undefined,
});
autosuggestInputRef.current?.close();
},
});
const autosuggestLoadMoreHandlers = useAutosuggestLoadMore({
options,
statusType,
onLoadItems: (detail: OptionsLoadItemsDetail) => fireNonCancelableEvent(onLoadItems, detail),
});
const handleChange = (event: NonCancelableCustomEvent<BaseChangeDetail>) => {
autosuggestItemsHandlers.setShowAll(false);
autosuggestItemsHandlers.resetHighlightWithKeyboard();
fireNonCancelableEvent(onChange, event.detail);
};
const handleDelayedInput = (event: NonCancelableCustomEvent<BaseChangeDetail>) => {
autosuggestLoadMoreHandlers.fireLoadMoreOnInputChange(event.detail.value);
};
const handleBlur = () => {
fireNonCancelableEvent(onBlur, null);
};
const handleFocus = () => {
autosuggestItemsHandlers.setShowAll(true);
autosuggestLoadMoreHandlers.fireLoadMoreOnInputFocus();
fireNonCancelableEvent(onFocus, null);
};
const handleKeyUp = (event: CustomEvent<BaseKeyDetail>) => {
fireCancelableEvent(onKeyUp, event.detail, event);
};
const handleKeyDown = (event: CustomEvent<BaseKeyDetail>) => {
fireCancelableEvent(onKeyDown, event.detail, event);
};
const handlePressArrowDown = () => {
if (autosuggestItemsState.items.length - 1 === autosuggestItemsState.highlightedIndex) {
autosuggestItemsHandlers.goHomeWithKeyboard();
return;
}
autosuggestItemsHandlers.moveHighlightWithKeyboard(1);
};
const handlePressArrowUp = () => {
if (
(autosuggestItemsState.highlightedOption?.type === 'child' && autosuggestItemsState.highlightedIndex === 1) ||
autosuggestItemsState.highlightedIndex === 0
) {
autosuggestItemsHandlers.goEndWithKeyboard();
return;
}
autosuggestItemsHandlers.moveHighlightWithKeyboard(-1);
};
const handlePressEnter = () => {
return autosuggestItemsHandlers.selectHighlightedOptionWithKeyboard();
};
const handleCloseDropdown = () => {
autosuggestItemsHandlers.resetHighlightWithKeyboard();
};
const handleRecoveryClick = () => {
autosuggestLoadMoreHandlers.fireLoadMoreOnRecoveryClick();
autosuggestInputRef.current?.focus();
};
const formFieldContext = useFormFieldContext(restProps);
const selfControlId = useUniqueId('input');
const footerControlId = useUniqueId('footer');
const controlId = formFieldContext.controlId ?? selfControlId;
const listId = useUniqueId('list');
const highlightedOptionIdSource = useUniqueId();
const highlightedOptionId = autosuggestItemsState.highlightedOption ? highlightedOptionIdSource : undefined;
const isEmpty = !value && !autosuggestItemsState.items.length;
const isFiltered = !!value && value.length !== 0 && !(filteringType === 'auto' && autosuggestItemsState.showAll);
const filteredText = isFiltered
? filteringResultsText?.(autosuggestItemsState.items.length, options?.length ?? 0)
: undefined;
const dropdownStatus = useDropdownStatus({
...props,
isEmpty,
recoveryText,
errorIconAriaLabel,
onRecoveryClick: handleRecoveryClick,
filteringResultsText: filteredText,
hasRecoveryCallback: !!onLoadItems,
});
const shouldRenderDropdownContent =
autosuggestItemsState.items.length !== 0 || !!dropdownStatus.content || (!hideEnteredTextOption && !!value);
const hasItems = useRef(autosuggestItemsState.items.length > 0);
hasItems.current = hasItems.current || autosuggestItemsState.items.length > 0;
return (
<AutosuggestInput
{...restProps}
className={clsx(styles.root, restProps.className)}
ref={autosuggestInputRef}
__internalRootRef={__internalRootRef}
value={value}
onChange={handleChange}
onBlur={handleBlur}
onFocus={handleFocus}
onKeyUp={handleKeyUp}
onKeyDown={handleKeyDown}
name={name}
controlId={controlId}
placeholder={placeholder}
disabled={disabled}
readOnly={readOnly}
autoFocus={autoFocus}
ariaLabel={ariaLabel}
ariaRequired={ariaRequired}
clearAriaLabel={clearAriaLabel}
disableBrowserAutocorrect={disableBrowserAutocorrect}
expandToViewport={expandToViewport}
ariaControls={listId}
ariaActivedescendant={highlightedOptionId}
dropdownExpanded={shouldRenderDropdownContent}
style={style}
dropdownContent={
shouldRenderDropdownContent && (
<AutosuggestOptionsList
statusType={statusType}
autosuggestItemsState={autosuggestItemsState}
autosuggestItemsHandlers={autosuggestItemsHandlers}
highlightedOptionId={highlightedOptionId}
highlightText={value}
listId={listId}
controlId={controlId}
handleLoadMore={autosuggestLoadMoreHandlers.fireLoadMoreOnScroll}
hasDropdownStatus={dropdownStatus.content !== null}
virtualScroll={virtualScroll}
selectedAriaLabel={selectedAriaLabel}
renderHighlightedAriaLive={renderHighlightedAriaLive}
listBottom={
!dropdownStatus.isSticky ? <DropdownFooter content={dropdownStatus.content} id={footerControlId} /> : null
}
ariaDescribedby={dropdownStatus.content ? footerControlId : undefined}
/>
)
}
dropdownFooter={
dropdownStatus.isSticky && dropdownStatus.content ? (
<DropdownFooter
id={footerControlId}
content={dropdownStatus.content}
hasItems={autosuggestItemsState.items.length >= 1}
/>
) : null
}
// Forces dropdown position recalculation when new options are loaded
dropdownContentKey={hasItems.current.toString()}
loopFocus={dropdownStatus.hasRecoveryButton}
onCloseDropdown={handleCloseDropdown}
onDelayedInput={handleDelayedInput}
onPressArrowDown={handlePressArrowDown}
onPressArrowUp={handlePressArrowUp}
onPressEnter={handlePressEnter}
/>
);
});
export default InternalAutosuggest;