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: Table crash when dataSource contains number #26042

Merged
merged 1 commit into from Aug 5, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/table/Table.tsx
Expand Up @@ -162,7 +162,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
const { childrenColumnName = 'children' } = mergedExpandable;

const expandType: ExpandType = React.useMemo<ExpandType>(() => {
if (rawData.some(item => (item as any)[childrenColumnName])) {
if (rawData.some(item => (item as any)?.[childrenColumnName])) {
return 'nest';
}

Expand All @@ -183,7 +183,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
return rowKey;
}

return (record: RecordType) => (record as any)[rowKey as string];
return (record: RecordType) => (record as any)?.[rowKey as string];
}, [rowKey]);

const [getRecordByKey] = useLazyKVMap(rawData, childrenColumnName, getRowKey);
Expand Down
13 changes: 13 additions & 0 deletions components/table/__tests__/Table.test.js
Expand Up @@ -160,6 +160,19 @@ describe('Table', () => {
);
});

it('should not crash when dataSource is array with none-object items', () => {
mount(
<Table
columns={[
{
title: 'name',
},
]}
dataSource={['1', 2, undefined, {}, null, true, false, 0]}
/>,
);
});

it('prevent touch event', () => {
const wrapper = mount(
<Table
Expand Down
2 changes: 1 addition & 1 deletion components/table/hooks/useLazyKVMap.ts
Expand Up @@ -30,7 +30,7 @@ export default function useLazyKVMap<RecordType>(
const rowKey = getRowKey(record, index);
kvMap.set(rowKey, record);

if (childrenColumnName in record) {
if (record && typeof record === 'object' && childrenColumnName in record) {
dig((record as any)[childrenColumnName] || []);
}
});
Expand Down
2 changes: 1 addition & 1 deletion components/table/hooks/useSelection.tsx
Expand Up @@ -59,7 +59,7 @@ function flattenData<RecordType>(
(data || []).forEach(record => {
list.push(record);

if (childrenColumnName in record) {
if (record && typeof record === 'object' && childrenColumnName in record) {
list = [
...list,
...flattenData<RecordType>((record as any)[childrenColumnName], childrenColumnName),
Expand Down