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

Function useActionBarContainer

packages/@react-spectrum/s2/src/ActionBar.tsx:249–300  ·  view source on GitHub ↗
(
  props: ActionBarContainerHookProps
)

Source from the content-addressed store, hash-verified

247}
248
249export function useActionBarContainer(
250 props: ActionBarContainerHookProps
251): ActionBarContainerHookResult {
252 let {renderActionBar, scrollRef} = props;
253 let [selectedKeys, setSelectedKeys] = useControlledState(
254 props.selectedKeys,
255 props.defaultSelectedKeys || new Set(),
256 props.onSelectionChange
257 );
258 let selectedKeysSet = useMemo(
259 () => (selectedKeys === 'all' ? (selectedKeys as 'all') : new Set(selectedKeys)),
260 [selectedKeys]
261 );
262 let actionBar = useMemo(
263 () => renderActionBar?.(selectedKeysSet),
264 [renderActionBar, selectedKeysSet]
265 );
266 let selectedItemCount = selectedKeysSet === 'all' ? ('all' as const) : selectedKeysSet.size;
267 let [actionBarHeight, setActionBarHeight] = useState(0);
268 let actionBarRef = useCallback((ref: DOMRefValue | null) => {
269 let actionBar = ref?.UNSAFE_getDOMNode();
270 if (actionBar) {
271 setActionBarHeight(actionBar.offsetHeight + 8);
272 } else {
273 setActionBarHeight(0);
274 }
275 }, []);
276
277 let actionBarContext = useMemo(
278 () => ({
279 ref: actionBarRef,
280 scrollRef,
281 selectedItemCount,
282 onClearSelection: () => setSelectedKeys(new Set())
283 }),
284 [scrollRef, actionBarRef, selectedItemCount, setSelectedKeys]
285 );
286
287 let wrappedActionBar = useMemo(
288 () => (
289 <ActionBarContext.Provider value={actionBarContext}>{actionBar}</ActionBarContext.Provider>
290 ),
291 [actionBarContext, actionBar]
292 );
293
294 return {
295 selectedKeys,
296 onSelectionChange: setSelectedKeys,
297 actionBar: wrappedActionBar,
298 actionBarHeight
299 };
300}

Callers 4

TreeView.tsxFile · 0.90
CardView.tsxFile · 0.90
TableView.tsxFile · 0.90
ListView.tsxFile · 0.90

Calls 3

useControlledStateFunction · 0.90
UNSAFE_getDOMNodeMethod · 0.80
setSelectedKeysFunction · 0.50

Tested by

no test coverage detected