11import React , { useMemo } from 'react' ;
22import { Checkbox , FormControlLabel , FormGroup , FormLabel , Typography } from '@mui/material' ;
33import useTranslation from "@/ui/language/useTranslation" ;
4- import { expand } from "@foerderfunke/sem-ops-utils" ;
54
65const ProfileInputMultiSelection = ( {
76 value,
@@ -33,32 +32,16 @@ const ProfileInputMultiSelection = ({
3332 return map ;
3433 } , [ answerOptions , optionsFormat ] ) ;
3534
36- const normalizedValue = useMemo ( ( ) => {
37- if ( ! Array . isArray ( value ) ) return [ ] ;
38- if ( optionsFormat === "flat" ) {
39- return value . map ( ( v ) => expand ( v ) ) ;
40- }
41- return value ;
42- } , [ value , optionsFormat ] ) ;
43-
4435 const handleChange = ( event ) => {
4536 const selectedValue = event . target . name ;
4637 const isChecked = event . target . checked ;
4738
4839 if ( isChecked ) {
49- setValue ( ( prevValue = [ ] ) => {
50- const toStore = optionsFormat === "flat" ? expand ( selectedValue ) : selectedValue ;
51- if ( prevValue . includes ( toStore ) ) return prevValue ;
52- return [ ...prevValue , toStore ] ;
40+ setValue ( ( prevValue ) => {
41+ return prevValue ? [ ...prevValue , selectedValue ] : [ selectedValue ] ;
5342 } ) ;
5443 } else {
55- setValue ( ( prevValue = [ ] ) => {
56- const expandedToRemove = optionsFormat === "flat" ? expand ( selectedValue ) : selectedValue ;
57- // remove both the expanded and the raw form to be safe
58- return prevValue . filter (
59- ( item ) => item !== expandedToRemove && item !== selectedValue
60- ) ;
61- } ) ;
44+ setValue ( value . filter ( ( item ) => item !== selectedValue ) ) ;
6245 }
6346 } ;
6447
@@ -72,7 +55,7 @@ const ProfileInputMultiSelection = ({
7255 sx = { { mb : 2 } }
7356 control = {
7457 < Checkbox
75- checked = { normalizedValue . includes ( key ) }
58+ checked = { value . includes ( key ) }
7659 onChange = { handleChange }
7760 name = { key }
7861 />
0 commit comments