@@ -24,17 +24,16 @@ import { findOptions, isOptionsGroup } from '../utils/options-utils';
2424import RruMultiCheckboxInputProps from './types/RruMultiCheckboxInputProps' ;
2525
2626const RruMultiCheckboxInput : FC < RruMultiCheckboxInputProps > = ( props ) => {
27- const [ value , setValue ] = useState < string | null > ( null ) ;
27+ const [ hasBeenInitialized , setHasBeenInitialized ] = useState < boolean > ( false ) ;
28+ const [ selectedOptions , setSelectedOptions ] = useState < readonly RruOption [ ] > ( [ ] ) ;
2829
2930 const field = useField ( props . name , ( serializedValue ) => {
30- if ( ! deepEqual ( serializedValue , value ) ) {
31- setValue ( serializedValue ) ;
31+ if ( ! deepEqual ( serializedValue , selectedOptions . map ( o => o . value ) ) ) {
32+ const options = findOptions ( props . options , serializedValue || [ ] ) ;
33+ setSelectedOptions ( options ) ;
3234 }
3335 } ) ;
3436
35- const [ hasBeenInitialized , setHasBeenInitialized ] = useState < boolean > ( false ) ;
36- const [ selectedOptions , setSelectedOptions ] = useState < readonly RruOption [ ] > ( [ ] ) ;
37-
3837 const onSelectChange = ( options : readonly RruOption [ ] ) => {
3938 setSelectedOptions ( options ) ;
4039 field . setValue ( options . map ( ( opt ) => opt . value ) ) ;
@@ -106,7 +105,7 @@ const RruMultiCheckboxInput: FC<RruMultiCheckboxInputProps> = (props) => {
106105 < div
107106 className = 'form-group'
108107 data-field-name = { props . name }
109- data-field-value = { value }
108+ data-field-value = { selectedOptions . map ( o => o . value ) . toSorted ( ) }
110109 data-field-error = { field . error ? field . error . message : '' }
111110 >
112111 < Label label = { props . label } requiredAsterisk = { props . requiredAsterisk } > </ Label >
0 commit comments