UNPKG

react-native-form-model

Version:

An easily testable and opinionated React Native form model builder written in pure JavaScript.

78 lines (77 loc) 2.54 kB
import LabelField, { LabelFieldProps, LabelFieldState } from './LabelField'; export interface ControlFieldProps<OutputType, InputType> extends LabelFieldProps<OutputType> { /** How to parse input value into output value. */ parse?: (userInput: InputType) => Partial<ParseResult<OutputType>>; /** How to clean user input for display. Defaults to using format(). */ encode?: (value: OutputType) => InputType; validate?: (value: OutputType) => { valid: boolean; error?: any; }; onValueChange?: (x: { value: OutputType; error?: any; }) => void; onValidation?: (x: { valid: boolean; error?: any; }) => void; onFocus?: (event: any) => void; onBlur?: (event: any) => void; onSubmit?: (event: any) => void; } export interface ParseResult<OutputType> { /** The final value. */ value?: OutputType; /** User input error. */ error?: any; /** Is user input prasable. */ parsable: boolean; } export interface ControlFieldState<OutputType, InputType> extends LabelFieldState<OutputType>, Omit<ParseResult<OutputType>, 'value'> { /** User input value. */ userInput: InputType; /** Did current value pass validation. */ valid: boolean; } export interface ControlFieldBlurInfo { didSubmit?: boolean; } /** @deprecated */ export default class ControlField<O = any, I = any, P extends ControlFieldProps<O, I> = ControlFieldProps<O, I>, S extends ControlFieldState<O, I> = ControlFieldState<O, I>> extends LabelField<O, P, S> { editing: boolean; didChangeValue: boolean; constructor(props: P); componentDidMount(): void; getSnapshotBeforeUpdate(prevProps: P, prevState: S): { value: P["value"]; } | null; componentDidUpdate(prevProps: P, prevState: S, snapshot: any): void; setValue(value: O): void; submit(): void; reset(): void; validate(): { valid: boolean; error?: any; }; parse(userInput: I): ParseResult<O>; validation(value: O): { valid: boolean; error?: any; }; onValueChange: ({ value, error }: { value: O; error?: string | undefined; }) => void; handleFocus(event: any): void; handleBlur(event: any): void; handleSubmit(event: any): void; handleUserInput({ userInput }: { userInput: I; }): any; renderContent(): JSX.Element[]; renderControl(): JSX.Element; private setValueMutation; private validationMutation; private encodeMutation; }