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

Function useOption

packages/react-aria/src/listbox/useOption.ts:101–189  ·  view source on GitHub ↗
(
  props: AriaOptionProps,
  state: ListState<T>,
  ref: RefObject<FocusableElement | null>
)

Source from the content-addressed store, hash-verified

99 * @param state - State for the listbox, as returned by `useListState`.
100 */
101export function useOption<T>(
102 props: AriaOptionProps,
103 state: ListState<T>,
104 ref: RefObject<FocusableElement | null>
105): OptionAria {
106 let {key} = props;
107
108 let data = listData.get(state);
109
110 let isDisabled = props.isDisabled ?? state.selectionManager.isDisabled(key);
111 let isSelected = props.isSelected ?? state.selectionManager.isSelected(key);
112 let shouldSelectOnPressUp = props.shouldSelectOnPressUp ?? data?.shouldSelectOnPressUp;
113 let shouldFocusOnHover = props.shouldFocusOnHover ?? data?.shouldFocusOnHover;
114 let shouldUseVirtualFocus = props.shouldUseVirtualFocus ?? data?.shouldUseVirtualFocus;
115 let isVirtualized = props.isVirtualized ?? data?.isVirtualized;
116
117 let labelId = useSlotId();
118 let descriptionId = useSlotId();
119
120 let optionProps = {
121 role: 'option',
122 'aria-disabled': isDisabled || undefined,
123 'aria-selected': state.selectionManager.selectionMode !== 'none' ? isSelected : undefined,
124 'aria-label': props['aria-label'],
125 'aria-labelledby': labelId,
126 'aria-describedby': descriptionId
127 };
128
129 let item = state.collection.getItem(key);
130 if (isVirtualized) {
131 let index = Number(item?.index);
132 optionProps['aria-posinset'] = Number.isNaN(index) ? undefined : index + 1;
133 optionProps['aria-setsize'] = getItemCount(state.collection);
134 }
135
136 let onAction = data?.onAction ? () => data?.onAction?.(key) : undefined;
137 let id = getItemId(state, key);
138 let {itemProps, isPressed, isFocused, hasAction, allowsSelection} = useSelectableItem({
139 selectionManager: state.selectionManager,
140 key,
141 ref,
142 shouldSelectOnPressUp,
143 allowsDifferentPressOrigin: shouldSelectOnPressUp && shouldFocusOnHover,
144 isVirtualized,
145 shouldUseVirtualFocus,
146 isDisabled,
147 onAction:
148 onAction || item?.props?.onAction ? chain(item?.props?.onAction, onAction) : undefined,
149 linkBehavior: data?.linkBehavior,
150 // @ts-ignore
151 UNSTABLE_itemBehavior: data?.['UNSTABLE_itemBehavior'],
152 id
153 });
154
155 let {hoverProps} = useHover({
156 isDisabled: isDisabled || !shouldFocusOnHover,
157 onHoverStart() {
158 if (!isFocusVisible()) {

Callers 7

OptionFunction · 0.90
OptionFunction · 0.90
CollectionItemFunction · 0.90
CollectionItemFunction · 0.90
OptionFunction · 0.90
ListBox.tsxFile · 0.85
ListBoxOptionFunction · 0.85

Calls 13

useSlotIdFunction · 0.90
getItemCountFunction · 0.90
getItemIdFunction · 0.90
useSelectableItemFunction · 0.90
chainFunction · 0.90
useHoverFunction · 0.90
filterDOMPropsFunction · 0.90
useLinkPropsFunction · 0.90
mergePropsFunction · 0.90
isFocusVisibleFunction · 0.90
isDisabledMethod · 0.65
isSelectedMethod · 0.65

Tested by

no test coverage detected