File tree Expand file tree Collapse file tree 2 files changed +26
-5
lines changed
packages/components/src/custom-select-control-v2/legacy-component Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -34,12 +34,10 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) {
3434 return ;
3535 }
3636
37- // @todo add test to verify that a value passed programmatically is
38- // selected
39-
4037 // Executes the logic in a microtask after the popup is closed.
4138 // This is simply to ensure the isOpen state matches that in Downshift.
4239 await Promise . resolve ( ) ;
40+
4341 const state = store . getState ( ) ;
4442
4543 const option = options . find ( ( item ) => item . name === nextValue ) ;
@@ -60,7 +58,10 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) {
6058
6159 useEffect ( ( ) => {
6260 // This is a workaround for selecting the right item upon mount
63- store . setValue ( value ?. name ! ) ;
61+ if ( value ) {
62+ store . setValue ( value . name ) ;
63+ }
64+ // eslint-disable-next-line react-hooks/exhaustive-deps
6465 } ) ;
6566
6667 const children = options . map (
Original file line number Diff line number Diff line change @@ -60,7 +60,9 @@ const ControlledCustomSelectControl = ( {
6060 onChange,
6161 ...restProps
6262} : React . ComponentProps < typeof UncontrolledCustomSelectControl > ) => {
63+ const { value : overrideValue } = restProps ;
6364 const [ value , setValue ] = useState ( options [ 0 ] ) ;
65+ const initialValue = overrideValue ?? value ;
6466 return (
6567 < UncontrolledCustomSelectControl
6668 { ...restProps }
@@ -70,7 +72,7 @@ const ControlledCustomSelectControl = ( {
7072 setValue ( args . selectedItem ) ;
7173 } }
7274 value = { options . find (
73- ( option : any ) => option . key === value . key
75+ ( option : any ) => option . key === initialValue . key
7476 ) }
7577 />
7678 ) ;
@@ -500,4 +502,22 @@ describe.each( [
500502 } )
501503 ) ;
502504 } ) ;
505+
506+ it ( 'Should display the initial value passed as the selected value' , async ( ) => {
507+ const initialSelectedItem = legacyProps . options [ 5 ] ;
508+
509+ const testProps = {
510+ ...legacyProps ,
511+ value : initialSelectedItem ,
512+ } ;
513+
514+ render ( < Component { ...testProps } /> ) ;
515+
516+ const currentSelectedItem = await screen . findByRole ( 'combobox' , {
517+ expanded : false ,
518+ } ) ;
519+
520+ // Verify the initial selected value
521+ expect ( currentSelectedItem ) . toHaveTextContent ( 'aquarela' ) ;
522+ } ) ;
503523} ) ;
You can’t perform that action at this time.
0 commit comments