@@ -121,7 +121,7 @@ function Body({ type, optional, node, ctx }: Props<SimplifiedMcdocType>) {
121121 return < > </ >
122122 }
123123 return < div class = "node-body" >
124- < ListBody type = { type } node = { node } ctx = { ctx } />
124+ < ListBody type = { type } optional = { optional } node = { node } ctx = { ctx } />
125125 </ div >
126126 }
127127 if ( type . kind === 'tuple' ) {
@@ -849,7 +849,7 @@ function ListHead({ type, node, ctx }: Props<ListType | PrimitiveArrayType>) {
849849 </ button >
850850}
851851
852- function ListBody ( { type : outerType , node, ctx } : Props < ListType | PrimitiveArrayType > ) {
852+ function ListBody ( { type : outerType , optional , node, ctx } : Props < ListType | PrimitiveArrayType > ) {
853853 if ( ! JsonArrayNode . is ( node ) ) {
854854 return < > </ >
855855 }
@@ -892,6 +892,21 @@ function ListBody({ type: outerType, node, ctx }: Props<ListType | PrimitiveArra
892892 }
893893 } , [ type , node , ctx , canAdd ] )
894894
895+ const makeListEdit : MakeEdit = useCallback ( ( edit ) => {
896+ ctx . makeEdit ( ( ) => {
897+ const newNode = edit ( node . range )
898+ if ( JsonArrayNode . is ( newNode ) && newNode . children . length === 0 && optional && type . kind === 'list' && ( type . lengthRange ?. min ?? 0 ) > 0 ) {
899+ // Remove entire list when empty list is not allowed and field is optional
900+ return undefined
901+ }
902+ return newNode
903+ } )
904+ } , [ ctx , node , optional , type ] )
905+
906+ const listCtx = useMemo ( ( ) => {
907+ return { ...ctx , makeEdit : makeListEdit }
908+ } , [ ctx , makeListEdit ] )
909+
895910 return < >
896911 { node . children . map ( ( item , index ) => {
897912 if ( index === maxShown ) {
@@ -905,7 +920,7 @@ function ListBody({ type: outerType, node, ctx }: Props<ListType | PrimitiveArra
905920 return < > </ >
906921 }
907922 const key = index . toString ( )
908- return < ListItem key = { key } item = { item } index = { index } category = { category } type = { childType } isToggled = { isToggled ( key ) } expand = { expand ( key ) } collapse = { collapse ( key ) } node = { node } ctx = { ctx } />
923+ return < ListItem key = { key } item = { item } index = { index } category = { category } type = { childType } isToggled = { isToggled ( key ) } expand = { expand ( key ) } collapse = { collapse ( key ) } node = { node } ctx = { listCtx } />
909924 } ) }
910925 { node . children . length > 0 && < div class = "node-header" >
911926 < button class = "add tooltipped tip-se" aria-label = { locale ( 'add_bottom' ) } onClick = { ( ) => onAddBottom ( ) } disabled = { ! canAdd } >
0 commit comments