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

Function nodeInit

packages/core/src/node.ts:3306–3354  ·  view source on GitHub ↗

* Initialize a node object's internal properties. * * @param node - A FormKitNode | FormKitNode * @param options - An options object of FormKitOptions | FormKitOptions to override the defaults. * * @returns A FormKitNode | FormKitNode * * @internal

(
  node: FormKitNode,
  options: FormKitOptions
)

Source from the content-addressed store, hash-verified

3304 * @internal
3305 */
3306function nodeInit<V>(
3307 node: FormKitNode,
3308 options: FormKitOptions
3309): FormKitNode<V> {
3310 const hasInitialId = options.props?.id
3311 if (!hasInitialId) delete options.props?.id
3312 // Set the internal node on the props, config, ledger and store
3313 node.ledger.init((node.store._n = node.props._n = node.config._n = node))
3314 // Apply given in options to the node.
3315 node.props._emit = false
3316 // Sets the initial props and initial ID if not provided.
3317 Object.assign(
3318 node.props,
3319 hasInitialId ? {} : { id: `input_${idCount++}` },
3320 options.props ?? {}
3321 )
3322 node.props._emit = true
3323 // Attempt to find a definition from the pre-existing plugins.
3324 findDefinition(
3325 node,
3326 new Set([
3327 ...(options.plugins || []),
3328 ...(node.parent ? node.parent.plugins : []),
3329 ])
3330 )
3331 // Then we apply each plugin's root code, we do this with an explicit loop
3332 // for that ity-bitty performance bump.
3333 if (options.plugins) {
3334 for (const plugin of options.plugins) {
3335 use(node, node._c, plugin, true, false)
3336 }
3337 }
3338 // Apply the parent to each child.
3339 node.each((child) => node.add(child))
3340 // If the node has a parent, ensure it's properly nested bi-directionally.
3341 if (node.parent) node.parent.add(node, options.index)
3342 // Inputs are leafs, and cannot have children
3343 if (node.type === 'input' && node.children.length) error(100, node)
3344 // Apply the input hook to the initial value.
3345 input(node, node._c, node._value, false)
3346 // Release the store buffer
3347 node.store.release()
3348 // Register the node globally if someone explicitly gave it an id
3349 if (hasInitialId) register(node)
3350 // Our node is finally ready, emit it to the world
3351 node.emit('created', node)
3352 node.isCreated = true
3353 return node as FormKitNode<V>
3354}
3355
3356/**
3357 * Creates a placeholder node that can be used to hold a place in a the children

Callers 1

createNodeFunction · 0.85

Calls 5

errorFunction · 0.90
registerFunction · 0.90
findDefinitionFunction · 0.85
useFunction · 0.70
inputFunction · 0.70

Tested by

no test coverage detected