Skip to content
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
53 changes: 27 additions & 26 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 @@ -36,30 +36,31 @@ export function convertChildrenToColumns<RecordType>(
}

function flatColumns<RecordType>(columns: ColumnsType<RecordType>): ColumnType<RecordType>[] {
return columns.reduce((list, column) => {
const { fixed } = column;

// Convert `fixed='true'` to `fixed='left'` instead
const parsedFixed = fixed === true ? 'left' : fixed;
return columns
.filter(column => column && typeof column === 'object')
.reduce((list, column) => {
const { fixed } = column;
// 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,
})),
},
];
}
return [
...list,
{
...column,
fixed: parsedFixed,
},
];
}, []);
}, []);
}

function warningFixed(flattenColumns: readonly { fixed?: FixedType }[]) {
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