cosmo-ui
Version:
Common React components
39 lines (33 loc) • 1.02 kB
text/typescript
import { AbstractFormField } from '../interfaces'
export const createDefaultFormField = (ownProps: any) =>
createEmptyFormField(
ownProps.value || '',
ownProps.required === false ? false : true,
false,
)
export const createAsyncFormField = (ownProps: any) =>
createEmptyFormField(
ownProps.value || '',
ownProps.required === false ? false : true,
true,
ownProps.asyncValue || undefined,
)
export const createEmptyFormField =
(value: any,
required: boolean = true,
includeAsyncFields: boolean = false,
asyncValue: any = undefined): AbstractFormField<any> => {
const res: AbstractFormField<any> = { dirty: false,
valid: false,
errors: [],
submitted: false,
focused: false,
required,
value,
}
if (includeAsyncFields) {
res.loading = false
res.asyncValue = asyncValue
}
return res
}