ui
ธีม รูปแบบ CarbonKanok ภาษา English
ภาพรวมติดตั้งการย้ายไป 1.0ธีมและ layerดีไซน์ Kanokปรับแต่งหน้าตาไอคอนฟอร์มและการตรวจค่าใช้กับ frameworkใช้กับ AIButtonText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderModalSide panelTearsheetNotificationToastTooltipDefinition tooltipPopoverToggletipMenuData tableStructured listPaginationContained listTree viewTabsBreadcrumbLinkPagination navContent switcherAccordionProgress indicatorUI shellTagTileCardProduct cardCode snippetCopy buttonUser avatarTruncated textListLoadingInline loadingProgress barSkeletonBadge indicatorStatus indicatorGridHeadingLayerAction setPage headerBig numberEdit in placeแนะนำText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderTabsPagination navContent switcherTooltipDefinition tooltipPopoverMenuAccordionProgress indicatorUI shellModalSide panelTearsheetNotificationToastData tableStructured listPaginationTree viewCode snippetCopy buttonTruncated textLoadingTagCardProduct cardEdit in place

Search

ช่องค้นหาให้ผู้ใช้พิมพ์เพื่อหาหรือกรองข้อมูล Carbon ซ่อน label ไว้และแสดงแว่นขยายแทน ปุ่มล้างข้อความจะขึ้นมาเมื่อมีข้อความในช่อง

HTMLReactReact + Tailwind<ui-search label="ค้นหาสัญญา" placeholder="ค้นจากชื่อหรือเลขที่สัญญา"></ui-search> <UiSearch label="ค้นหาสัญญา" placeholder="ค้นจากชื่อหรือเลขที่สัญญา" /> <Search label="ค้นหาสัญญา" placeholder="ค้นจากชื่อหรือเลขที่สัญญา" />

ติดตั้ง

npmshadcn · Web componentshadcn · React + Tailwindคัดลอกเองnpm install @purpurabog/ui npx shadcn@4 add @kanok-ui/search npx shadcn@4 add @kanok-ui/tw-search # Web component # 1. ติดตั้ง dependency npm install lit@^3.3.0 # 2. คัดลอก "content" ของแต่ละไฟล์จาก # https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/search.json # ไปไว้ที่ target ของมัน: # components/ui/primitives/search/search-skeleton.ts # components/ui/primitives/search/search.styles.ts # components/ui/primitives/search/search.ts # 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json): # shared-element, shared-field, shared-form-control, shared-localize, shared-skeleton, tooltip, tokens # React + Tailwind # 1. ติดตั้ง dependency npm install @purpurabog/ui@^2.3.0 # 2. คัดลอก "content" ของแต่ละไฟล์จาก # https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-search.json # ไปไว้ที่ target ของมัน: # components/ui/react-tw/search.tsx # 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json): # tw-field, tw-i18n, tw-skeleton, tw-theme, tw-tooltip, tw-utils

npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง

วิธีใช้

HTMLReactReact + Tailwind<script type="module">import '@purpurabog/ui/search';</script> <ui-search label="ค้นหาสัญญา" name="q"></ui-search> import { UiSearch } from '@purpurabog/ui/react'; <UiSearch label="ค้นหาสัญญา" name="q" onInput={(e) => setQuery((e.target as HTMLInputElement).value)} onUiClear={() => setQuery('')} /> import { Search } from '@/components/ui/react-tw/search'; <Search label="ค้นหาสัญญา" name="q" value={query} onChange={(e) => setQuery(e.target.value)} />
  • element ยิง input ทุกครั้งที่พิมพ์ ยิง change เมื่อค่าถูกยืนยัน (ออกจากช่อง หรือกด Enter) หรือถูกล้าง และยิง ui-clear หลังจากปุ่มล้างหรือ Esc ล้างช่องแล้ว
  • ใน React + Tailwind การล้างจะยิง event input จริง onChange ของคุณจึงเห็นด้วย จากนั้นจึงเรียก onClear
  • ใส่ label เสมอ แม้จะไม่แสดงบนจอ เพราะเป็นชื่อของ search landmark และของช่อง (ถ้าไม่ใส่จะเป็น "ค้นหา")

ขนาด

size: sm 32px, md 40px (ค่าเริ่มต้น), lg 48px ให้สูงเท่าสิ่งที่อยู่ข้าง ๆ เช่น แถบเครื่องมือของตาราง หรือปุ่ม

HTMLReactReact + Tailwind<ui-search label="ค้นหา" size="sm" placeholder="เล็ก"></ui-search> <ui-search label="ค้นหา" placeholder="กลาง"></ui-search> <ui-search label="ค้นหา" size="lg" placeholder="ใหญ่"></ui-search> <UiSearch label="ค้นหา" size="sm" placeholder="เล็ก" /> <UiSearch label="ค้นหา" placeholder="กลาง" /> <UiSearch label="ค้นหา" size="lg" placeholder="ใหญ่" /> <Search label="ค้นหา" size="sm" placeholder="เล็ก" /> <Search label="ค้นหา" placeholder="กลาง" /> <Search label="ค้นหา" size="lg" placeholder="ใหญ่" />

ล้างข้อความ

ปุ่ม × ขึ้นมาเมื่อมีข้อความ กดแล้วช่องจะว่างโดยโฟกัสยังอยู่ในช่อง กด Esc ก็ได้ผลเดียวกัน ถ้าช่องว่างอยู่แล้ว Esc จะไม่ทำอะไร dialog ที่ครอบช่องค้นหาจึงยังปิดด้วย Esc ได้ clear-label เปลี่ยนชื่อปุ่ม (ค่าเริ่มต้นคือ "ล้างคำค้นหา" ตามภาษาของหน้า)

HTMLReactReact + Tailwind<ui-search label="ค้นหาผู้เช่า" value="สมชาย" clear-label="ล้างคำค้นหาผู้เช่า"></ui-search> <UiSearch label="ค้นหาผู้เช่า" value="สมชาย" clearLabel="ล้างคำค้นหาผู้เช่า" onUiClear={() => resetResults()} /> <Search label="ค้นหาผู้เช่า" defaultValue="สมชาย" clearLabel="ล้างคำค้นหาผู้เช่า" onClear={() => resetResults()} />

กรองรายการระหว่างพิมพ์

ถ้ารายการสั้นและอยู่บนหน้าแล้ว ให้กรองตอน input event นี้ยิงทุกครั้งที่พิมพ์และเมื่อช่องถูกล้าง listener ตัวเดียวจึงทำให้รายการตรงกับคำค้นเสมอ

  • ฝ่ายกฎหมาย
  • ฝ่ายการเงิน
  • ฝ่ายจัดซื้อ
  • ฝ่ายทรัพยากรบุคคล
  • ฝ่ายอาคารสถานที่

แสดง 5 จาก 5 แผนก

HTMLReactReact + Tailwind<ui-search id="search-departments" label="กรองแผนก" placeholder="กรองแผนก"></ui-search> <ul id="search-departments-list"> <li>ฝ่ายกฎหมาย</li> <li>ฝ่ายการเงิน</li> <li>ฝ่ายจัดซื้อ</li> <li>ฝ่ายทรัพยากรบุคคล</li> <li>ฝ่ายอาคารสถานที่</li> </ul> <p>แสดง <output id="search-departments-out">5</output> จาก 5 แผนก</p> <script type="module"> const search = document.getElementById('search-departments'); const items = [...document.querySelectorAll('#search-departments-list li')]; search.addEventListener('input', () => { const query = search.value.trim().toLowerCase(); let shown = 0; for (const item of items) { item.hidden = !item.textContent.toLowerCase().includes(query); if (!item.hidden) shown += 1; } document.getElementById('search-departments-out').textContent = shown; }); </script> const shown = departments.filter((name) => name.toLowerCase().includes(query.trim().toLowerCase())); <UiSearch label="กรองแผนก" placeholder="กรองแผนก" onInput={(e) => setQuery((e.target as HTMLInputElement).value)} /> <ul>{shown.map((name) => <li key={name}>{name}</li>)}</ul> const shown = departments.filter((name) => name.toLowerCase().includes(query.trim().toLowerCase())); <Search label="กรองแผนก" placeholder="กรองแผนก" value={query} onChange={(e) => setQuery(e.target.value)} /> <ul>{shown.map((name) => <li key={name}>{name}</li>)}</ul>

ช่องค้นหาแบบย่อขยาย

expandable (ExpandableSearch ของ Carbon) แสดงแค่แว่นขยายจนกว่าจะมีคนคลิกหรือกด Enter หรือ Space แล้วช่องจะขยายเต็มความกว้างพร้อมโฟกัสในช่อง ถ้าออกจากช่องตอนที่ว่าง หรือกด Esc ในช่องว่าง ช่องจะยุบกลับ expanded คือสถานะ ตั้งค่านี้เพื่อเปิดช่องจากโค้ดได้ ทุกครั้งที่เปิดหรือปิดจะยิง ui-toggle พร้อม detail.expanded

HTMLReactReact + Tailwind<ui-search label="ค้นหาในตาราง" expandable></ui-search> <UiSearch label="ค้นหาในตาราง" expandable onUiToggle={(e) => setOpen(e.detail.expanded)} /> // หรือ <Search expandable … /> <ExpandableSearch label="ค้นหาในตาราง" onExpandedChange={setOpen} />

แว่นขยายเป็นปุ่มที่ใช้ label เป็นชื่อและเป็นข้อความ tooltip ExpandableSearch มากับไฟล์ tw-search ไฟล์เดียวกัน

เปิดพร้อมคำค้นเดิม

ช่องค้นหาแบบย่อขยายที่มีข้อความจะเปิดค้างไว้ และยุบเมื่อช่องว่างเท่านั้น ตั้ง expanded คู่กับ value เพื่อแสดงคำค้นที่ผู้ใช้เคยค้นไว้ เช่น ตัวกรองตารางที่เก็บไว้ใน URL ลองล้างข้อความแล้วออกจากช่อง ช่องจะยุบกลับ

เปิดอยู่: ใช่

HTMLReactReact + Tailwind<ui-search id="search-table" label="ค้นหาในตาราง" expandable expanded value="สัญญาเช่า"></ui-search> <p>เปิดอยู่: <output id="search-table-out">ใช่</output></p> <script type="module"> document.getElementById('search-table').addEventListener('ui-toggle', (event) => { document.getElementById('search-table-out').textContent = event.detail.expanded ? 'ใช่' : 'ไม่'; }); </script> <UiSearch label="ค้นหาในตาราง" expandable expanded={open} value={query} onUiToggle={(e) => setOpen(e.detail.expanded)} /> <ExpandableSearch label="ค้นหาในตาราง" defaultExpanded defaultValue="สัญญาเช่า" onExpandedChange={setOpen} />

แบบ fluid

fluid (FluidSearch ของ Carbon) หรือการครอบด้วย <ui-fluid-form> แสดง label ในกล่องสูง 64px ช่องค้นหาจึงเรียงตรงกับช่อง fluid อื่น ๆ ในฟอร์ม

HTMLReactReact + Tailwind<ui-search label="ค้นหาผู้ขาย" name="supplier" placeholder="ชื่อหรือเลขประจำตัวผู้เสียภาษี" fluid></ui-search> <UiSearch label="ค้นหาผู้ขาย" name="supplier" placeholder="ชื่อหรือเลขประจำตัวผู้เสียภาษี" fluid /> <Search label="ค้นหาผู้ขาย" name="supplier" placeholder="ชื่อหรือเลขประจำตัวผู้เสียภาษี" fluid />

แบบมีขอบ (ดีไซน์ Kanok)

ในดีไซน์ Kanok ช่องกรอกเป็นแบบ filled โดยปกติ คือพื้นเทาอ่อน ไม่มีขอบ ใส่ data-field-variant="outlined" ที่ element ที่ครอบช่อง (เช่น <form> หรือ <html> ถ้าต้องการทั้งแอป) จะเปลี่ยนเป็นขอบ 1px บนพื้นใส ส่วน focus ยังเป็นวงแสงสีฟ้าเหมือนเดิม attribute นี้ไม่มีผลในดีไซน์ Carbon ตัวอย่างด้านล่างจึงครอบด้วย data-design="kanok" ดูรายละเอียดทั้งหมดที่ ดีไซน์ Kanok

HTMLReactReact + Tailwind<div data-design="kanok" data-theme="paper" data-field-variant="outlined"> <ui-search label="ค้นหาสัญญา" placeholder="ค้นจากชื่อหรือเลขที่สัญญา"></ui-search> </div> <div data-field-variant="outlined"> <UiSearch label="ค้นหาสัญญา" placeholder="ค้นจากชื่อหรือเลขที่สัญญา" /> </div> <div data-field-variant="outlined"> <Search label="ค้นหาสัญญา" placeholder="ค้นจากชื่อหรือเลขที่สัญญา" /> </div>

อ่านอย่างเดียวและปิดใช้งาน

readonly คงข้อความไว้ ยังส่งค่าไปกับฟอร์ม และซ่อนปุ่มล้าง ใช้แสดงคำค้นของรายงานที่บันทึกไว้ ส่วน disabled ทำให้ช่องเป็นสีเทา ใช้กับช่องค้นหาที่ยังใช้ไม่ได้

HTMLReactReact + Tailwind<ui-search label="ค้นหาสัญญา" name="q" value="สัญญาเช่า" readonly></ui-search> <ui-search label="ค้นหาสัญญาที่เก็บถาวร" value="2562" disabled></ui-search> <UiSearch label="ค้นหาสัญญา" name="q" value="สัญญาเช่า" readonly /> <UiSearch label="ค้นหาสัญญาที่เก็บถาวร" value="2562" disabled /> <Search label="ค้นหาสัญญา" name="q" defaultValue="สัญญาเช่า" readOnly /> <Search label="ค้นหาสัญญาที่เก็บถาวร" defaultValue="2562" disabled />

ใช้ในฟอร์ม

  • ใส่ name แล้วช่องจะส่งค่าเหมือน <input type="search"> กด Enter แล้วฟอร์มที่ช่องอยู่จะถูกส่ง
  • required, pattern, minlength และ maxlength ทำงานเหมือน search input ปกติ ช่องค้นหาของ Carbon ไม่มีบรรทัด error ถ้าไม่ผ่านการตรวจจึงขึ้นเป็น bubble ของเบราว์เซอร์
  • readonly คงข้อความไว้และซ่อนปุ่มล้าง ส่วน disabled ทำให้ช่องเป็นสีเทา
  • fluid หรือการครอบด้วยฟอร์มแบบ fluid แสดง label ในกล่องสูง 64px (FluidSearch ของ Carbon)

พิมพ์อย่างน้อยสองตัวอักษรแล้วกด Enter

คำค้น:

HTMLReactReact + Tailwind<form id="search-form" method="dialog"> <ui-search label="ค้นหาสัญญา" name="q" minlength="2" placeholder="ชื่อหรือเลขที่สัญญา"></ui-search> </form> <p>คำค้น: <output id="search-form-out"></output></p> <script type="module"> const form = document.getElementById('search-form'); form.addEventListener('submit', (event) => { event.preventDefault(); document.getElementById('search-form-out').textContent = new FormData(form).get('q'); }); </script> <form onSubmit={(e) => { e.preventDefault(); runSearch(new FormData(e.currentTarget).get('q')); }}> <UiSearch label="ค้นหาสัญญา" name="q" minlength={2} placeholder="ชื่อหรือเลขที่สัญญา" /> </form> <form onSubmit={(e) => { e.preventDefault(); runSearch(new FormData(e.currentTarget).get('q')); }}> <Search label="ค้นหาสัญญา" name="q" minLength={2} placeholder="ชื่อหรือเลขที่สัญญา" /> </form>

Skeleton

ระหว่างที่ผลลัพธ์กำลังโหลด <ui-search-skeleton> จองที่ของช่องค้นหาไว้ เป็นแถบเดียวสูงตาม size หรือเป็นกล่องแบบ fluid เมื่อใส่ fluid screen reader ข้ามแถบนี้ จึงต้องบอกว่า "กำลังโหลด" ที่บริเวณรอบ ๆ แทน (เช่น aria-busy="true")

HTMLReactReact + Tailwind<ui-search-skeleton></ui-search-skeleton> <ui-search-skeleton fluid></ui-search-skeleton> import { UiSearchSkeleton } from '@purpurabog/ui/react'; <UiSearchSkeleton /> <UiSearchSkeleton fluid /> import { SearchSkeleton } from '@/components/ui/react-tw/search'; <SearchSkeleton /> <SearchSkeleton fluid />

คีย์บอร์ด

ปุ่ม ผล
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)