RadioGroup 是一个已弃用的实验性组件,用于让用户从少量选项中选择一个。建议开发者改用 RadioControl 或 ToggleGroupControl。
import { useState } from 'react';
import {
__experimentalRadio as Radio,
__experimentalRadioGroup as RadioGroup,
} from '@wordpress/components';
const MyControlledRadioRadioGroup = () => {
const [ checked, setChecked ] = useState( '25' );
return (
<RadioGroup label="Width" onChange={ setChecked } checked={ checked }>
<Radio __next40pxDefaultSize value="25">25%</Radio>
<Radio __next40pxDefaultSize value="50">50%</Radio>
<Radio __next40pxDefaultSize value="75">75%</Radio>
<Radio __next40pxDefaultSize value="100">100%</Radio>
</RadioGroup>
);
};import { useState } from 'react';
import {
__experimentalRadio as Radio,
__experimentalRadioGroup as RadioGroup,
} from '@wordpress/components';
const MyUncontrolledRadioRadioGroup = () => {
return (
<RadioGroup label="Width" defaultChecked="25">
<Radio __next40pxDefaultSize value="25">25%</Radio>
<Radio __next40pxDefaultSize value="50">50%</Radio>
<Radio __next40pxDefaultSize value="75">75%</Radio>
<Radio __next40pxDefaultSize value="100">100%</Radio>
</RadioGroup>
);
}; Use a RadioGroup component when you want users to select one option from a small set of options.

Only one option in a radio group can be selected and active at a time. Selecting one option deselects any other.
Radio groups should:
A radio group’s state makes it clear which option is active. Hover and focus states express the available selection options for buttons in a button group.
Radio groups that cannot be selected can either be given a disabled state, or be hidden.
import { useState } from 'react';
import {
__experimentalRadio as Radio,
__experimentalRadioGroup as RadioGroup,
} from '@wordpress/components';
const MyControlledRadioRadioGroup = () => {
const [ checked, setChecked ] = useState( '25' );
return (
<RadioGroup label="Width" onChange={ setChecked } checked={ checked }>
<Radio __next40pxDefaultSize value="25">25%</Radio>
<Radio __next40pxDefaultSize value="50">50%</Radio>
<Radio __next40pxDefaultSize value="75">75%</Radio>
<Radio __next40pxDefaultSize value="100">100%</Radio>
</RadioGroup>
);
};
When using the RadioGroup component as an uncontrolled component, the default value can be set with the defaultChecked prop.
import { useState } from 'react';
import {
__experimentalRadio as Radio,
__experimentalRadioGroup as RadioGroup,
} from '@wordpress/components';
const MyUncontrolledRadioRadioGroup = () => {
return (
<RadioGroup label="Width" defaultChecked="25">
<Radio __next40pxDefaultSize value="25">25%</Radio>
<Radio __next40pxDefaultSize value="50">50%</Radio>
<Radio __next40pxDefaultSize value="75">75%</Radio>
<Radio __next40pxDefaultSize value="100">100%</Radio>
</RadioGroup>
);
};
ButtonGroup component.RadioControl component.