MCPcopy
hub / github.com/adobe/react-spectrum / useGridListItem

Function useGridListItem

packages/react-aria/src/gridlist/useGridListItem.ts:91–422  ·  view source on GitHub ↗
(
  props: AriaGridListItemOptions,
  state: ListState<T> | TreeState<T>,
  ref: RefObject<FocusableElement | null>
)

Source from the content-addressed store, hash-verified

89 * @param ref - The ref attached to the row element.
90 */
91export function useGridListItem<T>(
92 props: AriaGridListItemOptions,
93 state: ListState<T> | TreeState<T>,
94 ref: RefObject<FocusableElement | null>
95): GridListItemAria {
96 // Copied from useGridCell + some modifications to make it not so grid specific
97 let {node, isVirtualized} = props;
98
99 // let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/gridlist');
100 let {direction} = useLocale();
101 let {onAction, linkBehavior, keyboardNavigationBehavior, shouldSelectOnPressUp} =
102 listMap.get(state)!;
103 let descriptionId = useSlotId();
104
105 // We need to track the key of the item at the time it was last focused so that we force
106 // focus to go to the item when the DOM node is reused for a different item in a virtualizer.
107 let keyWhenFocused = useRef<Key | null>(null);
108 let focus = () => {
109 // Don't shift focus to the row if the active element is a element within the row already
110 // (e.g. clicking on a row button)
111 if (
112 ref.current !== null &&
113 ((keyWhenFocused.current != null && node.key !== keyWhenFocused.current) ||
114 !isFocusWithin(ref.current))
115 ) {
116 focusSafely(ref.current);
117 }
118 };
119
120 let treeGridRowProps: HTMLAttributes<HTMLElement> = {};
121 let hasChildRows = props.hasChildItems;
122 let hasLink = state.selectionManager.isLink(node.key);
123 if (node != null && 'expandedKeys' in state) {
124 // TODO: ideally node.hasChildNodes would be a way to tell if a row has child nodes, but the row's contents make it so that value is always
125 // true...
126 let children = state.collection.getChildren?.(node.key);
127 hasChildRows = hasChildRows || [...(children ?? [])].length > 1;
128
129 if (
130 onAction == null &&
131 !hasLink &&
132 state.selectionManager.selectionMode === 'none' &&
133 hasChildRows
134 ) {
135 onAction = () => state.toggleKey(node.key);
136 }
137
138 let isExpanded = hasChildRows ? state.expandedKeys.has(node.key) : undefined;
139 let setSize = 1;
140 let index = node.index;
141 if (node.level >= 0 && node?.parentKey != null) {
142 let parent = state.collection.getItem(node.parentKey);
143 if (parent) {
144 // siblings must exist because our original node exists
145 let siblings = getDirectChildren(parent, state.collection);
146 setSize = [...siblings].filter(row => row.type === 'item').length;
147 if (index > 0 && siblings[0].type !== 'item') {
148 index -= 1; // subtract one for the parent item's content node

Callers 4

useTagFunction · 0.90
useTreeItemFunction · 0.90
GridList.tsxFile · 0.90
ListViewItemFunction · 0.90

Calls 15

useLocaleFunction · 0.90
useSlotIdFunction · 0.90
useSelectableItemFunction · 0.90
chainFunction · 0.90
useSyntheticLinkPropsFunction · 0.90
mergePropsFunction · 0.90
getRowIdFunction · 0.90
getEventTargetFunction · 0.90
isTabbableFunction · 0.90
getDirectChildrenFunction · 0.85
onKeyDownFunction · 0.70
isLinkMethod · 0.65

Tested by

no test coverage detected