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