Skip to content

Smart form ​

A form built from a description of its fields.

Customer
Customer type
Contacts

Who to call while the courier is on the way

Use it for routine forms, such as a customer card, settings or filters, and for forms that come from the server as JSON. formConfig<Values>() describes the fields, and the same useForm as in the plain form validates and submits them.

Installation ​

ts
import { NuiSmartForm, formConfig, useForm } from '@numior/kit/forms';

Examples ​

Split the form into steps ​

Pass steps instead of fields.

Step 1 of 3: Buyer

The Next button checks the fields of the open step and moves on only when they pass. The stepper goes back to finished steps, and v-model:step holds the open step.

The button on the last step submits the whole form, and when a field on another step fails, that step opens. A screen reader announces each new step, such as "Step 2 of 3: Delivery".

Line up filters in a row ​

Add inline.

The fields take the width of their content and wrap to the next line when the row runs out of room. Set inline="grow" to stretch them to the full width.

Configuration ​

field(Component, (v) => v.email, options) describes a field: the component, a function that picks the value from the form values, and the options. The options repeat the props of NuiFormField, such as label, required and rules, and add props, when, area and model. props goes to the component and takes an object or a function of the form values.

TypeScript infers the value type from the picking function and checks it against the component model. When the component keeps its value in a named model in place of v-model, name it: model: 'values'. Set group: true for a set of controls, such as radios, and the label becomes the legend of the group.

section({ title, layout, fields }) renders a group of fields under one title, with an optional description. array((v) => v.contacts, { newRow, fields }) renders a repeatable group with the min, max and reorderable of NuiFieldArray. A separate formConfig<Row>() describes the fields of one row.

Conditions and layout ​

A field, a section or a group shows while its when(values) returns true. In a repeatable group, when gets the values of its row and the row itself. A hidden field is neither validated nor submitted, but its value is kept and comes back with the field.

Set layout on the form, a section, a step or the rows of a group. It is written like the CSS grid-template-areas property, such as '"name email" "company company"'. An area is named after the field path with dots replaced by hyphens, such as address-city, or after its area option.

A section takes its name or area as the area name, and inside a repeatable group the path counts from the row. A layout row whose fields are all hidden disappears, so a field that takes a whole row leaves no gap when hidden.

When the form is narrower than 36rem, the fields stack in one column, so the same form fits a dialog and a phone.

From JSON ​

parseFormConfig(json, { controls }) builds the configuration from JSON, with the components from a control set. Each entry has a kind of field, section or array. A field names its component with a string type from the set and its value with a name path, such as address.city.

A field also takes props, rules and a when condition. The rules are required, email, min, max and pattern, written as objects such as { "rule": "min", "limit": 2 }. A condition is { "path": "kind", "equals": "company" } or { "path": "delivery", "in": ["courier", "post"] }.

The component from the set checks its own props. An unknown type, rejected props or an entry of the wrong shape give a list of issues in place of the fields. Each issue has a code and a path, such as fields.2.type.

A pattern rule whose source is not a regular expression is reported in issues too. The path leads to the rule itself, such as fields.0.rules.1.

formConfig({ controls }).control('select', (v) => v.country, { props }) takes a component from the set by key, and TypeScript knows the types of its props. Values of JSON fields are typed unknown, because the configuration that arrives decides their shape. The useControls page shows how to build a control set.

Slots ​

A slot named after a field path fills the component, such as the radios of a group or the label of a checkbox. Inside a repeatable group the slot name skips the row number, such as contacts.phone. The slot gets error, invalid, touched and pending, and inside a group also row.

A slot named path:after, such as email:after, adds content under the field and gets row too. The form-errors slot replaces the error block above the fields and gets errors.

The actions slot replaces the form buttons. It gets submit, reset, pending and dirty, and in a wizard also step, first, last and back.

API ​

Props

Name
Type
Default
Description
formrequired
FormContext<T>
—
Form returned by useForm.
fields
readonly NuiSmartFormEntry<T>[]
—
Fields, sections and repeatable groups built with formConfig.
steps
readonly NuiSmartFormStep<T>[]
—
Wizard steps in place of fields. A stepper appears above the form.
layout
string
—
Grid of the fields in grid-template-areas syntax, such as "city zip".
inline
boolean | "grow"
—
Puts the fields and the button in one row, sizing the fields to their content; grow stretches them to the full width.
disabled
boolean
false
Disables every field and button of the form.
readonly
boolean
false
Shows every field value without letting the user change it. The submit button is disabled.
reserveError
boolean
false
Reserves space for an error under every field so the form does not shift.
submitLabel
string
—
Submit button text. Defaults to "Submit" from the dictionary.
nextLabel
string
—
Next step button text. Defaults to "Next" from the dictionary.
backLabel
string
—
Back button text. Defaults to "Back" from the dictionary.

Models

Name
Type
Default
Description
v-model:step
string
—
value of the current wizard step.

Slots

Name
Slot props
Description
form-errors
{ readonly errors: readonly FormMessage[]; }
Custom markup of form-level errors, such as server errors. Receives errors.
actions
NuiSmartFormActionsScope
Custom form buttons. Receives pending, dirty, submit, reset, plus step and back in a wizard.

Texts

Key
Russian
English
back
Назад
Back
next
Далее
Next
stepChanged
({ current, total, label }) => `Шаг ${current} из ${total}: ${label}`
({ current, total, label }) => `Step ${current} of ${total}: ${label}`

Edit this page

Search

Esc