Skip to content

Commit

Permalink
Fix loading placeholder rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Q committed Apr 28, 2020
1 parent 1d3c648 commit 03169ee
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/components/src/select-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import { useInstanceId } from '@wordpress/compose';
*/
import BaseControl from '../base-control';

/**
* Used to render the <select /> Loading... placeholder when the isLoading
* state is set.
*/
const loadingValue = '__loading__';

export default function SelectControl( {
disabled,
help,
Expand All @@ -24,6 +30,7 @@ export default function SelectControl( {
options = [],
className,
hideLabelFromVision,
value: valueProp,
...props
} ) {
const instanceId = useInstanceId( SelectControl );
Expand All @@ -46,6 +53,8 @@ export default function SelectControl( {
return null;
}

const value = isLoading ? loadingValue : valueProp;

// Disable reason: A select with an onchange throws a warning
/* eslint-disable jsx-a11y/no-onchange */
return (
Expand All @@ -65,6 +74,7 @@ export default function SelectControl( {
onChange={ onChangeValue }
aria-describedby={ !! help ? `${ id }__help` : undefined }
multiple={ multiple }
value={ value }
{ ...props }
>
<SelectOptions isLoading={ isLoading } options={ options } />
Expand All @@ -76,7 +86,11 @@ export default function SelectControl( {

function SelectOptions( { isLoading, options } ) {
if ( isLoading ) {
return <option disabled>{ __( 'Loading…' ) }</option>;
return (
<option disabled value={ loadingValue }>
{ __( 'Loading…' ) }
</option>
);
}

return options.map( ( option, index ) => (
Expand Down

0 comments on commit 03169ee

Please sign in to comment.