MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useListBox

Function useListBox

packages/react-aria/src/listbox/useListBox.ts:130–202  ·  view source on GitHub ↗
(
  props: AriaListBoxOptions<T>,
  state: ListState<T>,
  ref: RefObject<HTMLElement | null>
)

Source from the content-addressed store, hash-verified

128 * @param state - State for the listbox, as returned by `useListState`.
129 */
130export function useListBox<T>(
131 props: AriaListBoxOptions<T>,
132 state: ListState<T>,
133 ref: RefObject<HTMLElement | null>
134): ListBoxAria {
135 let domProps = filterDOMProps(props, {labelable: true});
136 // Use props instead of state here. We don't want this to change due to long press.
137 let selectionBehavior = props.selectionBehavior || 'toggle';
138 let orientation = props.orientation || 'vertical';
139 let linkBehavior =
140 props.linkBehavior || (selectionBehavior === 'replace' ? 'action' : 'override');
141 if (selectionBehavior === 'toggle' && linkBehavior === 'action') {
142 // linkBehavior="action" does not work with selectionBehavior="toggle" because there is no way
143 // to initiate selection (checkboxes are not allowed inside a listbox). Link items will not be
144 // selectable in this configuration.
145 linkBehavior = 'override';
146 }
147
148 let {listProps} = useSelectableList({
149 ...props,
150 ref,
151 selectionManager: state.selectionManager,
152 collection: state.collection,
153 disabledKeys: state.disabledKeys,
154 linkBehavior
155 });
156
157 let {focusWithinProps} = useFocusWithin({
158 onFocusWithin: props.onFocus,
159 onBlurWithin: props.onBlur,
160 onFocusWithinChange: props.onFocusChange
161 });
162
163 // Share list id and some props with child options.
164 let id = useId(props.id);
165 listData.set(state, {
166 id,
167 shouldUseVirtualFocus: props.shouldUseVirtualFocus,
168 shouldSelectOnPressUp: props.shouldSelectOnPressUp,
169 shouldFocusOnHover: props.shouldFocusOnHover,
170 isVirtualized: props.isVirtualized,
171 onAction: props.onAction,
172 linkBehavior,
173 // @ts-ignore
174 UNSTABLE_itemBehavior: props['UNSTABLE_itemBehavior']
175 });
176
177 let {labelProps, fieldProps} = useLabel({
178 ...props,
179 id,
180 // listbox is not an HTML input element so it
181 // shouldn't be labeled by a <label> element.
182 labelElementType: 'span'
183 });
184
185 return {
186 labelProps,
187 listBoxProps: mergeProps(

Callers 7

ListBoxFunction · 0.90
ListBoxFunction · 0.90
DraggableListBoxFunction · 0.90
ListBoxInnerFunction · 0.90
ListBoxBase.tsxFile · 0.90

Calls 7

filterDOMPropsFunction · 0.90
useSelectableListFunction · 0.90
useFocusWithinFunction · 0.90
useIdFunction · 0.90
useLabelFunction · 0.90
mergePropsFunction · 0.90
setMethod · 0.45

Tested by

no test coverage detected