@@ -15,6 +15,7 @@ import { useEffect, useMemo } from "react"
1515import { ArrayControlOptions , ControlUISchema } from "../ui-schema"
1616import { usePreviousValue } from "../common/usePreviousValue"
1717import React from "react"
18+ import { NestedAntDFormContext } from "../contexts/NestedAntDFormContext"
1819
1920type ArrayLayoutProps = Omit < JSFArrayLayoutProps , "uischema" > & {
2021 uischema : ControlUISchema < unknown > | JSFArrayLayoutProps [ "uischema" ]
@@ -81,13 +82,14 @@ export function ObjectArrayControl({
8182 return moveDown ?.( path , index ) ( )
8283 }
8384
84- const addButton = (
85+ const addButton = ( antdAdd : ( ) => void ) => (
8586 < Flex justify = "center" >
8687 < Button
8788 { ...options . addButtonProps }
8889 onClick = { ( e ) => {
8990 e . stopPropagation ( )
9091 addItemToList ( )
92+ antdAdd ( )
9193 } }
9294 >
9395 { options . addButtonProps ?. children ?? `Add ${ label } ` }
@@ -98,6 +100,7 @@ export function ObjectArrayControl({
98100 if ( ! visible ) {
99101 return null
100102 }
103+
101104 return (
102105 < Form . Item
103106 required = { required }
@@ -106,67 +109,74 @@ export function ObjectArrayControl({
106109 { ...formItemProps }
107110 >
108111 < > { label } </ >
109- < List < unknown >
110- dataSource = { dataSource }
111- renderItem = { ( _item : unknown , index : number ) => {
112- return (
113- < List . Item
114- key = { index }
115- actions = { [
116- dataSource . length > 1 && options . showSortButtons ? (
117- < Space key = "sort" >
118- < Button
119- aria-label = { `Move up` }
120- disabled = { index === 0 }
121- { ...options . moveUpButtonProps }
122- onClick = { handleUpClick ( path , index ) }
123- >
124- { ! options . moveUpButtonProps ?. icon && "Up" }
125- </ Button >
112+ < Form . List name = { path } >
113+ { ( _ , { add, remove } ) => (
114+ < List < unknown >
115+ dataSource = { dataSource }
116+ renderItem = { ( _item : unknown , index : number ) => {
117+ return (
118+ < List . Item
119+ key = { index }
120+ actions = { [
121+ dataSource . length > 1 && options . showSortButtons ? (
122+ < Space key = "sort" >
123+ < Button
124+ aria-label = { `Move up` }
125+ disabled = { index === 0 }
126+ { ...options . moveUpButtonProps }
127+ onClick = { handleUpClick ( path , index ) }
128+ >
129+ { ! options . moveUpButtonProps ?. icon && "Up" }
130+ </ Button >
131+ < Button
132+ aria-label = { `Move down` }
133+ disabled = { index === dataSource . length - 1 }
134+ { ...options . moveDownButtonProps }
135+ onClick = { handleDownClick ( path , index ) }
136+ >
137+ { ! options . moveDownButtonProps ?. icon && "Down" }
138+ </ Button >
139+ </ Space >
140+ ) : undefined ,
126141 < Button
127- aria-label = { `Move down` }
128- disabled = { index === dataSource . length - 1 }
129- { ...options . moveDownButtonProps }
130- onClick = { handleDownClick ( path , index ) }
142+ key = "remove"
143+ { ...options . removeButtonProps }
144+ disabled = {
145+ ! removeItems ||
146+ ( required && dataSource . length == 1 && index === 0 )
147+ }
148+ onClick = { ( e ) => {
149+ e . stopPropagation ( )
150+ removeItems ?.( path , [ index ] ) ( )
151+ remove ( index )
152+ } }
131153 >
132- { ! options . moveDownButtonProps ?. icon && "Down" }
133- </ Button >
134- </ Space >
135- ) : undefined ,
136- < Button
137- key = "remove"
138- { ...options . removeButtonProps }
139- disabled = {
140- ! removeItems ||
141- ( required && dataSource . length == 1 && index === 0 )
142- }
143- onClick = { ( e ) => {
144- e . stopPropagation ( )
145- removeItems ?.( path , [ index ] ) ( )
146- } }
154+ { options . removeButtonProps ?. children ?? "Delete" }
155+ </ Button > ,
156+ ] . filter ( Boolean ) }
147157 >
148- { options . removeButtonProps ?. children ?? "Delete" }
149- </ Button > ,
150- ] . filter ( Boolean ) }
151- >
152- < div style = { { width : "100%" } } >
153- < JsonFormsDispatch
154- enabled = { enabled }
155- schema = { schema }
156- path = { composePaths ( path , ` ${ index } ` ) }
157- uischema = { foundUISchema }
158- renderers = { renderers }
159- cells = { cells }
160- uischemas = { uischemas }
161- / >
162- </ div >
163- </ List . Item >
164- )
165- } }
166- { ... ( options . addButtonLocation === "top"
167- ? { header : addButton }
168- : { footer : addButton } ) }
169- / >
158+ < div style = { { width : "100%" } } >
159+ < NestedAntDFormContext . Provider value = { { path , index } } >
160+ < JsonFormsDispatch
161+ enabled = { enabled }
162+ schema = { schema }
163+ path = { composePaths ( path , ` ${ index } ` ) }
164+ uischema = { foundUISchema }
165+ renderers = { renderers }
166+ cells = { cells }
167+ uischemas = { uischemas }
168+ />
169+ </ NestedAntDFormContext . Provider >
170+ </ div >
171+ </ List . Item >
172+ )
173+ } }
174+ { ... ( options . addButtonLocation === "top"
175+ ? { header : addButton ( add ) }
176+ : { footer : addButton ( add ) } ) }
177+ />
178+ ) }
179+ </ Form . List >
170180 </ Form . Item >
171181 )
172182}
0 commit comments