Checkbox
チェックボックスを使用すると、ユーザーはセットから 1 つ以上の項目を選択できます。オプションのオン/オフを切り替えるために使用できます。
いつ使用するか
- 複数のオプションから複数の値を選択するために使用します。
- 1つのチェックボックスのみを使用する場合、Switchを使用して2つの状態を切り替えるのと同じです。違いは、Switchが状態の変化を直接トリガーするのに対し、Checkboxは状態が変更されたとマークするだけであり、これを送信する必要があることです。
使用例
チェックボックスの基本的な使用法。
astro
---
import Checkbox from '@/components/data-entry/Checkbox.astro';
---
<Checkbox id="terms" label="Accept terms and conditions" />
<Checkbox id="newsletter" label="Subscribe to newsletter" checked /> 無効
無効なチェックボックス。
astro
---
import Checkbox from '@/components/data-entry/Checkbox.astro';
---
<Checkbox id="chk-disabled" label="Disabled unchecked" disabled />
<Checkbox id="chk-disabled-checked" label="Disabled checked" checked disabled /> 状態
チェックボックスには、エラー状態と無効状態があります。
astro
---
import Checkbox from '@/components/data-entry/Checkbox.astro';
---
<Checkbox
id="required-chk"
label="Required field"
error="You must check this box"
/> スロットを使用したカスタム ラベルとエラー
スロットを使用して、リンクなどの HTML 要素でラベルとエラー メッセージをカスタマイズできます。
astro
---
import Checkbox from '@/components/data-entry/Checkbox.astro';
---
<Checkbox id="chk-slot-label">
I accept the <a href="#" class="text-blue-500 hover:underline">Terms of Service</a>
</Checkbox>
<Checkbox id="chk-slot-error" error>
<span slot="error" class="text-red-500 text-xs mt-1 block">Custom error with <strong>HTML</strong> support</span>
Default slot is the label
</Checkbox> | プロパティ | 説明 | タイプ | デフォルト |
|---|---|---|---|
| id | Unique identifier for the input element. An auto-generated string is used if not provided. | string | auto-generated |
| label | The label text to display next to the checkbox. | string | |
| variant | Controls the visual variant of the checkbox. When the 'error' prop/slot is used, this defaults to 'error'. | 'default' | 'error' | 'default' |
| checked | Determines if the checkbox is checked by default. | boolean | false |
| class | Additional CSS classes to apply to the root element. | string | '' |
| error | Indicates an error state or displays an error message | string | boolean | |
| success | Indicates a success state or displays a success message | string | boolean | |
| disabled | Disables the component | boolean | false |