Search
ช่องค้นหาให้ผู้ใช้พิมพ์เพื่อหาหรือกรองข้อมูล Carbon ซ่อน label ไว้และแสดงแว่นขยายแทน ปุ่มล้างข้อความจะขึ้นมาเมื่อมีข้อความในช่อง
ติดตั้ง
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
- element ยิง
inputทุกครั้งที่พิมพ์ ยิงchangeเมื่อค่าถูกยืนยัน (ออกจากช่อง หรือกด Enter) หรือถูกล้าง และยิงui-clearหลังจากปุ่มล้างหรือ Esc ล้างช่องแล้ว - ใน React + Tailwind การล้างจะยิง event
inputจริงonChangeของคุณจึงเห็นด้วย จากนั้นจึงเรียกonClear - ใส่
labelเสมอ แม้จะไม่แสดงบนจอ เพราะเป็นชื่อของ search landmark และของช่อง (ถ้าไม่ใส่จะเป็น "ค้นหา")
ขนาด
size: sm 32px, md 40px (ค่าเริ่มต้น), lg 48px ให้สูงเท่าสิ่งที่อยู่ข้าง ๆ เช่น แถบเครื่องมือของตาราง หรือปุ่ม
ล้างข้อความ
ปุ่ม × ขึ้นมาเมื่อมีข้อความ กดแล้วช่องจะว่างโดยโฟกัสยังอยู่ในช่อง กด Esc ก็ได้ผลเดียวกัน ถ้าช่องว่างอยู่แล้ว Esc จะไม่ทำอะไร dialog ที่ครอบช่องค้นหาจึงยังปิดด้วย Esc ได้ clear-label เปลี่ยนชื่อปุ่ม (ค่าเริ่มต้นคือ "ล้างคำค้นหา" ตามภาษาของหน้า)
กรองรายการระหว่างพิมพ์
ถ้ารายการสั้นและอยู่บนหน้าแล้ว ให้กรองตอน input event นี้ยิงทุกครั้งที่พิมพ์และเมื่อช่องถูกล้าง listener ตัวเดียวจึงทำให้รายการตรงกับคำค้นเสมอ
- ฝ่ายกฎหมาย
- ฝ่ายการเงิน
- ฝ่ายจัดซื้อ
- ฝ่ายทรัพยากรบุคคล
- ฝ่ายอาคารสถานที่
แสดง จาก 5 แผนก
ช่องค้นหาแบบย่อขยาย
expandable (ExpandableSearch ของ Carbon) แสดงแค่แว่นขยายจนกว่าจะมีคนคลิกหรือกด Enter หรือ Space แล้วช่องจะขยายเต็มความกว้างพร้อมโฟกัสในช่อง ถ้าออกจากช่องตอนที่ว่าง หรือกด Esc ในช่องว่าง ช่องจะยุบกลับ expanded คือสถานะ ตั้งค่านี้เพื่อเปิดช่องจากโค้ดได้ ทุกครั้งที่เปิดหรือปิดจะยิง ui-toggle พร้อม detail.expanded
แว่นขยายเป็นปุ่มที่ใช้ label เป็นชื่อและเป็นข้อความ tooltip ExpandableSearch มากับไฟล์ tw-search ไฟล์เดียวกัน
เปิดพร้อมคำค้นเดิม
ช่องค้นหาแบบย่อขยายที่มีข้อความจะเปิดค้างไว้ และยุบเมื่อช่องว่างเท่านั้น ตั้ง expanded คู่กับ value เพื่อแสดงคำค้นที่ผู้ใช้เคยค้นไว้ เช่น ตัวกรองตารางที่เก็บไว้ใน URL ลองล้างข้อความแล้วออกจากช่อง ช่องจะยุบกลับ
เปิดอยู่:
แบบ fluid
fluid (FluidSearch ของ Carbon) หรือการครอบด้วย <ui-fluid-form> แสดง label ในกล่องสูง 64px ช่องค้นหาจึงเรียงตรงกับช่อง fluid อื่น ๆ ในฟอร์ม
แบบมีขอบ (ดีไซน์ Kanok)
ในดีไซน์ Kanok ช่องกรอกเป็นแบบ filled โดยปกติ คือพื้นเทาอ่อน ไม่มีขอบ ใส่ data-field-variant="outlined" ที่ element ที่ครอบช่อง (เช่น <form> หรือ <html> ถ้าต้องการทั้งแอป) จะเปลี่ยนเป็นขอบ 1px บนพื้นใส ส่วน focus ยังเป็นวงแสงสีฟ้าเหมือนเดิม attribute นี้ไม่มีผลในดีไซน์ Carbon ตัวอย่างด้านล่างจึงครอบด้วย data-design="kanok" ดูรายละเอียดทั้งหมดที่ ดีไซน์ Kanok
อ่านอย่างเดียวและปิดใช้งาน
readonly คงข้อความไว้ ยังส่งค่าไปกับฟอร์ม และซ่อนปุ่มล้าง ใช้แสดงคำค้นของรายงานที่บันทึกไว้ ส่วน disabled ทำให้ช่องเป็นสีเทา ใช้กับช่องค้นหาที่ยังใช้ไม่ได้
ใช้ในฟอร์ม
- ใส่
nameแล้วช่องจะส่งค่าเหมือน<input type="search">กด Enter แล้วฟอร์มที่ช่องอยู่จะถูกส่ง required,pattern,minlengthและmaxlengthทำงานเหมือน search input ปกติ ช่องค้นหาของ Carbon ไม่มีบรรทัด error ถ้าไม่ผ่านการตรวจจึงขึ้นเป็น bubble ของเบราว์เซอร์readonlyคงข้อความไว้และซ่อนปุ่มล้าง ส่วนdisabledทำให้ช่องเป็นสีเทาfluidหรือการครอบด้วยฟอร์มแบบ fluid แสดง label ในกล่องสูง 64px (FluidSearchของ Carbon)
พิมพ์อย่างน้อยสองตัวอักษรแล้วกด Enter
คำค้น:
Skeleton
ระหว่างที่ผลลัพธ์กำลังโหลด <ui-search-skeleton> จองที่ของช่องค้นหาไว้ เป็นแถบเดียวสูงตาม size หรือเป็นกล่องแบบ fluid เมื่อใส่ fluid screen reader ข้ามแถบนี้ จึงต้องบอกว่า "กำลังโหลด" ที่บริเวณรอบ ๆ แทน (เช่น aria-busy="true")
คีย์บอร์ด
| ปุ่ม | ผล |
|---|---|
| Esc | ล้างข้อความโดยโฟกัสยังอยู่ในช่อง ถ้าเป็นช่องแบบย่อขยายที่ว่างอยู่ จะยุบช่องแล้วโฟกัสไปที่แว่นขยาย |
| Enter | ยืนยันค่าและส่งฟอร์มที่ช่องนี้อยู่ |
| Enter หรือ Space ที่แว่นขยาย | เปิดช่องค้นหาแบบย่อขยาย |
| Tab | ไปที่ปุ่มล้างเมื่อมีข้อความ (ถ้าไม่มีอะไรให้ล้างจะข้ามไป) |
Accessibility
- เป็น landmark
role="search"ที่ใช้ label เป็นชื่อ ผู้ใช้ screen reader จึงกระโดดมาที่ช่องนี้ได้ - label ถูกซ่อนจากจอแบบ Carbon แต่ช่องและ landmark ยังใช้ label เป็นชื่อ
aria-labelที่ตัว element ไปไม่ถึง input ข้างใน ให้ใช้labelแทน - กดปุ่มล้างแล้วโฟกัสยังอยู่ในช่อง ผู้ใช้คีย์บอร์ดพิมพ์ต่อได้ทันที
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Search
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-search> · React UiSearch
Attribute
| Attribute | Property | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|---|
label |
label |
string |
'' |
Accessible name of the field (visually hidden, as in Carbon). |
placeholder |
placeholder |
string |
'' |
|
value |
value |
string |
'' |
|
name |
name |
string |
'' |
Reflected: form-associated elements submit under the name attribute. |
size |
size |
SearchSize |
'md' |
Field height: sm 32px, md 40px, lg 48px (tokens --ui-field-height-*). |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
autocomplete |
autocomplete |
string |
'off' |
|
inputmode |
inputmode |
string |
'' |
Passed to the text box: the virtual keyboard to show (numeric, decimal, email, tel, url, search…). |
enterkeyhint |
enterkeyhint |
string |
'' |
Passed to the text box: the label of the virtual keyboard's Enter key (search, send, next, done…). |
clear-label |
clearLabel |
string |
'' |
Accessible name of the clear button. |
required |
required |
boolean |
false |
Constraint validation like <input type="search">: blocks submit and reports in the browser's bubble (Carbon Search has no error line). |
pattern |
pattern |
string |
'' |
|
minlength |
minlength |
number | undefined |
||
maxlength |
maxlength |
number | undefined |
||
readonly |
readonly |
boolean |
false |
Value can be selected and submitted but not changed; no clear button. |
fluid |
fluid |
boolean |
false |
Carbon FluidSearch: the label inside a 64px box. <ui-fluid-form> sets it for every field inside. |
expandable |
expandable |
boolean |
false |
Carbon ExpandableSearch: collapsed to its magnifier until opened. |
expanded |
expanded |
boolean |
false |
Whether an expandable search is open. |
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 until cleared with ''. |
clear() |
Empties the field, keeps focus in it and announces the change (input, change, ui-clear). |
Event
| Event | React | คำอธิบาย |
|---|---|---|
change |
onChange |
When the value is committed (blur / Enter) or cleared |
ui-toggle |
onUiToggle |
detail: { expanded } when an expandable search opens or collapses |
input |
onInput |
On every keystroke |
ui-clear |
onUiClear |
After the clear button or Esc emptied the field |
CSS part
| Part | คำอธิบาย |
|---|---|
base |
The role=search wrapper |
input |
The native search input |
label |
The label (visually hidden, as in Carbon; shown inside the box when fluid) |
magnifier |
The search icon |
clear-button |
The clear button |
magnifier-button |
The magnifier of an expandable search |
CSS custom property
| Custom property | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|
--ui-field-border |
Border on every side of the box (default none: Carbon's underline only; Kanok: a 1px outline) | |
--ui-field-disabled-border-color |
Border colour when disabled (default: the underline transparent) | |
--ui-field-readonly-border-color |
Border colour when read-only (default: a hairline underline) | |
--ui-field-radius |
Corner radius of the box (default 0; Kanok: 6px) | |
--ui-field-background |
Fill of the box (default --ui-surface-1; Kanok: transparent) |
|
--ui-label-font |
Font of the label (default --ui-font-caption; Kanok: 14px semibold) |
|
--ui-label-color |
Colour of the label (default --ui-ink-muted; Kanok: --ui-ink) |
|
--ui-icon-control-radius |
Corners of its small icon buttons, their hover fill and ring (default 0; Kanok: round) |