UNPKG

react-antd-admin-panel

Version:

Modern TypeScript-first React admin panel builder with Ant Design 6

1 lines 9.86 kB
{"version":3,"file":"Section-QsEya_jl.cjs","sources":["../src/section/Section.tsx"],"sourcesContent":["import React from 'react';\nimport { Row, Col, Card, Modal, Drawer } from 'antd';\nimport type { RowProps, ColProps, CardProps, ModalProps, DrawerProps } from 'antd';\nimport { BaseBuilder } from '../base/BaseBuilder';\nimport type { ComponentConfig } from '../types';\n\nexport interface ResponsiveBreakpoint {\n xs?: number; // <576px\n sm?: number; // ≥576px\n md?: number; // ≥768px\n lg?: number; // ≥992px\n xl?: number; // ≥1200px\n xxl?: number; // ≥1600px\n}\n\nexport interface SectionConfig extends ComponentConfig {\n col?: number;\n responsive?: ResponsiveBreakpoint;\n row?: boolean;\n card?: boolean;\n cardProps?: CardProps;\n modal?: boolean;\n modalProps?: ModalProps;\n drawer?: boolean;\n drawerProps?: DrawerProps;\n justify?: 'start' | 'end' | 'center' | 'space-around' | 'space-between' | 'space-evenly';\n align?: 'top' | 'middle' | 'bottom';\n gutter?: number | [number, number];\n style?: React.CSSProperties;\n}\n\n/**\n * Section - Layout and composition component\n * Manages grid layout, child components, and form integration\n * \n * @example\n * const section = new Section()\n * .card(true)\n * .col(12)\n * .add(new Input().key('name').label('Name'))\n * .add(new Input().key('email').label('Email'));\n */\nexport class Section extends BaseBuilder<SectionConfig> {\n public _children: Array<{ render(): React.ReactNode } | React.ReactNode> = [];\n public _rowChildren: Array<{ render(): React.ReactNode } | React.ReactNode> = [];\n\n /**\n * Set column span (24-grid system)\n */\n col(span: number): this {\n this._config.col = span;\n return this;\n }\n\n /**\n * Set responsive breakpoints for column span\n */\n responsive(breakpoints: ResponsiveBreakpoint): this {\n this._config.responsive = breakpoints;\n return this;\n }\n\n /**\n * Enable row layout (horizontal)\n */\n row(value: boolean = true): this {\n this._config.row = value;\n return this;\n }\n\n /**\n * Wrap in Card component\n */\n card(value: boolean | CardProps = true): this {\n if (typeof value === 'boolean') {\n this._config.card = value;\n } else {\n this._config.card = true;\n this._config.cardProps = value;\n }\n return this;\n }\n\n /**\n * Render in Modal\n */\n modal(value: boolean | ModalProps = true): this {\n if (typeof value === 'boolean') {\n this._config.modal = value;\n } else {\n this._config.modal = true;\n this._config.modalProps = value;\n }\n return this;\n }\n\n /**\n * Render in Drawer\n */\n drawer(value: boolean | DrawerProps = true): this {\n if (typeof value === 'boolean') {\n this._config.drawer = value;\n } else {\n this._config.drawer = true;\n this._config.drawerProps = value;\n }\n return this;\n }\n\n /**\n * Set row justify alignment\n */\n justify(value: 'start' | 'end' | 'center' | 'space-around' | 'space-between' | 'space-evenly'): this {\n this._config.justify = value;\n return this;\n }\n\n /**\n * Set row align\n */\n align(value: 'top' | 'middle' | 'bottom'): this {\n this._config.align = value;\n return this;\n }\n\n /**\n * Set gutter (spacing between columns)\n */\n gutter(value: number | [number, number]): this {\n this._config.gutter = value;\n return this;\n }\n\n /**\n * Set custom styles\n */\n style(value: React.CSSProperties): this {\n this._config.style = value;\n return this;\n }\n\n /**\n * Add a child component\n */\n add(child: { render(): React.ReactNode } | React.ReactNode): this {\n // Store the builder itself, not the rendered result\n this._children.push(child);\n return this;\n }\n\n /**\n * Add multiple children\n */\n addMore(children: Array<{ render(): React.ReactNode } | React.ReactNode>): this {\n children.forEach(child => this.add(child));\n return this;\n }\n\n /**\n * Add child to row-end position\n */\n addRowEnd(child: { render(): React.ReactNode } | React.ReactNode): this {\n // Store the builder itself, not the rendered result\n this._rowChildren.push(child);\n return this;\n }\n\n /**\n * Clear all children\n */\n clear(): this {\n this._children = [];\n this._rowChildren = [];\n return this;\n }\n\n /**\n * Get all children\n */\n getChildren(): Array<{ render(): React.ReactNode } | React.ReactNode> {\n return [...this._children];\n }\n\n /**\n * Render method for compatibility - returns a SectionComponent\n */\n render(): React.ReactNode {\n if (this._config.hidden) {\n return null;\n }\n return <SectionComponent section={this} />;\n }\n}\n\n/**\n * SectionComponent - React component that renders a Section builder\n * Wrapped with React.memo to prevent unnecessary re-renders\n */\nconst SectionComponent = React.memo<{ section: Section }>(({ section }) => {\n if (section._config.hidden) {\n return null;\n }\n\n const children: Array<{ render(): React.ReactNode } | React.ReactNode> = [...section._children, ...section._rowChildren];\n\n // Render each child dynamically\n const renderedChildren = children.map((child, index) => {\n if (child && typeof child === 'object' && 'render' in child && typeof (child as any).render === 'function') {\n return <React.Fragment key={index}>{(child as { render(): React.ReactNode }).render()}</React.Fragment>;\n }\n return <React.Fragment key={index}>{child as React.ReactNode}</React.Fragment>;\n });\n\n let content: React.ReactNode;\n\n if (section._config.row) {\n // Row layout\n const rowProps: RowProps = {\n justify: section._config.justify,\n align: section._config.align,\n gutter: section._config.gutter,\n style: section._config.style,\n };\n content = <Row {...rowProps}>{renderedChildren}</Row>;\n } else {\n // Column layout (default)\n content = <div style={section._config.style}>{renderedChildren}</div>;\n }\n\n // Apply wrappers\n if (section._config.card) {\n content = <Card {...section._config.cardProps}>{content}</Card>;\n }\n\n if (section._config.col || section._config.responsive) {\n const colProps: ColProps = {\n span: section._config.col,\n ...section._config.responsive,\n };\n content = <Col {...colProps}>{content}</Col>;\n }\n\n if (section._config.modal) {\n content = (\n <Modal open={true} {...section._config.modalProps}>\n {content}\n </Modal>\n );\n }\n\n if (section._config.drawer) {\n content = (\n <Drawer open={true} {...section._config.drawerProps}>\n {content}\n </Drawer>\n );\n }\n\n return <>{content}</>;\n});\n"],"names":["BaseBuilder","jsx","Row","Card","Col","Modal","Drawer"],"mappings":";;;;;;;;AA0CO,MAAM,gBAAgBA,YAAAA,YAA2B;AAAA,EAAjD;AAAA;AACE,qCAAoE,CAAA;AACpE,wCAAuE,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAK9E,IAAI,MAAoB;AACtB,SAAK,QAAQ,MAAM;AACnB,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,WAAW,aAAyC;AAClD,SAAK,QAAQ,aAAa;AAC1B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAK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