Skip to content

Commit

Permalink
Fixes fieldProps and error types
Browse files Browse the repository at this point in the history
  • Loading branch information
lemcii committed Nov 2, 2018
1 parent 067332d commit a336ce9
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -87,7 +97,7 @@ export interface FormProps {
event: React.FormEvent
nextValue: string
prevValue: string
fieldProps: React.InputHTMLAttributes<HTMLInputElement>
fieldProps: FieldProps
fields: Fields
form: Form
},
Expand All @@ -107,15 +117,15 @@ export interface FormProps {
export class Form extends React.Component<FormProps> {
reset: () => void
serialize: () => SerializedForm
setErrors: FormErrors
setErrors: (errors: FieldErrors) => void
submit: () => Promise<void>
validate: () => Promise<void>
}

export interface RuleParameters {
get: (path: string[]) => string | undefined
value: string
fieldProps: React.InputHTMLAttributes<HTMLInputElement>
fieldProps: FieldProps
fields: Fields
form: Form
}
Expand All @@ -129,7 +139,7 @@ export type AsyncRulePayload = {

interface Event {
event: React.FormEvent<HTMLInputElement>
fieldProps: React.InputHTMLAttributes<HTMLInputElement>
fieldProps: FieldProps
fields: Fields
form: Form
}
Expand Down Expand Up @@ -160,7 +170,7 @@ export interface FieldProps {
onFocus?: FocusFunction
}

export interface CreateFieldState {
export interface FieldState {
asyncRule?: AsyncRule
controlled: boolean
debounceValidate: () => any // ?
Expand Down Expand Up @@ -193,11 +203,6 @@ export interface CreateFieldState {
valuePropName: string
}

export interface CreateFieldProps {
fieldProps: React.InputHTMLAttributes<HTMLInputElement>
fieldState: CreateFieldState
}

type FieldPreset =
| React.InputHTMLAttributes<HTMLInputElement>
| React.InputHTMLAttributes<HTMLSelectElement>
Expand All @@ -211,10 +216,18 @@ export const fieldPresets: {
textarea: React.TextareaHTMLAttributes<HTMLTextAreaElement>
}

export interface CreateFieldProps {
fieldProps: FieldProps
fieldState: FieldState
}

export function createField<P>(
preset: FieldPreset,
): (
component: (
props: CreateFieldProps & P,
) => React.ReactElement<FieldProps & P>,
) => React.ComponentClass<FieldProps & P>
props: P & {
fieldProps: React.InputHTMLAttributes<HTMLInputElement>
fieldState: FieldState
},
) => React.ReactElement<P & FieldProps>,
) => React.ComponentClass<P & FieldProps>

0 comments on commit a336ce9

Please sign in to comment.