clear form action - draft
This commit is contained in:
parent
1b54e53ec6
commit
07553b9937
|
@ -42,4 +42,7 @@ export default [
|
|||
name: "Log Out",
|
||||
component: LogOut,
|
||||
},
|
||||
{
|
||||
name: "Clear Form",
|
||||
},
|
||||
]
|
||||
|
|
|
@ -6,6 +6,7 @@ export const ActionTypes = {
|
|||
ValidateForm: "ValidateForm",
|
||||
RefreshDatasource: "RefreshDatasource",
|
||||
SetDataProviderQuery: "SetDataProviderQuery",
|
||||
ClearForm: "ClearForm"
|
||||
}
|
||||
|
||||
export const ApiVersion = "1"
|
||||
|
|
|
@ -77,6 +77,14 @@ const refreshDatasourceHandler = async (action, context) => {
|
|||
)
|
||||
}
|
||||
|
||||
const clearFormHandler = async (action, context) => {
|
||||
return await executeActionHandler(
|
||||
context,
|
||||
action.parameters.componentId,
|
||||
ActionTypes.ClearForm
|
||||
)
|
||||
}
|
||||
|
||||
const handlerMap = {
|
||||
["Save Row"]: saveRowHandler,
|
||||
["Delete Row"]: deleteRowHandler,
|
||||
|
@ -85,6 +93,7 @@ const handlerMap = {
|
|||
["Trigger Automation"]: triggerAutomationHandler,
|
||||
["Validate Form"]: validateFormHandler,
|
||||
["Refresh Datasource"]: refreshDatasourceHandler,
|
||||
["Clear Form"]: clearFormHandler
|
||||
}
|
||||
|
||||
const confirmTextMap = {
|
||||
|
|
|
@ -64,6 +64,13 @@
|
|||
})
|
||||
return get(formState).valid
|
||||
},
|
||||
clear: () => {
|
||||
const fields = Object.keys(fieldMap)
|
||||
fields.forEach(field => {
|
||||
const { fieldApi } = fieldMap[field]
|
||||
fieldApi.clearValue();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Provide both form API and state to children
|
||||
|
@ -72,6 +79,7 @@
|
|||
// Action context to pass to children
|
||||
const actions = [
|
||||
{ type: ActionTypes.ValidateForm, callback: formApi.validate },
|
||||
{ type: ActionTypes.ClearForm, callback: formApi.clear },
|
||||
]
|
||||
|
||||
// Creates an API for a specific field
|
||||
|
@ -108,8 +116,28 @@
|
|||
|
||||
return !newError
|
||||
}
|
||||
|
||||
const clearValue = () => {
|
||||
const { fieldState } = fieldMap[field]
|
||||
fieldState.update(state => {
|
||||
state.value = defaultValue
|
||||
state.error = null
|
||||
return state
|
||||
})
|
||||
|
||||
formState.update(state => {
|
||||
state.values = { ...state.values, [field]: defaultValue }
|
||||
delete state.errors[field]
|
||||
state.valid = Object.keys(state.errors).length === 0
|
||||
return state
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return {
|
||||
setValue,
|
||||
clearValue,
|
||||
validate: () => {
|
||||
const { fieldState } = fieldMap[field]
|
||||
setValue(get(fieldState).value, true)
|
||||
|
|
Loading…
Reference in New Issue