fix: 修复 inputTable 列中设置默认值无效的问题 (#2673)

* fix: 修复 inputTable 列中设置默认值无效的问题

* feat: inputTable 支持unique 唯一验证
This commit is contained in:
liaoxuezhi 2021-10-11 13:51:09 +08:00 committed by GitHub
parent 720c9e0378
commit f9dfdaa6a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 22 deletions

View File

@ -248,5 +248,6 @@ register('de-DE', {
'Condition.placeholder': 'Text einfügen',
'Condition.cond_placeholder': 'Bedingung auswählen',
'Condition.field_placeholder': 'Feld auswählen',
'Condition.blank': 'leer'
'Condition.blank': 'leer',
'InputTable.uniqueError': 'Column `{{label}}` unique validate failed'
});

View File

@ -249,5 +249,6 @@ register('en-US', {
'Condition.placeholder': 'Please enter text',
'Condition.cond_placeholder': 'select condition',
'Condition.field_placeholder': 'select field',
'Condition.blank': 'blank'
'Condition.blank': 'blank',
'InputTable.uniqueError': 'Column `{{label}}` unique validate failed'
});

View File

@ -253,5 +253,6 @@ register('zh-CN', {
'Condition.placeholder': '请输入文本',
'Condition.cond_placeholder': '请选择操作',
'Condition.field_placeholder': '请选择字段',
'Condition.blank': '空'
'Condition.blank': '空',
'InputTable.uniqueError': '列`{{label}}`没有通过唯一验证'
});

View File

@ -1,7 +1,13 @@
import React from 'react';
import {FormItem, FormControlProps, FormBaseControl} from './Item';
import Button from '../../components/Button';
import {createObject, getTree, spliceTree} from '../../utils/helper';
import {
createObject,
getTree,
getVariable,
setVariable,
spliceTree
} from '../../utils/helper';
import {RendererData, Action, Api, Payload, ApiObject} from '../../types';
import {isEffectiveApi} from '../../utils/api';
import {filter} from '../../utils/tpl';
@ -248,8 +254,8 @@ export default class FormTable extends React.Component<TableProps, TableState> {
this.subForms[`${x}-${y}`] = form;
}
validate(): any {
const {value, minLength, maxLength, translate: __} = this.props;
async validate(): Promise<string | void> {
const {value, minLength, maxLength, translate: __, columns} = this.props;
// todo: 如果当前正在编辑中,表单提交了,应该先让正在编辑的东西提交然后再做验证。
if (~this.state.editIndex) {
@ -266,15 +272,43 @@ export default class FormTable extends React.Component<TableProps, TableState> {
key => this.subForms[key] && subForms.push(this.subForms[key])
);
if (subForms.length) {
return Promise.all(subForms.map(item => item.validate())).then(
values => {
if (~values.indexOf(false)) {
return __('Form.validateFailed');
const results = await Promise.all(
subForms.map(item => item.validate())
);
let msg = ~results.indexOf(false) ? __('Form.validateFailed') : '';
let uniqueColumn = '';
if (
!msg &&
Array.isArray(columns) &&
Array.isArray(value) &&
columns.some(item => {
if (item.unique && item.name) {
let exists: Array<any> = [];
return value.some((obj: any) => {
const value = getVariable(obj, item.name);
if (~exists.indexOf(value)) {
uniqueColumn = `${item.label || item.name}`;
return true;
}
exists.push(value);
return false;
});
}
return;
}
);
return false;
})
) {
msg = __('InputTable.uniqueError', {
label: uniqueColumn
});
}
return msg;
}
}
}
@ -390,13 +424,28 @@ export default class FormTable extends React.Component<TableProps, TableState> {
}
addItem(index: number) {
const {needConfirm, payload} = this.props;
const {needConfirm, scaffold, columns} = this.props;
const items = this.state.items.concat();
const value = {
...payload,
let value: any = {
__isPlaceholder: true
};
if (Array.isArray(columns)) {
columns.forEach(column => {
if (
typeof column.value !== 'undefined' &&
typeof column.name === 'string'
) {
setVariable(value, column.name, column.value);
}
});
}
value = {
...value,
...scaffold
};
if (needConfirm === false) {
delete value.__isPlaceholder;
}

View File

@ -129,6 +129,16 @@ export type TableColumnObject = {
*
*/
remark?: SchemaRemark;
/**
* , inputTable
*/
value?: any;
/**
* , inputTable
*/
unique?: boolean;
};
export type TableColumnWithType = SchemaObject & TableColumnObject;

View File

@ -352,11 +352,7 @@ export const FormItemStore = StoreNode.named('FormItemStore')
self.validated = true;
if (
self.unique &&
self.form.parentStore &&
self.form.parentStore.storeType === 'ComboStore'
) {
if (self.unique && self.form?.parentStore?.storeType === 'ComboStore') {
const combo = self.form.parentStore as IComboStore;
const group = combo.uniques.get(self.name) as IUniqueGroup;
@ -368,7 +364,7 @@ export const FormItemStore = StoreNode.named('FormItemStore')
item.value === self.tmpValue
)
) {
addError(self.__('`当前值不唯一`'));
addError(self.__('Form.unique'));
}
}