| 9 | } |
| 10 | |
| 11 | class Composer { |
| 12 | components: IComponents = {} |
| 13 | |
| 14 | rootComponentType: ComponentType | undefined = undefined |
| 15 | |
| 16 | constructor(name?: ComponentType) { |
| 17 | if (name) { |
| 18 | this.rootComponentType = name |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | addNode = ({ |
| 23 | type, |
| 24 | parent = 'root', |
| 25 | props = {}, |
| 26 | rootParentType, |
| 27 | }: AddNode): string => { |
| 28 | const id = generateId() |
| 29 | |
| 30 | if (parent === 'root' && !this.rootComponentType) { |
| 31 | this.rootComponentType = type |
| 32 | } |
| 33 | const localRootParentType = rootParentType || this.rootComponentType |
| 34 | |
| 35 | const { form, ...defaultProps } = DEFAULT_PROPS[type] || {} |
| 36 | |
| 37 | this.components = { |
| 38 | ...this.components, |
| 39 | [id]: { |
| 40 | children: [], |
| 41 | type, |
| 42 | parent, |
| 43 | id, |
| 44 | props: { ...defaultProps, ...props }, |
| 45 | rootParentType: localRootParentType, |
| 46 | }, |
| 47 | } |
| 48 | |
| 49 | if (parent !== 'root' && this.components[parent]) { |
| 50 | this.components[parent].children.push(id) |
| 51 | } |
| 52 | |
| 53 | return id |
| 54 | } |
| 55 | |
| 56 | getComponents() { |
| 57 | return this.components |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | export default Composer |
nothing calls this directly
no test coverage detected