@@ -50,13 +50,20 @@ import {
5050 Tooltip ,
5151 Typography ,
5252} from 'antd' ;
53- import get from 'lodash/get' ;
5453import map from 'lodash/map' ;
5554import merge from 'lodash/merge' ;
5655import range from 'lodash/range' ;
5756import React , { ComponentType , useCallback , useMemo , useState } from 'react' ;
5857import { ArrayLayoutToolbar } from './ArrayToolbar' ;
5958
59+ interface ExtraProps {
60+ rowIndex : number ;
61+ enableUp : boolean ;
62+ enableDown : boolean ;
63+ showSortButtons : boolean ;
64+ disableRemove ?: boolean ;
65+ }
66+
6067const ArrayLayoutComponent = (
6168 props : { ctx : JsonFormsStateContext } & ArrayLayoutProps & {
6269 translations : ArrayTranslations ;
@@ -111,41 +118,55 @@ const ArrayLayoutComponent = (
111118 return style ;
112119 } , [ expanded ] ) ;
113120
114- const getExtra = ( data : number , index : number ) => {
121+ const getExtra = ( {
122+ rowIndex,
123+ enableUp,
124+ enableDown,
125+ showSortButtons,
126+ disableRemove,
127+ } : ExtraProps ) => {
115128 return (
116129 < >
117- { showSortButtons && enabled ? (
130+ { showSortButtons ? (
118131 < >
119- < Tooltip key = 'tooltip-up' title = { translations . up } >
132+ < Tooltip title = { translations . up } >
120133 < Button
121- onClick = { moveUp ( path , index ) }
122134 shape = 'circle'
123- icon = { < ArrowUpOutlined rev = { undefined } /> }
124- disabled = { index == 0 }
125135 aria-label = { translations . upAriaLabel }
136+ icon = { < ArrowUpOutlined rev = { undefined } /> }
137+ onClick = { ( event ) => {
138+ event . stopPropagation ( ) ;
139+ moveUp ( path , rowIndex ) ( ) ;
140+ } }
141+ disabled = { ! enabled || ! enableUp }
126142 />
127143 </ Tooltip >
128- < Tooltip key = 'tooltip-down' title = { translations . down } >
144+ < Tooltip title = { translations . down } >
129145 < Button
130- onClick = { moveDown ( path , index ) }
131146 shape = 'circle'
132- icon = { < ArrowDownOutlined rev = { undefined } /> }
133- disabled = { index >= data - 1 }
134147 aria-label = { translations . downAriaLabel }
148+ icon = { < ArrowDownOutlined rev = { undefined } /> }
149+ onClick = { ( event ) => {
150+ event . stopPropagation ( ) ;
151+ moveDown ( path , rowIndex ) ( ) ;
152+ } }
153+ disabled = { ! enabled || ! enableDown }
135154 />
136155 </ Tooltip >
137156 </ >
138157 ) : null }
139- { enabled ? (
140- < Tooltip key = 'tooltip-remove' title = { translations . removeTooltip } >
141- < Button
142- onClick = { removeItems ( path , [ index ] ) }
143- shape = 'circle'
144- icon = { < DeleteFilled rev = { undefined } /> }
145- aria-label = { translations . removeAriaLabel }
146- />
147- </ Tooltip >
148- ) : null }
158+ < Tooltip key = 'tooltip-remove' title = { translations . removeTooltip } >
159+ < Button
160+ onClick = { ( event ) => {
161+ event . stopPropagation ( ) ;
162+ removeItems ( path , [ rowIndex ] ) ( ) ;
163+ } }
164+ shape = 'circle'
165+ disabled = { ! enabled || disableRemove }
166+ icon = { < DeleteFilled rev = { undefined } /> }
167+ aria-label = { translations . removeAriaLabel }
168+ />
169+ </ Tooltip >
149170 </ >
150171 ) ;
151172 } ;
@@ -163,11 +184,44 @@ const ArrayLayoutComponent = (
163184 ) ,
164185 [ uischemas , schema , uischema . scope , path , uischema , rootSchema ]
165186 ) ;
166- const doDisableAdd = disableAdd || appliedUiSchemaOptions . disableAdd ;
167- const doDisableRemove = disableRemove || appliedUiSchemaOptions . disableRemove ;
168- if ( doDisableRemove ) {
169- // TODO
170- }
187+
188+ const arraySchema = Resolve . schema ( rootSchema , uischema . scope , rootSchema ) ;
189+
190+ const doDisableAdd =
191+ disableAdd ||
192+ appliedUiSchemaOptions . disableAdd ||
193+ ( appliedUiSchemaOptions . restrict &&
194+ arraySchema !== undefined &&
195+ arraySchema . maxItems !== undefined &&
196+ data >= arraySchema . maxItems ) ;
197+
198+ const doDisableRemove =
199+ disableRemove ||
200+ appliedUiSchemaOptions . disableRemove ||
201+ ( appliedUiSchemaOptions . restrict &&
202+ arraySchema !== undefined &&
203+ arraySchema . minItems !== undefined &&
204+ data <= arraySchema . minItems ) ;
205+
206+ const childLabelForIndex = ( childPath : string , index : number ) => {
207+ const childLabelProp =
208+ uischema . options ?. childLabelProp ?? getFirstPrimitiveProp ( schema ) ;
209+ if ( ! childLabelProp ) {
210+ return `${ index } ` ;
211+ }
212+ const labelValue = Resolve . data (
213+ props . ctx . core . data ,
214+ composePaths ( childPath , childLabelProp )
215+ ) ;
216+ if (
217+ labelValue === undefined ||
218+ labelValue === null ||
219+ Number . isNaN ( labelValue )
220+ ) {
221+ return '' ;
222+ }
223+ return `${ labelValue } ` ;
224+ } ;
171225
172226 return (
173227 < ArrayLayoutToolbar
@@ -188,13 +242,12 @@ const ArrayLayoutComponent = (
188242 { data > 0 ? (
189243 < Collapse
190244 accordion
245+ expandIconPosition = 'end'
191246 onChange = { ( value : any ) => handleChange ( value ) }
192247 items = { map ( range ( data ) , ( index ) => {
193248 const childPath = composePaths ( path , `${ index } ` ) ;
194- const childData = Resolve . data ( props . ctx . core . data , childPath ) ;
195- const childLabel = appliedUiSchemaOptions . elementLabelProp
196- ? get ( childData , appliedUiSchemaOptions . elementLabelProp , '' )
197- : get ( childData , getFirstPrimitiveProp ( schema ) , '' ) ;
249+
250+ const text = childLabelForIndex ( childPath , index ) ;
198251
199252 return {
200253 key : String ( index ) ,
@@ -203,12 +256,20 @@ const ArrayLayoutComponent = (
203256 < Avatar aria-label = 'Index' style = { avatarStyle } >
204257 { index + 1 }
205258 </ Avatar >
206- { ! childLabel ? (
207- < Typography . Text > { childLabel } </ Typography . Text >
208- ) : null }
259+ { text ? < Typography . Text > { text } </ Typography . Text > : null }
209260 </ >
210261 ) ,
211- extra : < Space > { getExtra ( data , index ) } </ Space > ,
262+ extra : (
263+ < Space >
264+ { getExtra ( {
265+ rowIndex : index ,
266+ enableUp : index !== 0 ,
267+ enableDown : index !== props . data - 1 ,
268+ showSortButtons : showSortButtons ,
269+ disableRemove : doDisableRemove ,
270+ } ) }
271+ </ Space >
272+ ) ,
212273 children : (
213274 < JsonFormsDispatch
214275 schema = { schema }
0 commit comments