( props: ActionBarContainerHookProps )
| 247 | } |
| 248 | |
| 249 | export 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 | } |
no test coverage detected