Skip to content

Commit

Permalink
feat(table): add row selection tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
lihang committed May 25, 2021
1 parent 014cc31 commit 297d8c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/components/table/demo/TablePagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const TablePagination: Story<TableProps<any> & PaginationProps> = (args)
rowSelection={{
getCheckboxProps: (record: any) => ({
disabled: record.a === 1,
title: record.a
}),
}}
{...rest}
Expand Down
31 changes: 19 additions & 12 deletions src/components/table/hook/useSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useMemo, useCallback } from 'react';
import { get, intersection, isUndefined, difference, union, isFunction, isString, compact } from 'lodash';
import { ColumnsType, RowSelection, ColumnType } from '../interface';
import Checkbox from '../../checkbox';
import Tooltip from '../../tooltip';
import useControlledState from '../../../utils/hooks/useControlledState';

export const getRowKey = <RecordType,>(row: RecordType, rowKey?: string | ((record: RecordType) => string)): string => {
Expand Down Expand Up @@ -85,19 +86,25 @@ const useSelection = <RecordType,>(
render: (...rest) => {
const key = getRowKey(rest[1], rowKey);
const thisCheckboxProps = getCheckboxProps?.(rest[1]) || {};
const { title, disabled, ...restCheckboxProps } = thisCheckboxProps;
return (
<Checkbox
{...thisCheckboxProps}
checked={localSelectedRowKeys.includes(key)}
onClick={(e) => e.stopPropagation()}
onChange={(e) => {
const latestLocalSelectedRowKeys = e.target.checked
? union(localSelectedRowKeys, [key])
: difference(localSelectedRowKeys, [key]);
setLocalSelectedRowKeys(latestLocalSelectedRowKeys);
onChange?.(latestLocalSelectedRowKeys, getSelectRows(latestLocalSelectedRowKeys));
}}
/>
<Tooltip placement='topLeft' arrowPointAtCenter title={title} disabled={!disabled}>
<div>
<Checkbox
{...restCheckboxProps}
disabled={disabled}
checked={localSelectedRowKeys.includes(key)}
onClick={(e) => e.stopPropagation()}
onChange={(e) => {
const latestLocalSelectedRowKeys = e.target.checked
? union(localSelectedRowKeys, [key])
: difference(localSelectedRowKeys, [key]);
setLocalSelectedRowKeys(latestLocalSelectedRowKeys);
onChange?.(latestLocalSelectedRowKeys, getSelectRows(latestLocalSelectedRowKeys));
}}
>&nbsp;</Checkbox>
</div>
</Tooltip>
);
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/table/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export interface RowSelection<RecordType> {
columnWidth?: number | string;
fixed?: 'left' | 'right' | boolean;
onChange?: (selectedRowKeys: string[], selectedRows: RecordType[]) => void;
getCheckboxProps?: (record: RecordType) => CheckboxProps;
getCheckboxProps?: (record: RecordType) => CheckboxProps & { title?: React.ReactNode };
}

export interface TableProps<RecordType> {
Expand Down

0 comments on commit 297d8c2

Please sign in to comment.