RadioButton

单选按钮是标准单选按钮元素的扩展,具有主题功能。


import RadioButton from 'primevue/radiobutton';

单选按钮与v-model属性一起使用,以进行双向值绑定。


<div class="flex flex-wrap gap-4">
    <div class="flex items-center gap-2">
        <RadioButton v-model="ingredient" inputId="ingredient1" name="pizza" value="Cheese" />
        <label for="ingredient1">Cheese</label>
    </div>
    <div class="flex items-center gap-2">
        <RadioButton v-model="ingredient" inputId="ingredient2" name="pizza" value="Mushroom" />
        <label for="ingredient2">Mushroom</label>
    </div>
    <div class="flex items-center gap-2">
        <RadioButton v-model="ingredient" inputId="ingredient3" name="pizza" value="Pepper" />
        <label for="ingredient3">Pepper</label>
    </div>
    <div class="flex items-center gap-2">
        <RadioButton v-model="ingredient" inputId="ingredient4" name="pizza" value="Onion" />
        <label for="ingredient4">Onion</label>
    </div>
</div>

单选按钮与 PrimeVue 表单库无缝集成。


<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4">
    <div class="flex flex-col gap-2">
        <RadioButtonGroup name="ingredient" class="flex flex-wrap gap-4">
            <div class="flex items-center gap-2">
                <RadioButton inputId="cheese" value="Cheese" />
                <label for="cheese">Cheese</label>
            </div>
            <div class="flex items-center gap-2">
                <RadioButton inputId="mushroom" value="Mushroom" />
                <label for="mushroom">Mushroom</label>
            </div>
            <div class="flex items-center gap-2">
                <RadioButton inputId="pepper" value="Pepper" />
                <label for="pepper">Pepper</label>
            </div>
            <div class="flex items-center gap-2">
                <RadioButton inputId="onion" value="Onion" />
                <label for="onion">Onion</label>
            </div>
        </RadioButtonGroup>
        <Message v-if="$form.ingredient?.invalid" severity="error" size="small" variant="simple">{{ $form.ingredient.error?.message }}</Message>
    </div>
    <Button type="submit" severity="secondary" label="Submit" />
</Form>

可以使用值列表生成单选按钮。


<div v-for="category in categories" :key="category.key" class="flex items-center gap-2">
    <RadioButton v-model="selectedCategory" :inputId="category.key" name="dynamic" :value="category.name" />
    <label :for="category.key">{{ category.name }}</label>
</div>

variant属性指定为filled以显示比默认outlined样式具有更高视觉强调的组件。


<RadioButton v-model="value" value="1" variant="filled" />

单选按钮提供smalllarge尺寸作为基本尺寸的替代。


<div class="flex flex-wrap gap-4">
    <div class="flex items-center gap-2">
        <RadioButton v-model="size" inputId="size_small" name="size" value="Small" size="small" />
        <label for="size_small" class="text-sm">Small</label>
    </div>
    <div class="flex items-center gap-2">
        <RadioButton v-model="size" inputId="size_normal" name="size" value="Normal" />
        <label for="size_normal">Normal</label>
    </div>
    <div class="flex items-center gap-2">
        <RadioButton v-model="size" inputId="size_large" name="size" value="Large" size="large" />
        <label for="size_large" class="text-lg">Large</label>
    </div>
</div>

无效状态使用invalid属性显示,以指示验证失败。在与表单验证库集成时,可以使用此样式。


<RadioButton v-model="value" value="1" :invalid="value === null" />

当存在disabled时,无法编辑和聚焦该元素。


<RadioButton v-model="value" :value="1" disabled />
<RadioButton v-model="value" :value="2" disabled />

屏幕阅读器

单选按钮组件内部使用一个隐藏的原生单选按钮元素,该元素仅对屏幕阅读器可见。描述组件的值可以通过label标签结合id属性或使用aria-labelledbyaria-label属性提供。


<label for="rb1">One</label>
<RadioButton inputId="rb1" />

<span id="rb2">Two</span>
<RadioButton aria-labelledby="rb2" />

<RadioButton aria-label="Three" />

键盘支持

功能
tab将焦点移动到选中的单选按钮,如果组内没有,则第一个单选按钮接收焦点。
左箭头上箭头将焦点移动到上一个单选按钮,如果没有,则最后一个单选按钮接收焦点。
右箭头下箭头将焦点移动到下一个单选按钮,如果没有,则第一个单选按钮接收焦点。
空格如果焦点单选按钮未选中,则将其状态更改为选中。