@aura-react-comp/ui 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -2
  3. package/esm/alert/index.d.ts +28 -0
  4. package/esm/alert/index.js +76 -0
  5. package/esm/alert/index.less +172 -0
  6. package/esm/avatar/index.d.ts +41 -0
  7. package/esm/avatar/index.js +117 -0
  8. package/esm/avatar/index.less +162 -0
  9. package/esm/badge/index.d.ts +29 -0
  10. package/esm/badge/index.js +84 -0
  11. package/esm/badge/index.less +215 -0
  12. package/esm/breadcrumb/index.d.ts +31 -0
  13. package/esm/breadcrumb/index.js +91 -0
  14. package/esm/breadcrumb/index.less +108 -0
  15. package/esm/button/index.d.ts +28 -0
  16. package/esm/button/index.js +59 -0
  17. package/esm/button/index.less +191 -0
  18. package/esm/card/index.d.ts +67 -0
  19. package/esm/card/index.js +156 -0
  20. package/esm/card/index.less +231 -0
  21. package/esm/checkbox/index.d.ts +35 -0
  22. package/esm/checkbox/index.js +118 -0
  23. package/esm/checkbox/index.less +141 -0
  24. package/esm/collapse/index.d.ts +45 -0
  25. package/esm/collapse/index.js +157 -0
  26. package/esm/collapse/index.less +144 -0
  27. package/esm/divider/index.d.ts +16 -0
  28. package/esm/divider/index.js +37 -0
  29. package/esm/divider/index.less +115 -0
  30. package/esm/dropdown/index.d.ts +38 -0
  31. package/esm/dropdown/index.js +108 -0
  32. package/esm/dropdown/index.less +245 -0
  33. package/esm/empty/index.d.ts +35 -0
  34. package/esm/empty/index.js +87 -0
  35. package/esm/empty/index.less +67 -0
  36. package/esm/flex/index.d.ts +24 -0
  37. package/esm/flex/index.js +64 -0
  38. package/esm/flex/index.less +9 -0
  39. package/esm/form/index.d.ts +121 -0
  40. package/esm/form/index.js +443 -0
  41. package/esm/form/index.less +257 -0
  42. package/esm/global.d.ts +6 -0
  43. package/esm/index.d.ts +78 -0
  44. package/esm/index.js +41 -0
  45. package/esm/input/index.d.ts +51 -0
  46. package/esm/input/index.js +217 -0
  47. package/esm/input/index.less +369 -0
  48. package/esm/layout/index.d.ts +46 -0
  49. package/esm/layout/index.js +135 -0
  50. package/esm/layout/index.less +132 -0
  51. package/esm/menu/index.d.ts +91 -0
  52. package/esm/menu/index.js +294 -0
  53. package/esm/menu/index.less +340 -0
  54. package/esm/message/index.d.ts +33 -0
  55. package/esm/message/index.js +199 -0
  56. package/esm/message/index.less +113 -0
  57. package/esm/notification/index.d.ts +33 -0
  58. package/esm/notification/index.js +229 -0
  59. package/esm/notification/index.less +175 -0
  60. package/esm/pagination/index.d.ts +38 -0
  61. package/esm/pagination/index.js +180 -0
  62. package/esm/pagination/index.less +238 -0
  63. package/esm/popconfirm/index.d.ts +37 -0
  64. package/esm/popconfirm/index.js +115 -0
  65. package/esm/popconfirm/index.less +229 -0
  66. package/esm/radio/index.d.ts +33 -0
  67. package/esm/radio/index.js +122 -0
  68. package/esm/radio/index.less +119 -0
  69. package/esm/rate/index.d.ts +30 -0
  70. package/esm/rate/index.js +143 -0
  71. package/esm/rate/index.less +87 -0
  72. package/esm/result/index.d.ts +19 -0
  73. package/esm/result/index.js +69 -0
  74. package/esm/result/index.less +103 -0
  75. package/esm/scrollbar/index.d.ts +18 -0
  76. package/esm/scrollbar/index.js +35 -0
  77. package/esm/scrollbar/index.less +67 -0
  78. package/esm/select/index.d.ts +63 -0
  79. package/esm/select/index.js +358 -0
  80. package/esm/select/index.less +427 -0
  81. package/esm/slider/index.d.ts +36 -0
  82. package/esm/slider/index.js +255 -0
  83. package/esm/slider/index.less +154 -0
  84. package/esm/space/index.d.ts +16 -0
  85. package/esm/space/index.js +34 -0
  86. package/esm/space/index.less +34 -0
  87. package/esm/spin/index.d.ts +44 -0
  88. package/esm/spin/index.js +152 -0
  89. package/esm/spin/index.less +170 -0
  90. package/esm/steps/index.d.ts +55 -0
  91. package/esm/steps/index.js +165 -0
  92. package/esm/steps/index.less +394 -0
  93. package/esm/switch/index.d.ts +30 -0
  94. package/esm/switch/index.js +56 -0
  95. package/esm/switch/index.less +135 -0
  96. package/esm/tabs/index.d.ts +45 -0
  97. package/esm/tabs/index.js +170 -0
  98. package/esm/tabs/index.less +226 -0
  99. package/esm/tag/index.d.ts +52 -0
  100. package/esm/tag/index.js +120 -0
  101. package/esm/tag/index.less +227 -0
  102. package/esm/textarea/index.d.ts +27 -0
  103. package/esm/textarea/index.js +84 -0
  104. package/esm/textarea/index.less +224 -0
  105. package/esm/theme/ThemeProvider.d.ts +15 -0
  106. package/esm/theme/ThemeProvider.js +32 -0
  107. package/esm/theme/index.d.ts +2 -0
  108. package/esm/theme/index.js +1 -0
  109. package/esm/theme/tokens.css +183 -0
  110. package/esm/theme/useTheme.d.ts +1 -0
  111. package/esm/theme/useTheme.js +1 -0
  112. package/esm/tooltip/index.d.ts +38 -0
  113. package/esm/tooltip/index.js +149 -0
  114. package/esm/tooltip/index.less +244 -0
  115. package/esm/typography/index.d.ts +26 -0
  116. package/esm/typography/index.js +92 -0
  117. package/esm/typography/index.less +106 -0
  118. package/esm/upload/index.d.ts +68 -0
  119. package/esm/upload/index.js +447 -0
  120. package/esm/upload/index.less +371 -0
  121. package/esm/upload/request.d.ts +26 -0
  122. package/esm/upload/request.js +38 -0
  123. package/package.json +56 -3
@@ -0,0 +1,24 @@
1
+ import React from 'react';
2
+ export interface FlexProps {
3
+ /** 主轴方向
4
+ * @default 'row'
5
+ */
6
+ direction?: 'row' | 'column' | 'row-reverse' | 'column-reverse';
7
+ /** 主轴对齐方式 */
8
+ justify?: 'start' | 'center' | 'end' | 'between' | 'around' | 'evenly';
9
+ /** 交叉轴对齐方式 */
10
+ align?: 'start' | 'center' | 'end' | 'stretch' | 'baseline';
11
+ /** 是否换行,传 true 等价于 'wrap'
12
+ * @default 'nowrap'
13
+ */
14
+ wrap?: 'nowrap' | 'wrap' | 'wrap-reverse' | true;
15
+ /** 间距:预设值或具体像素值 */
16
+ gap?: 'sm' | 'md' | 'lg' | number;
17
+ /** 自定义类名 */
18
+ className?: string;
19
+ /** 自定义样式 */
20
+ style?: React.CSSProperties;
21
+ /** 子元素 */
22
+ children?: React.ReactNode;
23
+ }
24
+ export declare const Flex: React.ForwardRefExoticComponent<FlexProps & React.RefAttributes<HTMLDivElement>>;
@@ -0,0 +1,64 @@
1
+ import React, { forwardRef } from 'react';
2
+ import { classNames, prefixCls } from '@aura-react-comp/shared';
3
+ import "./index.less";
4
+ import { jsx as _jsx } from "react/jsx-runtime";
5
+ /** justify 映射 */
6
+ const justifyMap = {
7
+ start: 'flex-start',
8
+ center: 'center',
9
+ end: 'flex-end',
10
+ between: 'space-between',
11
+ around: 'space-around',
12
+ evenly: 'space-evenly'
13
+ };
14
+
15
+ /** align 映射 */
16
+ const alignMap = {
17
+ start: 'flex-start',
18
+ center: 'center',
19
+ end: 'flex-end',
20
+ stretch: 'stretch',
21
+ baseline: 'baseline'
22
+ };
23
+
24
+ /** gap 预设映射 */
25
+ const gapMap = {
26
+ sm: 8,
27
+ md: 16,
28
+ lg: 24
29
+ };
30
+ export const Flex = /*#__PURE__*/forwardRef(({
31
+ direction = 'row',
32
+ justify,
33
+ align,
34
+ wrap = 'nowrap',
35
+ gap,
36
+ className,
37
+ style,
38
+ children
39
+ }, ref) => {
40
+ const cls = classNames(prefixCls('flex'), className);
41
+
42
+ // 构建 inline style 用于动态 flex 属性
43
+ const flexStyle = {
44
+ ...style,
45
+ flexDirection: direction,
46
+ flexWrap: wrap === true ? 'wrap' : wrap,
47
+ ...(justify ? {
48
+ justifyContent: justifyMap[justify]
49
+ } : {}),
50
+ ...(align ? {
51
+ alignItems: alignMap[align]
52
+ } : {}),
53
+ ...(gap !== undefined ? {
54
+ gap: typeof gap === 'number' ? `${gap}px` : `${gapMap[gap]}px`
55
+ } : {})
56
+ };
57
+ return /*#__PURE__*/_jsx("div", {
58
+ ref: ref,
59
+ className: cls,
60
+ style: flexStyle,
61
+ children: children
62
+ });
63
+ });
64
+ Flex.displayName = 'Flex';
@@ -0,0 +1,9 @@
1
+ /* ===== Flex 组件样式 ===== */
2
+
3
+ .aura-flex {
4
+ display: flex;
5
+ box-sizing: border-box;
6
+ }
7
+
8
+ // ===== 暗色模式 =====
9
+ // Flex 为纯布局容器,无需暗色模式特殊样式
@@ -0,0 +1,121 @@
1
+ import React from 'react';
2
+ export interface FormError {
3
+ name: string;
4
+ errors: string[];
5
+ }
6
+ export interface RuleType {
7
+ required?: boolean;
8
+ message?: string;
9
+ pattern?: RegExp;
10
+ min?: number;
11
+ max?: number;
12
+ validator?: (value: unknown) => boolean | Promise<boolean>;
13
+ }
14
+ export interface FormListFieldData {
15
+ key: number;
16
+ name: number;
17
+ }
18
+ export interface FormListOperation {
19
+ add: (defaultValue?: Record<string, unknown>) => void;
20
+ remove: (index: number) => void;
21
+ }
22
+ export interface FormInstance {
23
+ /** 获取全部字段值(浅拷贝) */
24
+ getFieldsValue(): Record<string, unknown>;
25
+ /** 获取单个字段值 */
26
+ getFieldValue(name: string): unknown;
27
+ /** 设置单个字段值 */
28
+ setFieldValue(name: string, value: unknown): void;
29
+ /** 批量合并字段值(编辑表单回填) */
30
+ setFieldsValue(values: Record<string, unknown>): void;
31
+ /**
32
+ * 校验全部或指定字段:通过时 resolve 全部字段值,
33
+ * 失败时 reject `FormError[]`
34
+ */
35
+ validateFields(names?: string | string[]): Promise<Record<string, unknown>>;
36
+ /** 重置为 initialValues 并清空校验错误 */
37
+ resetFields(): void;
38
+ }
39
+ export interface FormItemProps {
40
+ /** 字段名称 */
41
+ name?: string;
42
+ /** 标签文本 */
43
+ label?: React.ReactNode;
44
+ /** 标签对齐方式,覆盖 Form 级别 */
45
+ labelAlign?: 'left' | 'right';
46
+ /** 标签宽度,仅在 horizontal 模式下生效 */
47
+ labelWidth?: number | string;
48
+ /** 验证规则 */
49
+ rules?: RuleType[];
50
+ /** 是否必填(仅显示星号,实际验证由 rules 控制) */
51
+ required?: boolean;
52
+ /** 是否禁用 */
53
+ disabled?: boolean;
54
+ /** 无样式模式,不渲染 Form.Item 的标签、布局和错误信息 */
55
+ noStyle?: boolean;
56
+ /** 自定义值属性名,例如 Switch/Checkbox 使用 'checked',默认自动检测 */
57
+ valuePropName?: string;
58
+ /** 自定义类名 */
59
+ className?: string;
60
+ /** 自定义样式 */
61
+ style?: React.CSSProperties;
62
+ /** 子元素 */
63
+ children?: React.ReactNode;
64
+ }
65
+ declare const FormItem: React.FC<FormItemProps>;
66
+ export interface FormListProps {
67
+ /** 字段名,对应 values 中的数组 */
68
+ name: string;
69
+ /** 紧凑模式,相邻输入框无圆角看起来一体 */
70
+ compact?: boolean;
71
+ /** 渲染函数 */
72
+ children: (fields: FormListFieldData[], operations: FormListOperation) => React.ReactNode;
73
+ /** 自定义类名 */
74
+ className?: string;
75
+ /** 自定义样式 */
76
+ style?: React.CSSProperties;
77
+ }
78
+ declare const FormList: React.FC<FormListProps>;
79
+ export interface FormProps {
80
+ /** 布局方式
81
+ * @default 'vertical'
82
+ */
83
+ layout?: 'horizontal' | 'vertical' | 'inline';
84
+ /** 初始值 */
85
+ initialValues?: Record<string, unknown>;
86
+ /** 提交成功回调 */
87
+ onFinish?: (values: Record<string, unknown>) => void;
88
+ /** 提交失败回调 */
89
+ onFinishFailed?: (errors: FormError[]) => void;
90
+ /** 标签对齐方式
91
+ * @default 'right'
92
+ */
93
+ labelAlign?: 'left' | 'right';
94
+ /** 是否在标签后显示冒号
95
+ * @default false
96
+ */
97
+ colon?: boolean;
98
+ /** 是否禁用所有表单项 */
99
+ disabled?: boolean;
100
+ /** 表单尺寸
101
+ * @default 'md'
102
+ */
103
+ size?: 'sm' | 'md' | 'lg';
104
+ /** 命令式实例(由 `Form.useForm()` 创建),用于编辑回填、外部提交等场景 */
105
+ form?: FormInstance;
106
+ /** 自定义类名 */
107
+ className?: string;
108
+ /** 自定义样式 */
109
+ style?: React.CSSProperties;
110
+ /** 子元素 */
111
+ children?: React.ReactNode;
112
+ }
113
+ interface FormComponent extends React.ForwardRefExoticComponent<FormProps & React.RefAttributes<HTMLFormElement>> {
114
+ Item: typeof FormItem;
115
+ List: typeof FormList;
116
+ /** 创建 FormInstance(与 antd 用法一致:`const [form] = Form.useForm()`) */
117
+ useForm: () => [FormInstance];
118
+ }
119
+ declare const Form: FormComponent;
120
+ export { Form, FormItem, FormList };
121
+ export default Form;
@@ -0,0 +1,443 @@
1
+ import React, { forwardRef, createContext, useContext, useState, useCallback, useMemo, useRef, useEffect } from 'react';
2
+ import { classNames, prefixCls } from '@aura-react-comp/shared';
3
+ import "./index.less";
4
+
5
+ /* ===== 类型定义 ===== */
6
+
7
+ /* ===== FormInstance(命令式实例) ===== */
8
+
9
+ /** Form 内部引擎:由 Form 在挂载时注入,FormInstance 的方法委托给它 */
10
+ import { Fragment as _Fragment } from "react/jsx-runtime";
11
+ import { jsx as _jsx } from "react/jsx-runtime";
12
+ import { jsxs as _jsxs } from "react/jsx-runtime";
13
+ /** FormInstance → 引用槽注册表:Form 借此把引擎挂到实例上,不污染公开接口 */
14
+ const engineRegistry = new WeakMap();
15
+
16
+ /**
17
+ * 创建 FormInstance。传给 `<Form form={form}>` 后即可在表单外部命令式
18
+ * 读写与校验(编辑回填、外部提交按钮、跨组件联动等场景)。
19
+ * 对外通过 `Form.useForm()` 使用(返回数组,与 antd 用法一致)。
20
+ *
21
+ * 实现说明:刻意不使用 React hooks——与 antd 一致,允许在组件外
22
+ * (工具函数、测试、类组件)调用,仅要求把返回值传给 <Form form={...}>。
23
+ */
24
+ function useForm() {
25
+ const engineRef = {
26
+ current: null
27
+ };
28
+ const instance = {
29
+ getFieldsValue: () => engineRef.current?.getValues() ?? {},
30
+ getFieldValue: name => engineRef.current?.getValues()[name],
31
+ setFieldValue: (name, value) => engineRef.current?.setFieldValue(name, value),
32
+ setFieldsValue: values => engineRef.current?.setFieldsValue(values),
33
+ validateFields: async names => {
34
+ const engine = engineRef.current;
35
+ if (!engine) return {};
36
+ return engine.validateFields(names === undefined ? undefined : Array.isArray(names) ? names : [names]);
37
+ },
38
+ resetFields: () => engineRef.current?.reset()
39
+ };
40
+ engineRegistry.set(instance, engineRef);
41
+ return instance;
42
+ }
43
+ /* ===== Context ===== */
44
+
45
+ const FormContext = /*#__PURE__*/createContext(null);
46
+ function useFormContext() {
47
+ const ctx = useContext(FormContext);
48
+ return ctx;
49
+ }
50
+
51
+ /* ===== Form.Item ===== */
52
+
53
+ const FormItem = ({
54
+ name,
55
+ label,
56
+ labelAlign: itemLabelAlign,
57
+ labelWidth,
58
+ rules,
59
+ required,
60
+ disabled: itemDisabled,
61
+ noStyle = false,
62
+ valuePropName,
63
+ className,
64
+ style,
65
+ children
66
+ }) => {
67
+ const ctx = useFormContext();
68
+ const [localErrors, setLocalErrors] = useState([]);
69
+ const isRequired = required ?? rules?.some(r => r.required) ?? false;
70
+ const fieldErrors = name && ctx ? ctx.errors[name] ?? localErrors : localErrors;
71
+ const isDisabled = itemDisabled ?? ctx?.disabled ?? false;
72
+ const colon = ctx?.colon ?? false;
73
+ const labelAlign = itemLabelAlign ?? ctx?.labelAlign ?? 'right';
74
+
75
+ // 挂载时注册字段 rules 到 Form
76
+ useEffect(() => {
77
+ if (name && ctx && rules && rules.length > 0) {
78
+ ctx.registerField(name, rules);
79
+ }
80
+ }, [name, ctx, rules]);
81
+ const handleChange = useCallback(async e => {
82
+ if (!name || !ctx) return;
83
+ let value = e;
84
+ if (e && typeof e === 'object' && 'target' in e && e.target) {
85
+ // 事件来自被克隆的子组件,此处只能按 DOM 事件的公共形态取值
86
+ const target = e.target;
87
+ if (target.type === 'checkbox' || target.type === 'radio') {
88
+ value = target.type === 'checkbox' ? target.checked : target.value;
89
+ } else {
90
+ value = target.value;
91
+ }
92
+ }
93
+ ctx.setFieldValue(name, value);
94
+
95
+ // 实时验证
96
+ if (rules && rules.length > 0) {
97
+ const errors = await validateRules(rules, value);
98
+ setLocalErrors(errors);
99
+ }
100
+ }, [name, ctx, rules]);
101
+ const itemCls = classNames(prefixCls('form-item'), prefixCls(`form-item-${ctx?.layout ?? 'vertical'}`), fieldErrors.length > 0 && prefixCls('form-item-error'), isRequired && prefixCls('form-item-required'), isDisabled && prefixCls('form-item-disabled'), className);
102
+ const labelStyle = {};
103
+ if (labelWidth !== undefined && ctx?.layout === 'horizontal') {
104
+ labelStyle.width = labelWidth;
105
+ labelStyle.flexShrink = 0;
106
+ }
107
+
108
+ /** 克隆子元素注入 value/checked、onChange、disabled */
109
+ const renderChildren = () => {
110
+ if ( /*#__PURE__*/React.isValidElement(children)) {
111
+ const child = children;
112
+ const childProps = child.props;
113
+ // 子组件可能是组件(函数 / 类,取 displayName 或 name)或宿主元素(字符串标签名)
114
+ const childType = child.type;
115
+ const displayName = typeof childType === 'string' ? childType : childType?.displayName || childType?.name || '';
116
+ const isSwitchOrCheckbox = displayName === 'Switch' || displayName === 'Checkbox' || typeof childType === 'string' && childType === 'input' && childProps.type === 'checkbox';
117
+ const propName = valuePropName ?? (isSwitchOrCheckbox ? 'checked' : 'value');
118
+ if (!name || !ctx) {
119
+ return /*#__PURE__*/React.cloneElement(child, {
120
+ ...(isDisabled ? {
121
+ disabled: true
122
+ } : {}),
123
+ ...(ctx?.size && childProps.size === undefined ? {
124
+ size: ctx.size
125
+ } : {})
126
+ });
127
+ }
128
+ const value = ctx.values[name] ?? (isSwitchOrCheckbox ? false : '');
129
+ return /*#__PURE__*/React.cloneElement(child, {
130
+ [propName]: value,
131
+ onChange: e => {
132
+ handleChange(e);
133
+ const childOnChange = childProps.onChange;
134
+ childOnChange?.(e);
135
+ },
136
+ ...(isDisabled ? {
137
+ disabled: true
138
+ } : {}),
139
+ ...(ctx.size && childProps.size === undefined ? {
140
+ size: ctx.size
141
+ } : {})
142
+ });
143
+ }
144
+ return children;
145
+ };
146
+
147
+ // noStyle 模式:只注入 value/onChange/disabled,不渲染外层结构和标签
148
+ if (noStyle) {
149
+ return /*#__PURE__*/_jsx(_Fragment, {
150
+ children: renderChildren()
151
+ });
152
+ }
153
+ return /*#__PURE__*/_jsxs("div", {
154
+ className: itemCls,
155
+ style: style,
156
+ children: [label && /*#__PURE__*/_jsxs("label", {
157
+ className: classNames(prefixCls('form-item-label'), labelAlign === 'left' && prefixCls('form-item-label-left')),
158
+ htmlFor: name,
159
+ style: labelStyle,
160
+ children: [isRequired && /*#__PURE__*/_jsx("span", {
161
+ className: prefixCls('form-item-required-star'),
162
+ children: "*"
163
+ }), label, colon && /*#__PURE__*/_jsx("span", {
164
+ className: prefixCls('form-item-colon'),
165
+ children: ":"
166
+ })]
167
+ }), /*#__PURE__*/_jsxs("div", {
168
+ className: prefixCls('form-item-control'),
169
+ children: [/*#__PURE__*/_jsx("div", {
170
+ className: prefixCls('form-item-input'),
171
+ children: renderChildren()
172
+ }), fieldErrors.length > 0 && /*#__PURE__*/_jsx("div", {
173
+ className: prefixCls('form-item-errors'),
174
+ role: "alert",
175
+ children: fieldErrors.map((error, index) => /*#__PURE__*/_jsx("div", {
176
+ className: prefixCls('form-item-error-text'),
177
+ children: error
178
+ }, index))
179
+ })]
180
+ })]
181
+ });
182
+ };
183
+ FormItem.displayName = 'FormItem';
184
+
185
+ /* ===== Form.List ===== */
186
+
187
+ let listKeyCounter = 0;
188
+ const FormList = ({
189
+ name,
190
+ compact,
191
+ children,
192
+ className,
193
+ style
194
+ }) => {
195
+ const ctx = useFormContext();
196
+ const [keys, setKeys] = useState([]);
197
+ const getArray = useCallback(() => {
198
+ if (!ctx) return [];
199
+ const val = ctx.values[name];
200
+ return Array.isArray(val) ? val : [];
201
+ }, [ctx, name]);
202
+
203
+ // 初始化 keys 与 values
204
+ useEffect(() => {
205
+ const arr = getArray();
206
+ if (arr.length > 0 && keys.length === 0) {
207
+ setKeys(arr.map(() => ++listKeyCounter));
208
+ }
209
+ // 刻意只在挂载时初始化一次:这里读到的 getArray / keys.length 只作初始快照,
210
+ // 一旦加入依赖,每次表单值变化都会重跑并可能重置 keys。
211
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- 见上
212
+ }, []);
213
+ const add = useCallback(defaultValue => {
214
+ const newKey = ++listKeyCounter;
215
+ setKeys(prev => [...prev, newKey]);
216
+ if (ctx) {
217
+ const arr = getArray();
218
+ ctx.setFieldValue(name, [...arr, defaultValue ?? {}]);
219
+ }
220
+ }, [ctx, name, getArray]);
221
+ const remove = useCallback(index => {
222
+ setKeys(prev => prev.filter((_, i) => i !== index));
223
+ if (ctx) {
224
+ const arr = getArray();
225
+ ctx.setFieldValue(name, arr.filter((_, i) => i !== index));
226
+ }
227
+ }, [ctx, name, getArray]);
228
+ const fields = keys.map((key, index) => ({
229
+ key,
230
+ name: index
231
+ }));
232
+ const listCls = classNames(prefixCls('form-list'), compact && prefixCls('form-list-compact'), className);
233
+ return /*#__PURE__*/_jsx("div", {
234
+ className: listCls,
235
+ style: style,
236
+ children: children(fields, {
237
+ add,
238
+ remove
239
+ })
240
+ });
241
+ };
242
+ FormList.displayName = 'Form.List';
243
+
244
+ /* ===== 验证工具 ===== */
245
+
246
+ async function validateRules(rules, value) {
247
+ const errors = [];
248
+ const strValue = String(value ?? '');
249
+ for (const rule of rules) {
250
+ if (rule.required && (value === undefined || value === null || value === '')) {
251
+ errors.push(rule.message ?? '此字段为必填项');
252
+ continue;
253
+ }
254
+ if (rule.min !== undefined && strValue.length < rule.min) {
255
+ errors.push(rule.message ?? `最少输入 ${rule.min} 个字符`);
256
+ continue;
257
+ }
258
+ if (rule.max !== undefined && strValue.length > rule.max) {
259
+ errors.push(rule.message ?? `最多输入 ${rule.max} 个字符`);
260
+ continue;
261
+ }
262
+ if (rule.pattern && !rule.pattern.test(strValue)) {
263
+ errors.push(rule.message ?? '格式不正确');
264
+ continue;
265
+ }
266
+ if (rule.validator) {
267
+ try {
268
+ const result = await rule.validator(value);
269
+ if (!result) {
270
+ errors.push(rule.message ?? '验证失败');
271
+ }
272
+ } catch {
273
+ errors.push(rule.message ?? '验证失败');
274
+ }
275
+ }
276
+ }
277
+ return errors;
278
+ }
279
+
280
+ /* ===== Form 主组件 ===== */
281
+
282
+ const FormBase = /*#__PURE__*/forwardRef(({
283
+ layout = 'vertical',
284
+ initialValues = {},
285
+ onFinish,
286
+ onFinishFailed,
287
+ labelAlign = 'right',
288
+ colon = false,
289
+ disabled = false,
290
+ size = 'md',
291
+ form,
292
+ className,
293
+ style,
294
+ children
295
+ }, ref) => {
296
+ const [values, setValues] = useState({
297
+ ...initialValues
298
+ });
299
+ const [errors, setErrors] = useState({});
300
+
301
+ // valuesRef 是字段值的唯一事实来源:命令式 API 与渲染状态都从它出发,
302
+ // 保证 setFieldsValue 后同步读取(validateFields / getFieldValue)拿到最新值
303
+ const valuesRef = useRef({
304
+ ...initialValues
305
+ });
306
+ const initialValuesRef = useRef(initialValues);
307
+ const rulesMapRef = useRef({});
308
+
309
+ /** 统一的字段值变更入口:先写 ref(同步可读),再同步渲染状态 */
310
+ const applyValues = useCallback(updater => {
311
+ valuesRef.current = updater(valuesRef.current);
312
+ setValues(valuesRef.current);
313
+ }, []);
314
+ const setFieldValue = useCallback((name, value) => {
315
+ applyValues(prev => ({
316
+ ...prev,
317
+ [name]: value
318
+ }));
319
+ }, [applyValues]);
320
+ const validateField = useCallback(async name => {
321
+ const rules = rulesMapRef.current[name] ?? [];
322
+ if (rules.length === 0) return [];
323
+ const fieldErrors = await validateRules(rules, valuesRef.current[name]);
324
+ setErrors(prev => ({
325
+ ...prev,
326
+ [name]: fieldErrors
327
+ }));
328
+ return fieldErrors;
329
+ }, []);
330
+ const engine = useMemo(() => ({
331
+ getValues: () => ({
332
+ ...valuesRef.current
333
+ }),
334
+ setFieldValue: (name, value) => applyValues(prev => ({
335
+ ...prev,
336
+ [name]: value
337
+ })),
338
+ setFieldsValue: vals => applyValues(prev => ({
339
+ ...prev,
340
+ ...vals
341
+ })),
342
+ validateFields: async names => {
343
+ const fieldNames = names ?? Object.keys(rulesMapRef.current);
344
+ const allErrors = [];
345
+ const newErrors = {};
346
+ for (const name of fieldNames) {
347
+ const rules = rulesMapRef.current[name];
348
+ if (rules && rules.length > 0) {
349
+ const fieldErrors = await validateRules(rules, valuesRef.current[name]);
350
+ if (fieldErrors.length > 0) {
351
+ newErrors[name] = fieldErrors;
352
+ allErrors.push({
353
+ name,
354
+ errors: fieldErrors
355
+ });
356
+ }
357
+ }
358
+ }
359
+ // 对「本次校验过的字段」做替换式更新:通过的字段清除旧错误(与整表提交语义一致)
360
+ setErrors(prev => {
361
+ const next = {
362
+ ...prev
363
+ };
364
+ for (const name of fieldNames) {
365
+ if (newErrors[name]) next[name] = newErrors[name];else delete next[name];
366
+ }
367
+ return next;
368
+ });
369
+ if (allErrors.length > 0) {
370
+ throw allErrors;
371
+ }
372
+ return {
373
+ ...valuesRef.current
374
+ };
375
+ },
376
+ reset: () => {
377
+ valuesRef.current = {
378
+ ...initialValuesRef.current
379
+ };
380
+ setValues(valuesRef.current);
381
+ setErrors({});
382
+ }
383
+ }), [applyValues]);
384
+
385
+ // 把引擎挂到外部传入的 FormInstance 上(卸载时解绑)
386
+ useEffect(() => {
387
+ if (!form) return;
388
+ const slot = engineRegistry.get(form);
389
+ if (!slot) return;
390
+ slot.current = engine;
391
+ return () => {
392
+ slot.current = null;
393
+ };
394
+ }, [form, engine]);
395
+ const handleSubmit = useCallback(async e => {
396
+ e.preventDefault();
397
+ try {
398
+ const result = await engine.validateFields();
399
+ onFinish?.(result);
400
+ } catch (allErrors) {
401
+ onFinishFailed?.(allErrors);
402
+ }
403
+ }, [engine, onFinish, onFinishFailed]);
404
+ const registerField = useCallback((name, rules) => {
405
+ if (rules && rules.length > 0) {
406
+ rulesMapRef.current[name] = rules;
407
+ }
408
+ }, []);
409
+ const ctxValue = {
410
+ values,
411
+ errors,
412
+ setFieldValue,
413
+ validateField,
414
+ registerField,
415
+ layout,
416
+ disabled,
417
+ colon,
418
+ size,
419
+ labelAlign
420
+ };
421
+ const formCls = classNames(prefixCls('form'), prefixCls(`form-${layout}`), prefixCls(`form-${size}`), className);
422
+ return /*#__PURE__*/_jsx(FormContext.Provider, {
423
+ value: ctxValue,
424
+ children: /*#__PURE__*/_jsx("form", {
425
+ ref: ref,
426
+ className: formCls,
427
+ style: style,
428
+ onSubmit: handleSubmit,
429
+ noValidate: true,
430
+ children: children
431
+ })
432
+ });
433
+ });
434
+ FormBase.displayName = 'Form';
435
+
436
+ /* ===== 复合组件 ===== */
437
+
438
+ const Form = FormBase;
439
+ Form.Item = FormItem;
440
+ Form.List = FormList;
441
+ Form.useForm = () => [useForm()];
442
+ export { Form, FormItem, FormList };
443
+ export default Form;