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

Function addProps

packages/core/src/node.ts:2324–2375  ·  view source on GitHub ↗

* Adds props to a given node by stripping them out of the node.props.attrs and * then adding them to the nodes. * * @param node - A FormKitNode | FormKitNode * @param context - A FormKitContext | FormKitContext * @param props - An array of prop strings (in camelCase!) * * @ret

(
  node: FormKitNode,
  context: FormKitContext,
  props: FormKitPseudoProps
)

Source from the content-addressed store, hash-verified

2322 * @internal
2323 */
2324function addProps(
2325 node: FormKitNode,
2326 context: FormKitContext,
2327 props: FormKitPseudoProps
2328): FormKitNode {
2329 const propNames = Array.isArray(props) ? props : Object.keys(props)
2330 const defaults: Record<string, unknown> = !Array.isArray(props)
2331 ? propNames.reduce((defaults, name) => {
2332 if ('default' in props[name]) {
2333 defaults[name] = props[name].default
2334 }
2335 return defaults
2336 }, {} as Record<string, unknown>)
2337 : {}
2338 if (node.props.attrs) {
2339 const attrs = { ...node.props.attrs }
2340 // Temporarily disable prop emits
2341 node.props._emit = false
2342 for (const attr in attrs) {
2343 const camelName = camel(attr)
2344 if (propNames.includes(camelName)) {
2345 node.props[camelName] = attrs[attr]
2346 delete attrs[attr]
2347 }
2348 }
2349 // Assign defaults to any props
2350 if (!Array.isArray(props)) {
2351 propNames.forEach((prop) => {
2352 if ('default' in props[prop] && node.props[prop] === undefined) {
2353 node.props[prop] = defaults[prop]
2354 }
2355 })
2356 }
2357 const initial = cloneAny(context._value)
2358 node.props.initial =
2359 node.type !== 'input' ? init(initial as KeyedValue) : initial
2360 // Re-enable prop emits
2361 node.props._emit = true
2362 node.props.attrs = attrs
2363 }
2364 const mergedProps = mergeProps(node.props.__propDefs ?? [], props)
2365
2366 if (node.props.definition) {
2367 node.props.definition.props = mergedProps
2368 }
2369
2370 // @ts-ignore-next-line
2371 node.props.__propDefs = mergedProps
2372
2373 node.emit('added-props', props)
2374 return node
2375}
2376
2377function toPropsObj(
2378 props: FormKitPseudoProps

Callers

nothing calls this directly

Calls 4

camelFunction · 0.90
cloneAnyFunction · 0.90
initFunction · 0.90
mergePropsFunction · 0.85

Tested by

no test coverage detected