1- import React , { createContext , useContext } from 'react' ;
21import { Form , Grid } from 'antd-mobile' ;
32import { AddCircleOutline } from 'antd-mobile-icons' ;
4- import { useStore } from 'zustand' ;
5- import { FRContext , ConfigContext } from '../../models/context' ;
63import { 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' ;
77import { isFunction } from '../../utils' ;
88import './index.less' ;
99
1010const 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
1516export 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