diff --git a/types/react-form/index.d.ts b/types/react-form/index.d.ts index 0a7c1862c0..ca06be480c 100644 --- a/types/react-form/index.d.ts +++ b/types/react-form/index.d.ts @@ -6,27 +6,29 @@ import * as React from 'react'; -type FormValue = any; -type FormError = string | undefined; -type Nested = {[key: string]: T | Nested}; +export type FormValue = any; +export type FormError = string | undefined; +export interface Nested { + [key: string]: T | Nested; +} export type FormValues = Nested; export type Touched = Nested; export type FormErrors = {[key: string]: FormError} | [{[key: string]: FormError}]; export type NestedErrors = Nested; -type RenderReturn = JSX.Element | false | null; +export type RenderReturn = JSX.Element | false | null; export interface FormProps { - loadState?: (props: FormProps, self: Form) => FormState | undefined, - defaultValues?: FormValues, - preValidate?: (values: FormValues, state: FormState, props: FormProps, self: Form) => FormValues, - validate?: (values: FormValues, state: FormState, props: FormProps) => FormErrors, - onValidationFail?: (values: FormValues, state: FormState, props: FormProps, self: Form) => void, - onChange?: (state: FormState, props: FormProps, initial: boolean | FormProps, self: Form) => void, - saveState?: (state: FormState, props: FormProps, self: Form) => void, - willUnmount?: (state: FormState, props: FormProps, self: Form) => void, - preSubmit?: (values: FormValues, state: FormState, props: FormProps, self: Form) => FormValues, - onSubmit?: (values: FormValues, state: FormState, props: FormProps, self: Form) => void, - postSubmit?: (values: FormValues, state: FormState, props: FormProps, self: Form) => void + loadState?(props: FormProps, self: Form): FormState | undefined; + defaultValues?: FormValues; + preValidate?(values: FormValues, state: FormState, props: FormProps, self: Form): FormValues; + validate?(values: FormValues, state: FormState, props: FormProps): FormErrors; + onValidationFail?(values: FormValues, state: FormState, props: FormProps, self: Form): void; + onChange?(state: FormState, props: FormProps, initial: boolean | FormProps, self: Form): void; + saveState?(state: FormState, props: FormProps, self: Form): void; + willUnmount?(state: FormState, props: FormProps, self: Form): void; + preSubmit?(values: FormValues, state: FormState, props: FormProps, self: Form): FormValues; + onSubmit?(values: FormValues, state: FormState, props: FormProps, self: Form): void; + postSubmit?(values: FormValues, state: FormState, props: FormProps, self: Form): void; } export interface FormState { @@ -63,63 +65,62 @@ export class Form FormState > implements FormApi, React.ChildContextProvider<{}> { + static defaultProps: FormProps; + static childContextTypes: { + formApi: React.Validator + }; - static defaultProps: FormProps; - static childContextTypes: { - formApi: React.Validator - }; + getDefaultState(): FormState; + getChildContext(): { formApi: FormApi }; + componentWillMount(): void; + componentWillReceiveProps(nextProps: Readonly>, nextContext: any): void; + componentWillUmount(): void; - getDefaultState (): FormState; - getChildContext: () => { formApi: FormApi } - componentWillMount(): void; - componentWillReceiveProps(nextProps: Readonly>, nextContext: any): void; - componentWillUmount(): void; + // API + setAllValues(values: FormValues, noTouch?: boolean): void; + setValue(field: string, value: any, noTouch?: boolean): void; + getValue(field: string, fallback?: any): any; + setNestedError(field: string, value?: boolean): void; + getError(field: string): FormError; + setTouched(field: string, value?: boolean): void; + getTouched(field: string): boolean; + addValue(field: string, value: any): void; + removeValue(field: string, index: number): void; + swapValues(field: string, index: number, destIndex: number): void; + setAllTouched(dirty?: boolean, state?: Partial): void; + resetForm(): void; + submitForm(e?: Pick, 'preventDefault'>): void; - // API - setAllValues (values: FormValues, noTouch?: boolean): void; - setValue (field: string, value: any, noTouch?: boolean): void; - getValue (field: string, fallback?: any): any; - setNestedError (field: string, value?: boolean): void; - getError (field: string): FormError; - setTouched (field: string, value?: boolean): void; - getTouched (field: string): boolean; - addValue (field: string, value: any): void; - removeValue (field: string, index: number): void; - swapValues (field: string, index: number, destIndex: number): void; - setAllTouched (dirty?: boolean, state?: Partial): void; - resetForm (): void; - submitForm (e?: Pick, 'preventDefault'>): void; + // Utils + getAPI(): FormApi; + setFormState(newState: Partial, silent?: boolean): void; + emitChange(state: FormState, initial?: boolean): void; + validate(values: FormValues, state: FormState, props: FormProps): FormErrors; + render(): RenderReturn; + } - // Utils - getAPI (): FormApi; - setFormState (newState: Partial, silent?: boolean): void; - emitChange (state: FormState, initial?: boolean): void; - validate (values: FormValues, state: FormState, props: FormProps): FormErrors; - render (): RenderReturn; +export interface FormFieldApi { + setAllValues(values: FormValues, noTouch?: boolean): void; + setValue(value: any, noTouch?: boolean): void; + getValue(fallback?: any): any; + setNestedError(value?: boolean): void; + getError(): FormError; + setTouched(value?: boolean): void; + getTouched(): boolean; + addValue(value: any): void; + removeValue(index: number): void; + swapValues(index: number, destIndex: number): void; + setAllTouched(dirty?: boolean, state?: Partial): void; + resetForm(): void; + submitForm(e?: Pick, 'preventDefault'>): void; } -interface FormFieldApi { - setAllValues: (values: FormValues, noTouch?: boolean) => void; - setValue: (value: any, noTouch?: boolean) => void; - getValue: (fallback?: any) => any; - setNestedError: (value?: boolean) => void; - getError: () => FormError; - setTouched: (value?: boolean) => void; - getTouched: () => boolean; - addValue: (value: any) => void; - removeValue: (index: number) => void; - swapValues: (index: number, destIndex: number) => void; - setAllTouched: (dirty?: boolean, state?: Partial) => void; - resetForm: () => void; - submitForm: (e?: Pick, 'preventDefault'>) => void; -} - -interface FormFieldPropsWithField { +export interface FormFieldPropsWithField { field?: string; - children: (api: FormFieldApi) => React.ReactElement | null; + children(api: FormFieldApi): React.ReactElement | null; } -interface FormFieldPropsWithoutField { - children: (api: FormApi) => RenderReturn; +export interface FormFieldPropsWithoutField { + children(api: FormApi): RenderReturn; } export type FormFieldProps = FormFieldPropsWithField | FormFieldPropsWithoutField; export const FormField: React.SFC; @@ -141,8 +142,8 @@ export interface FormInputProps { errorProps?: {}; } -interface FormInputPropsWithChildren extends FormInputProps { - children: (api: FormFieldApi) => React.ReactElement | null; +export interface FormInputPropsWithChildren extends FormInputProps { + children(api: FormFieldApi): React.ReactElement | null; } export const FormInput: React.SFC; @@ -158,17 +159,17 @@ export type ClickHandler = EventHandler>; // Prop interfaces are intermediate interfaces to "redefine" the type of some events // onChange:React.EventHandler => onChange:any => onChange:CustomEventHandler -interface SelectOption { +export interface SelectOption { label: string; value: any; disabled?: boolean; } -interface SelectAttrs extends React.SelectHTMLAttributes { +export interface SelectAttrs extends React.SelectHTMLAttributes { onChange?: any; onBlur?: any; } export interface SelectProps extends SelectAttrs { - options: ReadonlyArray + options: ReadonlyArray; field?: FormInputProps['field']; showErrors?: FormInputProps['showErrors']; errorBefore?: FormInputProps['errorBefore']; @@ -181,7 +182,7 @@ export interface SelectProps extends SelectAttrs { } export const Select: React.SFC; -interface InputAttrs extends React.InputHTMLAttributes { +export interface InputAttrs extends React.InputHTMLAttributes { onChange?: any; onBlur?: any; } @@ -197,7 +198,7 @@ export interface CheckboxProps extends InputAttrs { } export const Checkbox: React.SFC; -interface TextareaAttrs extends React.TextareaHTMLAttributes { +export interface TextareaAttrs extends React.TextareaHTMLAttributes { onChange?: any; onBlur?: any; } @@ -205,8 +206,8 @@ export interface TextareaProps extends TextareaAttrs { field?: FormInputProps['field']; showErrors?: FormInputProps['showErrors']; errorBefore?: FormInputProps['errorBefore']; - onChange?: ChangeHandler - onBlur?: FocusHandler + onChange?: ChangeHandler; + onBlur?: FocusHandler; isForm?: FormInputProps['isForm']; noTouch?: boolean; errorProps?: FormInputProps['errorProps']; @@ -261,10 +262,10 @@ export class RadioGroup extends React.Component implements Form resetForm: FormFieldApi['resetForm']; submitForm: FormFieldApi['submitForm']; - getChildContext (): RadioGroupContext; + getChildContext(): RadioGroupContext; } -interface InputWIthoutClick extends InputAttrs { +export interface InputWIthoutClick extends InputAttrs { onClick?: any; } export interface RadioProps extends InputWIthoutClick { @@ -273,7 +274,6 @@ export interface RadioProps extends InputWIthoutClick { onBlur?: FocusHandler; } export class Radio extends React.Component { - static contextTypes: { formRadioGroup: React.Validator };