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): editable table support setting colKey to be like a.b.c #2381

Merged
merged 1 commit into from
Jul 21, 2023
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
21 changes: 13 additions & 8 deletions src/table/EditableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useMemo, useRef, useState, MouseEvent } from 'react';
import get from 'lodash/get';
import set from 'lodash/set';
import isFunction from 'lodash/isFunction';
import cloneDeep from 'lodash/cloneDeep';
import { Edit1Icon as TdEdit1Icon } from 'tdesign-icons-react';
import classNames from 'classnames';
import {
Expand Down Expand Up @@ -47,8 +48,16 @@ const EditableCell = (props: EditableCellProps) => {
const { classPrefix } = useConfig();

const getCurrentRow = (row: TableRowData, colKey: string, value: any) => {
if (!colKey) return row;
// handle colKey like a.b.c
const [firstKey, ...restKeys] = colKey.split('.') || [];
const newRow = { ...row };
set(newRow, colKey, value);
if (restKeys.length) {
newRow[firstKey] = cloneDeep(row[firstKey]);
set(newRow[firstKey], restKeys.join('.'), value);
} else {
set(newRow, colKey, value);
}
return newRow;
};

Expand All @@ -62,6 +71,8 @@ const EditableCell = (props: EditableCellProps) => {
[col, row, colIndex, rowIndex],
);

const cellValue = useMemo(() => get(row, col.colKey), [row, col.colKey]);

// eslint-disable-next-line react-hooks/exhaustive-deps
const currentRow = useMemo(() => getCurrentRow(row, col.colKey, editValue), [col.colKey, editValue, row]);

Expand Down Expand Up @@ -238,14 +249,8 @@ const EditableCell = (props: EditableCellProps) => {
});
};

const cellValue = useMemo(() => get(row, col.colKey), [row, col.colKey]);

useEffect(() => {
let val = cellValue;
if (typeof val === 'object' && val !== null) {
val = val instanceof Array ? [...val] : { ...val };
}
setEditValue(val);
setEditValue(cellValue);
}, [cellValue]);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/table/hooks/useEditableRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function useEditableRow(props: PrimaryTableProps) {
resolve({ ...item, errorList: [] });
return;
}
validate(editedRow[col.colKey], rules).then((r) => {
validate(get(editedRow, col.colKey), rules).then((r) => {
resolve({ ...item, errorList: r.filter((t) => !t.result) });
});
}),
Expand Down
Loading