Checkbox
Checkbox ใช้เปิดหรือปิดตัวเลือกหนึ่งอย่าง ถ้าอยู่เป็นกลุ่ม ผู้ใช้เลือกได้กี่อันก็ได้ รวมถึงไม่เลือกเลย
ติดตั้ง
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
ใน web component ข้อความ label คือเนื้อหาข้างใน element ส่วนใน React + Tailwind คือ prop label import checkbox-group เฉพาะเมื่อใช้กลุ่ม
Checkbox เดี่ยว
checkedคือสถานะ eventchangeยิงเมื่อผู้ใช้ติ๊กหรือเอาติ๊กออก- ช่องที่ติ๊กไว้จะส่ง
value(ค่าเริ่มต้นคือon) ภายใต้nameช่องที่ไม่ติ๊กไม่ส่งอะไร requiredทำให้ส่งฟอร์มไม่ได้จนกว่าจะติ๊ก เช่น การยอมรับเงื่อนไข
checked:
กลุ่มของ checkbox
ui-checkbox-group ใส่ label ให้ checkbox ทั้งชุดและถูกอ่านเป็นกลุ่มเดียว
requiredหมายถึง "อย่างน้อยหนึ่งช่อง" ส่งฟอร์มไม่ได้จนกว่าจะติ๊กสักช่องorientation="horizontal"เรียง checkbox เป็นแถวแนวนอนdisabledและreadonlyมีผลกับทุกช่อง โดยไม่ทับค่าที่ตั้งไว้ในแต่ละช่อง- แต่ละ checkbox ยังส่ง
nameและvalueของตัวเอง จึงอ่านด้วยFormData.getAll()
React + Tailwind ไม่มี checkbox group ให้ใช้ FormGroup (fieldset ของ Carbon) จัดวาง checkbox เอง และตรวจ "อย่างน้อยหนึ่งช่อง" ใน validation ของคุณเอง ในตัวอย่าง docsError มาจาก validation ของคุณ เช่น "กรุณาเลือกอย่างน้อยหนึ่งรายการ"
ข้อความช่วยเหลือ คำเตือน และ error ของช่องเดี่ยว
checkbox เดี่ยวมีบรรทัดข้อความของตัวเองใต้ label คือ helper-text, warn คู่กับ warn-text ที่ไม่บล็อกการส่ง และ invalid คู่กับ invalid-text error มาก่อนคำเตือน และคำเตือนมาก่อนข้อความช่วยเหลือ
คำเตือนและ error ของกลุ่ม
ใน ui-checkbox-group ข้อความเป็นของทั้งกลุ่มและแสดงครั้งเดียวใต้กลุ่ม error จะทำให้ทุกช่องที่ใช้งานได้เป็นสีแดงด้วย และแต่ละช่องทวน error ให้ screen reader
FormGroup ของ React + Tailwind มี invalid และ message (ข้อความเป็นสีแดง) แต่ไม่มีสถานะคำเตือน
Indeterminate
indeterminate แสดงขีดแทนเครื่องหมายถูก ใช้กับ checkbox แม่ที่ checkbox ลูกถูกติ๊กไว้บางส่วน การคลิกครั้งถัดไปของผู้ใช้จะล้างสถานะนี้ โค้ดของคุณต้องคอยปรับ checkbox แม่ให้ตรงกับลูกเอง
all, chosen, some และ pick เหมือนกันในทุกแบบของ React ส่วนใน web component ให้ทำแบบเดียวกันใน listener ของ change คือตั้ง checked และ indeterminate ของ checkbox แม่ตามลูก
ซ่อน label
hide-label เก็บ label ไว้ให้ screen reader อ่านแต่ไม่แสดง เช่น checkbox ในแถวของตาราง
อ่านอย่างเดียวและปิดใช้งาน
readonly (readOnly ใน React + Tailwind) คงสถานะไว้และส่งไปกับฟอร์ม แต่การคลิกและ Space ไม่เปลี่ยนอะไร disabled ทำให้ช่องเป็นสีเทาและไม่ถูกส่งไปกับฟอร์ม
บังคับเลือกในฟอร์ม
กลุ่มที่ required ต้องติ๊กอย่างน้อยหนึ่งช่อง ส่วน checkbox เดี่ยวที่ required ต้องติ๊ก error จะรอจนผู้ใช้ใช้ช่องนั้นแล้วหรือกดส่ง ลองกดส่งโดยไม่ติ๊กอะไรเพื่อดูทั้งสองแบบ
ค่าที่ส่ง:
submit กันไม่ให้หน้าเปลี่ยน แล้วเก็บ new FormData(form) ไว้ใน sent เหมือนใน HTML ข้อความของกลุ่มมีในตัวทั้งภาษาไทยและอังกฤษ ใช้ required-text หรือ invalid-text เพื่อเปลี่ยน ดูเพิ่มที่ ฟอร์มและการตรวจค่า
Selectable tile
ถ้าแต่ละตัวเลือกต้องมีหัวข้อและคำอธิบาย ให้ใช้ ui-selectable-tile คือ checkbox ที่วาดเป็น tile (SelectableTile ของ Carbon) import จาก @purpurabog/ui/selection-tiles ส่ง name และ value เหมือน checkbox และกด Space เพื่อติ๊กได้ ใน ui-checkbox-group tile เป็นสมาชิกเหมือน checkbox คือ required, disabled, readonly และ error ของกลุ่มมีผลกับ tile ด้วย
คำนวณและชำระให้
ไทย ⇄ อังกฤษ
ส่งสำเนาที่ลงนามแล้วด้วยแมสเซนเจอร์
Skeleton
ระหว่างโหลดตัวเลือก ให้แสดง ui-checkbox-skeleton แถวละหนึ่งตัว เป็นกล่องว่างกับแถบ label skeleton ถูกซ่อนจาก screen reader จึงต้องบอกว่ากำลังโหลดที่ container เช่น aria-busy
ใน web component ให้ import จาก @purpurabog/ui/checkbox-skeleton
คีย์บอร์ด
| ปุ่ม | ทำอะไร |
|---|---|
| Space | ติ๊กหรือเอาติ๊กออกจากช่องที่โฟกัสอยู่ |
| Tab | ไปช่องถัดไป checkbox แต่ละช่องเป็นจุดหยุดของ Tab แยกกัน |
Accessibility
- แต่ละช่องเป็น checkbox จริงอยู่ใน
<label>คลิกได้ทั้งแถว และ screen reader ได้ยินสถานะรวมถึง "mixed" ui-checkbox-groupเป็นrole="group"ที่ใช้ label เป็นชื่อ ข้อความช่วยเหลือ คำเตือน หรือ error เป็นคำอธิบายของกลุ่ม และทุกช่องจะทวน error ของกลุ่ม จึงได้ยินตั้งแต่ช่องแรก- อย่าใช้ checkbox กับการตั้งค่าที่มีผลทันที ให้ใช้ Toggle
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Checkbox
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-checkbox> · React UiCheckbox
Attribute
| Attribute | Property | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|---|
checked |
checked |
boolean |
false |
|
indeterminate |
indeterminate |
boolean |
false |
Mixed state, e.g. a "select all" box with some children checked. Cleared on user toggle. |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
required |
required |
boolean |
false |
Blocks form submission while the field is empty. |
readonly |
readonly |
boolean |
false |
Shows and submits the value but blocks changes. |
hide-label |
hideLabel |
boolean |
false |
Keep the label text for screen readers but don't show it. |
name |
name |
string |
'' |
Reflected: form-associated elements submit under the name attribute. |
value |
value |
string |
'on' |
|
helper-text |
helperText |
string |
'' |
Helper text under the field, shown while there is no error or warning. |
invalid-text |
invalidText |
string |
'' |
Error text shown while invalid or while the field fails validation. |
invalid |
invalid |
boolean |
false |
Forces the error state, e.g. after server-side validation. |
warn |
warn |
boolean |
false |
Non-blocking warning; shown when there is no error. |
warn-text |
warnText |
string |
'' |
Text of the warning shown while warn. |
Property
| Property | ชนิด | คำอธิบาย |
|---|---|---|
groupDisabled |
boolean |
Set by ui-checkbox-group; kept apart from the author's own disabled / readonly. |
groupMessage |
string |
Error text of the owning ui-checkbox-group while the group is invalid. The group shows it once below the boxes; each box repeats it as its own accessible description and reports aria-invalid, so a screen reader landing on the first box hears why (an IDREF cannot cross the group's shadow root). |
willValidate |
(อ่านอย่างเดียว) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(อ่านอย่างเดียว) The <label>s that point at this field. |
Method
| Method | คำอธิบาย |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
Event
| Event | React | คำอธิบาย |
|---|---|---|
change |
ไม่มี (ใช้ ref) | When the user toggles the box |
Slot
| Slot | คำอธิบาย |
|---|---|
| (ค่าเริ่มต้น) | Label text |
CSS part
| Part | คำอธิบาย |
|---|---|
base |
The clickable row (label element) |
control |
The native checkbox input |
box |
The drawn 16px box |
label |
The text beside the box |
status-icon |
The error / warning icon |
helper-text |
The helper line |
warning-text |
The warning line |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
error-text |
The error line |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
CSS custom property
| Custom property | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|
--ui-checkbox-border |
Border of the box (default --ui-ink; Kanok: --ui-ink-subtle) |
|
--ui-checkbox-checked |
Fill and border when checked or mixed (default --ui-ink; Kanok: --ui-primary) |
|
--ui-checkbox-mark |
Check mark colour (default --ui-icon-inverse; Kanok: --ui-on-primary) |
<ui-checkbox-group> · React UiCheckboxGroup
Attribute
| Attribute | Property | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|---|
label |
label |
string |
'' |
Visible label text; the label slot replaces it when markup is needed. |
hide-label |
hideLabel |
boolean |
false |
Keep the group label for screen readers but don't show it. |
helper-text |
helperText |
string |
'' |
Helper text under the field, shown while there is no error or warning. |
required |
required |
boolean |
false |
Blocks form submission while the field is empty. |
required-text |
requiredText |
string |
'' |
Error for an empty required group. |
invalid |
invalid |
boolean |
false |
Forces the error state, e.g. after server-side validation. |
invalid-text |
invalidText |
string |
'' |
Error text shown while invalid or while the field fails validation. |
warn |
warn |
boolean |
false |
Non-blocking warning; shown when there is no error. |
warn-text |
warnText |
string |
'' |
Text of the warning shown while warn. |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
readonly |
readonly |
boolean |
false |
Shows and submits the value but blocks changes. |
orientation |
orientation |
'vertical' | 'horizontal' |
'vertical' |
Property
| Property | ชนิด | คำอธิบาย |
|---|---|---|
willValidate |
(อ่านอย่างเดียว) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(อ่านอย่างเดียว) The <label>s that point at this field. |
Method
| Method | คำอธิบาย |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
Slot
| Slot | คำอธิบาย |
|---|---|
| (ค่าเริ่มต้น) | <ui-checkbox> or <ui-selectable-tile> elements |
label |
Label content with markup; the label attribute is the plain-text fallback |
helper-text |
Helper text with markup (links, bold); overrides helper-text |
warn-text |
Warning text shown while warn; overrides warn-text |
invalid-text |
Error text shown while invalid; overrides invalid-text |
CSS part
| Part | คำอธิบาย |
|---|---|
base |
The group box holding the checkboxes |
label |
The group label |
status-icon |
The error / warning icon |
helper-text |
The helper line |
warning-text |
The warning line |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
error-text |
The error line |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
<ui-selectable-tile> · React UiSelectableTile
Attribute
| Attribute | Property | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|---|
checked |
checked |
boolean |
false |
|
indeterminate |
indeterminate |
boolean |
false |
Mixed state, e.g. a "select all" box with some children checked. Cleared on user toggle. |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
required |
required |
boolean |
false |
Blocks form submission while the field is empty. |
readonly |
readonly |
boolean |
false |
Shows and submits the value but blocks changes. |
hide-label |
hideLabel |
boolean |
false |
Keep the label text for screen readers but don't show it. |
name |
name |
string |
'' |
Reflected: form-associated elements submit under the name attribute. |
value |
value |
string |
'on' |
|
helper-text |
helperText |
string |
'' |
Helper text under the field, shown while there is no error or warning. |
invalid-text |
invalidText |
string |
'' |
Error text shown while invalid or while the field fails validation. |
invalid |
invalid |
boolean |
false |
Forces the error state, e.g. after server-side validation. |
warn |
warn |
boolean |
false |
Non-blocking warning; shown when there is no error. |
warn-text |
warnText |
string |
'' |
Text of the warning shown while warn. |
Property
| Property | ชนิด | คำอธิบาย |
|---|---|---|
groupDisabled |
boolean |
Set by ui-checkbox-group; kept apart from the author's own disabled / readonly. |
groupMessage |
string |
Error text of the owning ui-checkbox-group while the group is invalid. The group shows it once below the boxes; each box repeats it as its own accessible description and reports aria-invalid, so a screen reader landing on the first box hears why (an IDREF cannot cross the group's shadow root). |
willValidate |
(อ่านอย่างเดียว) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(อ่านอย่างเดียว) The <label>s that point at this field. |
Method
| Method | คำอธิบาย |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
Event
| Event | React | คำอธิบาย |
|---|---|---|
change |
ไม่มี (ใช้ ref) | When the user toggles the box |
Slot
| Slot | คำอธิบาย |
|---|---|
| (ค่าเริ่มต้น) | Tile content (title, description…) |
CSS part
| Part | คำอธิบาย |
|---|---|
base |
The clickable row (label element) |
control |
The native checkbox input |
box |
The drawn 16px box |
label |
The text beside the box |
status-icon |
The error / warning icon |
helper-text |
The helper line |
warning-text |
The warning line |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
error-text |
The error line |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
CSS custom property
| Custom property | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|
--ui-tile-border-color |
Border of an unchosen tile (default transparent; Kanok: a hairline); --ui-card-radius its corners |
|
--ui-card-background |
Its fill (default --ui-surface-1; Kanok paper: a cool grey that sets it apart from the page, no border) |
|
--ui-tile-selected-border |
Border of the chosen tile (default --ui-layer-selected-inverse; Kanok: primary blue); --ui-tile-selected-mark its check mark (default --ui-icon-primary) |
|
--ui-tile-selected-background |
Fill of the chosen tile (default --ui-card-background; Kanok: soft highlight wash) |
|
--ui-tile-mark-space |
End padding kept clear for the mark (default 48px, Carbon's); e.g. 16px lets the content run under the mark in the top-end corner (beyond Carbon) | |
--ui-checkbox-border |
Border of the box (default --ui-ink; Kanok: --ui-ink-subtle) |
|
--ui-checkbox-checked |
Fill and border when checked or mixed (default --ui-ink; Kanok: --ui-primary) |
|
--ui-checkbox-mark |
Check mark colour (default --ui-icon-inverse; Kanok: --ui-on-primary) |