clear form action - draft
This commit is contained in:
parent
3b3b90c721
commit
35d5a2d082
|
@ -42,4 +42,7 @@ export default [
|
||||||
name: "Log Out",
|
name: "Log Out",
|
||||||
component: LogOut,
|
component: LogOut,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Clear Form",
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
|
@ -6,6 +6,7 @@ export const ActionTypes = {
|
||||||
ValidateForm: "ValidateForm",
|
ValidateForm: "ValidateForm",
|
||||||
RefreshDatasource: "RefreshDatasource",
|
RefreshDatasource: "RefreshDatasource",
|
||||||
SetDataProviderQuery: "SetDataProviderQuery",
|
SetDataProviderQuery: "SetDataProviderQuery",
|
||||||
|
ClearForm: "ClearForm"
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ApiVersion = "1"
|
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 = {
|
const handlerMap = {
|
||||||
["Save Row"]: saveRowHandler,
|
["Save Row"]: saveRowHandler,
|
||||||
["Delete Row"]: deleteRowHandler,
|
["Delete Row"]: deleteRowHandler,
|
||||||
|
@ -85,6 +93,7 @@ const handlerMap = {
|
||||||
["Trigger Automation"]: triggerAutomationHandler,
|
["Trigger Automation"]: triggerAutomationHandler,
|
||||||
["Validate Form"]: validateFormHandler,
|
["Validate Form"]: validateFormHandler,
|
||||||
["Refresh Datasource"]: refreshDatasourceHandler,
|
["Refresh Datasource"]: refreshDatasourceHandler,
|
||||||
|
["Clear Form"]: clearFormHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
const confirmTextMap = {
|
const confirmTextMap = {
|
||||||
|
|
|
@ -64,6 +64,13 @@
|
||||||
})
|
})
|
||||||
return get(formState).valid
|
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
|
// Provide both form API and state to children
|
||||||
|
@ -72,6 +79,7 @@
|
||||||
// Action context to pass to children
|
// Action context to pass to children
|
||||||
const actions = [
|
const actions = [
|
||||||
{ type: ActionTypes.ValidateForm, callback: formApi.validate },
|
{ type: ActionTypes.ValidateForm, callback: formApi.validate },
|
||||||
|
{ type: ActionTypes.ClearForm, callback: formApi.clear },
|
||||||
]
|
]
|
||||||
|
|
||||||
// Creates an API for a specific field
|
// Creates an API for a specific field
|
||||||
|
@ -108,8 +116,28 @@
|
||||||
|
|
||||||
return !newError
|
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 {
|
return {
|
||||||
setValue,
|
setValue,
|
||||||
|
clearValue,
|
||||||
validate: () => {
|
validate: () => {
|
||||||
const { fieldState } = fieldMap[field]
|
const { fieldState } = fieldMap[field]
|
||||||
setValue(get(fieldState).value, true)
|
setValue(get(fieldState).value, true)
|
||||||
|
|
Loading…
Reference in New Issue