Skip to content

Checkbox ​

Vue 已实现 · 还原验收 0/2 · 上游演示 · 固定版本源码

预览 ​

用法 ​

vue
<script setup lang="ts">
import { Checkbox } from 'beui-vue'

import { ref, onBeforeUnmount, onMounted, shallowRef } from 'vue'

function usePreviewVariant(alternate: string) {
  const variant = shallowRef('default')
  function sync() {
    const requested = new URLSearchParams(location.search).get('variant')
    variant.value = requested === alternate ? alternate : 'default'
  }
  function select(name: string) {
    const url = new URL(location.href)
    url.searchParams.set('variant', name)
    history.replaceState(history.state, '', url)
    sync()
  }
  onMounted(() => { sync(); window.addEventListener('popstate',sync) })
  onBeforeUnmount(() => window.removeEventListener('popstate',sync))
  return { variant, select }
}

const { variant, select } = usePreviewVariant('description')
const terms = ref(true)
const updates = ref(false)
const comments = ref(true)
const mentions = ref(true)
const digest = ref(false)
</script>
<template>
  <div class="choices">
    <label>Example <select :value="variant" @change="select(($event.target as HTMLSelectElement).value)"><option value="default">Default</option><option value="description">Description</option></select></label>
    <template v-if="variant==='description'">
      <Checkbox v-model="comments" label="Comments" description="Replies on threads you started."/>
      <Checkbox v-model="mentions" label="Mentions" description="Whenever someone tags you by name."/>
      <Checkbox v-model="digest" label="Weekly digest" description="A summary of activity every Monday."/>
      <Checkbox disabled label="SMS alerts" description="Add a phone number to enable."/>
    </template>
    <template v-else>
    <Checkbox v-model="terms" label="Accept terms and conditions" />
    <Checkbox v-model="updates" label="Email me product updates" />
    <Checkbox :model-value="true" indeterminate label="Select all (partial)" />
    <Checkbox :model-value="true" disabled label="Disabled" />
    </template>
  </div>
</template>
<style scoped>.choices { display: grid; gap: .75rem; }</style>

API ​

v-model 绑定布尔值,indeterminate 表示部分选中。支持 label、disabled、name、value 和 required。

上游变体 ​

还原验收 ​

基于 beUI 的独立 Vue 移植,依 MIT 许可证分发。