import React from 'react'; import {Renderer, RendererProps} from '../factory'; import {BaseSchema, SchemaCollection} from '../Schema'; import {resolveVariable} from '../utils/tpl-builtin'; import {SchemaNode} from '../types'; import mapValues from 'lodash/mapValues'; /** * Wrapper 容器渲染器。 * 文档:https://baidu.gitee.io/amis/docs/components/wrapper */ export interface WrapperSchema extends BaseSchema { /** * 指定为 container 类型 */ type: 'wrapper'; /** * 内容 */ body: SchemaCollection; size?: 'xs' | 'sm' | 'md' | 'lg' | 'none'; /** * 自定义样式 */ style?: { [propName: string]: any; }; } export interface WrapperProps extends RendererProps, Omit { children?: JSX.Element | ((props?: any) => JSX.Element); } export default class Wrapper extends React.Component { static propsList: Array = ['body', 'className', 'children', 'size']; static defaultProps: Partial = { className: '', size: 'md' }; renderBody(): JSX.Element | null { const {children, body, render} = this.props; return children ? typeof children === 'function' ? (children(this.props) as JSX.Element) : (children as JSX.Element) : body ? (render('body', body) as JSX.Element) : null; } render() { const {className, size, classnames: cx, style, data} = this.props; let styleVar = typeof style === 'string' ? resolveVariable(style, data) || {} : mapValues(style, s => resolveVariable(s, data) || s); return (
{this.renderBody()}
); } } @Renderer({ type: 'wrapper', name: 'wrapper' }) export class WrapperRenderer extends Wrapper {}