11import classnames from 'classnames' ;
2- import React , { useState } from 'react' ;
2+ import React , { useState , useEffect } from 'react' ;
33
44import { type SelectDropdownProps } from '@gsa-tts/forms-core' ;
55import { type SelectDropdownPattern } from '@gsa-tts/forms-core' ;
@@ -8,6 +8,7 @@ import SelectDropdown from '../../../../Form/components/SelectDropdown/index.js'
88import { PatternEditComponent } from '../../types.js' ;
99
1010import { PatternEditActions } from '../common/PatternEditActions.js' ;
11+ import { PatternOptionActions } from '../common/PatternOptionActions.js' ;
1112import { PatternEditForm } from '../common/PatternEditForm.js' ;
1213import { usePatternEditFormContext } from '../common/hooks.js' ;
1314import { enLocale as message } from '@gsa-tts/forms-common' ;
@@ -38,12 +39,27 @@ const SelectDropdownPatternEdit: PatternEditComponent<SelectDropdownProps> = ({
3839} ;
3940
4041const EditComponent = ( { pattern } : { pattern : SelectDropdownPattern } ) => {
41- const { fieldId, getFieldState, register } =
42+ const { fieldId, getFieldState, register, setValue } =
4243 usePatternEditFormContext < SelectDropdownPattern > ( pattern . id ) ;
4344 const [ options , setOptions ] = useState ( pattern . data . options ) ;
4445 const label = getFieldState ( 'label' ) ;
4546 const hint = getFieldState ( 'hint' ) ;
4647
48+ const handleOptionLabelChange = ( index : number , value : string ) => {
49+ const newOptions = [ ...options ] ;
50+ newOptions [ index ] . label = value ;
51+ setOptions ( newOptions ) ;
52+ } ;
53+
54+ const handleDeleteOption = ( optionId : string ) => {
55+ const newOptions = options . filter ( o => o . id !== optionId ) ;
56+ setOptions ( newOptions ) ;
57+ } ;
58+
59+ useEffect ( ( ) => {
60+ setValue ( `options` , options ) ;
61+ } , [ options , setValue ] ) ;
62+
4763 return (
4864 < div className = "grid-row grid-gap" >
4965 < div className = "mobile-lg:grid-col-12 margin-bottom-2" >
@@ -133,22 +149,32 @@ const EditComponent = ({ pattern }: { pattern: SelectDropdownPattern }) => {
133149 className = "usa-input bg-primary-lighter"
134150 id = { fieldId ( `options.${ index } .label` ) }
135151 { ...register ( `options.${ index } .label` ) }
136- defaultValue = { option . label }
152+ value = { option . label }
153+ onChange = { e => handleOptionLabelChange ( index , e . target . value ) }
137154 aria-label = { `Option ${ index + 1 } label` }
138155 />
156+ < PatternOptionActions
157+ optionId = { option . id }
158+ onDelete = { handleDeleteOption }
159+ />
139160 </ div >
140161 </ div >
141162 ) ;
142163 } ) }
143164 < button
144- className = " usa-button usa-button--outline margin-top-1"
165+ className = { ` usa-link ${ styles . addMorePatternButton } margin-top-1` }
145166 type = "button"
146167 onClick = { event => {
147168 event . preventDefault ( ) ;
148169 const optionLabel = `Option ${ options . length + 1 } ` ;
149170 const optionValue = `value-${ options . length + 1 } ` ;
171+ const optionId = `option-${ crypto . randomUUID ( ) } ` ;
150172 setOptions (
151- options . concat ( { value : optionValue , label : optionLabel } )
173+ options . concat ( {
174+ value : optionValue ,
175+ label : optionLabel ,
176+ id : optionId ,
177+ } )
152178 ) ;
153179 } }
154180 >
0 commit comments