@@ -6,7 +6,19 @@ import { UndoOutlined, RedoOutlined } from '@ant-design/icons';
66
77import AntForm from '../common/form-wrapper' ;
88
9- const ArrayItem = ( { fields, fieldIndex, name, remove, length, minItems, removeLabel } ) => {
9+ const ArrayItem = ( {
10+ fields,
11+ fieldIndex,
12+ name,
13+ remove,
14+ length,
15+ minItems,
16+ removeLabel,
17+ ArrayItemProps,
18+ FieldsContainerProps,
19+ RemoveContainerProps,
20+ RemoveButtonProps
21+ } ) => {
1022 const { renderForm } = useFormApi ( ) ;
1123
1224 const editedFields = fields . map ( ( field , index ) => {
@@ -15,10 +27,12 @@ const ArrayItem = ({ fields, fieldIndex, name, remove, length, minItems, removeL
1527 } ) ;
1628
1729 return (
18- < Row >
19- < Col span = { 24 } > { renderForm ( [ editedFields ] ) } </ Col >
20- < Col span = { 24 } >
21- < Button type = "primary" danger onClick = { ( ) => remove ( fieldIndex ) } disabled = { length <= minItems } >
30+ < Row { ...ArrayItemProps } >
31+ < Col span = { 24 } { ...FieldsContainerProps } >
32+ { renderForm ( [ editedFields ] ) }
33+ </ Col >
34+ < Col span = { 24 } { ...RemoveContainerProps } >
35+ < Button type = "primary" danger { ...RemoveButtonProps } onClick = { ( ) => remove ( fieldIndex ) } disabled = { length <= minItems } >
2236 { removeLabel }
2337 </ Button >
2438 </ Col >
@@ -33,7 +47,11 @@ ArrayItem.propTypes = {
3347 remove : PropTypes . func . isRequired ,
3448 length : PropTypes . number ,
3549 minItems : PropTypes . number ,
36- removeLabel : PropTypes . node . isRequired
50+ removeLabel : PropTypes . node . isRequired ,
51+ ArrayItemProps : PropTypes . object . isRequired ,
52+ FieldsContainerProps : PropTypes . object . isRequired ,
53+ RemoveContainerProps : PropTypes . object . isRequired ,
54+ RemoveButtonProps : PropTypes . object . isRequired
3755} ;
3856
3957const defaultButtonLabels = {
@@ -90,7 +108,23 @@ const DynamicArray = ({ ...props }) => {
90108 validateOnMount,
91109 isRequired,
92110 helperText,
111+ // customization props
93112 FormItemProps,
113+ ArrayItemProps,
114+ FieldsContainerProps,
115+ RemoveContainerProps,
116+ RemoveButtonProps,
117+ FieldArrayRowProps,
118+ FieldArrayRowCol,
119+ FieldArrayHeaderProps,
120+ FieldArrayLabelProps,
121+ FieldArrayButtonsProps,
122+ UndoButtonProps,
123+ RedoButtonProps,
124+ AddButtonProps,
125+ FieldArrayDescriptionProps,
126+ NoItemsMessageProps,
127+ ErrorMessageProps,
94128 ...rest
95129 } = useFieldApi ( props ) ;
96130 const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
@@ -133,15 +167,27 @@ const DynamicArray = ({ ...props }) => {
133167 } ;
134168
135169 return (
136- < Row gutter = { [ 0 , 16 ] } >
137- < Col span = { 24 } >
138- < Row justify = "space-between" >
139- < Col > { label && < Typography . Title level = { 4 } > { label } </ Typography . Title > } </ Col >
170+ < Row gutter = { [ 0 , 16 ] } { ...FieldArrayRowProps } >
171+ < Col span = { 24 } { ...FieldArrayRowCol } >
172+ < Row justify = "space-between" { ...FieldArrayHeaderProps } >
173+ < Col >
174+ { label && (
175+ < Typography . Title level = { 4 } { ...FieldArrayLabelProps } >
176+ { label }
177+ </ Typography . Title >
178+ ) }
179+ </ Col >
140180 < Col >
141- < Space >
142- < Button type = "default" onClick = { undo } disabled = { state . index === 0 } icon = { < UndoOutlined /> } />
143- < Button type = "default" onClick = { redo } disabled = { state . index === state . history . length } icon = { < RedoOutlined /> } />
144- < Button type = "primary" onClick = { pushWrapper } disabled = { value . length >= maxItems } >
181+ < Space { ...FieldArrayButtonsProps } >
182+ < Button type = "default" icon = { < UndoOutlined /> } { ...UndoButtonProps } onClick = { undo } disabled = { state . index === 0 } />
183+ < Button
184+ type = "default"
185+ icon = { < RedoOutlined /> }
186+ { ...RedoButtonProps }
187+ onClick = { redo }
188+ disabled = { state . index === state . history . length }
189+ />
190+ < Button type = "primary" { ...AddButtonProps } onClick = { pushWrapper } disabled = { value . length >= maxItems } >
145191 { combinedButtonLabels . add }
146192 </ Button >
147193 </ Space >
@@ -150,13 +196,17 @@ const DynamicArray = ({ ...props }) => {
150196 </ Col >
151197 { description && (
152198 < Col span = { 24 } >
153- < Typography . Text > { description } </ Typography . Text >
199+ < Typography . Text { ... FieldArrayDescriptionProps } > { description } </ Typography . Text >
154200 </ Col >
155201 ) }
156202 < Col span = { 24 } >
157203 < Row gutter = { [ 0 , 16 ] } >
158204 { value . length <= 0 ? (
159- < Typography . Text > { noItemsMessage } </ Typography . Text >
205+ typeof noItemsMessage === 'string' ? (
206+ < Typography . Text { ...NoItemsMessageProps } > { noItemsMessage } </ Typography . Text >
207+ ) : (
208+ React . cloneElement ( noItemsMessage , NoItemsMessageProps )
209+ )
160210 ) : (
161211 map ( ( name , index ) => (
162212 < Col span = { 24 } key = { name } >
@@ -168,6 +218,10 @@ const DynamicArray = ({ ...props }) => {
168218 length = { value . length }
169219 minItems = { minItems }
170220 removeLabel = { combinedButtonLabels . remove }
221+ ArrayItemProps = { ArrayItemProps }
222+ FieldsContainerProps = { FieldsContainerProps }
223+ RemoveContainerProps = { RemoveContainerProps }
224+ RemoveButtonProps = { RemoveButtonProps }
171225 />
172226 </ Col >
173227 ) )
@@ -176,7 +230,9 @@ const DynamicArray = ({ ...props }) => {
176230 </ Col >
177231 { isError && (
178232 < Col span = { 12 } >
179- < Typography . Text type = "danger" > { typeof error === 'object' ? error . name : error } </ Typography . Text >
233+ < Typography . Text type = "danger" { ...ErrorMessageProps } >
234+ { typeof error === 'object' ? error . name : error }
235+ </ Typography . Text >
180236 </ Col >
181237 ) }
182238 </ Row >
@@ -195,15 +251,46 @@ DynamicArray.propTypes = {
195251 minItems : PropTypes . number ,
196252 maxItems : PropTypes . number ,
197253 noItemsMessage : PropTypes . node ,
254+ buttonLabels : PropTypes . object ,
255+ // customization props
198256 FormItemProps : PropTypes . object ,
199- buttonLabels : PropTypes . object
257+ ArrayItemProps : PropTypes . object ,
258+ FieldsContainerProps : PropTypes . object ,
259+ RemoveContainerProps : PropTypes . object ,
260+ RemoveButtonProps : PropTypes . object ,
261+ FieldArrayRowProps : PropTypes . object ,
262+ FieldArrayRowCol : PropTypes . object ,
263+ FieldArrayHeaderProps : PropTypes . object ,
264+ FieldArrayLabelProps : PropTypes . object ,
265+ FieldArrayButtonsProps : PropTypes . object ,
266+ UndoButtonProps : PropTypes . object ,
267+ RedoButtonProps : PropTypes . object ,
268+ AddButtonProps : PropTypes . object ,
269+ FieldArrayDescriptionProps : PropTypes . object ,
270+ NoItemsMessageProps : PropTypes . object ,
271+ ErrorMessageProps : PropTypes . object
200272} ;
201273
202274DynamicArray . defaultProps = {
203275 maxItems : Infinity ,
204276 minItems : 0 ,
205277 noItemsMessage : 'No items added' ,
206- FormItemProps : { }
278+ FormItemProps : { } ,
279+ ArrayItemProps : { } ,
280+ FieldsContainerProps : { } ,
281+ RemoveContainerProps : { } ,
282+ RemoveButtonProps : { } ,
283+ FieldArrayRowProps : { } ,
284+ FieldArrayRowCol : { } ,
285+ FieldArrayHeaderProps : { } ,
286+ FieldArrayLabelProps : { } ,
287+ FieldArrayButtonsProps : { } ,
288+ UndoButtonProps : { } ,
289+ RedoButtonProps : { } ,
290+ AddButtonProps : { } ,
291+ FieldArrayDescriptionProps : { } ,
292+ NoItemsMessageProps : { } ,
293+ ErrorMessageProps : { }
207294} ;
208295
209296export default DynamicArray ;
0 commit comments