import { Component, ReactNode } from 'react'; export interface FormProps { className?: string; /** * Specify whether the form controls inside is controlled. Defaults to `false`. */ controlled?: boolean; /** * Determine whether all controls of this form are read-only. */ readOnly?: boolean; submitOnTypedEnter?: boolean; /** * Gets called after the form is requested to submit and it passes validation. */ onSubmit?(data: Map): Promise | void; } export interface IForm extends Component { /** * Indicates whether the form controls inside is controlled by default. */ readonly controlled: boolean; /** * Indicates whether all controls inside is valid. */ readonly valid: boolean; readonly values: Map; /** * To register a control on `componentDidMount`. */ register(control: IFormControl): void; /** * To unregister a control on `componentWillUnmount`. */ unregister(control: IFormControl): void; /** * Submit the form and eventually triggers `onSubmit`. */ submit(): void; /** * Called before submitting process validates form controls. */ preSubmit(): void; /** * Reset the form. */ reset(): void; } export interface FormControlProps { /** * Name of the form control, required if a form control is within a form. The * same name will be the key of data `Map` in `onSubmit(data)` handler. */ name?: string; /** * Whether a value for this form control is required. This property is * already being handled by `FormControl`, custom implementation may also * take the value for other purposes. */ required?: boolean; /** * Value input. This property is **NOT** taken care of by the abstract * `FormControl` implementation and has to be handled by custom * implementation. */ value?: T; /** * Indicates whether this is a controlled form control. Defaults to `true` if * the control is not within a form. Otherwise it inherits the value from the * form. */ controlled?: boolean; /** * Initial value. */ initialValue?: T; /** * Determine whether this form control is read-only. This property is **NOT** * taken care of by the abstract `FormControl` implementation and has to be * handled by custom implementation. */ readOnly?: boolean; autoFocus?: boolean; /** * Called on the control focused. */ onFocus?(): void; /** * Called on the control blurred */ onBlur?(): void; } export interface IFormControl> extends Component { /** * Current dataEntry getter of this form control. */ dataEntry: IFormDataEntry | undefined; /** * An error string getter of value violation. */ error: string | undefined; /** * Indicates whether the form control has been touched. Initialized with * `false`. * - On blur, if the value not `empty` or the value is `required`, make it * `true`; otherwise, make it `false`. * - On change, if the value is `empty`, reset it to `false`. * - Reset it to `false` during `reset`. * - Make it `true` in `prepareControlForSubmission`. */ touched: boolean; /** * Whether the value of control has changed. */ changed: boolean; /** * Whether the control is focused. */ active: boolean; /** * Uncontrolled value getter, the form control implementation should update * this value on value change. */ uncontrolledValue: T | undefined; /** * Current name getter of this form control. */ name: string | undefined; /** * Current value getter of this form control. */ value: T | undefined; /** * A getter determine whether current `value` is "empty". */ empty: boolean; /** * Determine whether the given value is "empty". By default it compares the * value with `undefined`, and can be overridden for custom use case. E.g., * for `Input`, it compares the value with `''` (empty string) instead of * `undefined`. */ isEmpty(value: T | undefined): boolean; /** * A concrete `FormControl` should implement this method instead of * `render()` (which is implemented by the abstract `FormControl` for some * post-processing purpose). * * abstract */ renderControl(): ReactNode; /** * A concrete `FormControl` should invoke this method on value change. * * () => ... */ onChange(): void; /** * A concrete `FormControl` should invoke this method when it gets focused. * * () => ... */ onFocus(): void; /** * A concrete `FormControl` should invoke this method when it gets blurred. * * () => ... */ onBlur(): void; /** * Focus the control. This method will call `focusControl` (see below) for * resetting. */ focus(): void; /** * Blur the control. This method will call `blurControl` (see below) for * resetting. */ blur(): void; /** * Reset the control to a clean state. This method will call `resetControl` * (see below) for resetting. */ reset(): void; /** * Called before submitting process validates a form control. Implemented by * abstract `FormControl` as a noop function, and can be override. */ preSubmit(): void; } export interface FormDataEntryProps { name: string; } export interface IFormDataEntry extends Component { /** * Current name getter of this form dataEntry. */ name: string; }