Skip to content

Commit

Permalink
Merge pull request #221 from Mezzanine-UI/feat/table-rowselection-dis…
Browse files Browse the repository at this point in the history
…abled

Feature: Table `props.rowSelection.disabledRowKeys`
  • Loading branch information
travor20814 authored Apr 12, 2024
2 parents 84d9e2c + b09ae73 commit 6c80b35
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/core/src/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export interface TableRowSelection {
selectedRowKey?: string[];
onChange?(keys: string[]): void;
actions?: TableRowAction[];
disabledRowKeys?: string[];
}

/** === Feature draggable */
Expand Down
5 changes: 5 additions & 0 deletions packages/react/src/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ export const Selections = () => {
rowSelection={{
selectedRowKey: selectedRowKeys,
onChange: (keys) => setSelectedRowKeys(keys),
disabledRowKeys: dataSource.map((d) => {
if (d.age >= 25) return d.key;

return null;
}).filter((k) => k),
}}
/>
</div>
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,14 @@ const Table = forwardRef<HTMLTableElement, TableProps<Record<string, unknown>>>(
selectedRowKey: rowSelectionProp?.selectedRowKey,
onChange: rowSelectionProp?.onChange,
dataSource: dataSourceProp,
disabledRowKeys: rowSelectionProp?.disabledRowKeys,
});

const rowSelection = useMemo(() => (rowSelectionProp ? {
selectedRowKeys,
onChange: setSelectedRowKey,
actions: rowSelectionProp.actions,
disabledRowKeys: rowSelectionProp.disabledRowKeys,
} : undefined), [
rowSelectionProp,
selectedRowKeys,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/Table/TableContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EmptyProps } from '../Empty';
export interface RowSelectionContext extends Pick<TableRowSelection, 'actions'> {
selectedRowKeys: string[];
onChange(v: string): void;
disabledRowKeys?: string[];
}

/** typeof sorting */
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/Table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export {
export {
useTableDraggable,
} from './draggable/useTableDraggable';
export {
useTableRowSelection,
SELECTED_ALL_KEY,
} from './rowSelection/useTableRowSelection';
export {
default as useTableScroll,
} from './useTableScroll';
15 changes: 12 additions & 3 deletions packages/react/src/Table/rowSelection/TableRowSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
tableClasses as classes,
TableRowAction,
} from '@mezzanine-ui/core/table';
import xor from 'lodash/xor';
import { MoreVerticalIcon } from '@mezzanine-ui/icons';
import { TableContext, TableDataContext, RowSelectionContext } from '../TableContext';
import { NativeElementPropsWithoutKeyAndRef } from '../../utils/jsx-types';
Expand Down Expand Up @@ -64,10 +65,16 @@ const TableRowSelection = forwardRef<HTMLDivElement, TableRowSelectionProps>(
const selectedRowKeys = rowSelection?.selectedRowKeys ?? [];

if (!selectedRowKeys.length) return 'none';
if (selectedRowKeys.length === dataSource.length) return 'all';

const validDataSource = xor(
rowSelection?.disabledRowKeys ?? [],
dataSource.map((d) => (d.id || d.key) as string),
);

if (selectedRowKeys.length === validDataSource.length) return 'all';

return 'indeterminate';
}, [rowSelection?.selectedRowKeys, dataSource.length]);
}, [rowSelection?.selectedRowKeys, rowSelection?.disabledRowKeys, dataSource]);

const selfChecked = useMemo(() => (
(rowSelection?.selectedRowKeys ?? []).some((key) => key === rowKey)
Expand Down Expand Up @@ -138,7 +145,9 @@ const TableRowSelection = forwardRef<HTMLDivElement, TableRowSelectionProps>(
>
<Checkbox
checked={checked}
disabled={false}
disabled={rowSelection?.disabledRowKeys?.length
? rowSelection.disabledRowKeys.includes(rowKey)
: false}
indeterminate={indeterminate}
inputProps={{
name,
Expand Down
7 changes: 5 additions & 2 deletions packages/react/src/Table/rowSelection/useTableRowSelection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TableRowSelection, TableDataSource } from '@mezzanine-ui/core/table';
import xor from 'lodash/xor';
import { useControlValueState } from '../../Form/useControlValueState';
import { useLastCallback } from '../../hooks/useLastCallback';

Expand All @@ -19,6 +20,7 @@ export function useTableRowSelection(props: UseTableRowSelection) {
const {
selectedRowKey: selectedRowKeyProp,
onChange: onChangeProp,
disabledRowKeys = [],
dataSource,
} = props;

Expand All @@ -32,13 +34,14 @@ export function useTableRowSelection(props: UseTableRowSelection) {
if (!dataSource.length) return;

const allSourceKeys = dataSource.map((source) => (source.key || source.id) as string);
const validSourceKeys = xor(disabledRowKeys, allSourceKeys);
let nextSelectedRowKey = selectedRowKey;

if (rowKey === SELECTED_ALL_KEY) {
if (equalityFn(selectedRowKey, allSourceKeys)) {
if (equalityFn(selectedRowKey, validSourceKeys)) {
nextSelectedRowKey = [];
} else {
nextSelectedRowKey = allSourceKeys;
nextSelectedRowKey = validSourceKeys;
}
} else {
const existedRowKeyIdx = selectedRowKey.findIndex((key) => key === rowKey);
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ export {
TableRefresh,
useTableDraggable,
useTableScroll,
useTableRowSelection,
SELECTED_ALL_KEY,
default as Table,
} from './Table';
export {
Expand Down

0 comments on commit 6c80b35

Please sign in to comment.