( props: AriaOptionProps, state: ListState<T>, ref: RefObject<FocusableElement | null> )
| 99 | * @param state - State for the listbox, as returned by `useListState`. |
| 100 | */ |
| 101 | export 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()) { |
no test coverage detected