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

perf: make column support jsx conditions (#40568) #954

Merged
merged 2 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 26 additions & 23 deletions src/hooks/useColumns.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as React from 'react';
import warning from 'rc-util/lib/warning';
import toArray from 'rc-util/lib/Children/toArray';
import warning from 'rc-util/lib/warning';
import * as React from 'react';
import { EXPAND_COLUMN } from '../constant';
import type {
ColumnGroupType,
ColumnsType,
ColumnType,
Direction,
FixedType,
Key,
GetRowKey,
TriggerEventHandler,
Key,
RenderExpandIcon,
ColumnGroupType,
Direction,
TriggerEventHandler,
} from '../interface';
import { INTERNAL_COL_DEFINE } from '../utils/legacyUtil';
import { EXPAND_COLUMN } from '../constant';

export function convertChildrenToColumns<RecordType>(
children: React.ReactNode,
Expand All @@ -37,28 +37,31 @@ export function convertChildrenToColumns<RecordType>(

function flatColumns<RecordType>(columns: ColumnsType<RecordType>): ColumnType<RecordType>[] {
return columns.reduce((list, column) => {
const { fixed } = column;
if (column) {
zombieJ marked this conversation as resolved.
Show resolved Hide resolved
const { fixed } = column;
// Convert `fixed='true'` to `fixed='left'` instead
const parsedFixed = fixed === true ? 'left' : fixed;

// Convert `fixed='true'` to `fixed='left'` instead
const parsedFixed = fixed === true ? 'left' : fixed;

const subColumns = (column as ColumnGroupType<RecordType>).children;
if (subColumns && subColumns.length > 0) {
const subColumns = (column as ColumnGroupType<RecordType>).children;
if (subColumns && subColumns.length > 0) {
return [
...list,
...flatColumns(subColumns).map(subColum => ({
fixed: parsedFixed,
...subColum,
})),
];
}
return [
...list,
...flatColumns(subColumns).map(subColum => ({
{
...column,
fixed: parsedFixed,
...subColum,
})),
},
];
} else {
return [...list];
}
return [
...list,
{
...column,
fixed: parsedFixed,
},
];
}, []);
}

Expand Down
45 changes: 45 additions & 0 deletions tests/Table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,4 +1153,49 @@ describe('Table.Basic', () => {
expect(wrapper.render()).toMatchSnapshot();
expect(wrapper.find('col')).toHaveLength(tColumns.length + 1);
});
it('columns support JSX condition', () => {
const Example = () => {
const [count, setCount] = React.useState(0);
const columns = [
{
title: 'title',
dataIndex: 'a',
render: () => count,
},
count === 1 && {
title: 'title2',
dataIndex: 'b',
render: () => count + 1,
},
count === 2
? {
title: 'title3',
dataIndex: 'c',
render: () => count + 1,
}
: null,
];
return (
<>
<button
onClick={() => {
setCount(val => val + 1);
}}
>
Click {count} times
</button>
<Table columns={columns} data={data} />
</>
);
};
const wrapper = mount(<Example />);

wrapper.find('button').simulate('click');
expect(wrapper.find('.rc-table-cell').at(1).text()).toEqual('title2');

wrapper.find('button').simulate('click');
expect(wrapper.find('.rc-table-cell').at(1).text()).toEqual('title3');

expect(wrapper.render()).toMatchSnapshot();
});
});
83 changes: 77 additions & 6 deletions tests/__snapshots__/Table.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,82 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Table.Basic columns support JSX condition 1`] = `
[
<button>
Click 2 times
</button>,
<div
class="rc-table"
>
<div
class="rc-table-container"
>
<div
class="rc-table-content"
>
<table
style="table-layout: auto;"
>
<colgroup />
<thead
class="rc-table-thead"
>
<tr>
<th
class="rc-table-cell"
scope="col"
>
title
</th>
<th
class="rc-table-cell"
scope="col"
>
title3
</th>
</tr>
</thead>
<tbody
class="rc-table-tbody"
>
<tr
class="rc-table-row rc-table-row-level-0"
data-row-key="key0"
>
<td
class="rc-table-cell"
>
2
</td>
<td
class="rc-table-cell"
>
3
</td>
</tr>
<tr
class="rc-table-row rc-table-row-level-0"
data-row-key="key1"
>
<td
class="rc-table-cell"
>
2
</td>
<td
class="rc-table-cell"
>
3
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>,
]
`;

exports[`Table.Basic custom components renders correctly 1`] = `
<div
class="rc-table"
Expand Down Expand Up @@ -609,9 +686,6 @@ exports[`Table.Basic renders correctly falsy columns 1`] = `
>
Lucy
</td>
<td
class="rc-table-cell"
/>
</tr>
<tr
class="rc-table-row rc-table-row-level-0"
Expand All @@ -622,9 +696,6 @@ exports[`Table.Basic renders correctly falsy columns 1`] = `
>
Jack
</td>
<td
class="rc-table-cell"
/>
</tr>
</tbody>
</table>
Expand Down