Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(design): Table locale & BatchOperationBar keys should related rowSelection #591

Merged
merged 5 commits into from
May 23, 2024
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
14 changes: 14 additions & 0 deletions packages/design/src/table/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ describe('Table', () => {
expect(asFragment().firstChild).toMatchSnapshot();
});

it('row selection should work', () => {
const { container, asFragment } = render(
<TableTest
rowSelection={{
selectedRowKeys: ['1', '2'],
}}
/>
);
// selection column
expect(container.querySelector('.ant-table-selection-column')).toBeTruthy();
// batch operation bar
expect(container.querySelector('.ant-table-batch-operation-bar')).toBeTruthy();
});

it('default pagination should work', () => {
const { container, asFragment } = render(<TableTest />);
// pagination.showTotal
Expand Down
2 changes: 1 addition & 1 deletion packages/design/src/table/demo/row-selection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, Table } from '@oceanbase/design';
import React, { useState } from 'react';

const App: React.FC = () => {
const [selectedRowKeys, setSelectedRowKeys] = useState<any[]>([]);
const [selectedRowKeys, setSelectedRowKeys] = useState<any[]>(['4', '5']);
const dataSource = [
{
key: '1',
Expand Down
28 changes: 23 additions & 5 deletions packages/design/src/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import type { ReactElement, ReactNode } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import ConfigProvider from '../config-provider';
import Typography from '../typography';
import enUS from '../locale/en-US';
import useStyle from './style';
import type { AnyObject } from '../_util/type';
import useDefaultPagination from './hooks/useDefaultPagination';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import enUS from '../locale/en-US';

export * from 'antd/es/table';

Expand All @@ -29,6 +30,9 @@ export interface TableLocale extends AntTableLocale {

export interface TableProps<T> extends AntTableProps<T> {
columns?: ColumnsType<T>;
cancelText?: string;
collapseText?: string;
openText?: string;
hiddenCancelBtn?: boolean;
toolOptionsRender?: (selectedRowKeys, selectedRows) => ReactNode[];
toolAlertRender?: false | ((selectedRowKeys, selectedRows) => ReactNode);
Expand All @@ -45,6 +49,9 @@ function Table<T>(props: TableProps<T>, ref: React.Ref<Reference>) {
toolAlertRender,
toolOptionsRender,
toolSelectedContent,
cancelText,
collapseText,
openText,
expandable,
hiddenCancelBtn = false,
prefixCls: customizePrefixCls,
Expand All @@ -53,15 +60,18 @@ function Table<T>(props: TableProps<T>, ref: React.Ref<Reference>) {
const extendedContext = useContext(ConfigProvider.ExtendedConfigContext);
const pagination = useDefaultPagination(customPagination);

const { getPrefixCls, locale, table } = useContext(ConfigProvider.ConfigContext);
const { batchOperationBar, ...restLocale } = {
...enUS.Table,
...locale?.Table,
...customLocale,
batchOperationBar: {
...enUS.Table?.batchOperationBar,
...locale?.Table?.batchOperationBar,
...customLocale?.batchOperationBar,
},
};

const { getPrefixCls, table } = useContext(ConfigProvider.ConfigContext);
const prefixCls = getPrefixCls('table', customizePrefixCls);
const { wrapSSR } = useStyle(prefixCls);
const tableCls = classNames(
Expand All @@ -72,7 +82,11 @@ function Table<T>(props: TableProps<T>, ref: React.Ref<Reference>) {
);

const [openPopover, setOpenPopover] = useState<boolean>(false);
const [currentSelectedRowKeys, setCurrentSelectedRowKeys] = useState<any[]>();
const [currentSelectedRowKeys, setCurrentSelectedRowKeys] = useMergedState([], {
value: rowSelection?.selectedRowKeys,
defaultValue: rowSelection?.defaultSelectedRowKeys,
});

const [currentSelectedRows, setCurrentSelectedRows] = useState<any[]>([]);
const [currentSelectedInfo, setCurrentSelectedInfo] = useState<any>({});

Expand Down Expand Up @@ -157,7 +171,9 @@ function Table<T>(props: TableProps<T>, ref: React.Ref<Reference>) {
<span>{batchOperationBar?.object}</span>
</span>
)}
{!hiddenCancelBtn && <a onClick={handleOptionsCancel}>{batchOperationBar?.cancel}</a>}
{!hiddenCancelBtn && (
<a onClick={handleOptionsCancel}>{cancelText ?? batchOperationBar?.cancel}</a>
)}
{toolSelectedContent && (
<Popover
placement="top"
Expand All @@ -167,7 +183,9 @@ function Table<T>(props: TableProps<T>, ref: React.Ref<Reference>) {
open={openPopover}
>
<a onClick={() => setOpenPopover(!openPopover)}>
{openPopover ? batchOperationBar?.collapse : batchOperationBar?.open}
{openPopover
? collapseText ?? batchOperationBar?.collapse
: openText ?? batchOperationBar?.open}
</a>
</Popover>
)}
Expand Down
Loading