@formkit/core

Introduction

The official FormKit core library. This package is responsible for most of FormKit’s internal functionality. You can read documentation specifically on how it works at formkit.com.

You can add this package by using npm install @formkit/core or yarn add @formkit/core.

FormKitNode

FormKit's Node object produced by createNode(). Every <FormKit /> input has 1 FormKitNode ("core node") associated with it. All inputs, forms, and groups are instances of nodes. Read more about core nodes in the architecture documentation.

add()

Add a child to a node. The node must be a group or list.

Signature

Parameters

  • node — A FormKitNode.
  • index optional — A index to where it will added to.

Returns

The added FormKitNode.

address

The address of the current node from the root of the tree.

Signature

Returns

A FormKitAddress.

addProps()

Adds props to the given node by removing them from node.props.attrs and moving them to the top-level node.props object.

Signature

Parameters

  • props — An array of strings to be added as keys for props.

Returns

The FormKitNode.

at()

Gets a node at another address. Addresses are dot-syntax paths (or arrays) of node names. For example: form.users.0.first_name. There are a few "special" traversal tokens as well:

  • $root — Selects the root node.
  • $parent — Selects the parent node.
  • $self — Selects the current node.

Signature

Parameters

Returns

The found FormKitNode or undefined.

children

An array of child nodes (groups and lists).

Signature

Returns

An array of FormKitNode.

clearErrors()

Clears the errors of the node, and optionally all the children.

Signature

Parameters

  • clearChildren optional — If it should clear the children.
  • sourceKey optional — A source key to use for reset.

Returns

The FormKitNode.

config

An object of FormKitConfig that is shared tree -wide with various configuration options that should be applied to the entire tree.

Signature

Returns

A FormKitConfig.

define()

Defines the current input's library type definition including node type, schema, and props.

Signature

Parameters

destroy()

Removes the node from the global registry, its parent, and emits the 'destroying' event.

Signature

each()

Perform given callback on each of the given node's children.

Signature

Parameters

emit()

Emit an event from the node so it can be listened by on.

Signature

Parameters

  • event — The event name to be emitted.
  • payload optional — A value to be passed together with the event.
  • bubble optional — If the event should bubble to the parent.

Returns

The FormKitNode.

find()

Within a given tree, find a node matching a given selector. Selectors can be simple strings or a function.

Signature

Parameters

Returns

The found FormKitNode or undefined.

hook

Set of hooks.

Signature

Returns

The FormKitHooks.

index

The index of a node compared to its siblings. This is only applicable in cases where a node is a child of a list.

Signature

Returns

A number.

input()

The function used to set the value of a node. All changes to a node's value should use this function as it ensures the tree's state is always fully tracked.

Signature

Parameters

  • value — Any value to used for the node.
  • async optional — If the input should happen asynchronously.

Returns

A Promise<unknown>.

isCreated

Begins as false, set to true when the node is finished being created.

Signature

Returns

A boolean.

isSettled

Boolean reflecting the settlement state of the node and its subtree.

Signature

Returns

A boolean.

ledger

A counting ledger for arbitrary message counters.

Signature

Returns

A FormKitLedger.

name

The name of the input in the node tree. When a node is a child of a list, this automatically becomes its index.

Signature

Returns

A string.

off()

Removes an event listener by its token. Receipts can be shared among many event listeners by explicitly declaring the "receipt" property of the listener function.

Signature

Parameters

  • receipt — A receipt generated by the on function.

Returns

A receipt string.

on()

Adds an event listener for a given event, and returns a "receipt" which is a random string token. This token should be used to remove the listener in the future. Alternatively you can assign a "receipt" property to the listener function and that receipt will be used instead. This allows multiple listeners to all be de-registered with a single off() call if they share the same receipt.

Signature

Parameters

  • eventName — The event name to listen to.
  • listener — A FormKitEventListener to run when the event happens.

Returns

A receipt string.

parent

The parent of a node.

Signature

Returns

If found a FormKitNode or null.

props

An proxied object of props. These are typically provided by the adapter of choice.

Signature

Returns

An optional list of FormKitProps.

remove()

Removes a child from the node.

Signature

Parameters

Returns

The FormKitNode.

reset()

Resets the node’s value back to its original value.

Signature

Returns

The FormKitNode.

root

Retrieves the root node of a tree. This is accomplished via tree-traversal on-request, and as such should not be used in frequently called functions.

Signature

Returns

The FormKitNode.

setErrors()

Sets errors on the input, and optionally to child inputs.

Signature

Parameters

settled

A promise that resolves when a node and its entire subtree is settled. In other words — all the inputs are done committing their values.

Signature

Returns

A Promise<unknown>.

store

The internal node store.

Signature

Returns

A FormKitStore.

submit()

Triggers a submit event on the nearest form.

Signature

t()

A text or translation function that exposes a given string to the "text" hook. All text shown to users should be passed through this function before being displayed — especially for core and plugin authors.

Signature

Parameters

Returns

The translated string.

type

The type of node, should only be 'input', 'list', or 'group'.

Signature

Returns

A FormKitNodeType.

use()

Registers a new plugin on the node and its subtree.

Signature

Parameters

  • plugin — A FormKitPlugin or an Array or Set of FormKitPlugin.
  • run optional — Should the plugin be executed on creation.
  • library optional — Should the plugin's library function be executed on creation.

Returns

The FormKitNode.

value

The value of the input. This should never be directly modified. Any desired mutations should be made through input.

Signature

walk()

Performs a function on the node and every node in its subtree. This is an expensive operation so it should be done very rarely and only lifecycle events that are relatively rare like boot up and shut down.

Signature

Parameters

  • callback — A FormKitChildCallback to be executed for each child.
  • stopOnFalse optional — If it should stop when the return is false.

Functions

bfs()

Perform a breadth-first search on a node subtree and locate the first instance of a match.

Signature

Parameters

  • tree — A FormKitNode to start from.
  • searchValue — A value to be searched.
  • searchGoal — A goal value.

Returns

A FormKitNode or undefined.

clearErrors()

Clears errors on the node and optionally its children.

Signature

Parameters

  • id — The id of the node you want to clear errors for.
  • clearChildren — Determines if the children of this node should have their errors cleared.

compile()

Compiles a logical string like "a != z || b == c" into a single function. The return value is an object with a "provide" method that iterates over all requirement tokens to use as replacements.

Signature

Parameters

  • expr — A string to compile.

Returns

A FormKitCompilerOutput.

Examples

createClasses()

Function that produces a standardized object representation of CSS classes.

Signature

Parameters

  • propertyKey — the section key.
  • node — A FormKitNode.
  • sectionClassList — A string | Record<string, boolean> or a FormKitClasses.

Returns

Record<string, boolean>

createConfig()

Creates a new instance of a global configuration option. This object is essentially just a FormKitOption object, but it can be used as the root for FormKitConfig's proxy and retain event "emitting".

Signature

Parameters

Returns

A FormKitRootConfig.

createMessage()

Creates a new FormKitMessage object.

Signature

Parameters

Returns

A FormKitMessageProps.

createNode()

Creates a new instance of a FormKit Node. Nodes are the atomic unit of a FormKit graph.

Signature

Parameters

  • options — An options object of FormKitOptions to override the defaults.

Returns

A FormKitNode.

Examples

createValue()

Creates the initial value for a node based on the options passed in and the type of the input.

Signature

Parameters

Returns

unknown

deregister()

Deregister a node from the registry.

Signature

Parameters

error()

Emits an error. Generally should result in an exception.

Signature

Parameters

  • code — The integer error code.
  • data — Usually an object of information to include.

generateClassList()

Combines multiple class lists into a single list.

Signature

Parameters

  • node — A FormKitNode.
  • property — The property key to which the class list will be applied.
  • args — And array of Record<string, boolean> of CSS class list(s).

Returns

string | null

getNode()

Get a node by a particular id.

Signature

Parameters

  • id — Get a node by a given id.

Returns

A FormKitNode or undefined.

isComponent()

Type narrow that a node is a DOM node.

Signature

Parameters

  • node — A schema node to check.

Returns

boolean

isConditional()

Determines if a node is conditionally rendered or not.

Signature

Parameters

  • node — A schema node to check.

Returns

boolean

isDOM()

Type narrow that a node is a DOM node.

Signature

Parameters

  • node — A schema node to check

Returns

boolean

isList()

A simple type guard to determine if the context being evaluated is a list type.

Signature

Parameters

Returns

Returns a boolean.

isNode()

Determine if a given object is a node.

Signature

Parameters

  • node — Any value.

Returns

Returns a boolean.

Examples

isSugar()

Determines if the node is syntactic sugar or not.

Signature

Parameters

  • node — A schema node to check.

Returns

boolean

names()

Create a name-based dictionary of all children in an array.

Signature

Parameters

Returns

A dictionary of named FormKitNode.

register()

Registers a node to the registry if the node is a root node, or if the node has an explicit node.props.alias. If these two things are not true, then no node is registered (idempotent).

Signature

Parameters

reset()

Resets an input to its "initial" value. If the input is a group or list it resets all the children as well.

Signature

Parameters

  • id — The id of an input to reset.
  • resetTo — A value to reset the node to.

Returns

A FormKitNode or undefined.

resetCount()

Resets the global number of node registrations, useful for deterministic node naming.

Signature

resetRegistry()

Resets the entire registry. Deregisters all nodes and removes all listeners.

Signature

setErrors()

Sets errors on a form, group, or input.

Signature

Parameters

  • id — The id of a form.
  • localErrors — The errors to set on the form or the form’s inputs in the format of ErrorMessages.
  • childErrors — (optional) The errors to set on the form or the form’s inputs in the format of ErrorMessages.

submitForm()

Submits a FormKit form programmatically.

Signature

Parameters

  • id — The id of the form.

sugar()

Converts syntactic sugar nodes to standard nodes.

Signature

Parameters

  • node — A node to covert.

Returns

A FormKitSchemaNode without the properties of FormKitSchemaFormKit.

warn()

Globally emits a warning.

Signature

Parameters

  • code — The integer warning code.
  • data — Usually an object of information to include.

watchRegistry()

A way of watching changes in the global registry.

Signature

Parameters

  • id — A dot

-syntax id where the node is located.

  • callback — A callback in the format of FormKitEventListener to notify when the node is set or removed.

TypeScript

FormKitChildCallback

The callback type for node.each().

FormKitChildValue

A descriptor of a child value, generally passed up a node tree.

FormKitClasses

Definition for a function that produces CSS classes.

FormKitCompilerOutput

The compiler output, a function that adds the required tokens.

FormKitConfig

General "app" like configuration options, these are automatically inherited by all children — they are not reactive.

FormKitContext

The interface of a FormKit node's context object. A FormKit node is a proxy of this object.

FormKitContextShape

Define the most basic shape of a context object for type guards trying to reason about a context's value.

FormKitCounter

The counter object used to perform instance counting within a tree.

FormKitCounterCondition

Ledger counters require a condition function that determines if a given message applies to it or not.

FormKitDispatcher

The FormKitDispatcher interface is responsible creating/running "hooks".

FormKitEvent

The internal structure of a FormKitEvent.

FormKitEventEmitter

The FormKitEventEmitter definition.

FormKitEventListener

Event listener functions definition.

FormKitExtendableSchemaRoot

Defines a function that allows selectively overriding a given schema.

FormKitFrameworkContext

Context object to be created by and used by each respective UI framework. No values are created or output by FormKitCore, but this interface should be followed by each respective plugin.

FormKitFrameworkContextState

The state inside a node’s framework context. Usually used to track things like blurred and validity states.

FormKitGroupValue

FormKit inputs of type 'group' must have keyed values by default.

FormKitHandlerPayload

Describes the data passing through the error and warning handlers.

FormKitHooks

The available hooks for middleware.

FormKitLedger

The FormKit ledger, a general-purpose message counting service provided by FormKit core for counting messages throughout a tree.

FormKitLibrary

A library of inputs, keyed by the name of the type.

FormKitListContext

The simplest definition for a context of type "list".

FormKitMessageMeta

Messages have can have any arbitrary meta data attached to them.

FormKitMessageProps

The structure of a core FormKitMessage. These messages are used to store information about the state of a node.

FormKitMessageStore

Defines the actual store of messages.

FormKitPlugin

The base interface definition for a FormKitPlugin. It's just a function that accepts a node argument.

FormKitSchemaAttributesCondition

Conditions nested inside attribute declarations.

FormKitSchemaComposable

Definition for a function that can extend a given schema node.

FormKitSchemaContext

The context that is passed from one schema render to the next.

FormKitSchemaProps

Properties available in all schema nodes.

FormKitTrap

The definition of a FormKitTrap. These are somewhat like methods on each FormKitNode. They are always symmetrical (get/set) — although it's acceptable for either to throw an Exception.

KeyedValue

Arbitrary data that has properties. Could be a POJO, could be an array.

FormKitAddress

Describes the path to a particular node from the top of the tree.

FormKitAttributeValue

The possible value types of attributes (in the schema).

FormKitCompilerProvider

A function that accepts a callback with a token as the only argument, and must return a function that provides the true value of the token.

FormKitListStatement

A full loop statement in tuple syntax. Can be read like "foreach value, key? in list".

FormKitListValue

The value being listed out. Can be an array, an object, or a number.

FormKitMessage

A FormKit message is immutable, so all properties should be readonly.

FormKitMiddleware

All FormKitMiddleware conform to the pattern of accepting a payload and a next() function. They can either pass the payload to the next middleware explicitly (as an argument of next), or implicitly (no argument for next).

FormKitNodeType

These are the types of nodes that can be created. These are different from the type of inputs available and rather describe their purpose in the tree.

FormKitOptions

Options that can be used to instantiate a new node via createNode().

FormKitProps

The user-land per-instance "props", which are generally akin to the props passed into components on the front end.

FormKitSchemaAttributes

DOM attributes are simple string dictionaries.

FormKitSchemaComponent

Properties available when defining a generic non-FormKit component.

FormKitSchemaCondition

A schema node that determines which content to render.

FormKitSchemaDOMNode

Properties available when using a DOM node.

FormKitSchemaFormKit

Syntactic sugar for a FormKitSchemaComponent node that uses FormKit.

FormKitSchemaNode

Properties available then defining a schema node.

FormKitSchemaTextNode

A simple text node.

FormKitSearchFunction

Breadth and depth-first searches can use a callback of this notation.

FormKitStore

The message store contains all of the messages that pertain to a given node.

FormKitTextFragment

Text fragments are small pieces of text used for things like interface validation messages, or errors that may be exposed for modification or even translation.

FormKitTraps

The map signature for a node's traps Map.

FormKitTypeDefinition

Definition of a library item — when registering a new library item, these are the required and available properties.

TrapGetter

Signature for any of the node's getter traps. Keep in mind that because these are traps and not class methods, their response types are declared explicitly in the FormKitNode interface.

TrapSetter

The signature for a node's trap setter — these are more rare than getter traps, but can be useful for blocking access to certain context properties or modifying the behavior of an assignment (ex. see setParent).