UNPKG

@trellixio/roaster-coffee

Version:
58 lines (55 loc) 1.91 kB
import { useContext, useCallback } from 'react'; import { SelectionType, SELECT_ALL_ITEMS } from './types.js'; import { TableContext, TableSelectionChangeContext, TableRowContext } from './context.js'; function useTableSelectionChange() { const onSelectionChange = useContext(TableSelectionChangeContext); if (!onSelectionChange) { throw new Error(`Missing TableProvider context`); } return onSelectionChange; } function useTableRow() { const tableRow = useContext(TableRowContext); if (!tableRow) { throw new Error(`Missing TableProvider context`); } return tableRow; } function useTableValue() { const table = useContext(TableContext); if (!table) { throw new Error(`Missing TableProvider context`); } return table; } function useBulkSelectionData({ selectedItemsCount, itemCount }) { const selectable = Boolean(selectedItemsCount); const selectMode = selectedItemsCount === "All" || selectedItemsCount > 0; let bulkSelectState = "indeterminate"; if (!selectedItemsCount || selectedItemsCount === 0) { bulkSelectState = void 0; } else if (selectedItemsCount === SELECT_ALL_ITEMS || selectedItemsCount === itemCount) { bulkSelectState = true; } return { selectMode, bulkSelectState, selectable }; } function useHandleBulkSelection({ onSelectionChange = () => { } }) { const handleSelectionChange = useCallback( (selectionType, selection) => { if (selectionType === SelectionType.Single) { onSelectionChange(SelectionType.Single, selection); } else if (selectionType === SelectionType.Page || selectionType === SelectionType.All) { onSelectionChange(selectionType, SELECT_ALL_ITEMS); } }, [onSelectionChange] ); return handleSelectionChange; } export { useBulkSelectionData, useHandleBulkSelection, useTableRow, useTableSelectionChange, useTableValue }; //# sourceMappingURL=hooks.js.map