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

Function onSelect

packages/react-aria/src/selection/useSelectableItem.ts:144–182  ·  view source on GitHub ↗
(e: PressEvent | LongPressEvent | PointerEvent)

Source from the content-addressed store, hash-verified

142 let router = useRouter();
143 id = useId(id);
144 let onSelect = (e: PressEvent | LongPressEvent | PointerEvent) => {
145 if (e.pointerType === 'keyboard' && isNonContiguousSelectionModifier(e)) {
146 manager.toggleSelection(key);
147 } else {
148 if (manager.selectionMode === 'none') {
149 return;
150 }
151
152 if (manager.isLink(key)) {
153 if (linkBehavior === 'selection' && ref.current) {
154 let itemProps = manager.getItemProps(key);
155 router.open(ref.current, e, itemProps.href, itemProps.routerOptions);
156 // Always set selected keys back to what they were so that select and combobox close.
157 manager.setSelectedKeys(manager.selectedKeys);
158 return;
159 } else if (linkBehavior === 'override' || linkBehavior === 'none') {
160 return;
161 }
162 }
163
164 if (manager.selectionMode === 'single') {
165 if (manager.isSelected(key) && !manager.disallowEmptySelection) {
166 manager.toggleSelection(key);
167 } else {
168 manager.replaceSelection(key);
169 }
170 } else if (e && e.shiftKey) {
171 manager.extendSelection(key);
172 } else if (
173 manager.selectionBehavior === 'toggle' ||
174 (e && (isCtrlKeyPressed(e) || e.pointerType === 'touch' || e.pointerType === 'virtual'))
175 ) {
176 // if touch or virtual (VO) then we just want to toggle, otherwise it's impossible to multi select because they don't have modifier keys
177 manager.toggleSelection(key);
178 } else {
179 manager.replaceSelection(key);
180 }
181 }
182 };
183
184 // Focus the associated DOM node when this item becomes the focusedKey
185 // TODO: can't make this useLayoutEffect bacause it breaks menus inside dialogs

Callers 3

useSelectableItemFunction · 0.85
onLongPressFunction · 0.85
FileTrigger.tsxFile · 0.85

Calls 10

isCtrlKeyPressedFunction · 0.90
toggleSelectionMethod · 0.65
isLinkMethod · 0.65
getItemPropsMethod · 0.65
openMethod · 0.65
setSelectedKeysMethod · 0.65
isSelectedMethod · 0.65
replaceSelectionMethod · 0.65
extendSelectionMethod · 0.65

Tested by

no test coverage detected