File size: 4,700 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
/* eslint-disable max-len */
import {
BoxProps,
HeaderProps,
TextProps,
BadgeProps,
ButtonProps,
LinkProps,
LabelProps,
IconProps,
} from '@adminjs/design-system'
import { PropsWithChildren } from 'react'
import { CurrentAdmin } from '../../../current-admin.interface.js'
export type LayoutElement =
string |
Array<string> |
Array<LayoutElement> |
[string, PropsWithChildren<BoxProps>] |
[PropsWithChildren<BoxProps>, Array<LayoutElement>] |
['@Header', PropsWithChildren<HeaderProps>] |
['@H1', PropsWithChildren<HeaderProps>] |
['@H2', PropsWithChildren<HeaderProps>] |
['@H3', PropsWithChildren<HeaderProps>] |
['@H4', PropsWithChildren<HeaderProps>] |
['@H5', PropsWithChildren<HeaderProps>] |
['@Text', PropsWithChildren<TextProps>] |
['@Badge', PropsWithChildren<BadgeProps>] |
['@Button', PropsWithChildren<ButtonProps>] |
['@Link', PropsWithChildren<LinkProps>] |
['@Label', PropsWithChildren<LabelProps>] |
['@Icon', PropsWithChildren<IconProps>] |
[string, PropsWithChildren<any>]
/**
* Function returning Array<LayoutElement> used by {@link Action#layout}
*
* @return {Array<LayoutElement>}
* @memberof Action
* @alias LayoutElementFunction
*/
export type LayoutElementFunction = (currentAdmin?: CurrentAdmin) => Array<LayoutElement>
/**
* It is generated from {@link Array<LayoutElement>} passed in {@link Action#layout}
*
* @alias ParsedLayoutElement
* @memberof ActionJSON
*/
export type ParsedLayoutElement = {
/** List of paths to properties which should be rendered by given element */
properties: Array<string>;
/** props passed to React component which wraps elements */
props: PropsWithChildren<any>;
/** Nested layout elements */
layoutElements: Array<ParsedLayoutElement>;
/** Component which should be used as a wrapper */
component: string;
}
const isProp = (element): boolean => !!element
&& typeof element === 'object'
&& !Array.isArray(element)
const isComponentTag = (layoutElement: LayoutElement): boolean => (
Array.isArray(layoutElement)
&& typeof layoutElement[0] === 'string'
&& layoutElement[0].startsWith('@')
&& isProp(layoutElement[1])
)
const hasOnlyStringsProperties = function (
layoutElement: LayoutElement,
): layoutElement is [string] {
return Array.isArray(layoutElement)
&& layoutElement.length > 0
&& !isComponentTag(layoutElement)
&& !(layoutElement as Array<any>).find((el) => (typeof el !== 'string'))
}
const hasArrayOfLayoutElements = function (
layoutElement: LayoutElement,
): layoutElement is [LayoutElement] {
return Array.isArray(layoutElement)
&& layoutElement.length > 0
&& !(layoutElement as Array<any>).find((element) => !Array.isArray(element))
}
const hasFirstStringProperty = function (layoutElement: LayoutElement): boolean {
return Array.isArray(layoutElement)
&& typeof layoutElement[0] === 'string'
&& !isComponentTag(layoutElement)
}
const getPropertyNames = (layoutElement: LayoutElement): Array<string> => {
if (typeof layoutElement === 'string') { return [layoutElement] }
if (hasOnlyStringsProperties(layoutElement)) {
return layoutElement
}
if (hasFirstStringProperty(layoutElement)) {
return [layoutElement[0]] as Array<string>
}
return []
}
const getInnerLayoutElements = (layoutElement: LayoutElement): Array<LayoutElement> => {
// only cases like [{}, layoutElement] (whatever follows props)
if (Array.isArray(layoutElement)
&& isProp(layoutElement[0])) {
return layoutElement[1] as Array<LayoutElement>
}
if (hasArrayOfLayoutElements(layoutElement)) {
return layoutElement
}
return []
}
const getComponent = (layoutElement: LayoutElement): string => {
if (isComponentTag(layoutElement)) {
return (layoutElement[0] as string).slice(1)
}
return 'Box'
}
const getProps = (layoutElement: LayoutElement): BoxProps => {
if (Array.isArray(layoutElement) && layoutElement.length) {
const boxProps = (layoutElement as Array<any>).find(isProp)
return boxProps as unknown as BoxProps || {}
}
return {}
}
export const layoutElementParser = (layoutElement: LayoutElement): ParsedLayoutElement => {
const props = getProps(layoutElement)
const innerLayoutElements = getInnerLayoutElements(layoutElement)
const properties = getPropertyNames(layoutElement)
const component = getComponent(layoutElement)
return {
props,
layoutElements: innerLayoutElements.map((el) => layoutElementParser(el)),
properties,
component,
}
}
export default layoutElementParser
/**
* @load layout-element.doc.md
* @name LayoutElement
* @typedef {String | Array} LayoutElement
* @memberof Action
* @alias LayoutElement
*/
|