单选框
单选框组件。
基础使用
- 通过
options属性设置选项数据。 - 通过
v-model属性绑定选中项的值。- 单独使用时,
v-model是一个布尔值。 - 非单独使用时,
v-model是一个数组,绑定的是选中项的value值。
- 单独使用时,
vue
<template>
<!-- 第一种使用方式:使用 options 设置选项数据方式 -->
<!-- 可以通过 labelKey 和 valueKey 分别设置标签和值对应的字段名称 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
labelKey="label"
valueKey="value"
></pure-radio-group>
<!-- 第二种使用方式:使用 v-for 循环渲染选项 -->
<pure-radio-group v-model="hobbyChecked">
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1" },
{ label: "足球", value: "2" },
{ label: "篮球", value: "3" },
{ label: "网球", value: "4" },
{ label: "游泳", value: "5" }
]);
const hobbyChecked = ref("1");
</script>单独使用
默认情况下 <pure-radio-group> 和 <pure-radio> 组件需结合使用,如需单独使用 <pure-radio> 组件,需设置 alone 属性 true 并通过 v-model 属性绑定选中状态。
vue
<template>
<pure-radio
label="已阅读并同意《用户协议》和《隐私政策》"
v-model="isChecked"
alone
></pure-radio>
</template>
<script setup>
import { ref } from "vue";
// 选中状态
const isChecked = ref(false);
</script>禁用状态
将 disabled 属性设置为 true 开启禁用状态。
- 设置在
<pure-radio-group>组件上,会禁用所有子项。 - 设置在
<pure-radio>组件上,会禁用单个子项。
vue
<template>
<!-- 禁用整租 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
disabled
></pure-radio-group>
<!-- 禁用单项 -->
<pure-radio-group v-model="hobbyChecked">
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
disabled="item.disabled"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1" },
{ label: "足球", value: "2" },
{ label: "篮球", value: "3", disabled: true },
{ label: "网球", value: "4" },
{ label: "游泳", value: "5" }
]);
const hobbyChecked = ref("1");
</script>只读状态
将 readonly 属性设置为 true 开启只读状态。
- 设置在
<pure-radio-group>组件上,会只读所有子项。 - 设置在
<pure-radio>组件上,会只读单个子项。
vue
<template>
<!-- 只读整租 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
readonly
></pure-radio-group>
<!-- 只读单项 -->
<pure-radio-group v-model="hobbyChecked">
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
readonly="item.readonly"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1" },
{ label: "足球", value: "2" },
{ label: "篮球", value: "3", readonly: true },
{ label: "网球", value: "4" },
{ label: "游泳", value: "5" }
]);
const hobbyChecked = ref("1");
</script>主题
通过 theme 属性设置多选框主题。
- 设置在
<pure-radio-group>组件上,会给所有子项设置同一主题。 - 设置在
<pure-radio>组件上,会给单个子项设置主题。
vue
<template>
<!-- 给所有子项设置同一主题 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
theme="danger"
></pure-radio-group>
<!-- 给单个子项设置主题 -->
<pure-radio-group v-model="hobbyChecked">
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
:theme="item.theme"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1", theme: "primary" },
{ label: "足球", value: "2", theme: "success" },
{ label: "篮球", value: "3", theme: "danger" },
{ label: "网球", value: "4", theme: "warning" },
{ label: "游泳", value: "5", theme: "error" }
]);
const hobbyChecked = ref("1");
</script>幽灵样式
通过 ghost 属性设置多选框幽灵样式。
- 设置在
<pure-radio-group>组件上,会给所有子项设置同一幽灵样式。 - 设置在
<pure-radio>组件上,会给单个子项设置幽灵样式。
vue
<template>
<!-- 给所有子项设置同一幽灵样式 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
theme="danger"
ghost
></pure-radio-group>
<!-- 给单个子项设置幽灵样式 -->
<pure-radio-group v-model="hobbyChecked">
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
:theme="item.theme"
:ghost="item.ghost"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1", ghost: true, theme: "primary" },
{ label: "足球", value: "2", ghost: false, theme: "success" },
{ label: "篮球", value: "3", ghost: true, theme: "danger" },
{ label: "网球", value: "4", ghost: false, theme: "warning" },
{ label: "游泳", value: "5", ghost: true, theme: "error" }
]);
const hobbyChecked = ref("1");
</script>镂空样式
通过 plain 属性设置多选框镂空样式。
- 设置在
<pure-radio-group>组件上,会给所有子项设置同一镂空样式。 - 设置在
<pure-radio>组件上,会给单个子项设置镂空样式。
vue
<template>
<!-- 给所有子项设置同一镂空样式 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
theme="danger"
plain
></pure-radio-group>
<!-- 给单个子项设置镂空样式 -->
<pure-radio-group v-model="hobbyChecked">
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
:theme="item.theme"
:plain="item.plain"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1", plain: true, theme: "primary" },
{ label: "足球", value: "2", plain: false, theme: "success" },
{ label: "篮球", value: "3", plain: true, theme: "danger" },
{ label: "网球", value: "4", plain: false, theme: "warning" },
{ label: "游泳", value: "5", plain: true, theme: "error" }
]);
const hobbyChecked = ref("1");
</script>方形框
通过 square 属性将框框设置为方形。
- 设置在
<pure-radio-group>组件上,会将所有子项设置为方形。 - 设置在
<pure-radio>组件上,将单个子项设置为方形。
vue
<template>
<!-- 将所有子项设置为方形 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
theme="danger"
square
></pure-radio-group>
<!-- 将单个子项设置为圆形 -->
<pure-radio-group v-model="hobbyChecked">
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
:theme="item.theme"
:square="item.square"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1", square: true, theme: "primary" },
{ label: "足球", value: "2", square: false, theme: "success" },
{ label: "篮球", value: "3", square: true, theme: "danger" },
{ label: "网球", value: "4", square: false, theme: "warning" },
{ label: "游泳", value: "5", square: true, theme: "error" }
]);
const hobbyChecked = ref("1");
</script>块级布局
通过 block 属性将多选框设置为块级布局,块级布局时,每个子项独占一行。
- 设置在
<pure-radio-group>组件上,会将所有子项设置为块级布局。 - 设置在
<pure-radio>组件上,将单个子项设置为块级布局。
vue
<template>
<!-- 将所有子项设置为块级布局 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
theme="danger"
block
></pure-radio-group>
<!-- 将单个子项设置为块级布局 -->
<pure-radio-group v-model="hobbyChecked">
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
:theme="item.theme"
:block="item.block"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1", block: true, theme: "primary" },
{ label: "足球", value: "2", block: false, theme: "success" },
{ label: "篮球", value: "3", block: true, theme: "danger" },
{ label: "网球", value: "4", block: false, theme: "warning" },
{ label: "游泳", value: "5", block: true, theme: "error" }
]);
const hobbyChecked = ref("1");
</script>反转布局
通过 reverse 属性将多选框设置为反转布局,反转布局时,标签在左侧,勾选框在右侧。
- 设置在
<pure-radio-group>组件上,会将所有子项设置为反转布局。 - 设置在
<pure-radio>组件上,将单个子项设置为反转布局。
vue
<template>
<!-- 将所有子项设置为反转布局 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
theme="danger"
block
reverse
></pure-radio-group>
<!-- 将单个子项设置为反转布局 -->
<pure-radio-group
v-model="hobbyChecked"
block
>
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
:theme="item.theme"
:reverse="item.reverse"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1", reverse: true, theme: "primary" },
{ label: "足球", value: "2", reverse: false, theme: "success" },
{ label: "篮球", value: "3", reverse: true, theme: "danger" },
{ label: "网球", value: "4", reverse: false, theme: "warning" },
{ label: "游泳", value: "5", reverse: true, theme: "error" }
]);
const hobbyChecked = ref("1");
</script>自定义颜色
- 通过
iconColor属性自定义图标颜色。 - 通过
defaultColor属性自定义未选中时的背景色。 - 通过
activeColor属性自定义选中时的背景色。
同样,以上属性可以设置在 <pure-radio-group> 和 <pure-chechbox> 上。
- 设置在
<pure-radio-group>组件上,影响所有子项。 - 设置在
<pure-radio>组件上,影响单个子项。
vue
<template>
<!-- 同一设置所有子项的自定义颜色 -->
<pure-radio-group
:options="hobby"
v-model="hobbyChecked"
iconColor="red"
defaultColor="yellow"
activeColor="blue"
></pure-radio-group>
<!-- 为每个子项设置不同的自定义颜色 -->
<pure-radio-group v-model="hobbyChecked">
<pure-radio
v-for="item in hobby"
:key="item.value"
:label="item.label"
:value="item.value"
:iconColor="item.iconColor"
:defaultColor="item.defaultColor"
:activeColor="item.activeColor"
></pure-radio>
</pure-radio-group>
</template>
<script setup>
import { ref } from "vue";
// 爱好
const hobby = ref([
{ label: "跑步", value: "1", iconColor: "red", defaultColor: "yellow", activeColor: "blue" },
{ label: "足球", value: "2", iconColor: "blue", defaultColor: "yellow", activeColor: "red" },
{ label: "篮球", value: "3", iconColor: "yellow", defaultColor: "red", activeColor: "blue" },
{ label: "网球", value: "4", iconColor: "red", defaultColor: "blue", activeColor: "yellow" },
{ label: "游泳", value: "5", iconColor: "orange", defaultColor: "pink", activeColor: "red" }
]);
const hobbyChecked = ref(["1"]);
</script>属性
<pure-checkbox-group>
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
hoverClass | String | pure-hover | 按下去的样式类 |
modelValue | Array | 选中项的值 | |
options | Array | 选项 | |
labelKey | String | label | 选项标签键名 |
valueKey | String | value | 选项值键名 |
iconName | String | 选中图标名称 | |
disabled | Boolean | false | 是否禁用 |
readonly | Boolean | false | 是否只读 |
theme | String | 主题 | |
ghost | Boolean | false | 幽灵样式 |
plain | Boolean | false | 镂空样式 |
square | Boolean | false | 是否为方形样式 |
block | Boolean | false | 是否为块级元素 |
reverse | Boolean | false | 是否反向布局 |
size | String | 1.2em | 选择框的尺寸 |
iconColor | String | 图标颜色 | |
defaultColor | String | 默认背景色 | |
activeColor | String | 选中时的背景色 | |
tabs | String | 选项卡模式 |
<pure-checkbox>
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
hoverClass | String | pure-hover | 按下去的样式类 |
label | String | label | 选项标签 |
value | String | value | 选项值 |
alone | Boolean | false | 是否单独使用 |
modelValue | Boolean | false | 单独使用时的选中状态 |
iconName | String | __checked | 选中图标名称 |
disabled | Boolean | false | 是否禁用 |
readonly | Boolean | false | 是否只读 |
theme | String | 主题 | |
ghost | Boolean | false | 幽灵样式 |
plain | Boolean | false | 镂空样式 |
square | Boolean | false | 是否为方形样式 |
block | Boolean | false | 是否为块级元素 |
reverse | Boolean | false | 是否反向布局 |
size | String | 1.2em | 选择框的尺寸 |
iconColor | String | 图标颜色 | |
defaultColor | String | 默认背景色 | |
activeColor | String | 选中时的背景色 |
事件
<pure-checkbox>
| 名称 | 参数 | 说明 |
|---|---|---|
onClick | 点击事件 |
插槽
<pure-checkbox-group>
| 名称 | 参数 | 说明 |
|---|---|---|
#default | 默认,组内容 |
<pure-checkbox>
| 名称 | 参数 | 说明 |
|---|---|---|
#default | 默认,标签 |
样式变量
<pure-radio-group>
| 名称 | 说明 |
|---|---|
--pure-radio-group-display | 显示类型 |
--pure-radio-group-flex-wrap | 内容 flex 换行模式 |
--pure-radio-group-flex-direction | 内容 flex 方向 |
--pure-radio-group-gap | 选项间距 |
--pure-radio-group-disabled-opacity | 禁用时的透明度 |
--pure-radio-group-border | 边框样式 |
--pure-radio-group-border-width | 边框宽度 |
--pure-radio-group-border-color | 边框颜色 |
--pure-radio-group-background | 背景 |
<pure-radio>
| 名称 | 说明 |
|---|---|
--pure-radio-display | 显示类型 |
--pure-radio-flex-direction | 内容 flex 布局方向 |
--pure-radio-align-items | 内容对齐方式 |
--pure-radio-justify-content | 内容对齐方式 |
--pure-radio-gap | 内容之间的间距 |
--pure-radio-box-size | 复选框大小 |
--pure-radio-box-border-radius | 复选框圆角大小 |
--pure-radio-box-margin | 复选框外边距 |
--pure-radio-box-padding | 复选框内边距 |
--pure-radio-box-background | 复选框默认背景 |
--pure-radio-checked-box-background | 选中时的背景颜色 |
--pure-radio-box-background-opacity | 复选框默认背景透明度 |
--pure-radio-checked-box-background-opacity | 选中时的背景透明度 |
--pure-radio-box-border | 复选框边框样式 |
--pure-radio-box-border-width | 复选框边框宽度 |
--pure-radio-checked-box-border-width | 选中时的边框宽度 |
--pure-radio-box-border-style | 复选框边框类型 |
--pure-radio-checked-box-border-style | 选中时的边框类型 |
--pure-radio-box-border-color | 复选框边框颜色 |
--pure-radio-checked-box-border-color | 选中时的边框颜色 |
--pure-radio-icon-font-size | 图标字体大小 |
--pure-radio-checked-icon-font-size | 选中时的图标字体大小 |
--pure-radio-icon-font-weight | 图标字体粗细 |
--pure-radio-icon-color | 图标颜色 |
--pure-radio-icon-transition | 图标过渡效果 |
--pure-radio-disabled-opacity | 禁用时的透明度 |
--pure-radio-tab-padding | 选项卡内边距 |
--pure-radio-tab-color | 选项卡文字颜色 |
--pure-radio-tab-text-align | 选项卡文字对齐方式 |
--pure-radio-tab-background | 选项卡默认背景 |
--pure-radio-tab-background-opacity | 选项卡默认背景透明度 |
--pure-radio-tab-border-radius | 选项卡圆角大小 |
--pure-radio-tab-checked-color | 选项卡选中时的文字颜色 |
--pure-radio-tab-checked-background | 选项卡选中时的背景颜色 |
--pure-radio-tab-checked-background-opacity | 选项卡选中时背景透明度 |