Checkbox
通过动态勾选标记表示选中、未选中和半选状态。
适用场景
- 当界面需要以下行为时使用 Checkbox:通过动态勾选标记表示选中、未选中和半选状态。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/checkbox.json
首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。
示例
默认
checkbox.vue检查选中与半选状态,查看标记描绘和弹簧按压反馈。
packages/vue/src/motion/checkbox/examples/default.vue
<script setup lang="ts">
import { reactive } from "vue";
import { Checkbox } from "@/components/orvui/motion/checkbox";
const settings = reactive({ terms: true, announcements: false });
</script>
<template>
<div class="flex flex-col gap-3">
<Checkbox v-model="settings.terms" label="Agree to the service terms" />
<Checkbox v-model="settings.announcements" label="Receive product announcements" />
<Checkbox :model-value="true" indeterminate label="Some options selected" />
<Checkbox :model-value="true" disabled label="Unavailable" />
</div>
</template>
packages/vue/src/motion/checkbox/src/Checkbox.vue
With Description
description.vue在标签下添加解释文字,并将其关联为复选框的描述。
packages/vue/src/motion/checkbox/examples/description.vue
<script setup lang="ts">
import { reactive } from "vue";
import { Checkbox } from "@/components/orvui/motion/checkbox";
const selected = reactive<Record<string, boolean>>({ comments: true, mentions: true, digest: false });
const options = [
{ id: "comments", label: "Comments", description: "Replies to conversations you began." },
{ id: "mentions", label: "Mentions", description: "A notification when your name is tagged." },
{ id: "digest", label: "Weekly digest", description: "A Monday overview of recent activity." },
];
</script>
<template><div class="flex w-full max-w-64 flex-col gap-3">
<Checkbox v-for="option in options" :key="option.id" v-model="selected[option.id]" :label="option.label" :description="option.description" />
<Checkbox :model-value="false" disabled label="SMS alerts" description="Connect your phone number to begin." />
</div></template>
packages/vue/src/motion/checkbox/src/Checkbox.vue
状态与事件
需要由表单、路由或父组件管理时,绑定下列状态并接收对应更新。v-model 是属性与 update 事件的简写;值的类型应与 API 一致。
| 组件 | Vue 绑定 | 更新事件 |
|---|---|---|
Checkbox | v-modelboolean | update:modelValue |
可选受控属性未传入时,按组件文档的默认行为工作;不要用固定的 false 或空值代替未传入。异步请求和持久化在应用中处理,再把结果传回组件。
API 参考
默认
modelValue?booleanundefineddefaultValue?booleanfalsedisabled?booleanfalseindeterminate?booleanfalselabel?string—description?string—id?stringgeneratedIdariaLabel?stringattrs["aria-label"] as string | undefinedariaDescribedBy?stringattrs["aria-describedby"]事件
update:modelValue—@update:modelValuechange—@changeWith Description
modelValue?booleanundefineddefaultValue?booleanfalsedisabled?booleanfalseindeterminate?booleanfalselabel?string—description?string—id?stringgeneratedIdariaLabel?stringattrs["aria-label"] as string | undefinedariaDescribedBy?stringattrs["aria-describedby"]事件
update:modelValue—@update:modelValuechange—@change无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 Checkbox?
- 通过 modelValue、defaultValue、disabled、indeterminate、label、description、id、ariaLabel、ariaDescribedBy 配置 Checkbox;API 表列出其类型与默认值。
- 如何连接到应用状态?
- 监听 update:modelValue、change,将组件连接到应用状态。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
更新于
来源与许可 ↗MIT · 0.1.1