* Create props based on initial values * * @param initial - An initial value to be transformed * * @internal
(initial: unknown)
| 3145 | * @internal |
| 3146 | */ |
| 3147 | function createProps(initial: unknown) { |
| 3148 | const props: Record<PropertyKey, any> = { |
| 3149 | initial: typeof initial === 'object' ? cloneAny(initial) : initial, |
| 3150 | } |
| 3151 | let node: FormKitNode |
| 3152 | let isEmitting = true |
| 3153 | let propDefs: Record<PropertyKey, FormKitPseudoProp> = {} |
| 3154 | return new Proxy(props, { |
| 3155 | get(...args) { |
| 3156 | const [_t, prop] = args |
| 3157 | let val |
| 3158 | if (has(props, prop)) { |
| 3159 | val = Reflect.get(...args) |
| 3160 | if (propDefs[prop]?.boolean) val = boolGetter(val) |
| 3161 | } else if ( |
| 3162 | node && |
| 3163 | typeof prop === 'string' && |
| 3164 | node.config[prop] !== undefined |
| 3165 | ) { |
| 3166 | val = node.config[prop] |
| 3167 | // If we are getting the merge strategy for an input, only retrieve this |
| 3168 | // actual node’s merge strategy. |
| 3169 | if ( |
| 3170 | prop === 'mergeStrategy' && |
| 3171 | node?.type === 'input' && |
| 3172 | isRecord(val) && |
| 3173 | node.name in val |
| 3174 | ) { |
| 3175 | val = val[node.name] |
| 3176 | } |
| 3177 | } else { |
| 3178 | // default or undefined |
| 3179 | val = propDefs[prop]?.default |
| 3180 | } |
| 3181 | const getter = propDefs[prop]?.getter |
| 3182 | if (propDefs[prop]?.boolean) val = !!val |
| 3183 | return getter ? getter(val, node) : val |
| 3184 | }, |
| 3185 | set(target, property, originalValue, receiver) { |
| 3186 | if (property === '_n') { |
| 3187 | node = originalValue |
| 3188 | return true |
| 3189 | } |
| 3190 | if (property === '_emit') { |
| 3191 | isEmitting = originalValue |
| 3192 | return true |
| 3193 | } |
| 3194 | // eslint-disable-next-line prefer-const |
| 3195 | let { prop, value } = node.hook.prop.dispatch({ |
| 3196 | prop: property, |
| 3197 | value: originalValue, |
| 3198 | }) |
| 3199 | const setter = propDefs[prop]?.setter |
| 3200 | value = setter ? setter(value, node) : value |
| 3201 | // Typescript compiler cannot handle a symbol index, even though js can: |
| 3202 | if ( |
| 3203 | !eq(props[prop as string], value, false) || |
| 3204 | typeof value === 'object' |
no test coverage detected