MCPcopy Create free account
hub / github.com/formkit/formkit / toggleChecked

Function toggleChecked

packages/inputs/src/features/checkboxes.ts:14–45  ·  view source on GitHub ↗

* Event handler when an input is toggled. * * @param node - The @formkit/node#FormKitNode | FormKitNode being toggled. * @param e - The input event related to the toggling. * * @public

(node: FormKitNode, e: Event)

Source from the content-addressed store, hash-verified

12 * @public
13 */
14function toggleChecked(node: FormKitNode, e: Event) {
15 const el = e.target
16 if (el instanceof HTMLInputElement) {
17 const value = Array.isArray(node.props.options)
18 ? optionValue(node.props.options, el.value)
19 : el.value
20 if (Array.isArray(node.props.options) && node.props.options.length) {
21 if (!Array.isArray(node._value)) {
22 // There is no array value set
23 node.input([value])
24 } else if (
25 !node._value.some((existingValue) => shouldSelect(value, existingValue))
26 ) {
27 // The value is not in the current set
28 node.input([...node._value, value])
29 } else {
30 // Filter out equivalent values
31 node.input(
32 node._value.filter(
33 (existingValue) => !shouldSelect(value, existingValue)
34 )
35 )
36 }
37 } else {
38 if (el.checked) {
39 node.input(node.props.onValue)
40 } else {
41 node.input(node.props.offValue)
42 }
43 }
44 }
45}
46
47/**
48 * Checks if a given option is present in the node value.

Callers

nothing calls this directly

Calls 2

optionValueFunction · 0.90
shouldSelectFunction · 0.90

Tested by

no test coverage detected