Skip to content

Commit

Permalink
[Components - ToggleGroupControl]: Fix extra update on incoming change (
Browse files Browse the repository at this point in the history
#37224)

* [Components - ToggleGroupControl]: Fix extra update on incoming change

* add changelog entry
  • Loading branch information
ntsekouras authored Dec 9, 2021
1 parent 5531530 commit 52012ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Added `__experimentalHideHeader` prop to `Modal` component ([#36831](https://github.com/WordPress/gutenberg/pull/36831)).
- Added experimental `ConfirmDialog` component ([#34153](https://github.com/WordPress/gutenberg/pull/34153)).
- Divider: improve support for vertical orientation and RTL styles, use start/end logical props instead of top/bottom, change border-color to `currentColor` ([#36579](https://github.com/WordPress/gutenberg/pull/36579)).
- ToggleGroupControl: Avoid calling `onChange` if radio state changed from an incoming value ([#37224](https://github.com/WordPress/gutenberg/pull/37224/)).

### Bug Fix

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import useResizeAware from 'react-resize-aware';
*/
import { __ } from '@wordpress/i18n';
import { useRef, useMemo } from '@wordpress/element';
import { useMergeRefs, useInstanceId } from '@wordpress/compose';
import { useMergeRefs, useInstanceId, usePrevious } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -59,10 +59,15 @@ function ToggleGroupControl(
baseId,
state: value,
} );
const previousValue = usePrevious( value );

// Propagate radio.state change
useUpdateEffect( () => {
onChange( radio.state );
// Avoid calling onChange if radio state changed
// from incoming value.
if ( previousValue !== radio.state ) {
onChange( radio.state );
}
}, [ radio.state ] );

// Sync incoming value with radio.state
Expand Down

0 comments on commit 52012ee

Please sign in to comment.