Skip to content

Commit

Permalink
fix: row selection when grouping (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran McDade authored and Fran McDade committed Jan 1, 2025
1 parent 7b97ccd commit 8aef566
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
11 changes: 2 additions & 9 deletions src/components/Table/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,8 @@ function filterGroupedColumn<T extends RowData>(column: Column<T>): boolean {
* @returns true if a row is selected.
*/
export function isAnyRowSelected<T extends RowData>(table: Table<T>): boolean {
const {
getIsAllPageRowsSelected,
getIsSomePageRowsSelected,
options: { enableRowSelection },
} = table;
return Boolean(
enableRowSelection &&
(getIsSomePageRowsSelected() || getIsAllPageRowsSelected())
);
const { getIsAllPageRowsSelected, getIsSomePageRowsSelected } = table;
return getIsSomePageRowsSelected() || getIsAllPageRowsSelected();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/components/Table/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ TableProps<T>): JSX.Element => {

const onRowSelectionChange = useCallback(
(updater: Updater<RowSelectionState>): void => {
// TODO(cc) refactor `onRowSelectionChange` to /options/rowSelection/hook.ts see onGroupingChange.
exploreDispatch({
payload:
typeof updater === "function" ? updater(rowSelection) : updater,
Expand Down Expand Up @@ -198,7 +199,7 @@ TableProps<T>): JSX.Element => {
data: items,
enableColumnFilters: true, // client-side filtering.
enableFilters: true, // client-side filtering.
enableMultiSort: clientFiltering,
enableMultiSort: clientFiltering, // TODO(cc) move to sorting options; default to false and let the table options in config flag this value.
enableRowPreview,
enableRowSelection,
getCoreRowModel: getCoreRowModel(),
Expand Down
3 changes: 3 additions & 0 deletions src/components/TableCreator/options/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RowData, TableOptions } from "@tanstack/react-table";
import { useConfig } from "../../../hooks/useConfig";
import { useExpandedOptions } from "./expanded/hook";
import { useGroupingOptions } from "./grouping/hook";
import { useRowSelectionOptions } from "./rowSelection/hook";
import { useSortingOptions } from "./sorting/hook";

export function useTableOptions<T extends RowData>(): Partial<TableOptions<T>> {
Expand All @@ -12,10 +13,12 @@ export function useTableOptions<T extends RowData>(): Partial<TableOptions<T>> {
} = useConfig();
const expandedOptions = useExpandedOptions<T>();
const groupingOptions = useGroupingOptions();
const rowSelectionOptions = useRowSelectionOptions<T>();
const sortingOptions = useSortingOptions<T>();
return {
...expandedOptions,
...groupingOptions,
...rowSelectionOptions,
...sortingOptions, // TODO(cc) merge of all sorting options.
...tableOptions,
initialState: {
Expand Down
10 changes: 10 additions & 0 deletions src/components/TableCreator/options/rowSelection/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RowData, RowSelectionOptions } from "@tanstack/react-table";

export const ROW_SELECTION_OPTIONS: Pick<
RowSelectionOptions<RowData>,
"enableRowSelection" | "enableSubRowSelection" | "enableMultiRowSelection"
> = {
enableMultiRowSelection: false,
enableRowSelection: false,
enableSubRowSelection: false,
};
8 changes: 8 additions & 0 deletions src/components/TableCreator/options/rowSelection/hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RowData, RowSelectionOptions } from "@tanstack/react-table";
import { ROW_SELECTION_OPTIONS } from "./constants";

export function useRowSelectionOptions<
T extends RowData
>(): RowSelectionOptions<T> {
return { ...(ROW_SELECTION_OPTIONS as RowSelectionOptions<T>) };
}
1 change: 0 additions & 1 deletion src/config/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ export interface ListViewConfig {
disablePagination?: boolean;
enableDownload?: boolean;
enableRowPreview?: boolean;
enableRowSelection?: boolean;
enableTab?: boolean;
listHero?: ComponentsConfig;
rowPreviewView?: ComponentsConfig; // Row preview view is expected to be a modal or drawer or similar.
Expand Down
19 changes: 15 additions & 4 deletions src/providers/exploreState/initializer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,29 @@ function initColumnVisibility(entityConfig: EntityConfig): VisibilityState {
const {
list: {
columns,
tableOptions: { initialState: { columnVisibility = {} } = {} } = {},
tableOptions: {
enableRowSelection,
initialState: { columnVisibility = {} } = {},
} = {},
},
listView: { enableRowSelection = false } = {},
} = entityConfig;
return {
[ACCESSOR_KEYS.ROW_POSITION]: false, // Explicitly setting row position to false; required - currently `columnVisibility` is initialized from columns configuration.
[ACCESSOR_KEYS.SELECT]: enableRowSelection,
[ACCESSOR_KEYS.SELECT]: Boolean(enableRowSelection),
...getInitialTableColumnVisibility(columns),
...columnVisibility, // `columnVisibility` is managed by ExploreState; use table options to override this setting.
};
}

/**
* Returns the initial `enableRowSelection` option for the specified entity list configuration.
* @param entityConfig - Entity configuration.
* @returns initial `enableRowSelection` option.
*/
function initEnableRowSelection(entityConfig: EntityConfig): boolean {
return Boolean(entityConfig.list.tableOptions?.enableRowSelection);
}

/**
* Initializes entity page state.
* @param config - Site config.
Expand All @@ -177,7 +188,7 @@ function initEntityPageState(config: SiteConfig): EntityPageStateMapper {
[entity.route]: {
categoryGroupConfigKey: initCategoryGroupConfigKey(config, entity),
columnsVisibility: initColumnVisibility(entity),
enableRowSelection: Boolean(entity.listView?.enableRowSelection),
enableRowSelection: initEnableRowSelection(entity),
grouping: initGrouping(entity),
rowPreview: undefined,
rowSelection: {},
Expand Down

0 comments on commit 8aef566

Please sign in to comment.