Trên trang này

Checkbox

Ví dụ trực tiếp của Checkbox, kèm đúng file Vue hiển thị từng ví dụ.

Mỗi ví dụ là một file Vue hoàn chỉnh. Chép vào ứng dụng đã cài registry, hoặc mở trong StackBlitz.

Có nhãn

Nhãn cũng là vùng bấm.

CheckboxWithLabel.vue
<script setup lang="ts">
import { ref } from 'vue'
import { Checkbox } from '@/components/ui/checkbox'
import { Field, FieldLabel } from '@/components/ui/field'

const notify = ref(true)
</script>

<template>
  <Field orientation="horizontal" class="max-w-sm">
    <Checkbox id="notify-failed" v-model="notify" />
    <FieldLabel for="notify-failed">Gửi email cho tôi khi đồng bộ bị lỗi</FieldLabel>
  </Field>
</template>

Có mô tả

Một dòng dưới nhãn giải thích tùy chọn làm gì.

CheckboxWithDescription.vue
<script setup lang="ts">
import { ref } from 'vue'
import { Checkbox } from '@/components/ui/checkbox'
import { Field, FieldContent, FieldDescription, FieldLabel } from '@/components/ui/field'

const splitShipments = ref(false)
</script>

<template>
  <Field orientation="horizontal" class="max-w-md">
    <Checkbox id="split-shipments" v-model="splitShipments" aria-describedby="split-shipments-help" />
    <FieldContent>
      <FieldLabel for="split-shipments">Tách đơn hàng giữa các kho</FieldLabel>
      <FieldDescription id="split-shipments-help">Khi một kho thiếu hàng, giao phần còn lại từ kho gần tiếp theo. Khách có thể nhận nhiều hơn một kiện.</FieldDescription>
    </FieldContent>
  </Field>
</template>

Chọn tất cả

Ô cha hiện dấu gạch khi chỉ một số ô con được chọn.

Đồng bộ tồn kho tới
CheckboxSelectAll.vue
<script setup lang="ts">
import { computed, ref } from 'vue'
import { Checkbox } from '@/components/ui/checkbox'
import { Field, FieldGroup, FieldLabel, FieldLegend, FieldSet } from '@/components/ui/field'

// "Tất cả kênh" được chọn, bỏ chọn, hoặc ở trạng thái chọn một phần (dấu gạch) khi chỉ vài kênh được chọn.
const channels = [
  { id: 'channel-web', label: 'Cửa hàng online' },
  { id: 'channel-marketplace', label: 'Sàn thương mại điện tử' },
  { id: 'channel-pos', label: 'Điểm bán hàng (POS)' },
]
const selected = ref<string[]>(['channel-web'])

const all = computed<boolean | 'indeterminate'>({
  get: () => selected.value.length === channels.length ? true : selected.value.length === 0 ? false : 'indeterminate',
  set: value => { selected.value = value === true ? channels.map(channel => channel.id) : [] },
})

function toggle(id: string, on: boolean | 'indeterminate') {
  selected.value = on === true ? [...selected.value, id] : selected.value.filter(item => item !== id)
}
</script>

<template>
  <FieldSet class="max-w-sm">
    <FieldLegend variant="label">Đồng bộ tồn kho tới</FieldLegend>
    <FieldGroup data-slot="checkbox-group">
      <Field orientation="horizontal">
        <Checkbox id="channel-all" v-model="all" />
        <FieldLabel for="channel-all">Tất cả kênh</FieldLabel>
      </Field>
      <FieldGroup data-slot="checkbox-group" class="pl-(--bh-space-300)">
        <Field v-for="channel in channels" :key="channel.id" orientation="horizontal">
          <Checkbox
            :id="channel.id"
            :model-value="selected.includes(channel.id)"
            @update:model-value="value => toggle(channel.id, value)"
          />
          <FieldLabel :for="channel.id">{{ channel.label }}</FieldLabel>
        </Field>
      </FieldGroup>
    </FieldGroup>
  </FieldSet>
</template>