diff --git a/components/table/Table.tsx b/components/table/Table.tsx index be99cdb5ee00..903765027fc2 100644 --- a/components/table/Table.tsx +++ b/components/table/Table.tsx @@ -162,7 +162,7 @@ function Table(props: TableProps) { const { childrenColumnName = 'children' } = mergedExpandable; const expandType: ExpandType = React.useMemo(() => { - if (rawData.some(item => (item as any)[childrenColumnName])) { + if (rawData.some(item => (item as any)?.[childrenColumnName])) { return 'nest'; } @@ -183,7 +183,7 @@ function Table(props: TableProps) { 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); diff --git a/components/table/__tests__/Table.test.js b/components/table/__tests__/Table.test.js index 6d0e55d3669e..8c4988e43121 100644 --- a/components/table/__tests__/Table.test.js +++ b/components/table/__tests__/Table.test.js @@ -160,6 +160,19 @@ describe('Table', () => { ); }); + it('should not crash when dataSource is array with none-object items', () => { + mount( + , + ); + }); + it('prevent touch event', () => { const wrapper = mount(
( 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] || []); } }); diff --git a/components/table/hooks/useSelection.tsx b/components/table/hooks/useSelection.tsx index 2136201764ed..f87193f6b7a6 100644 --- a/components/table/hooks/useSelection.tsx +++ b/components/table/hooks/useSelection.tsx @@ -59,7 +59,7 @@ function flattenData( (data || []).forEach(record => { list.push(record); - if (childrenColumnName in record) { + if (record && typeof record === 'object' && childrenColumnName in record) { list = [ ...list, ...flattenData((record as any)[childrenColumnName], childrenColumnName),