Skip to content

Commit c4acda9

Browse files
committed
fix empty not show when not columns
1 parent 1af97cd commit c4acda9

File tree

4 files changed

+84
-15
lines changed

4 files changed

+84
-15
lines changed

examples/debug.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ const Demo = () => {
120120
}
121121
}, []);
122122

123-
const [_, forceUpdate] = React.useState();
123+
const [, forceUpdate] = React.useState();
124124

125125
return (
126126
<div>
127-
<h2>simple table</h2>
127+
<h2>Debug Usage, remove after stable released</h2>
128128
<button type="button" onClick={addData}>
129129
Add Row
130130
</button>
@@ -148,6 +148,8 @@ const Demo = () => {
148148
<Column dataIndex="date" colSpan={0} />
149149
</Table>
150150

151+
<Table<RecordType> />
152+
151153
<button
152154
type="button"
153155
onClick={() => {

src/hooks/useColumns.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,23 @@ function useColumns<RecordType>(
151151
return baseColumns;
152152
}, [expandable, baseColumns, getRowKey, expandedKeys, expandIcon]);
153153

154-
const mergedColumns = React.useMemo(
155-
() => (transformColumns ? transformColumns(withExpandColumns) : withExpandColumns),
156-
[transformColumns, withExpandColumns],
157-
);
154+
const mergedColumns = React.useMemo(() => {
155+
let finalColumns = withExpandColumns;
156+
if (transformColumns) {
157+
finalColumns = transformColumns(finalColumns);
158+
}
159+
160+
// Always provides at least one column for table display
161+
if (!finalColumns.length) {
162+
finalColumns = [
163+
{
164+
render: () => null,
165+
},
166+
];
167+
}
168+
169+
return finalColumns;
170+
}, [transformColumns, withExpandColumns]);
158171

159172
const flattenColumns = React.useMemo(() => flatColumns(mergedColumns), [mergedColumns]);
160173

tests/Table.spec.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ describe('Table.Basic', () => {
1111
return <Table columns={columns} data={data} {...props} />;
1212
};
1313

14-
it('renders correctly', () => {
15-
const wrapper = mount(
16-
createTable({
17-
prefixCls: 'test-prefix',
18-
className: 'test-class-name',
19-
}),
20-
);
21-
expect(wrapper.render()).toMatchSnapshot();
14+
describe('renders correctly', () => {
15+
it('basic', () => {
16+
const wrapper = mount(
17+
createTable({
18+
prefixCls: 'test-prefix',
19+
className: 'test-class-name',
20+
}),
21+
);
22+
expect(wrapper.render()).toMatchSnapshot();
23+
});
24+
25+
it('no columns', () => {
26+
const wrapper = mount(createTable({ columns: [] }));
27+
expect(wrapper.render()).toMatchSnapshot();
28+
});
2229
});
2330

2431
it('renders empty text correctly', () => {

tests/__snapshots__/Table.spec.js.snap

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ exports[`Table.Basic renders colSpan correctly 1`] = `
308308
</div>
309309
`;
310310

311-
exports[`Table.Basic renders correctly 1`] = `
311+
exports[`Table.Basic renders correctly basic 1`] = `
312312
<div
313313
class="test-prefix test-class-name"
314314
>
@@ -361,6 +361,53 @@ exports[`Table.Basic renders correctly 1`] = `
361361
</div>
362362
`;
363363

364+
exports[`Table.Basic renders correctly no columns 1`] = `
365+
<div
366+
class="rc-table"
367+
>
368+
<div
369+
class="rc-table-container"
370+
>
371+
<div
372+
class="rc-table-content"
373+
>
374+
<table
375+
style="table-layout: auto;"
376+
>
377+
<colgroup>
378+
<col />
379+
</colgroup>
380+
<thead>
381+
<tr>
382+
<th
383+
class="rc-table-cell"
384+
/>
385+
</tr>
386+
</thead>
387+
<tbody
388+
class="rc-table-tbody"
389+
>
390+
<tr
391+
class="rc-table-row rc-table-row-level-0"
392+
>
393+
<td
394+
class="rc-table-cell"
395+
/>
396+
</tr>
397+
<tr
398+
class="rc-table-row rc-table-row-level-0"
399+
>
400+
<td
401+
class="rc-table-cell"
402+
/>
403+
</tr>
404+
</tbody>
405+
</table>
406+
</div>
407+
</div>
408+
</div>
409+
`;
410+
364411
exports[`Table.Basic renders rowSpan correctly 1`] = `
365412
<div
366413
class="rc-table"

0 commit comments

Comments
 (0)