MCPcopy Index your code
hub / github.com/adobe/react-spectrum / onKeyDown

Function onKeyDown

packages/react-aria/src/combobox/useComboBox.ts:180–240  ·  view source on GitHub ↗
(e: BaseEvent<KeyboardEvent<any>>)

Source from the content-addressed store, hash-verified

178
179 // For textfield specific keydown operations
180 let onKeyDown = (e: BaseEvent<KeyboardEvent<any>>) => {
181 if (e.nativeEvent.isComposing) {
182 return;
183 }
184 switch (e.key) {
185 case 'Enter':
186 case 'Tab':
187 // Prevent form submission if menu is open since we may be selecting a option
188 if (state.isOpen && e.key === 'Enter') {
189 e.preventDefault();
190 }
191
192 // If the focused item is a link, trigger opening it. Items that are links are not selectable.
193 if (state.isOpen && listBoxRef.current && state.selectionManager.focusedKey != null) {
194 let collectionItem = state.collection.getItem(state.selectionManager.focusedKey);
195 if (collectionItem?.props.href) {
196 let item = listBoxRef.current.querySelector(
197 `[data-key="${CSS.escape(state.selectionManager.focusedKey.toString())}"]`
198 );
199 if (e.key === 'Enter' && item instanceof HTMLAnchorElement) {
200 router.open(
201 item,
202 e,
203 collectionItem.props.href,
204 collectionItem.props.routerOptions as RouterOptions
205 );
206 }
207 state.close();
208 break;
209 } else if (collectionItem?.props.onAction) {
210 collectionItem.props.onAction();
211 state.close();
212 break;
213 }
214 }
215 if (e.key === 'Enter' || state.isOpen) {
216 state.commit();
217 }
218 if (e.key === 'Tab') {
219 e.continuePropagation();
220 }
221
222 break;
223 case 'Escape':
224 if (!state.selectionManager.isEmpty || state.inputValue === '' || props.allowsCustomValue) {
225 e.continuePropagation();
226 }
227 state.revert();
228 break;
229 case 'ArrowDown':
230 state.open('first', 'manual');
231 break;
232 case 'ArrowUp':
233 state.open('last', 'manual');
234 break;
235 case 'ArrowLeft':
236 case 'ArrowRight':
237 state.selectionManager.setFocusedKey(null);

Callers

nothing calls this directly

Calls 8

revertMethod · 0.80
getItemMethod · 0.65
toStringMethod · 0.65
openMethod · 0.65
closeMethod · 0.65
commitMethod · 0.65
continuePropagationMethod · 0.65
setFocusedKeyMethod · 0.65

Tested by

no test coverage detected