@@ -598,4 +598,81 @@ describe("useField", () => {
598598 expect ( renderSpy . mock . calls [ 0 ] [ 0 ] ) . toBe ( "Apple" ) ;
599599 expect ( getByTestId ( "array" ) . value ) . toBe ( "Apple" ) ;
600600 } ) ;
601+
602+ it ( "should default undefined value to [] for select multiple with type prop (fix react-final-form-arrays #185)" , ( ) => {
603+ const renderSpy = jest . fn ( ) ;
604+ const MySelectField = ( ) => {
605+ const { input } = useField ( "scopes" , { type : "select" , multiple : true } ) ;
606+ renderSpy ( input . value ) ;
607+ return (
608+ < select { ...input } multiple data-testid = "select" >
609+ < option value = "read" > Read</ option >
610+ < option value = "write" > Write</ option >
611+ </ select >
612+ ) ;
613+ } ;
614+ render (
615+ < Form onSubmit = { onSubmitMock } >
616+ { ( ) => (
617+ < form >
618+ < MySelectField />
619+ </ form >
620+ ) }
621+ </ Form > ,
622+ ) ;
623+ // When no initial value is provided, should default to [] not undefined
624+ // This prevents React warning: "The `value` prop supplied to <select> must be an array if `multiple` is true"
625+ expect ( renderSpy . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ ] ) ;
626+ } ) ;
627+
628+ it ( "should default undefined value to [] for select multiple with component prop" , ( ) => {
629+ const renderSpy = jest . fn ( ) ;
630+ const MySelectField = ( ) => {
631+ const { input } = useField ( "scopes" , { component : "select" , multiple : true } ) ;
632+ renderSpy ( input . value ) ;
633+ return (
634+ < select { ...input } multiple data-testid = "select" >
635+ < option value = "read" > Read</ option >
636+ < option value = "write" > Write</ option >
637+ </ select >
638+ ) ;
639+ } ;
640+ render (
641+ < Form onSubmit = { onSubmitMock } >
642+ { ( ) => (
643+ < form >
644+ < MySelectField />
645+ </ form >
646+ ) }
647+ </ Form > ,
648+ ) ;
649+ // When no initial value is provided, should default to [] not undefined
650+ // This prevents React warning: "The `value` prop supplied to <select> must be an array if `multiple` is true"
651+ expect ( renderSpy . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ ] ) ;
652+ } ) ;
653+
654+ it ( "should ensure select multiple value is always an array" , ( ) => {
655+ const renderSpy = jest . fn ( ) ;
656+ const MySelectField = ( ) => {
657+ const { input } = useField ( "scopes" , { type : "select" , multiple : true } ) ;
658+ renderSpy ( input . value ) ;
659+ return (
660+ < select { ...input } multiple data-testid = "select" >
661+ < option value = "read" > Read</ option >
662+ < option value = "write" > Write</ option >
663+ </ select >
664+ ) ;
665+ } ;
666+ render (
667+ < Form onSubmit = { onSubmitMock } initialValues = { { scopes : null } } >
668+ { ( ) => (
669+ < form >
670+ < MySelectField />
671+ </ form >
672+ ) }
673+ </ Form > ,
674+ ) ;
675+ // Even if the form value is null/undefined, input.value should be []
676+ expect ( Array . isArray ( renderSpy . mock . calls [ 0 ] [ 0 ] ) ) . toBe ( true ) ;
677+ } ) ;
601678} ) ;
0 commit comments