1
0
Fork 0
mirror of synced 2024-09-20 11:27:56 +12:00
budibase/packages/builder/src/components/userInterface/pagesParsing/createProps.js

97 lines
2.4 KiB
JavaScript
Raw Normal View History

import { isString, isUndefined } from "lodash/fp"
import { TYPE_MAP } from "./types"
import { assign } from "lodash"
2020-04-01 23:55:21 +13:00
import { uuid } from "builderStore/uuid"
2019-08-15 09:11:59 +12:00
2020-05-03 22:33:20 +12:00
export const getBuiltin = _component => {
const { props } = createProps({ _component })
2020-02-18 23:32:00 +13:00
return {
2020-05-03 22:33:20 +12:00
_component,
name: "Screenslot",
2020-02-18 23:32:00 +13:00
props,
}
}
/**
* @param {object} componentDefinition - component definition from a component library
* @param {object} derivedFromProps - extra props derived from a components given props.
* @return {object} the fully created properties for the component, and any property parsing errors
*/
export const createProps = (componentDefinition, derivedFromProps) => {
const errorOccurred = (propName, error) => errors.push({ propName, error })
2019-07-21 08:41:06 +12:00
const props = {
_id: uuid(),
_component: componentDefinition._component,
2020-05-23 02:30:29 +12:00
_styles: { normal: {}, hover: {}, active: {}, selected: {} },
2020-05-18 22:01:09 +12:00
_code: "",
}
2019-07-19 23:52:08 +12:00
const errors = []
2019-07-19 23:52:08 +12:00
2020-05-08 01:59:06 +12:00
if (!componentDefinition._component) {
errorOccurred("_component", "Component name not supplied")
2020-05-08 01:59:06 +12:00
}
2019-07-21 08:41:06 +12:00
for (let propName in componentDefinition.props) {
const parsedPropDef = parsePropDef(componentDefinition.props[propName])
2020-05-07 21:53:34 +12:00
if (parsedPropDef.error) {
errors.push({ propName, error: parsedPropDef.error })
2020-05-07 21:53:34 +12:00
} else {
props[propName] = parsedPropDef
}
}
2019-07-19 23:52:08 +12:00
if (derivedFromProps) {
assign(props, derivedFromProps)
}
2019-07-19 23:52:08 +12:00
if (componentDefinition.children !== false && isUndefined(props._children)) {
props._children = []
}
return {
props,
errors,
}
2019-07-19 23:52:08 +12:00
}
export const makePropsSafe = (componentDefinition, props) => {
const safeProps = createProps(componentDefinition, props).props
for (let propName in safeProps) {
props[propName] = safeProps[propName]
}
for (let propName in props) {
if (safeProps[propName] === undefined) {
delete props[propName]
}
}
if (!props._styles) {
2020-05-23 02:30:29 +12:00
props._styles = { normal: {}, hover: {}, active: {}, selected: {} }
}
return props
}
2019-07-19 23:52:08 +12:00
const parsePropDef = propDef => {
const error = message => ({ error: message, propDef })
2019-07-19 23:52:08 +12:00
if (isString(propDef)) {
if (!TYPE_MAP[propDef]) return error(`Type ${propDef} is not recognised.`)
return TYPE_MAP[propDef].default
}
2019-07-19 23:52:08 +12:00
const type = TYPE_MAP[propDef.type]
if (!type) return error(`Type ${propDef.type} is not recognised.`)
2019-07-19 23:52:08 +12:00
return propDef.default
2019-07-19 23:52:08 +12:00
}
export const arrayElementComponentName = (parentComponentName, arrayPropName) =>
`${parentComponentName}:${arrayPropName}`