import type {FC, ReactNode} from 'react'; import {useMemo} from 'react'; import {Element, Field, Group} from '@givewp/forms/types'; /** * Get the active template from the window * * @since 3.0.0 */ const template = window.givewp.form.templates /** * Get the NodeWrapper from active template * * @since 3.0.0 */ const NodeWrapper = template.layouts.wrapper; /** * Find the names of nodeType and type based on the value of the template. * * @since 3.0.0 */ export function findTemplateKeys( templateValue: ReactNode ): {nodeType: S | null; type: T | null} { let nodeType = null; let type = null; Object.entries(template).forEach((firstLevel) => { if (typeof firstLevel[1] === 'object') { const firstLevelKey = firstLevel[0]; const secondLevelKeys = Object.keys(firstLevel[1]); const templateType = secondLevelKeys.find((key) => firstLevel[1][key] === templateValue); if (templateType) { type = templateType; nodeType = firstLevelKey; } } }); return {nodeType, type}; } /** * This HOC will wrap a template component in our NodeWrapper and automatically figure out what nodeType and type to use as props. * * @since 3.0.0 */ export function withTemplateWrapper( Template: FC, htmlTag: keyof JSX.IntrinsicElements = 'div', name?: string ): FC { // @ts-ignore const {nodeType, type} = findTemplateKeys(Template); return (props: TemplateProps) => (