Skip to content

Commit

Permalink
[INLONG-6511][Dashboard] The Select/AutoComplete component in Editabl…
Browse files Browse the repository at this point in the history
…eTable will mistakenly change the value and cause re-rendering (#6512)
  • Loading branch information
leezng authored and dockerzhang committed Nov 11, 2022
1 parent c1b1ce4 commit 8c81459
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions inlong-dashboard/src/components/EditableTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import React, { useEffect, useState } from 'react';
import { AutoComplete, Button, Table, Input, InputNumber, Form } from 'antd';
import { FormItemProps } from 'antd/lib/form';
import { TableProps } from 'antd/lib/table';
import { useTranslation } from 'react-i18next';
import HighSelect from '@/components/HighSelect';
import { useUpdateEffect } from '@/hooks';
Expand Down Expand Up @@ -51,12 +52,12 @@ export interface ColumnsItemProps {
visible?: (val: unknown, rowVal: RowValueType) => boolean | boolean;
}

export interface EditableTableProps {
export interface EditableTableProps
extends Omit<TableProps<any>, 'value' | 'onChange' | 'columns'> {
// id comes from FormItem, like name
id?: string;
value?: RowValueType[];
onChange?: (value: RowValueType[]) => void;
size?: string;
columns: ColumnsItemProps[];
// Can Edit(Can be changed to read-only)? Default: true.
editing?: boolean;
Expand Down Expand Up @@ -102,7 +103,7 @@ const Comp = ({
required = true,
canDelete = true,
canAdd = true,
size,
...rest
}: EditableTableProps) => {
if (!id) {
console.error(
Expand Down Expand Up @@ -243,15 +244,12 @@ const Comp = ({
// Use div to wrap input, select, etc. so that the value and onChange events are not taken over by FormItem
// So the actual value change must be changed by onChange itself and then exposed to the outer component
<Form.Item
rules={
id
? item.rules
: item.rules?.map(rule =>
typeof rule === 'function' ? rule : { ...rule, transform: () => text ?? '' },
)
}
rules={item.rules?.map(rule =>
typeof rule === 'function' ? rule : { ...rule, transform: () => text ?? '' },
)}
messageVariables={{ label: item.title }}
name={id ? [id, idx, item.dataIndex] : ['__proto__', 'editableRow', idx, item.dataIndex]}
// If the `name=[id, idx, item.dataIndex]` is used, the array value error will occur when the select/autocomplete input is entered, and the setValue will be automatically reset.
name={['__proto__', 'editableRow', idx, item.dataIndex]}
className={styles.formItem}
>
<div>{formCompObj[item.type || 'input']}</div>
Expand Down Expand Up @@ -279,10 +277,10 @@ const Comp = ({

return (
<Table
{...rest}
dataSource={data}
columns={tableColumns}
rowKey="_etid"
size={size as any}
footer={
editing && canAdd
? () => (
Expand Down

0 comments on commit 8c81459

Please sign in to comment.