Skip to content

Field

Menggabungkan label, kontrol, dan teks bantuan menjadi satu baris form yang mudah diakses.

Payment Method

All transactions are secure and encrypted

Enter your 16-digit card number

Billing Address

The billing address associated with your payment method

Instalasi

bash
npx shadcn-vue@latest add field

Penggunaan

vue
<script setup lang="ts">
import {
  Field,
  FieldContent,
  FieldDescription,
  FieldError,
  FieldGroup,
  FieldLabel,
  FieldLegend,
  FieldSeparator,
  FieldSet,
  FieldTitle,
} from '@/components/ui/field'
</script>

<template>
  <FieldSet>
    <FieldLegend>Profile</FieldLegend>
    <FieldDescription>This appears on invoices and emails.</FieldDescription>
    <FieldGroup>
      <Field>
        <FieldLabel for="name">
          Full name
        </FieldLabel>
        <Input id="name" autocomplete="off" placeholder="Evil Rabbit" />
        <FieldDescription>This appears on invoices and emails.</FieldDescription>
      </Field>
      <Field>
        <FieldLabel for="username">
          Username
        </FieldLabel>
        <Input id="username" autocomplete="off" aria-invalid />
        <FieldError>Choose another username.</FieldError>
      </Field>
      <Field orientation="horizontal">
        <Switch id="newsletter" />
        <FieldLabel for="newsletter">
          Subscribe to the newsletter
        </FieldLabel>
      </Field>
    </FieldGroup>
  </FieldSet>
</template>

Anatomi

Keluarga komponen Field dirancang untuk menyusun form yang mudah diakses. Susunan satu field pada umumnya seperti ini:

vue
<template>
  <Field>
    <FieldLabel for="input-id">
      Label
    </FieldLabel>
    <!-- Input, Select, Switch, etc. -->
    <FieldDescription>Optional helper text.</FieldDescription>
    <FieldError>Validation message.</FieldError>
  </Field>
</template>
  • Field adalah pembungkus inti untuk satu field.
  • FieldContent adalah kolom flex yang menyatukan label dan keterangan. Tidak perlu dipakai kalau field Anda tanpa keterangan.
  • Bungkus field-field yang berkaitan dengan FieldGroup, dan pakai FieldSet bersama FieldLegend untuk pengelompokan yang bermakna.

Contoh

Input

Choose a unique username for your account.

Must be at least 8 characters long.

Textarea

Share your thoughts about our service.

Select

Select your department or area of work.

Slider

Price Range

Set your budget range ($ 200 - 800).

Fieldset

Address Information

We need your address to deliver your order.

Checkbox

Show these items on the desktop

Select the items you want to show on the desktop.

Your Desktop & Documents folders are being synced with iCloud Drive. You can access them from other devices.

Radio

Yearly and lifetime plans offer significant savings.

Switch

Enable multi-factor authentication. If you do not have a two-factor device, you can use a one-time code sent to your email.

Kartu Pilihan

Bungkus komponen Field di dalam FieldLabel untuk membuat kelompok field yang bisa dipilih. Cara ini berlaku untuk komponen RadioItem, Checkbox, dan Switch.

Select the compute environment for your cluster.

Field Group

Tumpuk komponen Field memakai FieldGroup. Tambahkan FieldSeparator untuk memisahkannya.

Get notified when ChatGPT responds to requests that take time, like research or image generation.

Get notified when tasks you've created have updates. Manage tasks

Tata Letak Responsif

Kalau Anda memakai Tailwind CSS v3, Anda perlu memasang @tailwindcss/container-queries.

  • Field tegak: orientasi bawaan menumpuk label, kontrol, dan teks bantuan — cocok untuk tata letak yang mengutamakan mobile.
  • Field mendatar: isi orientation="horizontal" pada Field agar label dan kontrolnya bersebelahan. Padukan dengan FieldContent supaya keterangannya tetap sejajar.
  • Field responsif: isi orientation="responsive" agar susunan kolomnya menyesuaikan otomatis di dalam induk yang sadar ukuran container. Pakai class @container/field-group pada FieldGroup untuk berganti orientasi di breakpoint tertentu.
Profile

Fill in your profile information.

Provide your full name for identification

You can write your message here. Keep it short, preferably under 100 characters.

Validasi dan Pesan Error

  • Tambahkan data-invalid pada Field untuk mengubah seluruh blok menjadi keadaan error.
  • Tambahkan aria-invalid pada input-nya sendiri demi teknologi bantu.
  • Tempatkan FieldError tepat setelah kontrolnya, atau di dalam FieldContent, supaya pesan error tetap sejajar dengan field-nya.
vue
<template>
  <Field data-invalid>
    <FieldLabel for="email">
      Email
    </FieldLabel>
    <Input id="email" type="email" aria-invalid />
    <FieldError>Enter a valid email address.</FieldError>
  </Field>
</template>

Aksesibilitas

  • FieldSet dan FieldLegend menjaga kontrol yang berkaitan tetap satu kelompok bagi pengguna keyboard dan teknologi bantu.
  • Field menghasilkan role="group", sehingga kontrol di dalamnya mewarisi pelabelan dari FieldLabel dan FieldLegend.
  • Pakai FieldSeparator seperlunya saja, supaya batas antar bagian tetap jelas terbaca pembaca layar.

Referensi API

FieldSet

Wadah yang menghasilkan fieldset bermakna, lengkap dengan pengaturan jarak bawaan.

PropTipeBawaan
classstring
vue
<template>
  <FieldSet>
    <FieldLegend>Delivery</FieldLegend>
    <FieldGroup>
      <!-- Fields -->
    </FieldGroup>
  </FieldSet>
</template>

FieldLegend

Elemen legend untuk sebuah FieldSet. Ganti ke varian label agar ukurannya selaras dengan label.

PropTypeDefault
variant"legend" | "label""legend"
classstring
vue
<FieldLegend variant="label">
Notification Preferences
</FieldLegend>

FieldLegend punya dua varian: legend dan label. Varian label memakai ukuran dan perataan seperti label — berguna kalau FieldSet Anda bersarang.

FieldGroup

Pembungkus tata letak yang menumpuk komponen Field dan mengaktifkan container query untuk orientasi responsif.

PropTipeBawaan
classstring
vue
<template>
  <FieldGroup class="@container/field-group flex flex-col gap-6">
    <Field><!-- Fields --></Field>
    <Field><!-- Fields --></Field>
  </FieldGroup>
</template>

Field

Pembungkus inti untuk satu field. Mengatur orientasi, style keadaan tidak valid, dan jarak antar elemen.

PropTypeDefault
orientation"vertical" | "horizontal" | "responsive""vertical"
classstring
data-invalidboolean
vue
<Field orientation="horizontal">
  <FieldLabel for="remember">Remember me</FieldLabel>
  <Switch id="remember" />
</Field>

FieldContent

Kolom flex yang menyatukan kontrol dan keterangannya saat label berada di samping kontrol. Tidak perlu dipakai kalau field Anda tanpa keterangan.

PropTipeBawaan
classstring
vue
<Field>
  <Checkbox id="notifications" />
  <FieldContent>
    <FieldLabel for="notifications">Notifications</FieldLabel>
    <FieldDescription>Email, SMS, and push options.</FieldDescription>
  </FieldContent>
</Field>

FieldLabel

Label yang style-nya cocok baik untuk input langsung maupun Field yang bersarang di dalamnya.

PropTypeDefault
classstring
asChildbooleanfalse
vue
<FieldLabel for="email">
Email
</FieldLabel>

FieldTitle

Menampilkan judul bergaya label di dalam FieldContent.

PropTipeBawaan
classstring
vue
<FieldContent>
  <FieldTitle>Enable Touch ID</FieldTitle>
  <FieldDescription>Unlock your device faster.</FieldDescription>
</FieldContent>

FieldDescription

Slot teks bantuan yang otomatis menyeimbangkan baris panjang pada tata letak mendatar.

PropTipeBawaan
classstring
vue
<FieldDescription>
We never share your email with anyone.
</FieldDescription>

FieldSeparator

Pembatas visual antar bagian di dalam FieldGroup. Bisa diisi konten sebaris kalau perlu.

PropTipeBawaan
classstring
vue
<FieldSeparator>
Or continue with
</FieldSeparator>

FieldError

Wadah pesan error yang mudah diakses. Bisa diisi langsung, atau menerima array errors — misalnya dari vee-validate.

PropTypeDefault
errorsArray<{ message?: string } | undefined>
classstring
vue
<FieldError :errors="errors.username" />

Kalau array errors memuat lebih dari satu pesan, komponen ini otomatis menampilkannya sebagai daftar.

FieldError juga menerima issue dari validator mana pun yang menerapkan Standard Schema, termasuk Zod, Valibot, dan ArkType. Teruskan array issues dari hasil validasi skema untuk menampilkan daftar error yang seragam lintas library.

Dirilis di bawah lisensi MIT. Fork dari shadcn-vue.