Skip to content

Commit 9bc96e9

Browse files
author
昔梦
committed
feat:form-render-mobile 列表控件cardList支持min,max
1 parent b6c3a47 commit 9bc96e9

File tree

2 files changed

+57
-23
lines changed
  • docs/form-render-mobile/demo
  • packages/form-render-mobile/src/render-core/FieldList

2 files changed

+57
-23
lines changed

docs/form-render-mobile/demo/list.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const schema = {
1010
title: '对象数组',
1111
type: 'array',
1212
widget: 'cardList',
13+
min: 2,
14+
max:3,
1315
items: {
1416
type: 'object',
1517
widget: 'card',
Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import React, { createContext, useContext } from 'react';
21
import { Form, Grid } from 'antd-mobile';
32
import { AddCircleOutline } from 'antd-mobile-icons';
4-
import { useStore } from 'zustand';
5-
import { FRContext, ConfigContext } from '../../models/context';
63
import { parseAllExpression } from 'form-render/es/models/expression';
4+
import React, { createContext, useContext, useEffect, useState } from 'react';
5+
import { useStore } from 'zustand';
6+
import { ConfigContext, FRContext } from '../../models/context';
77
import { isFunction } from '../../utils';
88
import './index.less';
99

1010
const UpperContext = createContext(() => {});
11-
const getParamValue = (formCtx: any, upperCtx: any, schema: any) => (valueKey: string) => {
12-
return schema[valueKey] ?? upperCtx[valueKey] ?? formCtx[valueKey];
13-
};
11+
const getParamValue =
12+
(formCtx: any, upperCtx: any, schema: any) => (valueKey: string) => {
13+
return schema[valueKey] ?? upperCtx[valueKey] ?? formCtx[valueKey];
14+
};
1415

1516
export default (props: any) => {
1617
const { schema: _schema, path, renderCore, rootPath: _rootPath } = props;
@@ -26,12 +27,23 @@ export default (props: any) => {
2627
const { items, ...otherSchema } = _schema;
2728
const schema = {
2829
items,
29-
...parseAllExpression(otherSchema, formData, _rootPath, formSchema)
30+
...parseAllExpression(otherSchema, formData, _rootPath, formSchema),
3031
};
3132

3233
const defaultValue = schema.default ?? (schema.defaultValue || [{}]);
3334
const { onAdd, onRemove } = schema.props || {};
34-
35+
const [fieldLength, setFieldLength] = useState<number>();
36+
37+
useEffect(() => {
38+
const fieldsLength = getFieldsLength();
39+
setFieldLength(fieldsLength);
40+
}, []);
41+
42+
const getFieldsLength = () => {
43+
const fieldValue = form.getFieldValue(path);
44+
return Array.isArray(fieldValue) ? fieldValue.length : 0;
45+
};
46+
3547
const handleAdd = (add: any, data?: any) => {
3648
let addFunc = onAdd;
3749
if (typeof onAdd === 'string') {
@@ -43,6 +55,8 @@ export default (props: any) => {
4355
return;
4456
}
4557
add(data);
58+
const fieldsLength = getFieldsLength();
59+
setFieldLength(fieldsLength);
4660
};
4761

4862
const handleRemove = (remove: any, index: number) => {
@@ -57,47 +71,65 @@ export default (props: any) => {
5771
}
5872

5973
remove(index);
74+
75+
const fieldsLength = getFieldsLength();
76+
setFieldLength(fieldsLength);
6077
};
6178

6279
const getValueFromKey = getParamValue(formCtx, upperCtx, schema);
6380

6481
const readOnly = getValueFromKey('readOnly');
65-
82+
6683
if (schema.hidden) {
6784
return null;
6885
}
6986

70-
const preRootPath = [...(_rootPath || [])].splice(0, _rootPath.length - 1);
87+
const preRootPath = [...(_rootPath || [])].splice(0, _rootPath.length - 1);
7188
const rootPath = [...preRootPath, ...path];
72-
89+
7390
return (
7491
<Grid.Item className="frm-list">
7592
<Form.Array
7693
name={path}
7794
initialValue={defaultValue}
78-
renderAdd={!readOnly ? () => (
79-
<span>
80-
<AddCircleOutline /> 添加
81-
</span>
82-
) : undefined}
95+
renderAdd={
96+
!readOnly && (!schema.max || fieldLength < schema.max)
97+
? () => (
98+
<span>
99+
<AddCircleOutline /> 添加
100+
</span>
101+
)
102+
: undefined
103+
}
83104
onAdd={({ add }) => handleAdd(add)}
84105
renderHeader={({ index }, { remove }) => (
85106
<>
86107
{schema.title && (
87-
<span>{schema.title} {index + 1}</span>
108+
<span>
109+
{schema.title} {index + 1}
110+
</span>
88111
)}
89-
{!readOnly && (
90-
<a onClick={() => handleRemove(remove, index)} style={{ float: 'right' }}>
112+
{!readOnly && (!schema.min || fieldLength > schema.min) && (
113+
<a
114+
onClick={() => handleRemove(remove, index)}
115+
style={{ float: 'right' }}
116+
>
91117
删除
92118
</a>
93119
)}
94120
</>
95121
)}
96122
>
97-
{fields => fields.map(({ index }) => {
98-
return renderCore({ schema, parentPath: [index], rootPath: [...rootPath, index] })
99-
})}
123+
{fields =>
124+
fields.map(({ index }) => {
125+
return renderCore({
126+
schema,
127+
parentPath: [index],
128+
rootPath: [...rootPath, index],
129+
});
130+
})
131+
}
100132
</Form.Array>
101133
</Grid.Item>
102134
);
103-
}
135+
};

0 commit comments

Comments
 (0)