react-bfm
Version:
A basic field / form manager for React using hooks
26 lines (21 loc) • 800 B
text/typescript
import { clearField, resetField } from '../field/actions'
import { getNamespaceState } from '../state'
import { FieldNameType, NamespaceType } from '../common'
/**
* Reset namespace, but ignoring the initial values of the fields
*/
export const clearNamespace = (namespace: NamespaceType): void => {
const fieldNames = Object.keys(getNamespaceState(namespace) || {})
fieldNames.forEach((fieldName: FieldNameType) => {
clearField(namespace, fieldName)
})
}
/**
* Reset namespace to default state and setting last provided initial value per field
*/
export const resetNamespace = (namespace: NamespaceType): void => {
const fieldNames = Object.keys(getNamespaceState(namespace) || {})
fieldNames.forEach((fieldName: FieldNameType) => {
resetField(namespace, fieldName)
})
}