UNPKG

@canard/schema-form

Version:

React-based component library that renders forms based on JSON Schema with plugin system support for validators and UI components

220 lines (219 loc) 10.1 kB
import type { Fn } from '../../../@aileron/declare'; import type { AllowedValue, JsonSchemaError, JsonSchemaWithVirtual } from '../../../types'; import { type ChildNode, type NodeEvent, type NodeListener, type NodeStateFlags, type SchemaNode, type SchemaNodeConstructorProps, type UnionSetValueOption } from '../type'; export declare abstract class AbstractNode<Schema extends JsonSchemaWithVirtual = JsonSchemaWithVirtual, Value extends AllowedValue = any> { #private; /** [readonly] Node group, `branch` or `terminal` */ readonly group: 'branch' | 'terminal'; /** [readonly] Node type, `array`, `number`, `object`, `string`, `boolean`, `virtual`, `null` */ readonly type: Exclude<Schema['type'], 'integer'>; /** [readonly] Node depth */ readonly depth: number; /** [readonly] Whether this is the root node */ readonly isRoot: boolean; /** [readonly] Root node */ readonly rootNode: SchemaNode; /** [readonly] Parent node */ readonly parentNode: SchemaNode | null; /** [readonly] Node's JSON Schema */ readonly jsonSchema: Schema; /** [readonly] Original property key of the node */ readonly propertyKey: string; /** [readonly] Node's escaped key(it can be same as propertyKey if not escape needed) */ readonly escapedKey: string; /** [readonly] Whether the node is required */ readonly required: boolean; /** * [readonly] Node name * @note Basically it is readonly, but can be changed with `setName` by the parent node. * */ get name(): string; /** * Sets the node's name. Only the parent can change the name. * @param name - The name to set * @param actor - The node setting the name */ setName(this: AbstractNode, name: string, actor: SchemaNode): void; /** * [readonly] Node path. * @note Basically it is readonly, but can be changed with `updatePath` by the parent node. * */ get path(): string; /** * Updates the node's path. Updates its own path by referencing the parent node's path. * @returns Whether the path was changed * @returns {boolean} Whether the path was changed */ updatePath(this: AbstractNode): boolean; /** [readonly] Node key */ get key(): string | undefined; /** * Node's default value * - set: `setDefaultValue`, can only be updated by inherited nodes * - get: `defaultValue`, can be read in all situations */ get defaultValue(): Value | undefined; /** * Changes the node's default value, can only be performed by inherited nodes * For use in `constructor` * @param value input value for updating defaultValue */ protected setDefaultValue(this: AbstractNode, value: Value | undefined): void; /** * Changes the node's default value and publishes a Refresh event. Can only be performed by inherited nodes * For use outside of `constructor` * @param value input value for updating defaultValue * @returns {Promise<void>} A promise that resolves when the refresh is complete */ protected refresh(this: AbstractNode, value: Value | undefined): void; /** * Gets the node's value * @returns The node's value */ abstract get value(): Value | undefined; /** * Sets the node's value * @param input - The value to set */ abstract set value(input: Value | undefined); /** * Sets the node's value, can be redefined by inherited nodes * @param input The value to set or a function that returns a value * @param options Set options * - replace(boolean): Overwrite existing value, (default: false, merge with existing value) */ protected abstract applyValue(this: AbstractNode, input: Value | undefined, option: UnionSetValueOption): void; /** * Sets the node's value. Performs preprocessing on the input before reflecting the actual data with applyValue. * @param input - The value to set or a function that returns a value * @param option - Set options, can be combined using bitwise operators * - `Overwrite`(default): `Replace` | `Propagate` | `Refresh` * - `Merge`: `Propagate` | `Refresh` * - `Replace`: Replace the current value * - `Propagate`: Propagate the update to child nodes * - `Refresh`: Trigger a refresh to update the FormTypeInput * - `Normal`: Only update the value */ setValue(this: AbstractNode, input: Value | undefined | ((prev: Value | undefined) => Value | undefined), option?: UnionSetValueOption): void; /** * Function called when the node's value changes. * @param input - The changed value */ protected onChange(this: AbstractNode, input: Value | undefined): void; /** List of child nodes, nodes without child nodes return an `null` */ get children(): ChildNode[] | null; constructor({ key, name, jsonSchema, defaultValue, onChange, parentNode, validationMode, validatorFactory, required, }: SchemaNodeConstructorProps<Schema, Value>); /** * Finds the node corresponding to the given path in the node tree. * @param path - Path of the node to find (e.g., '/foo/0/bar'), returns itself if not provided * @returns {SchemaNode|null} The found node, null if not found */ find(this: AbstractNode, path?: string): SchemaNode | null; /** * Saves an event unsubscribe function. * @param unsubscribe - The unsubscribe function to save */ protected saveUnsubscribe(this: AbstractNode, unsubscribe: Fn): void; /** * Initializes the node's event listener/subscription list. Initialization must be called by itself or by the parent node. * @param actor - The node requesting initialization * @internal Internal implementation method. Do not call directly. */ cleanUp(this: AbstractNode, actor?: SchemaNode): void; /** * Registers a node event listener * @param listener Event listener * @returns Event listener removal function */ subscribe(this: AbstractNode, listener: NodeListener): () => void; /** * Publishes an event to the node's listeners * @param event Event to publish * - type: Event type (see NodeEventType) * - payload: Data for the event (see MethodPayload) * - options: Options for the event (see MethodOptions) */ publish(this: AbstractNode, event: NodeEvent): void; /** [readonly] Whether the node is activated */ get activated(): boolean; /** * Activates the node. Activation must be called by itself or by the parent node. * @param actor - The node requesting activation * @returns {boolean} Whether activation occurred * @internal Internal implementation method. Do not call directly. */ activate(this: AbstractNode, actor?: SchemaNode): boolean; /** [readonly] Whether the node is visible */ get visible(): boolean; /** [readonly] Whether the node is read only */ get readOnly(): boolean; /** [readonly] Whether the node is disabled */ get disabled(): boolean; /** [readonly] Index of the oneOf branch */ get oneOfIndex(): number; /** [readonly] List of values to watch */ get watchValues(): readonly any[]; /** * Updates the node's computed properties. * @internal Internal implementation method. Do not call directly. */ protected updateComputedProperties(this: AbstractNode): void; /** * Resets the current node to its initial value. Uses the current node's initial value, or the provided value if one is given. * @param preferLatest - Whether to use the latest value, uses the latest value if available * @param input - The value to set, uses the provided value if given * @internal Internal implementation method. Do not call directly. */ resetNode(this: AbstractNode, preferLatest: boolean, input?: Value | undefined): void; /** * [readonly] Node's state flags * @note use `setState` method to set the state * */ get state(): NodeStateFlags; /** * Sets the node's state. Maintains existing state unless explicitly passing undefined. * @param input - The state to set or a function that computes new state based on previous state */ setState(this: AbstractNode, input?: ((prev: NodeStateFlags) => NodeStateFlags) | NodeStateFlags): void; /** * Returns the merged result of errors that occurred inside the form and externally received errors. * @returns All of the errors that occurred inside the form and externally received errors */ get globalErrors(): JsonSchemaError[]; /** * Returns the merged result of own errors and externally received errors. * @returns Local errors and externally received errors */ get errors(): JsonSchemaError[]; /** * Updates own errors and then merges them with externally received errors. * @param errors - List of errors to set */ setErrors(this: AbstractNode, errors: JsonSchemaError[]): void; /** * Clears own errors. * @note Does not clear externally received errors. */ clearErrors(this: AbstractNode): void; /** * Merges externally received errors with local errors. For rootNode, also merges internal errors. * @param errors - List of received errors */ setExternalErrors(this: AbstractNode, errors?: JsonSchemaError[]): void; /** * Clears externally received errors. * @note Does not clear localErrors / internalErrors. */ clearExternalErrors(this: AbstractNode): void; /** * Finds and removes specific errors from the externally received errors. * @param errors - List of errors to remove */ removeFromExternalErrors(this: AbstractNode, errors: JsonSchemaError[]): void; /** * Performs validation based on the current value. * @returns {Promise<JsonSchemaError[]>} List of errors that occurred inside the form * @note If `ValidationMode.None` is set, an empty array is returned. */ validate(this: AbstractNode): Promise<JsonSchemaError[]>; }