添加 clear clear-and-submit 并且将 searchable 里面的重置换成 clear-and-submit 行为 (#1549)

This commit is contained in:
liaoxuezhi 2021-02-05 12:51:51 +08:00 committed by GitHub
parent 8f469b34bd
commit 6b73df7ecc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 4 deletions

View File

@ -542,6 +542,61 @@ icon 也可以是 url 地址,比如
}
```
### 清空表单
在 form 中,配置`"actionType": "clear"`的按钮,可以实现清空表单数据的功能,跟重置不同的是,重置其实是还原到初始值,并不一定是清空。
```schema: scope="body"
{
"type": "form",
"api": "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/form/saveForm",
"controls": [
{
"type": "text",
"name": "username",
"placeholder": "请输入用户名",
"label": "用户名",
"value": "rick"
},
{
"type": "password",
"name": "password",
"label": "密码",
"placeholder": "请输入密码"
},
{
"type": "checkbox",
"name": "rememberMe",
"option": "记住登录"
}
],
"actions": [
{
"type": "button",
"actionType": "clear",
"label": "清空"
},
{
"type": "reset",
"label": "重置"
},
{
"type": "submit",
"label": "提交",
"level": "primary"
}
]
}
```
### 重置表单并提交
`actionType` 配置成 `"reset-and-submit"`
### 清空表单并提交
`actionType` 配置成 `"clear-and-submit"`
## 通用属性表
所有`actionType`都支持的通用配置项

View File

@ -188,6 +188,11 @@ export interface FormSchema extends BaseSchema {
*/
resetAfterSubmit?: boolean;
/**
*
*/
clearAfterSubmit?: boolean;
/**
*
*/
@ -363,6 +368,7 @@ export default class Form extends React.Component<FormProps, object> {
'messages',
'wrapperComponent',
'resetAfterSubmit',
'clearAfterSubmit',
'submitOnInit',
'submitOnChange',
'onInit',
@ -836,6 +842,7 @@ export default class Form extends React.Component<FormProps, object> {
checkInterval,
messages: {saveSuccess, saveFailed},
resetAfterSubmit,
clearAfterSubmit,
onAction,
onSaved,
onReset,
@ -883,12 +890,15 @@ export default class Form extends React.Component<FormProps, object> {
action.type === 'submit' ||
action.actionType === 'submit' ||
action.actionType === 'confirm' ||
action.actionType === 'reset-and-submit'
action.actionType === 'reset-and-submit' ||
action.actionType === 'clear-and-submit'
) {
store.setCurrentAction(action);
if (action.actionType === 'reset-and-submit') {
store.reset(onReset);
} else if (action.actionType === 'clear-and-submit') {
store.clear(onReset);
}
return this.submit((values): any => {
@ -963,6 +973,7 @@ export default class Form extends React.Component<FormProps, object> {
}
resetAfterSubmit && store.reset(onReset);
clearAfterSubmit && store.clear(onReset);
clearPersistDataAfterSubmit && store.clearPersistData();
if (action.redirect || redirect) {
@ -992,6 +1003,9 @@ export default class Form extends React.Component<FormProps, object> {
} else if (action.type === 'reset' || action.actionType === 'reset') {
store.setCurrentAction(action);
store.reset(onReset);
} else if (action.actionType === 'clear') {
store.setCurrentAction(action);
store.clear(onReset);
} else if (action.actionType === 'dialog') {
store.setCurrentAction(action);
store.openDialog(data);

View File

@ -124,7 +124,7 @@ export class HeadCellSearchDropDown extends React.Component<
{
type: 'button',
label: __('reset'),
actionType: 'reset'
actionType: 'clear-and-submit'
},
{

View File

@ -514,6 +514,20 @@ export const FormStore = ServiceStore.named('FormStore')
cb && cb(self.data);
}
function clear(cb?: (data: any) => void) {
const toClear: any = {};
self.items.forEach(item => {
if (item.name && item.type !== 'hidden') {
toClear[item.name] = item.resetValue;
}
});
setValues(toClear);
self.validated = false;
self.submited = false;
self.items.forEach(item => item.reset());
cb && cb(self.data);
}
function addFormItem(item: IFormItemStore) {
self.itemsRef.push(item.id);
// 默认值可能在原型上,把他挪到当前对象上。
@ -589,6 +603,7 @@ export const FormStore = ServiceStore.named('FormStore')
getPersistData,
setPersistData,
clearPersistData,
clear,
onChildStoreDispose,
updateSavedData,
getItemsByPath,

View File

@ -83,7 +83,8 @@ export const FormItemStore = StoreNode.named('FormItemStore')
filteredOptions: types.optional(types.frozen(), []),
dialogSchema: types.frozen(),
dialogOpen: false,
dialogData: types.frozen()
dialogData: types.frozen(),
resetValue: types.optional(types.frozen(), '')
})
.views(self => {
function getForm(): any {

View File

@ -94,7 +94,9 @@ export interface Action extends Button {
| 'next'
| 'prev'
| 'reset'
| 'reset-and-submit';
| 'reset-and-submit'
| 'clear'
| 'clear-and-submit';
api?: Api;
asyncApi?: Api;
payload?: any;