Skip to content

Commit 1cd9f44

Browse files
committed
update 0.0.17
1 parent ffbe0e5 commit 1cd9f44

File tree

6 files changed

+59
-11
lines changed

6 files changed

+59
-11
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codingapi/ui-framework",
3-
"version": "0.0.15",
3+
"version": "0.0.17",
44
"description": "A UI Framework built with React and Typescript",
55
"keywords": [
66
"ui",

src/Form/content.ts

-4
This file was deleted.

src/Form/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ export * from './antd';
33
export * from './instance';
44
export * from './listener';
55
export * from './utils';
6-
export * from './validate';
7-
export * from './content';
6+
export * from './validate';

src/Form/instance.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,53 @@ import React from "react";
55
import {AntdForm, AntdFormInstance} from "./antd";
66

77
export class FormInstance {
8+
// 校验上下文
89
private readonly validateContext: FormValidateContext;
10+
// 重新加载上下文
911
private readonly reloadContext: FormFieldReloadListenerContext;
12+
// 选项重新加载上下文
1013
private readonly optionContext: FormFieldOptionListenerContext;
14+
// 表单实例
1115
private readonly formInstance: AntdFormInstance|undefined;
16+
// 表单操作对象
1217
private readonly formAction: FormAction;
18+
// 动态表单字段
1319
private fields: FormField[];
14-
20+
// 表单字段组件
21+
private formFields: FormField[];
22+
// 表单字段更新函数
1523
private fieldsUpdateDispatch: React.Dispatch<React.SetStateAction<FormField[]>> | undefined;
1624

25+
/**
26+
* 表单字段更新函数
27+
* @param fieldsUpdateDispatch
28+
*/
1729
public setFieldsUpdateDispatch = (fieldsUpdateDispatch: React.Dispatch<React.SetStateAction<FormField[]>>) => {
1830
this.fieldsUpdateDispatch = fieldsUpdateDispatch;
1931
}
2032

33+
/**
34+
* 更新表单字段
35+
* @param resetFields
36+
*/
2137
private updateFields = (resetFields: (prevState: FormField[]) => FormField[]) => {
2238
this.fields = resetFields(this.fields);
2339
if (this.fieldsUpdateDispatch) {
2440
this.fieldsUpdateDispatch(resetFields);
2541
}
2642
}
2743

44+
/**
45+
* 添加表单字段
46+
* @param field
47+
*/
48+
public addFormField = (field: FormField) => {
49+
const formFieldNames = this.formFields.map((item) => item.props.name);
50+
if (formFieldNames.indexOf(field.props.name) === -1) {
51+
this.formFields.push(field);
52+
}
53+
}
54+
2855
private namePathEqual = (name1: NamePath, name2: NamePath) => {
2956
if (Array.isArray(name1) && Array.isArray(name2)) {
3057
if (name1.length !== name2.length) {
@@ -216,6 +243,24 @@ export class FormInstance {
216243
return this.formInstance?.getFieldsValue();
217244
}
218245

246+
247+
/**
248+
* 获取Form表单字段
249+
* @param name
250+
*/
251+
public getFormFieldProps = (name: NamePath) => {
252+
for (const field of this.formFields) {
253+
if (field.props.name && this.namePathEqual(field.props.name, name)) {
254+
return field;
255+
}
256+
}
257+
return null;
258+
}
259+
260+
/**
261+
* 获取动态表单字段
262+
* @param name
263+
*/
219264
public getFieldProps = (name: NamePath) => {
220265
for (const field of this.fields) {
221266
if (field.props.name && this.namePathEqual(field.props.name, name)) {
@@ -262,6 +307,7 @@ export class FormInstance {
262307
this.optionContext = new FormFieldOptionListenerContext();
263308
this.formInstance = AntdForm.getInstance().useForm();
264309
this.fields = [];
310+
this.formFields = [];
265311
this.formAction = {
266312
submit: this.submit,
267313
reset: this.reset,

src/Form/utils.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ export class ValidateUtils {
99
*/
1010
static validateNotEmpty = async (content: FormValidateContent, message?: string) => {
1111
const field = content.getFieldProps();
12-
const errorMessage = message ? message : field?.props.label + '不能为空' || '不能为空';
12+
let errorMessage = message;
13+
if (!errorMessage) {
14+
if (field) {
15+
errorMessage = field.props?.label + '不能为空';
16+
} else {
17+
errorMessage = '不能为空';
18+
}
19+
}
1320
const value = content.value;
1421
if (value) {
1522
if (Array.isArray(value)) {
1623
if (value.length === 0) {
1724
return [errorMessage];
18-
}else {
25+
} else {
1926
return [];
2027
}
2128
} else {

src/Form/validate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class FormValidateContent {
1515
}
1616

1717
getFieldProps = () => {
18-
return this.form.getFieldProps(this.name);
18+
return this.form.getFieldProps(this.name) || this.form.getFormFieldProps(this.name);
1919
}
2020
}
2121

0 commit comments

Comments
 (0)