Skip to content

Commit

Permalink
CustomSelectControl: Add describedBy fallback (#34385)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Sep 4, 2021
1 parent 389a1cc commit 5bd0b40
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/components/src/custom-select-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,18 @@ export default function CustomSelectControl( {
stateReducer,
} );

const controlDescribedBy = describedBy
? describedBy
: // translators: %s: The selected option.
sprintf( __( 'Currently selected: %s' ), selectedItem?.name );
function getDescribedBy() {
if ( describedBy ) {
return describedBy;
}

if ( ! selectedItem ) {
return __( 'No selection' );
}

// translators: %s: The selected option.
return sprintf( __( 'Currently selected: %s' ), selectedItem.name );
}

const menuProps = getMenuProps( {
className: 'components-custom-select-control__menu',
Expand Down Expand Up @@ -129,7 +137,7 @@ export default function CustomSelectControl( {
'aria-labelledby': undefined,
className: 'components-custom-select-control__button',
isSmall: true,
describedBy: controlDescribedBy,
describedBy: getDescribedBy(),
} ) }
>
{ itemToString( selectedItem ) }
Expand Down

0 comments on commit 5bd0b40

Please sign in to comment.