Trên trang này

Ô nhập

Ví dụ trực tiếp của Ô nhập, 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. Bấm vào một ô, hoặc Tab tới nó, để xem focus.

Nhãn và chữ hướng dẫn

Nhãn nằm trên ô và chữ hướng dẫn nằm dưới. Chữ hướng dẫn được nối bằng aria-describedby, nên trình đọc màn hình đọc nó sau nhãn.

InputWithLabel.vue
<script setup lang="ts">
import { Field, FieldDescription, FieldLabel } from '@/components/ui/field'
import { Input } from '@/components/ui/input'
</script>

<template>
  <Field class="max-w-sm">
    <FieldLabel for="store-name">Tên cửa hàng</FieldLabel>
    <Input id="store-name" autocomplete="organization" aria-describedby="store-name-help" />
    <FieldDescription id="store-name-help">Hiện trên hóa đơn và trong email gửi khách hàng.</FieldDescription>
  </Field>
</template>

Lỗi

aria-invalid="true" làm viền đỏ, và thông báo dưới ô nói cách sửa. Thêm tên miền vào địa chỉ thì lỗi biến mất. Khi ô đang có focus, viền focus hiện thay cho viền đỏ.

InputInvalid.vue
<script setup lang="ts">
import { computed, ref } from 'vue'
import { Field, FieldError, FieldLabel } from '@/components/ui/field'
import { Input } from '@/components/ui/input'

// Lỗi hiện dưới ô nhập và nói cách sửa. Thêm tên miền để lỗi biến mất.
const email = ref('donhang@ngkimkhi')
const invalid = computed(() => !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.value))
</script>

<template>
  <Field class="max-w-sm" :data-invalid="invalid">
    <FieldLabel for="alert-email">Email nhận cảnh báo đồng bộ</FieldLabel>
    <Input
      id="alert-email"
      v-model="email"
      type="email"
      autocomplete="email"
      :aria-invalid="invalid"
      :aria-describedby="invalid ? 'alert-email-error' : undefined"
    />
    <FieldError v-if="invalid" id="alert-email-error">Nhập địa chỉ email có tên miền, như [email protected].</FieldError>
  </Field>
</template>

Vô hiệu cho tới khi một lựa chọn khác bật nó

Ô bị vô hiệu dành cho giá trị có thể đổi trong biểu mẫu này nhưng lúc này thì chưa. Chữ hướng dẫn nói điều gì làm nó dùng được.

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

// Chỉ bị vô hiệu khi một lựa chọn khác trong cùng biểu mẫu tắt nó, và chữ hướng dẫn nói cách bật lại.
const custom = ref(false)
const prefix = ref('')
</script>

<template>
  <FieldGroup class="max-w-sm">
    <Field orientation="horizontal">
      <Checkbox id="custom-prefix" v-model="custom" />
      <FieldLabel for="custom-prefix">Dùng tiền tố số đơn hàng riêng</FieldLabel>
    </Field>
    <Field :data-disabled="!custom">
      <FieldLabel for="order-prefix">Tiền tố số đơn hàng</FieldLabel>
      <Input id="order-prefix" v-model="prefix" :disabled="!custom" maxlength="6" aria-describedby="order-prefix-help" />
      <FieldDescription id="order-prefix-help">
        {{ custom ? 'Tối đa 6 chữ cái, như WEB.' : 'Chọn "Dùng tiền tố số đơn hàng riêng" để thay đổi.' }}
      </FieldDescription>
    </Field>
  </FieldGroup>
</template>

Mã có độ dài thay đổi

Ứng dụng xác thực cho 6 chữ số còn mã gửi qua email có thể dài hơn, nên mã nằm trong một ô, không phải mỗi ký tự một ô (Input OTP cần độ dài cố định). Cỡ lớn, font code, và autocomplete="one-time-code" để điện thoại tự điền.

Nhập mã từ ứng dụng xác thực, hoặc mã chúng tôi gửi qua email. Mã có 6 đến 8 ký tự.

InputCode.vue
<script setup lang="ts">
// A code whose length varies (an authenticator app gives 6 digits, an emailed code up to 8), so one field,
// not one box per character (Input OTP). Large (size.control.large), code font, autofill from SMS or email.
import { ref } from 'vue'
import { Input } from '@/components/ui/input'

const code = ref('')
</script>

<template>
  <div class="grid w-full max-w-xs gap-(--bh-space-100)">
    <label for="verify-code" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Mã xác minh</label>
    <Input
      id="verify-code"
      v-model="code"
      inputmode="numeric"
      autocomplete="one-time-code"
      maxlength="8"
      spellcheck="false"
      placeholder="6 đến 8 ký tự"
      aria-describedby="verify-code-hint"
      class="h-(--bh-size-control-large) [font:var(--bh-font-code-default)]"
    />
    <p id="verify-code-hint" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">Nhập mã từ ứng dụng xác thực, hoặc mã chúng tôi gửi qua email. Mã có 6 đến 8 ký tự.</p>
  </div>
</template>