UNPKG

rehance-forms

Version:
208 lines (207 loc) 7.47 kB
import * as React from "react"; import { EventBus, EventBusSubscriber, FormEventSignal } from "./EventBus"; import { FieldMap, IScopeChild, ErrorMap } from "./types"; import { FieldContext } from "./FieldContext"; export declare type ScopeChild = ListScopeContext | FieldContext | ScopeContext; export declare const FormScopeConsumer: React.ExoticComponent<React.ConsumerProps<ScopeContext | null>>, FormScopeProvider: React.ProviderExoticComponent<React.ProviderProps<ScopeContext | null>>; export declare abstract class BaseContext implements IScopeChild { protected _parent: null | BaseContext; protected _events: EventBus; protected _id: string; constructor(parentScope?: null | BaseContext); /** * Returns the top level or root scope of the hierarchy that this scope belongs to. * Essentially, this will return the form scope. */ readonly root: BaseContext; /** * Returns the parent scope of this scope or null if no parent scope exists and this * is the top level scope. */ readonly parent: null | BaseContext; /** * Returns the event bus that this scope is using. If this scope is nested inside * of another scope, it will use the parent scope's event bus (all the way up the * scope tree to the root scope). */ readonly events: EventBus; /** * Returns the ID for the scope. The scope ID is a combination of its own internal * ID and the IDs of its parents. */ readonly id: string; /** * Returns true if the scope is the parent (or ancestor) of the given scope. */ isAncestorOf(scope: BaseContext): boolean; /** * Returns true if the scope is the child (or descendent) of the given scope. */ isDescendentOf(scope: BaseContext): boolean; /** * Subscribe to all events occurring within the hierarchy that this scope belongs to. */ listen(sub: EventBusSubscriber): Function; /** * Triggers an update that will be broadcasted to all scopes within the hierarchy that * this scope belongs to. */ broadcast(signal: FormEventSignal, field?: string): this; /** * Submits the form that the scope belongs to. */ submit(): this; abstract readonly touched: boolean; abstract readonly value: any; abstract readonly valid: boolean; abstract readonly changed: boolean; abstract reset(): void; abstract clear(): void; abstract commit(): void; } export declare class ScopeContext extends BaseContext implements IScopeChild { protected _initialValues: FieldMap; children: { [key: string]: ScopeChild; }; constructor(initialValues?: FieldMap, parentScope?: null | BaseContext); /** * Returns the initial values for the scope. */ readonly initialValues: FieldMap; /** * Returns a child scope or field of this scope. Returns null if a * valid child cannot be found. */ getChild(name: string): ScopeChild | null; /** * Register a child scope or field to this scope. */ setChild(name: string, child: ScopeChild): this; /** * Unregister a child scope or field from this scope. */ clearChild(name: string): this; /** * Builds and returns a map of key/value pairs with the data managed by this * scope, and the child scopes. */ readonly value: FieldMap; /** * Returns the errors for this scope and its descendents. */ readonly error: null | ErrorMap; /** * Returns the errors for the requested fields or null if no errors were found in * the specified fields. */ getErrors(fields: string[]): null | ErrorMap; /** * Returns an existing field or creates a new field context is one does not * exist. The field is automatically added as a child to the scope. */ field(name: string): FieldContext; /** * Returns true if none of the fields in the current scope have an error. */ readonly valid: boolean; /** * Returns true if all of the specified children of this scope are considered valid. */ areValid(fields: string[]): boolean; /** * Returns true if any of the fields have in the current scope have changed. */ readonly changed: boolean; /** * Returns true if any of the fields have in the current scope have been touched. */ readonly touched: boolean; /** * Returns true if any of the specified children of this scope have changed. */ hasChanges(fields: string[]): boolean; /** * Resets the values of the all fields and scopes with in the current scope * back to their initial values. */ reset(): this; /** * Clears the values of all fields and scopes within the current scope. */ clear(): this; /** * Convenience method for getting a single value from the scope. */ get(field: string, fallback?: any): any; /** * Adopts the current values for each field as the new initial value, setting the * changed state for the scope to `false`. If you want to commit changes for a full * form from a sub scope, use `scope.root.commit()`. Once you've committed the changes * you'll want to run the `.update()` method to broadcast the update. */ commit(): this; /** * Convenience method that broadcasts the scope update event. */ update(): this; } export declare class ListScopeContext extends BaseContext { children: ScopeContext[]; protected _initialValues: FieldMap[]; constructor(initialValues?: FieldMap[], parentScope?: null | BaseContext); /** * Returns the initial values for the list scope. */ readonly initialValues: FieldMap[]; /** * Adds a new child context to the list scope. */ addChildScope(values?: FieldMap): this; /** * Splices a specific child by index. */ removeChildScope(index: number): this | undefined; /** * Builds and returns an array with the data managed by this scope, * and the child scopes. */ readonly value: FieldMap[]; /** * Returns the errors for all of the scopes within this list scope as an array or * null if no errors are found in any of the nested scopes. */ readonly error: null | ErrorMap[]; /** * Returns true if none of the fields in the current scope have an error. */ readonly valid: boolean; /** * Returns true if any of the fields have in the current scope have changed. */ readonly changed: boolean; /** * Returns true if any of the fields have in the current scope have been touched. */ readonly touched: boolean; /** * Resets the values of the all fields and scopes with in the current scope * back to their initial values. */ reset(): this; /** * Clears the values of all fields and scopes within the current scope. */ clear(): this; /** * Adopts the current values for each field as the new initial value, setting the * changed state for the scope to `false`. If you want to commit changes for a full * form from a sub scope, use `scope.root.commit()`. Once you've committed the changes * you'll want to run the `.update()` method to broadcast the update. */ commit(): this; /** * Convenience method that broadcasts the scope update event. */ update(): this; }