Add autokeys

This commit is contained in:
Фролов 2021-08-19 11:40:26 +05:00
parent c90e3bde42
commit 240885cf93

View File

@ -37,6 +37,14 @@ const EditableCell = ({
</td>) </td>)
} }
const tryAddKeys = (items) => {
if(!items?.length)
return []
if(items[0].key)
return items
return items.map((item, index) => ({...item, key: item.key ?? item.id ?? index }))
}
export const EditableTable = ({ export const EditableTable = ({
columns, columns,
dataSource, dataSource,
@ -47,11 +55,11 @@ export const EditableTable = ({
...otherTableProps}) => { ...otherTableProps}) => {
const [form] = Form.useForm() const [form] = Form.useForm()
const [data, setData] = useState(dataSource?? []) const [data, setData] = useState(tryAddKeys(dataSource))
const [editingKey, setEditingKey] = useState('') const [editingKey, setEditingKey] = useState('')
useEffect(()=>{ useEffect(()=>{
setData(dataSource??[]) setData(tryAddKeys(dataSource))
},[dataSource]) },[dataSource])
const isEditing = (record) => record.key === editingKey const isEditing = (record) => record.key === editingKey
@ -168,6 +176,7 @@ export const EditableTable = ({
editing: isEditing(record), editing: isEditing(record),
record, record,
dataIndex: col.dataIndex ?? col.key, dataIndex: col.dataIndex ?? col.key,
key: col.key ?? col.dataIndex,
input: col.input, input: col.input,
isRequired: col.isRequired, isRequired: col.isRequired,
title: col.title, title: col.title,
@ -180,7 +189,7 @@ export const EditableTable = ({
} }
const mergedColumns = [...columns.map(handleColumn), operationColumn] const mergedColumns = [...columns.map(handleColumn), operationColumn]
return ( return (
<Form form={form} component={false}> <Form form={form} component={false}>
<Table <Table