import React, { useContext, forwardRef, ForwardedRef, ForwardRefExoticComponent, ForwardRefRenderFunction, } from 'react'; import classNames from 'classnames'; import isString from 'lodash/isString'; import Context from './common/context'; import Row from './row'; import Cell from './cell'; import { CnLayoutBlockProps, LayoutContextProps, TypeMark, CnLayoutBlockStatic, } from './types'; import { cacheFoldStore } from './utils/const'; type IBlock = ForwardRefExoticComponent & TypeMark & CnLayoutBlockStatic; /** * 区块,默认 flex 布局 * @param props * @param ref * @constructor */ const Block: ForwardRefRenderFunction = ( props, ref: ForwardedRef, ) => { const { className, title, titleAlign, extra, noPadding: noPaddingProp, mode, bordered, width, contentClassName, contentStyle = {}, span: spanProp, divider, children, align, verAlign, ...others } = props; const { prefix, maxNumberOfColumns } = useContext(Context); const isTransparent = mode === 'transparent'; const noPadding = !isTransparent ? noPaddingProp : true; const hasHead = !isTransparent && (title || extra); let span = spanProp; if (!span || span > maxNumberOfColumns || span <= 0) { span = maxNumberOfColumns; } const clsPrefix = `${prefix}block`; const blockCls = classNames(className, clsPrefix, { [`${prefix}bg--${mode}`]: mode, [`${clsPrefix}--no-padding`]: noPadding, [`${clsPrefix}--headless`]: !hasHead, [`${clsPrefix}--span-${span}`]: span > 0, [`${clsPrefix}--bordered`]: !isTransparent && bordered, [`${clsPrefix}--divided`]: divider, }); const headCls = classNames({ [`${clsPrefix}-head`]: true, }); const blockContentCls = classNames(contentClassName, { [`${clsPrefix}-content`]: true, [`${clsPrefix}-content--no-padding`]: noPadding, }); // 当有 title 或 extra 节点时 if (hasHead) { return (
{isString(title) ? {title} : title} {extra ? ( {isString(extra) ? {extra} : extra} ) : null} {children}
); } return ( {children} ); }; // @ts-ignore const RefBlock: IBlock = forwardRef(Block); RefBlock.displayName = 'Block'; RefBlock.defaultProps = { // mode: 'surface', noPadding: true, }; RefBlock.typeMark = 'Block'; RefBlock.updateCacheFoldStore = (_nodeId: string, value: boolean) => { cacheFoldStore[_nodeId] = value; }; export default RefBlock;