From a336ce9793ed8d57d79c13436e17827166f37c4b Mon Sep 17 00:00:00 2001 From: ell10t Date: Fri, 2 Nov 2018 10:10:55 +0000 Subject: [PATCH] Fixes fieldProps and error types --- index.d.ts | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9f7b2896..be29799f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -61,6 +61,16 @@ export interface ValidationMessages { general?: ValidationMessageSet } +export type Errors = string[] | string | null + +export interface FieldErrors { + [key: string]: + | Errors + | { + [key: string]: Errors + } +} + type GenericFormEvent = (args: { fields: Fields; form: Form }) => void type FormSubmitState = { @@ -87,7 +97,7 @@ export interface FormProps { event: React.FormEvent nextValue: string prevValue: string - fieldProps: React.InputHTMLAttributes + fieldProps: FieldProps fields: Fields form: Form }, @@ -107,7 +117,7 @@ export interface FormProps { export class Form extends React.Component { reset: () => void serialize: () => SerializedForm - setErrors: FormErrors + setErrors: (errors: FieldErrors) => void submit: () => Promise validate: () => Promise } @@ -115,7 +125,7 @@ export class Form extends React.Component { export interface RuleParameters { get: (path: string[]) => string | undefined value: string - fieldProps: React.InputHTMLAttributes + fieldProps: FieldProps fields: Fields form: Form } @@ -129,7 +139,7 @@ export type AsyncRulePayload = { interface Event { event: React.FormEvent - fieldProps: React.InputHTMLAttributes + fieldProps: FieldProps fields: Fields form: Form } @@ -160,7 +170,7 @@ export interface FieldProps { onFocus?: FocusFunction } -export interface CreateFieldState { +export interface FieldState { asyncRule?: AsyncRule controlled: boolean debounceValidate: () => any // ? @@ -193,11 +203,6 @@ export interface CreateFieldState { valuePropName: string } -export interface CreateFieldProps { - fieldProps: React.InputHTMLAttributes - fieldState: CreateFieldState -} - type FieldPreset = | React.InputHTMLAttributes | React.InputHTMLAttributes @@ -211,10 +216,18 @@ export const fieldPresets: { textarea: React.TextareaHTMLAttributes } +export interface CreateFieldProps { + fieldProps: FieldProps + fieldState: FieldState +} + export function createField

( preset: FieldPreset, ): ( component: ( - props: CreateFieldProps & P, - ) => React.ReactElement, -) => React.ComponentClass + props: P & { + fieldProps: React.InputHTMLAttributes + fieldState: FieldState + }, + ) => React.ReactElement

, +) => React.ComponentClass