fix: table do not have unique key (#1512)

This commit is contained in:
Yaodong Yu 2023-02-02 13:53:18 +08:00 committed by GitHub
parent 11b56c340f
commit b34e16b145
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 26 deletions

View File

@ -25,11 +25,27 @@ class ManagedAccountTable extends React.Component {
super(props);
this.state = {
classes: props,
managedAccounts: [],
};
this.state.managedAccounts = this.props.table.map((item, index) => {
item.key = index;
return item;
});
}
count = this.props.table.length;
updateTable(table) {
this.props.onUpdateTable(table);
this.setState({
managedAccounts: table,
});
this.props.onUpdateTable([...table].map((item) => {
const newItem = Setting.deepCopy(item);
delete newItem.key;
return newItem;
}));
}
updateField(table, index, key, value) {
@ -38,10 +54,12 @@ class ManagedAccountTable extends React.Component {
}
addRow(table) {
const row = {application: "", username: "", password: ""};
const row = {key: this.count, application: "", username: "", password: ""};
if (table === undefined || table === null) {
table = [];
}
this.count += 1;
table = Setting.addRow(table, row);
this.updateTable(table);
}
@ -131,7 +149,7 @@ class ManagedAccountTable extends React.Component {
];
return (
<Table scroll={{x: "max-content"}} rowKey="name" columns={columns} dataSource={table} size="middle" bordered pagination={false}
<Table scroll={{x: "max-content"}} rowKey="key" columns={columns} dataSource={table} size="middle" bordered pagination={false}
title={() => (
<div>
{this.props.title}&nbsp;&nbsp;&nbsp;&nbsp;
@ -148,7 +166,7 @@ class ManagedAccountTable extends React.Component {
<Row style={{marginTop: "20px"}} >
<Col span={24}>
{
this.renderTable(this.props.table)
this.renderTable(this.state.managedAccounts)
}
</Col>
</Row>

View File

@ -571,7 +571,7 @@ class UserEditPage extends React.Component {
<Col span={22} >
<ManagedAccountTable
title={i18next.t("user:Managed accounts")}
table={this.state.user.managedAccounts ?? []}
table={this.state.user.managedAccounts}
onUpdateTable={(table) => {this.updateUserField("managedAccounts", table);}}
applications={this.state.applications}
/>

View File

@ -28,9 +28,18 @@ class PolicyTable extends React.Component {
editingIndex: "",
oldPolicy: "",
add: false,
page: 1,
};
}
count = 0;
pageSize = 10;
getIndex(index) {
// Need to be used in all place when modify table. Parameter is the row index in table, need to calculate the index in dataSource.
return index + (this.state.page - 1) * 10;
}
UNSAFE_componentWillMount() {
if (this.props.mode === "edit") {
this.synPolicies();
@ -46,8 +55,8 @@ class PolicyTable extends React.Component {
};
cancel = (table, index) => {
Object.keys(table[index]).forEach((key) => {
table[index][key] = this.state.oldPolicy[key];
Object.keys(table[this.getIndex(index)]).forEach((key) => {
table[this.getIndex(index)][key] = this.state.oldPolicy[key];
});
this.updateTable(table);
this.setState({editingIndex: "", oldPolicy: ""});
@ -62,23 +71,28 @@ class PolicyTable extends React.Component {
}
updateField(table, index, key, value) {
table[index][key] = value;
table[this.getIndex(index)][key] = value;
this.updateTable(table);
}
addRow(table) {
const row = {Ptype: "p"};
const row = {key: this.count, Ptype: "p"};
if (table === undefined) {
table = [];
}
table = Setting.addRow(table, row, "top");
this.count = this.count + 1;
this.updateTable(table);
this.edit(row, 0);
this.setState({add: true});
this.setState({
page: 1,
add: true,
});
}
deleteRow(table, i) {
table = Setting.deleteRow(table, i);
deleteRow(table, index) {
table = Setting.deleteRow(table, this.getIndex(index));
this.updateTable(table);
}
@ -91,8 +105,14 @@ class PolicyTable extends React.Component {
AdapterBackend.syncPolicies(this.props.owner, this.props.name)
.then((res) => {
if (res.status === "ok") {
this.setState({policyLists: res.data});
Setting.showMessage("success", i18next.t("adapter:Sync policies successfully"));
const policyList = res.data;
policyList.map((policy, index) => {
policy.key = index;
});
this.count = policyList.length;
this.setState({policyLists: policyList});
} else {
Setting.showMessage("error", `${i18next.t("adapter:Failed to sync policies")}: ${res.msg}`);
}
@ -129,12 +149,12 @@ class PolicyTable extends React.Component {
});
}
deletePolicy(table, i) {
AdapterBackend.RemovePolicy(this.props.owner, this.props.name, table[i]).then(res => {
deletePolicy(table, index) {
AdapterBackend.RemovePolicy(this.props.owner, this.props.name, table[this.getIndex(index)]).then(res => {
if (res.status === "ok") {
table = Setting.deleteRow(table, i);
this.updateTable(table);
Setting.showMessage("success", i18next.t("general:Successfully deleted"));
this.deleteRow(table, index);
} else {
Setting.showMessage("error", i18next.t("general:Failed to delete"));
}
@ -279,9 +299,14 @@ class PolicyTable extends React.Component {
return (
<Table
pagination={{
defaultPageSize: 10,
defaultPageSize: this.pageSize,
onChange: (page) => this.setState({
page: page,
}),
disabled: this.state.editingIndex !== "",
current: this.state.page,
}}
columns={columns} dataSource={table} rowKey="index" size="middle" bordered
columns={columns} dataSource={table} rowKey="key" size="middle" bordered
loading={this.state.loading}
title={() => (
<div>

View File

@ -23,18 +23,19 @@ class PropertyTable extends React.Component {
super(props);
this.state = {
properties: [],
count: this.props.properties !== null ? Object.entries(this.props.properties).length : 0,
};
// transfer the Object to object[]
if (this.props.properties !== null) {
Object.entries(this.props.properties).map((item, index) => {
this.state.properties.push({key: index, name: item[0], value: item[1]});
});
}
}
page = 1;
pageSize = 10;
count = this.props.properties !== null ? Object.entries(this.props.properties).length : 0;
updateTable(table) {
this.setState({properties: table});
@ -46,12 +47,12 @@ class PropertyTable extends React.Component {
}
addRow(table) {
const row = {key: this.state.count, name: "", value: ""};
const row = {key: this.count, name: "", value: ""};
if (table === undefined) {
table = [];
}
table = Setting.addRow(table, row);
this.setState({count: this.state.count + 1});
this.count = this.count + 1;
this.updateTable(table);
}
@ -61,8 +62,8 @@ class PropertyTable extends React.Component {
}
getIndex(index) {
// Parameter is the row index in table, need to calculate the index in dataSource. 10 is the pageSize.
return index + (this.page - 1) * 10;
// Need to be used in all place when modify table. Parameter is the row index in table, need to calculate the index in dataSource.
return index + (this.page - 1) * this.pageSize;
}
updateField(table, index, key, value) {
@ -114,7 +115,10 @@ class PropertyTable extends React.Component {
<Button style={{marginRight: "5px"}} type="primary" size="small" onClick={() => this.addRow(table)}>{i18next.t("general:Add")}</Button>
</div>
)}
pagination={{onChange: page => {this.page = page;}}}
pagination={{
defaultPageSize: this.pageSize,
onChange: page => {this.page = page;},
}}
columns={columns} dataSource={table} rowKey="key" size="middle" bordered
/>
);