@@ -11,32 +11,31 @@ import * as baseStyles from '@/components/Form/fields/BaseField.styles'
1111
1212export function SelectMultipleField ( props : FormFieldProps ) {
1313 const {
14- field : { onChange } ,
14+ field : { onChange, value } ,
1515 fieldData : { options = [ ] } ,
1616 } = props
1717
18- const [ valueArray , setValueArray ] = React . useState < string [ ] > ( [ ] )
18+ // Ensure we always work with an array of strings coming from RHF
19+ const valueArray : string [ ] = Array . isArray ( value ) ? value : [ ]
1920
20- function setValueInArray ( value : string ) {
21- let updatedValueArray = [ ]
22- if ( valueArray . includes ( value ) ) {
23- updatedValueArray = [ ...valueArray . filter ( ( v ) => v !== value ) ]
24- } else {
25- updatedValueArray = [ ...valueArray , value ]
26- }
27- setValueArray ( updatedValueArray )
21+ function toggleValue ( optionValue : string ) {
22+ const updatedValueArray = valueArray . includes ( optionValue )
23+ ? valueArray . filter ( ( v ) => v !== optionValue )
24+ : [ ...valueArray , optionValue ]
25+
26+ // Let react-hook-form handle the new value
2827 onChange ( updatedValueArray )
2928 }
3029
3130 return (
3231 < BaseField { ...props } >
3332 { ( ) => (
3433 < Flex . Column gap = { 2 } part = "field-select-multiple" >
35- { options . map ( ( { label, value } ) => (
34+ { options . map ( ( { label, value : optionValue } ) => (
3635 < Checkbox . Root
37- value = { valueArray . includes ( value ) ? value : undefined }
38- onCheckedChange = { ( ) => setValueInArray ( value ) }
39- key = { value }
36+ key = { optionValue }
37+ checked = { valueArray . includes ( optionValue ) }
38+ onCheckedChange = { ( ) => toggleValue ( optionValue ) }
4039 asChild
4140 >
4241 < Box
@@ -61,7 +60,7 @@ export function SelectMultipleField(props: FormFieldProps) {
6160 borderColor = "neutral.border"
6261 borderRadius = "100%"
6362 flex = "0 0 auto"
64- id = { value }
63+ id = { optionValue }
6564 padding = "0"
6665 part = "field-check-value"
6766 position = "relative"
0 commit comments