Skip to content

Commit

Permalink
Custom select control: don't announce external value changes (#22815)
Browse files Browse the repository at this point in the history
* Don't announce external value change

* Correct aria attributes

* Improve value equality checking.

* Selected item doesn't always exist

* Check if value exists and add default

* Fix initial selection

* Memoize getSelectOptions instead

* Switch to useMemo
  • Loading branch information
tellthemachines authored Jun 18, 2020
1 parent 8335c7c commit cbef072
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/components/src/font-size-picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { __ } from '@wordpress/i18n';
import { useInstanceId } from '@wordpress/compose';
import { textColor } from '@wordpress/icons';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -27,6 +28,9 @@ function getSelectValueFromFontSize( fontSizes, value ) {
}

function getSelectOptions( optionsArray, disableCustomFontSizes ) {
if ( disableCustomFontSizes && ! optionsArray.length ) {
return null;
}
optionsArray = [
{ slug: DEFAULT_FONT_SIZE, name: __( 'Default' ) },
...optionsArray,
Expand All @@ -51,12 +55,15 @@ export default function FontSizePicker( {
} ) {
const instanceId = useInstanceId( FontSizePicker );

if ( disableCustomFontSizes && ! fontSizes.length ) {
const options = useMemo(
() => getSelectOptions( fontSizes, disableCustomFontSizes ),
[ fontSizes, disableCustomFontSizes ]
);

if ( ! options ) {
return null;
}

const options = getSelectOptions( fontSizes, disableCustomFontSizes );

const selectedFontSizeSlug = getSelectValueFromFontSize( fontSizes, value );

const fontSizePickerNumberId = `components-font-size-picker__number#${ instanceId }`;
Expand Down

0 comments on commit cbef072

Please sign in to comment.