Skip to content

Commit 3d69bb7

Browse files
committed
Fix invalid state for ButtonGroupControl
1 parent bf1c37f commit 3d69bb7

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

  • packages/js/ui/src/wp/button-group-control

packages/js/ui/src/wp/button-group-control/index.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Button, __experimentalGrid as Grid } from '@wordpress/components';
2+
import { useCallback } from 'react';
23
import { FieldsetAsLabel } from '../fieldset-as-label/index.js';
34

45
type Option = {
@@ -19,15 +20,18 @@ export interface ButtonGroupControlProps {
1920
export function ButtonGroupControl({
2021
label,
2122
options,
22-
value,
23+
value = [],
2324
onChange,
2425
}: ButtonGroupControlProps) {
25-
const toggleValue = (val: string) => {
26-
const newValue = value.includes(val)
27-
? value.filter((v) => v !== val)
28-
: [...value, val];
29-
onChange(newValue);
30-
};
26+
const toggleValue = useCallback(
27+
(val: string) => {
28+
const newValue = value.includes(val)
29+
? value.filter((v) => v !== val)
30+
: [...value, val];
31+
onChange(newValue);
32+
},
33+
[value, onChange],
34+
);
3135

3236
return (
3337
<FieldsetAsLabel label={label}>

0 commit comments

Comments
 (0)