The toggle
input — sometimes called a "switch" — offers a choice between one of two values; it's a great option when you want the user to toggle a feature on or off:
The values for the toggle are undefined
if not-interacted with, true
if checked, and false
if unchecked. You can change these values by passing the onValue
and offValue
props. In this example, we will set the onValue
to the string 'active'
and the offValue
to the string 'inactive'
:
By default, the toggle input supports a primary label like the label
prop on any other FormKit input. The primary label is displayed to the right of the toggle unless you use a Value label
, which will then move it above the toggle:
If you would like the label to be displayed above the toggle, you can use the alt-label-position
prop:
Additionally, you may specify secondary labels by setting the on-value-label
and the off-value-label
props. These values are conditionally shown based on the on/off state of the toggle. The value labels render to the right of the toggle input. The "primary label" will be moved to the alternative label position above the toggle when value labels are used:
Additionally, you can set the value-label-display
prop to inner
to render the value labels inside the toggle. The Primary label
will return to being displayed to the right of the toggle:
The thumb-icon
prop allows you to insert an icon into the thumb
section of the toggle
input. This prop accepts a string that's passed to FormKit's icon plugin:
The toggle input supports color props for its various sections for easy styling. Each prop contains on and off states that are applied depending on whether the toggle is on or off. The following example uses some of these color props. See the Props & Attributes section below for a full list of these props:
The toggle
input is built atop HTML's native checkbox input.
Prop | Type | Default | Description |
---|---|---|---|
alt-label-position | Boolean | undefined | Moves the label above the toggle. |
off-value | any | false | The value when the toggle is unchecked. |
on-value | any | true | The value when the toggle is checked. |
off-value-label | String | undefined | The text of the Value label when the toggle is unchecked. |
on-value-label | String | undefined | The text of the Value label when the toggle is checked. |
value-label-display | String | undefined | The `on` and `off` value labels will always default to being displayed outside and to the right of the toggle. Other accepted values include `inner` to set the value labels inside of the toggle, and `hidden` which hides the value labels. |
value-label-color-off | String | undefined | Used to set the color of the value label when the toggle is unchecked. |
value-label-color-on | String | undefined | Used to set the color of the value label when the toggle is checked. |
thumb-icon | String | undefined | This prop is used to set the icon that will be placed inside the thumb section. |
thumb-color-off | String | undefined | Sets the background color of the thumb section when the toggle is unchecked. |
thumb-color-on | String | undefined | Sets the background color of the thumb section when the toggle is checked. |
icon-color-off | String | undefined | The color the icon should be set to when `toggle-icon` prop is set and the toggle is unchecked. |
icon-color-on | String | undefined | The color the icon should be set to when `toggle-icon` prop is set and the toggle is checked. |
track-color-off | String | undefined | Sets the background color of the `track` section when the toggle is unchecked. |
track-color-on | String | undefined | Sets the background color of the `track` section when toggle is checked. |
Show Universal props | |||
config | Object | {} | Configuration options to provide to the input’s node and any descendent node of this input. |
delay | Number | 20 | Number of milliseconds to debounce an input’s value before the commit hook is dispatched. |
dirtyBehavior | string | touched | Determines how the "dirty" flag of this input is set. Can be set to touched or compare — touched (the default) is more performant, but will not detect when the form is once again matching its initial state. |
errors | Array | [] | Array of strings to show as error messages on this field. |
help | String | '' | Text for help text associated with the input. |
id | String | input_{n} | The unique id of the input. Providing an id also allows the input’s node to be globally accessed. |
ignore | Boolean | false | Prevents an input from being included in any parent (group, list, form etc). Useful when using inputs for UI instead of actual values. |
index | Number | undefined | Allows an input to be inserted at the given index if the parent is a list. If the input’s value is undefined, it inherits the value from that index position. If it has a value it inserts it into the lists’s values at the given index. |
label | String | '' | Text for the label element associated with the input. |
name | String | input_{n} | The name of the input as identified in the data object. This should be unique within a group of fields. |
parent | FormKitNode | contextual | By default the parent is a wrapping group, list or form — but this props allows explicit assignment of the parent node. |
prefix-icon | String | '' | Specifies an icon to put in the prefixIcon section. |
preserve | boolean | false | Preserves the value of the input on a parent group, list, or form when the input unmounts. |
preserve-errors | boolean | false | By default errors set on inputs using setErrors are automatically cleared on input, setting this prop to true maintains the error until it is explicitly cleared. |
sections-schema | Object | {} | An object of section keys and schema partial values, where each schema partial is applied to the respective section. |
suffix-icon | String | '' | Specifies an icon to put in the suffixIcon section. |
type | String | text | The type of input to render from the library. |
validation | String, Array | [] | The validation rules to be applied to the input. |
validation-visibility | String | blur | Determines when to show an input's failing validation rules. Valid values are blur , dirty , and live . |
validation-label | String | {label prop} | Determines what label to use in validation error messages, by default it uses the label prop if available, otherwise it uses the name prop. |
validation-rules | Object | {} | Additional custom validation rules to make available to the validation prop. |
value | Any | undefined | Seeds the initial value of an input and/or its children. Not reactive. Can seed entire groups (forms) and lists.. |
You can target a specific section of an input using that section's "key", allowing you to modify that section's classes, HTML (via :sections-schema
, or content (via slots)). Read more about sections here.
Section-key | Description |
---|---|
track | The track section is the background of the toggle (the area that the thumb slides on). |
thumb | The thumb section is the element that slides on the track. |
thumbIcon | The thumbIcon section is the icon that is rendered inside the thumb section (when thumb-icon prop is set). |
valueLabel | An additional label element, rendered when value-label props are used. Displayed to the right of the toggle. |
altLabel | A label element for the label. This label is rendered when value-label props are being used. |
innerLabel | A label element used when value-label props are provided and value-label-display is set to inner. |
Show Universal section keys | |
outer | The outermost wrapping element. |
wrapper | A wrapper around the label and input. |
label | The label of the input. |
prefix | Has no output by default, but allows content directly before an input element. |
prefixIcon | An element for outputting an icon before the prefix section. |
inner | A wrapper around the actual input element. |
suffix | Has no output by default, but allows content directly after an input element. |
suffixIcon | An element for outputting an icon after the suffix section. |
input | The input element itself. |
help | The element containing help text. |
messages | A wrapper around all the messages. |
message | The element (or many elements) containing a message — most often validation and error messages. |