11/* eslint import/no-cycle: "off" */
22// Unfortunately, there has to be an import cycle, because this is by nature, recursive
3- import React from 'react' ;
4- import PropTypes from 'prop-types' ;
53import { Button } from '@material-ui/core' ;
4+ import _ from 'lodash' ;
5+ import PropTypes from 'prop-types' ;
6+ import React from 'react' ;
7+ import swal from 'sweetalert' ;
68
9+ import XIcon from '../../assets/x-icon.png' ;
10+ import { useTranslations } from '../../hooks/useTranslations' ;
711import StepField from '../StepField/StepField' ;
812import './Fields.scss' ;
9- import { useTranslations } from '../../hooks/useTranslations' ;
1013
1114const FieldGroup = ( {
1215 isDisabled,
@@ -53,6 +56,29 @@ const FieldGroup = ({
5356 handleSimpleUpdate ( getKeyBase ( getNumFields ( ) ) , { } ) ;
5457 } ;
5558
59+ const onRemoveGroup = ( groupNumber ) => {
60+ if ( isDisabled ) return ;
61+
62+ swal ( {
63+ title : translations . components . button . discard . question ,
64+ text : translations . components . button . discard . warningMessage ,
65+ icon : 'warning' ,
66+ dangerMode : true ,
67+ buttons : [
68+ translations . components . button . discard . cancelButton ,
69+ translations . components . button . discard . confirmButton ,
70+ ] ,
71+ } ) . then ( ( isDeleteConfirmed ) => {
72+ if ( isDeleteConfirmed ) doRemoveGroup ( groupNumber ) ;
73+ } ) ;
74+ } ;
75+
76+ const doRemoveGroup = ( groupNumber ) => {
77+ const newData = _ . cloneDeep ( value ) ;
78+ newData . splice ( groupNumber , 1 ) ;
79+ handleSimpleUpdate ( metadata . key , newData ) ;
80+ } ;
81+
5682 const generateSingleGroup = ( index ) => {
5783 return metadata ?. subFields ?. map ( ( field ) => {
5884 return (
@@ -85,6 +111,24 @@ const FieldGroup = ({
85111 } ) ;
86112 } ;
87113
114+ const generateHeader = ( groupNumber , displayName ) => {
115+ const buttonClass = `button-${ isDisabled ? 'disabled' : 'active' } ` ;
116+
117+ return (
118+ < div
119+ className = { `group-title-container-base group-title-container-${ selectedLang } ` }
120+ >
121+ < img
122+ src = { XIcon }
123+ alt = { translations . components . button . discard }
124+ className = { `xicon-base xicon-${ selectedLang } ${ buttonClass } ` }
125+ onClick = { ( ) => onRemoveGroup ( groupNumber ) }
126+ />
127+ < h3 key = { displayName } > { displayName } </ h3 >
128+ </ div >
129+ ) ;
130+ } ;
131+
88132 const generateAllGroups = ( ) => {
89133 const numFieldGroups = getNumFields ( ) ;
90134 const groups = [ ] ;
@@ -93,7 +137,7 @@ const FieldGroup = ({
93137 const displayName = `${ metadata ?. displayName [ selectedLang ] } ${
94138 i + 1
95139 } `;
96- groups . push ( < h3 key = { displayName } > { displayName } </ h3 > ) ;
140+ groups . push ( generateHeader ( i , displayName ) ) ;
97141 groups . push ( generateSingleGroup ( i ) ) ;
98142 }
99143
0 commit comments