@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
1 lines • 6.28 kB
Source Map (JSON)
{"version":3,"file":"Form.mjs","names":["Form","FormItem","FormGroup","keys: (string | number)[]","AntForm","FormFlatGroup"],"sources":["../../src/Form/Form.tsx"],"sourcesContent":["'use client';\n\nimport { Form as AntForm } from 'antd';\nimport { cx, useResponsive } from 'antd-style';\nimport { isUndefined } from 'es-toolkit/compat';\nimport { memo, useCallback, useState } from 'react';\n\nimport FormFlatGroup from './components/FormFlatGroup';\nimport FormGroup from './components/FormGroup';\nimport FormItem from './components/FormItem';\nimport { FormProvider } from './components/FormProvider';\nimport { variants } from './style';\nimport type { FormGroupItemType, FormItemProps, FormProps } from './type';\n\nconst Form = memo<FormProps>(\n ({\n className,\n itemMinWidth,\n footer,\n form,\n items = [],\n children,\n itemsType = 'group',\n itemVariant,\n variant = 'borderless',\n classNames,\n styles: customStyles,\n gap,\n style,\n collapsible,\n defaultActiveKey,\n initialValues,\n activeKey,\n onCollapse,\n onFinish,\n ref,\n layout,\n ...rest\n }) => {\n const { mobile } = useResponsive();\n const [submitLoading, setSubmitLoading] = useState(false);\n\n const mapFlat = useCallback(\n (item: FormItemProps, itemIndex: number) => (\n <FormItem\n className={classNames?.item}\n divider={itemIndex !== 0}\n key={itemIndex}\n minWidth={itemMinWidth}\n style={customStyles?.item}\n variant={variant}\n {...item}\n />\n ),\n [itemMinWidth, variant, classNames, customStyles],\n );\n\n const mapTree = useCallback(\n (group: FormGroupItemType, groupIndex: number) => {\n const key = group?.key || groupIndex;\n return (\n <FormGroup\n active={activeKey && group?.key ? activeKey.includes(key) : undefined}\n className={classNames?.group}\n classNames={classNames}\n collapsible={isUndefined(group.collapsible) ? collapsible : group.collapsible}\n defaultActive={\n defaultActiveKey && group?.key ? defaultActiveKey.includes(key) : group?.defaultActive\n }\n extra={group?.extra}\n icon={group?.icon}\n key={key}\n keyValue={key}\n onCollapse={(active) => {\n let keys: (string | number)[] = activeKey || defaultActiveKey || [];\n keys = keys.filter((k) => k !== key);\n onCollapse?.(active ? [...keys, key] : keys);\n }}\n style={customStyles?.group}\n styles={customStyles}\n title={group.title}\n variant={group?.variant || variant}\n >\n {Array.isArray(group.children)\n ? group.children.filter((item) => !item.hidden).map((item, i) => mapFlat(item, i))\n : group.children}\n </FormGroup>\n );\n },\n [activeKey, collapsible, defaultActiveKey, onCollapse, variant, classNames, customStyles],\n );\n\n return (\n <FormProvider\n config={{\n form,\n initialValues,\n submitLoading,\n }}\n >\n <AntForm\n className={cx(variants({ variant }), className)}\n colon={false}\n form={form}\n initialValues={initialValues}\n layout={layout || (mobile ? 'vertical' : 'horizontal')}\n onFinish={async (...finishProps) => {\n if (!onFinish) return;\n setSubmitLoading(true);\n await onFinish(...finishProps);\n setSubmitLoading(false);\n }}\n ref={ref}\n style={{\n gap,\n ...style,\n }}\n variant={itemVariant}\n {...rest}\n >\n {items && items?.length > 0 ? (\n itemsType === 'group' ? (\n (items as FormGroupItemType[])?.map((item, i) => mapTree(item, i))\n ) : (\n <FormFlatGroup\n className={classNames?.group}\n style={customStyles?.group}\n variant={variant}\n >\n {(items as FormItemProps[])\n ?.filter((item) => !item.hidden)\n .map((item, i) => mapFlat(item, i))}\n </FormFlatGroup>\n )\n ) : undefined}\n {children}\n {footer}\n </AntForm>\n </FormProvider>\n );\n },\n);\n\nForm.displayName = 'Form';\n\nexport default Form;\n"],"mappings":";;;;;;;;;;;;;;AAcA,MAAMA,SAAO,MACV,EACC,WACA,cACA,QACA,MACA,QAAQ,EAAE,EACV,UACA,YAAY,SACZ,aACA,UAAU,cACV,YACA,QAAQ,cACR,KACA,OACA,aACA,kBACA,eACA,WACA,YACA,UACA,KACA,QACA,GAAG,WACC;CACJ,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,CAAC,eAAe,oBAAoB,SAAS,MAAM;CAEzD,MAAM,UAAU,aACb,MAAqB,cACpB,oBAACC;EACC,WAAW,YAAY;EACvB,SAAS,cAAc;EAEvB,UAAU;EACV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