Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/TableToolsTable/TableToolsTable.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const argProps = {
customEmptyState: propTypes.bool,
enableExport: propTypes.bool,
enableDetails: propTypes.bool,
enableExpandAll: propTypes.bool,
enableBulkSelect: propTypes.bool,
enablePreselection: propTypes.bool,
enableSimpleBulkSelect: propTypes.bool,
Expand Down Expand Up @@ -84,6 +85,7 @@ const meta = {
customEmptyState: true,
enableExport: true,
enableDetails: true,
enableExpandAll: true,
enableBulkSelect: true,
enablePreselection: false,
enableSimpleBulkSelect: false,
Expand Down Expand Up @@ -122,6 +124,7 @@ const CommonExample = ({
customEmptyState,
enableExport,
enableDetails,
enableExpandAll,
enableBulkSelect,
enablePreselection,
enableSimpleBulkSelect,
Expand Down Expand Up @@ -193,6 +196,7 @@ const CommonExample = ({
...(customEmptyState ? { EmptyState: CustomEmptyState } : {}),
...(enableExport ? { exporter } : {}),
...(enableDetails ? { detailsComponent: DetailsRow } : {}),
canCollapseAll: enableExpandAll,
...(enableBulkSelect
? {
...(enablePreselection ? { selected: selectedItemIds } : {}),
Expand Down
23 changes: 20 additions & 3 deletions src/hooks/useExpandable/useExpandable.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { itemDetailsRow, addExpandProp } from './helpers';
* @param {object} [options] AsyncTableTools options
* @param {object} [options.detailsComponent] A component that should be rendered as a details row
* @param {object} [options.detailsProps] Props spread onto each details row
* @param {Array} [options.items] Items currently rendered in the table (used for expand/collapse all)
* @param {boolean} [options.canCollapseAll] Whether to enable the expand/collapse all toggle in the table header (defaults to true)
*
* @returns {useExpandableReturn} An object of props meant to be used in the {@link TableToolsTable}
*
Expand All @@ -27,12 +29,24 @@ import { itemDetailsRow, addExpandProp } from './helpers';
*/
const useExpandable = (options) => {
const enableExpandingRow = !!options?.detailsComponent || !!options.treeTable;
const { selection: openItems, toggle } = useSelectionManager([]);
const { selection: openItems, toggle, set, clear } = useSelectionManager([]);
// TODO If the selection manager is based on `useTableState`, observes can be used to reset open items
const [, setOpenItemsState] = useTableState('open-items');

const onCollapse = (_event, _index, _isOpen, { item: { itemId } }) =>
toggle(itemId);
const onCollapse = useCallback(
(_event, rowIndex, isOpen, rowData) => {
if (rowIndex === undefined) {
if (isOpen) {
set(options.items?.map((item) => item.itemId));
} else {
clear();
}
} else {
toggle(rowData?.item?.itemId);
}
},
[options.items, toggle, set, clear],
);

const isItemOpen = useCallback(
(itemId) => (openItems || []).includes(itemId),
Expand Down Expand Up @@ -71,6 +85,9 @@ const useExpandable = (options) => {
...(enableExpandingRow
? {
tableProps: {
...(!options.enableTreeView
? { canCollapseAll: options.canCollapseAll !== false }
: {}),
onCollapse,
},
}
Expand Down
61 changes: 60 additions & 1 deletion src/hooks/useExpandable/useExpandable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('useExpandable', () => {

expect(result.current).toEqual({
tableProps: {
canCollapseAll: true,
onCollapse: expect.any(Function),
},
tableView: {
Expand All @@ -45,7 +46,7 @@ describe('useExpandable', () => {
);

act(() => {
result.current.tableProps.onCollapse(undefined, undefined, undefined, {
result.current.tableProps.onCollapse(null, 0, true, {
item: { itemId },
});
});
Expand Down Expand Up @@ -104,6 +105,64 @@ describe('useExpandable', () => {
expect(result.current.tableView.isItemOpen('test-id')).toBe(false);
});

it('should expand all rows', () => {
const items = [
{ itemId: 'item-1' },
{ itemId: 'item-2' },
{ itemId: 'item-3' },
];
const { result } = renderHook(
() => useExpandable({ ...defaultOptions, items }),
DEFAULT_RENDER_OPTIONS,
);

act(() => {
result.current.tableProps.onCollapse(null, undefined, true);
});

expect(result.current.tableView.isItemOpen('item-1')).toBe(true);
expect(result.current.tableView.isItemOpen('item-2')).toBe(true);
expect(result.current.tableView.isItemOpen('item-3')).toBe(true);
});

it('should collapse all rows', () => {
const items = [{ itemId: 'item-1' }, { itemId: 'item-2' }];
const { result } = renderHook(
() => useExpandable({ ...defaultOptions, items }),
DEFAULT_RENDER_OPTIONS,
);

// expand all first
act(() => {
result.current.tableProps.onCollapse(null, undefined, true);
});

expect(result.current.tableView.isItemOpen('item-1')).toBe(true);
expect(result.current.tableView.isItemOpen('item-2')).toBe(true);

// collapse all
act(() => {
result.current.tableProps.onCollapse(null, undefined, false);
});

expect(result.current.tableView.isItemOpen('item-1')).toBe(false);
expect(result.current.tableView.isItemOpen('item-2')).toBe(false);
});

it('should not pass canCollapseAll when enableTreeView is set', () => {
const { result } = renderHook(
() =>
useExpandable({
...defaultOptions,
enableTreeView: true,
}),
DEFAULT_RENDER_OPTIONS,
);

expect(result.current.tableProps).not.toHaveProperty('canCollapseAll');
expect(result.current.tableProps).toHaveProperty('onCollapse');
});

it('includes control columns in details row colSpan', () => {
const { result } = renderHook(
() =>
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useTableTools/useTableTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const useTableTools = (
const {
tableProps: expandableTableProps,
tableView: expandableTableViewOptions,
} = useExpandable(options);
} = useExpandable({ ...options, items });

const { tableProps: radioSelectTableProps } = useRadioSelect({
...options,
Expand Down
Loading