UNPKG

@dynamicforms/vue-forms

Version:

Data entry forms for vue - logic (no controls here)

596 lines (518 loc) 21.2 kB
import { ComponentOptionsMixin } from 'vue'; import { ComponentProvideOptions } from 'vue'; import { ComputedRef } from 'vue'; import { DefineComponent } from 'vue'; import { PublicProps } from 'vue'; import { Ref } from 'vue'; export declare class AbortEventHandlingException extends Error { } export declare class Action<T extends ActionValue = ActionValue> extends Field<T> { constructor(guard?: symbol); protected init(params?: Partial<IFieldConstructorParams<T>>): void; static create<T extends ActionValue = ActionValue>(params?: Partial<IFieldConstructorParams<T>>): Action<T>; get icon(): string | undefined; set icon(newValue: string | undefined); get label(): string | undefined; set label(newValue: string | undefined); execute(params: any): void; } declare type ActionExecutor = (field: IField, supr: FieldActionExecute, ...params: any[]) => any; export declare class ActionsMap extends Map<symbol, FieldActionExecute> { private readonly eagerActions; private readonly registeredActions; register(action: FieldActionBase): void; trigger<T extends FieldActionBase>(ActionClass: { new (...args: any[]): T; classIdentifier: symbol; }, field: IField, ...params: any[]): any; triggerEager(field: IField, ...params: any[]): any; clone(): ActionsMap; cloneWithoutValidators(): ActionsMap; } export declare interface ActionValue { label?: string; icon?: string; } /** * Depending on the useMarkdownInValidators setting, return either markdown or a plain string error message * @param mdErrorString markdown source to optionally be stripped * @return MdString or string */ export declare function buildErrorMessage(mdErrorString: string): string | MdString; export declare type ClassType = string | string[] | Record<string, boolean>; export declare type ClassTypes = ClassType | ClassType[]; declare class CompareTo<T = any> extends Validator { private otherField; private isValidComparison; private unregistered; constructor(otherField: IField, isValidComparison: (myValue: T, otherValue: T) => boolean, message: RenderContentRef); unregister(): void; } export declare class ConditionalEnabledAction extends ConditionalStatementAction { constructor(statement: Statement); } declare type ConditionalExecutorFn = (field: IField, currentResult: boolean, previousResult: boolean | undefined) => void; export declare class ConditionalStatementAction extends ValueChangedAction { private lastResult; private readonly boundFields; constructor(statement: Statement, executorFn: ConditionalExecutorFn); static get classIdentifier(): symbol; get eager(): boolean; boundToField(field: IField): void; } export declare class ConditionalValueAction<T> extends ConditionalStatementAction { constructor(statement: Statement, trueValue: T); } export declare class ConditionalVisibilityAction extends ConditionalStatementAction { constructor(statement: Statement); } /** * DisplayMode enum provides an enumeration for supported ways of rendering a particular object in the DOM */ export declare enum DisplayMode { SUPPRESS = 1,// Field will be entirely suppressed. it will not render (not even to JSON) and will not parse for PUT HIDDEN = 5,// Field will render as <input type="hidden"> or <tr data-field_name> INVISIBLE = 8,// Field will render completely, but with display: none. Equal to setting its style = {display: none} FULL = 10 } export declare namespace DisplayMode { export function fromString(mode: string): DisplayMode; export function fromAny(mode: any): DisplayMode; export function isDefined(mode: number | string): boolean; } export declare const EmptyField: Field<unknown>; export declare class EnabledChangedAction extends FieldActionBase { constructor(executorFn: (field: IField, supr: FieldActionExecute, newValue: boolean, oldValue: boolean) => void); static get classIdentifier(): symbol; execute(field: IField, supr: FieldActionExecute, newValue: boolean, oldValue: boolean): void; } export declare class EnabledChangingAction extends FieldActionBase { constructor(executorFn: (field: IField, supr: FieldActionExecute, newValue: boolean, oldValue: boolean) => boolean); static get classIdentifier(): symbol; execute(field: IField, supr: FieldActionExecute, newValue: boolean, oldValue: boolean): boolean; } export declare class ExecuteAction extends FieldActionBase { constructor(executorFn: (field: IField, supr: FieldActionExecute, params: any) => any); static get classIdentifier(): symbol; execute(field: IField, supr: FieldActionExecute, params: any): any; } export declare class Field<T = any> extends FieldBase { protected _value: T; protected _touched: boolean; constructor(guard?: symbol); protected init(params?: Partial<IFieldConstructorParams<T>>): void; /** * Creates a new reactive Field instance. * @param params Initial field parameters * @returns Reactive Field instance */ static create<T = any>(this: new (guard?: symbol) => Field<T>, params?: Partial<IFieldConstructorParams<T>>): InstanceType<typeof this>; get value(): T; set value(newValue: T); get touched(): boolean; set touched(touched: boolean); clone(overrides?: Partial<IField<T>>): this; } export declare abstract class FieldActionBase implements IFieldAction { static get classIdentifier(): symbol; get classIdentifier(): symbol; private readonly executorFn; constructor(executorFn: ActionExecutor); execute(field: IField, supr: IFieldAction['execute'], ...params: any[]): any; get eager(): boolean; boundToField(field: IField): void; unregister(): void; } export declare type FieldActionExecute<T = any> = (field: IField<T>, ...params: any[]) => any; export declare abstract class FieldBase<T = any> implements IField<T> { abstract get value(): T; abstract set value(newValue: T); abstract get touched(): boolean; abstract set touched(touched: boolean); readonly reactiveValue: ComputedRef<T>; abstract clone(overrides?: Partial<IField<T>>): IField<T>; originalValue: T; protected validatingCount: number; readonly validating = false; protected _valid: boolean; errors: ValidationError[]; parent?: Group; fieldName?: string; protected actions: ActionsMap; private _visibility; get visibility(): DisplayMode; set visibility(newValue: DisplayMode); private _enabled; get enabled(): boolean; set enabled(newValue: boolean); validate(revalidate?: boolean): void; get valid(): boolean; get fullValue(): any; get isChanged(): boolean; registerAction(action: IFieldAction<T>): this; triggerAction<T2 extends IFieldAction<T>>(actionClass: new (...args: any[]) => T2, ...params: any[]): any; clearValidators(): void; } declare type FieldsToValues<T extends GenericFieldsInterface> = { [K in keyof T]: T[K] extends IField<infer U> ? U : T[K] extends Group<infer G> ? FieldsToValues<G> : any; }; declare namespace Form { export { DisplayMode, FieldActionBase, ActionsMap, EnabledChangedAction, EnabledChangingAction, ExecuteAction, VisibilityChangedAction, VisibilityChangingAction, ValidChangedAction, ValueChangedAction, ListItemAddedAction, ListItemRemovedAction, Operator, ConditionalStatementAction, ConditionalVisibilityAction, ConditionalEnabledAction, ConditionalValueAction, OperandType, Statement, ActionValue, Action, NullableAction, MessagesWidget, Field, NullableField, EmptyField, IField, IFieldConstructorActionsList, IFieldConstructorParams, AbortEventHandlingException, FieldActionExecute, IFieldAction, FieldBase, GenericFieldsInterface, Group, NullableGroup, List, NullableList, Validators, buildErrorMessage, isSimpleComponentDef, isCallableFunction, MdString, SimpleComponentDef, ClassType, ClassTypes, RenderContentNonCallable, RenderContentCallable, RenderContent, RenderContentRef, ValidationError, ValidationErrorText, ValidationErrorRenderContent, RenderableValue, ValidationFunctionResult, ValidationFunction, Validator } } export default Form; export declare const forms: { install(app: any, options?: Partial<FormsConfig>): void; }; declare interface FormsConfig { useMarkdownInValidators: boolean; } export declare type GenericFieldsInterface = Record<string, IField>; export declare class Group<T extends GenericFieldsInterface = GenericFieldsInterface> extends FieldBase { private readonly _fields; private _value; readonly reactiveValue: ComputedRef<FieldsToValues<T> | null>; private suppressNotifyValueChanged; constructor(fields: T, params?: Partial<IFieldConstructorParams>); private addField; private static isValidFields; static createFromFormData(data: Record<string, any> | null): Group; field<K extends keyof T>(fieldName: K): T[K] | null; get fields(): T; get value(): FieldsToValues<T> | null; set value(newValue: FieldsToValues<T> | null); get touched(): boolean; set touched(touched: boolean); get fullValue(): Record<string, any>; notifyValueChanged(): void; get valid(): boolean; validate(revalidate?: boolean): void; clone(overrides?: Partial<IField>): Group<T>; } export declare interface IField<T = any> { value: T; reactiveValue: ComputedRef<T>; fullValue: T; originalValue: T; valid: boolean; validating: boolean; errors: ValidationError[]; enabled: boolean; visibility: DisplayMode; touched: boolean; parent?: any; fieldName?: string; clone(overrides?: Partial<IField<T>>): IField<T>; registerAction(action: IFieldAction<T>): this; triggerAction<T2 extends IFieldAction<T>>(actionClass: abstract new (...args: any[]) => T2, ...params: any[]): any; validate(revalidate?: boolean): void; clearValidators(): void; isChanged: boolean; } export declare interface IFieldAction<T = any> { execute(field: IField<T>, supr: FieldActionExecute<T>, ...params: any[]): any; } export declare interface IFieldConstructorActionsList<T = any> { actions?: IFieldAction<T>[]; validators?: IFieldAction<T>[]; } export declare type IFieldConstructorParams<T = any> = IField<T> & IFieldConstructorActionsList<T>; declare class InAllowedValues<T = any> extends Validator { constructor(allowedValues: T[], message?: RenderContentRef); } export declare function isCallableFunction(msg?: RenderContentRef): msg is RenderContentCallable; /** * Type guard to check if content is a custom component definition * @param msg - Content to check * @returns True if content is a custom component definition */ export declare function isSimpleComponentDef(msg?: RenderContentRef): msg is SimpleComponentDef; declare class LengthInRange<T = any> extends Validator { constructor(minLength: number, maxLength: number, message?: RenderContentRef); } export declare class List<T extends GenericFieldsInterface = GenericFieldsInterface> extends FieldBase { private _value; private _itemTemplate?; private _previousValue; constructor(itemTemplate?: Group<T>, params?: Partial<IFieldConstructorParams>); private processSetValueItem; private setValueInternal; get value(): Record<string, any>[] | null; set value(newValue: Record<string, any>[]); get touched(): boolean; set touched(touched: boolean); clone(overrides?: Partial<IField>): List<T>; notifyValueChanged(): void; get valid(): boolean; validate(revalidate?: boolean): void; get(index: number): Group<T> | undefined; push(item: any): number; pop(): Group<T> | undefined; remove(index: number): Group<T> | undefined; insert(item: any, index: number): number; clear(): void; } export declare class ListItemAddedAction extends FieldActionBase { constructor(executorFn: (field: IField, supr: FieldActionExecute, item: any, index: number) => void); static get classIdentifier(): symbol; execute(field: IField, supr: FieldActionExecute, item: any, index: number): void; } export declare class ListItemRemovedAction extends FieldActionBase { constructor(executorFn: (field: IField, supr: FieldActionExecute, item: any, index: number) => void); static get classIdentifier(): symbol; execute(field: IField, supr: FieldActionExecute, item: any, index: number): void; } declare class MaxLength<T = any> extends Validator { constructor(maxLength: number, message?: RenderContentRef); } declare class MaxValue<T = any> extends Validator { constructor(maxValue: T, message?: RenderContentRef); } /** * Marks content for markdown rendering */ export declare class MdString extends String { plugins?: any[]; options?: any; constructor(value: string, options?: any, plugins?: any[]); } export declare const MessagesWidget: DefineComponent<Props, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, Readonly<Props> & Readonly<{}>, { classes: ClassTypes; }, {}, {}, {}, string, ComponentProvideOptions, false, {}, any>; declare class MinLength<T = any> extends Validator { constructor(minLength: number, message?: RenderContentRef); } declare class MinValue<T = any> extends Validator { constructor(minValue: T, message?: RenderContentRef); } export declare type NullableAction = Action | null; export declare type NullableField<T = any> = Field<T> | null; export declare type NullableGroup = Group | null; export declare type NullableList = List | null; export declare type OperandType = any | Statement | IField; /** * Operators provides us a functionality for backend to send us complex condition upon which we send * dynamic visibility prop for form input fields */ export declare enum Operator { NOT = 0, OR = 1, AND = 2, XOR = 3, NAND = 4, NOR = 5, EQUALS = -1, NOT_EQUALS = -2, GT = -3, LT = -4, GE = -5, LE = -6, IN = -7, NOT_IN = -8, INCLUDES = -9, NOT_INCLUDES = -10 } export declare namespace Operator { export function fromString(operator: string): Operator; export function fromAny(mode: any): Operator; export function isDefined(operator: number | string): boolean; export function isLogicOperator(operator: Operator): boolean; } declare class Pattern<T = any> extends Validator { constructor(pattern: RegExp, message?: RenderContentRef); } declare interface Props { message: string | ValidationError[]; classes?: ClassTypes; } /** * A value, renderable three different ways (plain text, markdown, component) - alias for ValidationErrorRenderContent */ export declare class RenderableValue extends ValidationErrorRenderContent { } /** * Type for different renderable content formats: plain string, markdown, or custom component */ export declare type RenderContent = RenderContentNonCallable | RenderContentCallable; export declare type RenderContentCallable = () => RenderContentNonCallable; export declare type RenderContentNonCallable = string | MdString | SimpleComponentDef; /** * Type for different renderable content formats (supporting references): plain string, markdown, or custom component */ export declare type RenderContentRef = RenderContent | Ref<RenderContent>; declare class Required_2<T = any> extends Validator { constructor(message?: RenderContentRef); } /** * Interface for custom component content definition */ export declare interface SimpleComponentDef { componentName: string; componentProps?: Record<any, any>; componentVHtml?: string; } export declare class Statement { private readonly operand1; private readonly operator; private readonly operand2; constructor(operand1: OperandType, operator: Operator, operand2: OperandType); get operand1Value(): any; get operand2Value(): any; evaluate(): boolean; /** * Recursively collects all fields used in this statement and its nested statements * @returns A set of all fields used in this statement */ collectFields(): Set<IField>; } /** * Base validation error class with component rendering capabilities */ export declare class ValidationError { get componentName(): string; get componentBindings(): {}; get componentBody(): string; get extraClasses(): ClassTypes; } /** * Validation error that supports multiple content types (plain text, markdown, component) */ export declare class ValidationErrorRenderContent extends ValidationError { classes: ClassTypes; private text; private textType; constructor(text: RenderContentRef, classes?: ClassTypes); get resolvedText(): RenderContentNonCallable; get getTextType(): "string" | "md" | "component"; get componentName(): string; get componentBindings(): Record<any, any>; get componentBody(): string; get extraClasses(): ClassTypes; } /** * Simple text-only ValidationError */ export declare class ValidationErrorText extends ValidationError { text: string; classes: ClassTypes; constructor(text: string, classes?: ClassTypes); get componentName(): string; get componentBody(): string; get extraClasses(): ClassTypes; } export declare type ValidationFunction<T = any> = (newValue: T, oldValue: T, field: IField<T>) => ValidationFunctionResult | Promise<ValidationFunctionResult>; export declare type ValidationFunctionResult = ValidationError[] | null; /** * Validator is a specialized action that performs validation when a field's value changes. * It automatically adds/removes validation errors from the field's errors array. */ export declare class Validator<T = any> extends ValueChangedAction { private readonly source; /** * Creates a new validator * @param validationFn Function that validates the field value and returns errors or null */ constructor(validationFn: ValidationFunction<T>); static get classIdentifier(): symbol; get eager(): boolean; protected replacePlaceholdersFunction(text: RenderContentRef, replace: Record<string, any>): RenderContentRef; protected replacePlaceholders(text: RenderContentRef, replace: Record<string, any>): RenderContentRef; } export declare namespace Validators { export { CompareTo, InAllowedValues, Required_2 as Required, Pattern, ValidationFunctionResult, ValidationFunction, Validator, MinValue, MaxValue, ValueInRange, MinLength, MaxLength, LengthInRange } } export declare class ValidChangedAction extends FieldActionBase { constructor(executorFn: (field: IField, supr: FieldActionExecute, newValue: boolean, oldValue: boolean) => void); static get classIdentifier(): symbol; execute(field: IField, supr: FieldActionExecute, newValue: boolean, oldValue: boolean): void; } export declare class ValueChangedAction<T = any> extends FieldActionBase { constructor(executorFn: (field: IField<T>, supr: FieldActionExecute<T>, newValue: T, oldValue: T) => void); static get classIdentifier(): symbol; execute(field: IField<T>, supr: FieldActionExecute<T>, newValue: T, oldValue: T): void; } declare class ValueInRange<T = any> extends Validator { constructor(minValue: T, maxValue: T, message?: RenderContentRef); } export declare class VisibilityChangedAction extends FieldActionBase { constructor(executorFn: (field: IField, supr: FieldActionExecute, newValue: DisplayMode, oldValue: DisplayMode) => void); static get classIdentifier(): symbol; execute(field: IField, supr: FieldActionExecute, newValue: DisplayMode, oldValue: DisplayMode): void; } export declare class VisibilityChangingAction extends FieldActionBase { constructor(executorFn: (field: IField, supr: FieldActionExecute, newValue: DisplayMode, oldValue: DisplayMode) => DisplayMode); static get classIdentifier(): symbol; execute(field: IField, supr: FieldActionExecute, newValue: DisplayMode, oldValue: DisplayMode): DisplayMode; } export { }