rehance-forms
Version:
Form utilities for React
76 lines (75 loc) • 2.7 kB
TypeScript
import * as React from "react";
import { FieldMap } from "./types";
import { FieldContext } from "./FieldContext";
import { WithFormScopeProps } from "./helpers";
import { FormEvent } from "./EventBus";
import { ScopeContext } from "./ScopeContext";
export interface Formatter {
(value: any, output: boolean): any;
}
export declare type GetClassName = (field: FieldContext) => string;
export declare type FieldProps<ElementType> = {
name: string;
className?: string | GetClassName;
validateOnChange?: boolean;
format?: Formatter;
keepChangesOnUnmount?: boolean;
validate?(field: string, values: FieldMap): string | null;
onFocus?(ev: React.FocusEvent<ElementType>, scope: ScopeContext): void;
onChange?(ev: React.ChangeEvent<ElementType>, scope: ScopeContext): void;
onBlur?(ev: React.FocusEvent<ElementType>, scope: ScopeContext): void;
};
export declare abstract class HTMLFieldComponent<Props extends FieldProps<ElementType>, ElementType extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, State = {}> extends React.PureComponent<WithFormScopeProps<Props>, State> {
private unsubscribe;
protected field: FieldContext;
protected element: ElementType;
constructor(props: WithFormScopeProps<Props>, context: any);
/**
* Inform the scope that this field exists and subscribe to scope events.
*/
componentWillMount(): void;
/**
* Unsubscribe from scope events and clear the field context.
*/
componentWillUnmount(): void;
/**
* Returns the formatted value for the field.
*/
protected readonly value: string;
/**
* Generates and return the class names for the element.
*/
protected readonly classes: string;
/**
* Format input using a given formatter prop.
*/
protected format(value: any, output: boolean): any;
/**
* Trigger an update for this field.
*/
protected triggerUpdate: () => void;
/**
* Bind the element reference for the field.
*/
protected bindRef(el: ElementType): void;
/**
* Process incoming scope events.
*/
protected handleScopeEvent: (ev: FormEvent) => void;
/**
* Handle focus events for this field.
*/
protected handleFocus(ev: React.FocusEvent<ElementType>): void;
/**
* Handle blur events for this field.
*/
protected handleBlur(ev: React.FocusEvent<ElementType>): void;
/**
* Handle change events for this field.
*/
protected handleChange(ev: React.ChangeEvent<ElementType>): void;
/**
* Validates the field and return either the error message or null.
*/
protected validateSelf(): any;
}