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

Text input

ช่องกรอกข้อความบรรทัดเดียว เช่น ชื่อ อีเมล รหัสผ่าน หรือเลขประจำตัว ถ้าต้องกรอกหลายบรรทัดให้ใช้ text area ถ้าเป็นจำนวนหรือยอดเงินให้ใช้ number input

HTMLReactReact + Tailwind<ui-text-input label="ชื่อ-นามสกุล" name="name" helper-text="ตามบัตรประชาชน"></ui-text-input> <ui-text-input label="รหัสผ่าน" name="password" type="password"></ui-text-input> <UiTextInput label="ชื่อ-นามสกุล" name="name" helperText="ตามบัตรประชาชน" /> <UiTextInput label="รหัสผ่าน" name="password" type="password" /> <TextInput label="ชื่อ-นามสกุล" name="name" helperText="ตามบัตรประชาชน" /> <TextInput label="รหัสผ่าน" name="password" type="password" />

ติดตั้ง

npmshadcn · Web componentshadcn · React + Tailwindคัดลอกเองnpm install @purpurabog/ui npx shadcn@4 add @kanok-ui/text-input npx shadcn@4 add @kanok-ui/tw-text-input # Web component # 1. ติดตั้ง dependency npm install lit@^3.3.0 # 2. คัดลอก "content" ของแต่ละไฟล์จาก # https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/text-input.json # ไปไว้ที่ target ของมัน: # components/ui/primitives/text-input/text-input-skeleton.ts # components/ui/primitives/text-input/text-input.styles.ts # components/ui/primitives/text-input/text-input.ts # 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json): # core-field, core-mask, shared-affixes, shared-element, shared-field, shared-form-control, shared-localize, shared-skeleton, shared-slots, tooltip, tokens # React + Tailwind # 1. ติดตั้ง dependency npm install class-variance-authority@0.7.1 @purpurabog/ui@^2.3.0 # 2. คัดลอก "content" ของแต่ละไฟล์จาก # https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-text-input.json # ไปไว้ที่ target ของมัน: # components/ui/react-tw/text-input.tsx # 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json): # tw-affix, tw-field, 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/text-input';</script> <ui-text-input label="ชื่อ-นามสกุล" name="name"></ui-text-input> import { UiTextInput } from '@purpurabog/ui/react'; <UiTextInput label="ชื่อ-นามสกุล" name="name" onInput={(e) => setName((e.target as HTMLInputElement).value)} /> import { TextInput } from '@/components/ui/react-tw/text-input'; <TextInput label="ชื่อ-นามสกุล" name="name" value={name} onChange={(e) => setName(e.target.value)} />
  • element ยิง event input แบบ native ทุกครั้งที่พิมพ์ และ change เมื่อผู้ใช้ยืนยันค่า wrapper ของ React จึงมี onInput กับ onChange
  • onChange ของ React + Tailwind เป็นของ React เอง (ยิงทุกครั้งที่พิมพ์) argument ตัวที่สองคือค่าที่ไม่มีตัวคั่นของ mask

ขนาด

size: sm 32px, md 40px (ค่าเริ่มต้น), lg 48px

HTMLReactReact + Tailwind<ui-text-input label="เล็ก" size="sm"></ui-text-input> <ui-text-input label="กลาง"></ui-text-input> <ui-text-input label="ใหญ่" size="lg"></ui-text-input> <UiTextInput label="เล็ก" size="sm" /> <UiTextInput label="กลาง" /> <UiTextInput label="ใหญ่" size="lg" /> <TextInput label="เล็ก" size="sm" /> <TextInput label="กลาง" /> <TextInput label="ใหญ่" size="lg" />

รหัสผ่าน

type="password" เพิ่มปุ่มแสดง / ซ่อนรหัสผ่านแบบ Carbon ที่ท้ายช่อง tooltip ของปุ่มเป็นชื่อปุ่มด้วย และบอกว่ากดแล้วจะเกิดอะไร: "แสดงรหัสผ่าน" แล้วเปลี่ยนเป็น "ซ่อนรหัสผ่าน" เปลี่ยนข้อความได้ด้วย show-password-label และ hide-password-label ทุกครั้งที่กดจะยิง ui-toggle พร้อม detail.visible

แสดงรหัสผ่านอยู่: ไม่

HTMLReactReact + Tailwind<ui-text-input id="text-input-password" label="รหัสผ่าน" name="password" type="password" autocomplete="current-password"></ui-text-input> <ui-text-input label="รหัส PIN" name="pin" type="password" inputmode="numeric" show-password-label="แสดง PIN" hide-password-label="ซ่อน PIN"></ui-text-input> <p>แสดงรหัสผ่านอยู่: <output id="text-input-password-out">ไม่</output></p> <script type="module"> document.getElementById('text-input-password').addEventListener('ui-toggle', (event) => { document.getElementById('text-input-password-out').textContent = event.detail.visible ? 'ใช่' : 'ไม่'; }); </script> <UiTextInput label="รหัสผ่าน" name="password" type="password" autocomplete="current-password" onUiToggle={(e) => track(e.detail.visible)} /> <UiTextInput label="รหัส PIN" name="pin" type="password" inputmode="numeric" showPasswordLabel="แสดง PIN" hidePasswordLabel="ซ่อน PIN" /> <TextInput label="รหัสผ่าน" name="password" type="password" autoComplete="current-password" onTogglePasswordVisibility={(e, visible) => track(visible)} /> <TextInput label="รหัส PIN" name="pin" type="password" inputMode="numeric" showPasswordLabel="แสดง PIN" hidePasswordLabel="ซ่อน PIN" />

อีเมล เบอร์โทร และเว็บไซต์

type รับ email, tel, url, search และ number ได้ด้วย เหมือน <input> ถ้าต้องการแค่เปลี่ยนคีย์บอร์ดบนมือถือ ให้ตั้ง inputmode (และ enterkeyhint สำหรับปุ่ม Enter ของคีย์บอร์ด)

ใส่ autocomplete ด้วย เบราว์เซอร์จะกรอกให้ได้ เบราว์เซอร์ตรวจรูปแบบอีเมลให้ ส่วน mask ช่วยให้อ่านเบอร์โทรได้ง่าย

HTMLReactReact + Tailwind<ui-text-input label="อีเมลที่ทำงาน" name="email" type="email" autocomplete="email"></ui-text-input> <ui-text-input label="เบอร์มือถือ" name="mobile" type="tel" autocomplete="tel" mask="000-000-0000" helper-text="รูปแบบ: 000-000-0000"></ui-text-input> <ui-text-input label="เว็บไซต์บริษัท" name="website" type="url" placeholder="https://"></ui-text-input> <UiTextInput label="อีเมลที่ทำงาน" name="email" type="email" autocomplete="email" /> <UiTextInput label="เบอร์มือถือ" name="mobile" type="tel" autocomplete="tel" mask="000-000-0000" helperText="รูปแบบ: 000-000-0000" /> <UiTextInput label="เว็บไซต์บริษัท" name="website" type="url" placeholder="https://" /> <TextInput label="อีเมลที่ทำงาน" name="email" type="email" autoComplete="email" /> <TextInput label="เบอร์มือถือ" name="mobile" type="tel" autoComplete="tel" mask="000-000-0000" helperText="รูปแบบ: 000-000-0000" /> <TextInput label="เว็บไซต์บริษัท" name="website" type="url" placeholder="https://" />

สกุลเงิน หน่วย และ mask

prefix-text และ suffix-text ใส่ข้อความไว้ในช่อง หน้าหรือหลังค่า เช่น สกุลเงินหรือหน่วย ข้อความนี้ไม่เป็นส่วนของค่า และ screen reader อ่านเป็นส่วนหนึ่งของคำอธิบายช่อง

mask จัดรูปแบบค่าระหว่างพิมพ์: 0 คือตัวเลข a คือตัวอักษร * คืออย่างใดอย่างหนึ่ง ส่วนตัวอักษรอื่นเป็นตัวคั่นที่ช่องใส่ให้เอง mask ที่มีแต่ตัวเลขจะเปิดคีย์บอร์ดตัวเลขบนมือถือ ควรเขียนรูปแบบไว้ใน helper text ด้วย ผู้ใช้จะได้รู้ก่อนพิมพ์

HTMLReactReact + Tailwind<ui-text-input label="น้ำหนัก" name="weight" suffix-text="กก."></ui-text-input> <ui-text-input label="เลขประจำตัวประชาชน" name="citizenId" mask="0-0000-00000-00-0" helper-text="ตัวเลข 13 หลัก: 0-0000-00000-00-0"></ui-text-input> <UiTextInput label="น้ำหนัก" name="weight" suffixText="กก." /> <UiTextInput label="เลขประจำตัวประชาชน" name="citizenId" mask="0-0000-00000-00-0" helperText="ตัวเลข 13 หลัก: 0-0000-00000-00-0" /> <TextInput label="น้ำหนัก" name="weight" suffixText="กก." /> <TextInput label="เลขประจำตัวประชาชน" name="citizenId" mask="0-0000-00000-00-0" helperText="ตัวเลข 13 หลัก: 0-0000-00000-00-0" onChange={(e, raw) => setCitizenId(raw)} />
  • value ของ element และค่าที่ส่งไปกับฟอร์มเป็นข้อความที่จัดรูปแบบแล้ว (1-2345-67890-12-3) ส่วน property rawValue คือสิ่งที่พิมพ์โดยไม่มีตัวคั่น (1234567890123) ใน React + Tailwind ค่านี้มาเป็น argument ตัวที่สองของ onChange
  • ค่าที่โค้ดของคุณตั้งให้ จะมีหรือไม่มีตัวคั่นก็ได้ ช่องจะแสดงแบบจัดรูปแบบแล้วเสมอ
  • ทั้งสามอย่างนี้ไม่มีใน Carbon (Carbon ให้เขียนหน่วยหรือรูปแบบไว้ใน label หรือ helper text) เราทำตาม USWDS

อ่านค่า

ฟัง input เพื่อทำงานทุกครั้งที่พิมพ์ หรือฟัง change เมื่อผู้ใช้กรอกเสร็จ ถ้ามี mask value คือข้อความที่ช่องแสดง ส่วน rawValue คือสิ่งที่ผู้ใช้พิมพ์

value:
rawValue:

HTMLReactReact + Tailwind<ui-text-input id="text-input-tax" label="เลขประจำตัวผู้เสียภาษี" name="taxId" mask="0-0000-00000-00-0" helper-text="ตัวเลข 13 หลัก"></ui-text-input> <p>value: <output id="text-input-tax-value"></output><br>rawValue: <output id="text-input-tax-raw"></output></p> <script type="module"> const tax = document.getElementById('text-input-tax'); tax.addEventListener('input', () => { document.getElementById('text-input-tax-value').textContent = tax.value; document.getElementById('text-input-tax-raw').textContent = tax.rawValue; }); </script> import type { UiTextInput as UiTextInputElement } from '@purpurabog/ui/text-input'; <UiTextInput label="เลขประจำตัวผู้เสียภาษี" name="taxId" mask="0-0000-00000-00-0" helperText="ตัวเลข 13 หลัก" onInput={(e) => setTaxId((e.target as UiTextInputElement).rawValue)} /> <TextInput label="เลขประจำตัวผู้เสียภาษี" name="taxId" mask="0-0000-00000-00-0" helperText="ตัวเลข 13 หลัก" onChange={(e, raw) => setTaxId(raw)} />

ตัวนับตัวอักษร

max-count แสดงตัวนับไว้เหนือช่อง เป็นขีดจำกัดแบบไม่บังคับ ผู้ใช้วางข้อความยาว ๆ แล้วค่อยตัดทีหลังได้ ถ้าเกิน ตัวนับจะเป็นสีแดง error ขึ้นทันที และฟอร์มส่งไม่ได้ screen reader จะอ่านเตือนเมื่อใกล้ครบ ไม่ได้อ่านทุกครั้งที่พิมพ์

HTMLReactReact + Tailwind<ui-text-input label="ชื่อสัญญา" name="title" max-count="30" value="สัญญาเช่าโกดัง ถนนพระรามสี่"></ui-text-input> <UiTextInput label="ชื่อสัญญา" name="title" maxCount={30} value="สัญญาเช่าโกดัง ถนนพระรามสี่" /> <TextInput label="ชื่อสัญญา" name="title" maxCount={30} defaultValue="สัญญาเช่าโกดัง ถนนพระรามสี่" />

เปลี่ยนข้อความได้ด้วย too-long-text ({max} จะถูกแทนด้วยจำนวนที่กำหนด) และ near-limit-text ถ้าต้องการขีดจำกัดที่พิมพ์เกินไม่ได้เลย ให้ใช้ maxlength รายละเอียดเพิ่มเติมที่ ฟอร์มและการตรวจค่า

ข้อความช่วย คำเตือน และ error

ใต้ช่องแสดงได้ทีละบรรทัด: error มาก่อนคำเตือน และคำเตือนมาก่อน helper text

HTMLReactReact + Tailwind<ui-text-input label="เบอร์โทร" name="phone" helper-text="เราจะโทรหาเฉพาะเรื่องสัญญานี้"></ui-text-input> <ui-text-input label="อีเมล" name="email" value="somchai@gmial.com" warn warn-text="ตรวจตัวสะกดของโดเมนอีกครั้ง"></ui-text-input> <ui-text-input label="เลขประจำตัวผู้เสียภาษี" name="taxId" value="12345" invalid invalid-text="เลขประจำตัวผู้เสียภาษีมี 13 หลัก"></ui-text-input> <UiTextInput label="เบอร์โทร" name="phone" helperText="เราจะโทรหาเฉพาะเรื่องสัญญานี้" /> <UiTextInput label="อีเมล" name="email" value="somchai@gmial.com" warn warnText="ตรวจตัวสะกดของโดเมนอีกครั้ง" /> <UiTextInput label="เลขประจำตัวผู้เสียภาษี" name="taxId" value="12345" invalid invalidText="เลขประจำตัวผู้เสียภาษีมี 13 หลัก" /> <TextInput label="เบอร์โทร" name="phone" helperText="เราจะโทรหาเฉพาะเรื่องสัญญานี้" /> <TextInput label="อีเมล" name="email" defaultValue="somchai@gmial.com" warn warnText="ตรวจตัวสะกดของโดเมนอีกครั้ง" /> <TextInput label="เลขประจำตัวผู้เสียภาษี" name="taxId" defaultValue="12345" invalid invalidText="เลขประจำตัวผู้เสียภาษีมี 13 หลัก" />
  • ใน element required, pattern, minlength และการตรวจของเบราว์เซอร์ทำงานเหมือน <input> และ error จะรอจนผู้ใช้ออกจากช่อง ส่วนช่องของ React + Tailwind แสดงตามที่คุณส่ง invalid และ invalidText มา การตรวจค่าเป็นหน้าที่ของแอป แบบ Carbon React
  • ถ้าจะใส่ลิงก์ในข้อความ ให้ใช้ slot ชื่อเดียวกัน (helper-text, warn-text, invalid-text) ใน React + Tailwind ส่ง markup ให้ helperText, warnText หรือ invalidText ได้เลย
  • เรื่องจังหวะที่ error ขึ้น custom validity และ error จาก server ดูที่ ฟอร์มและการตรวจค่า

label และข้อความช่วยที่มี markup

ถ้า label หรือข้อความมี markup เช่น ลิงก์หรือตัวเน้น ให้ใช้ slot label และ helper-text (warn-text กับ invalid-text ก็ใช้แบบเดียวกัน) ช่องของ React + Tailwind รับ markup ทาง label และ helperText

อีเมลผู้ลงนาม (ต้องกรอก) เราจะส่งสัญญาไปที่อีเมลนี้ วิธีตรวจค่าในช่อง
HTMLReactReact + Tailwind<ui-text-input name="signerEmail" type="email" required> <span slot="label">อีเมลผู้ลงนาม <em>(ต้องกรอก)</em></span> <span slot="helper-text">เราจะส่งสัญญาไปที่อีเมลนี้ <a href="/th/forms/">วิธีตรวจค่าในช่อง</a></span> </ui-text-input> <UiTextInput name="signerEmail" type="email" required> <span slot="label">อีเมลผู้ลงนาม <em>(ต้องกรอก)</em></span> <span slot="helper-text">เราจะส่งสัญญาไปที่อีเมลนี้ <a href="/forms">วิธีตรวจค่าในช่อง</a></span> </UiTextInput> <TextInput name="signerEmail" type="email" required label={<>อีเมลผู้ลงนาม <em>(ต้องกรอก)</em></>} helperText={<>เราจะส่งสัญญาไปที่อีเมลนี้ <a href="/forms">วิธีตรวจค่าในช่อง</a></>} />

ตำแหน่ง label, อ่านอย่างเดียว และปิดใช้งาน

  • hide-label ซ่อน label จากสายตาแต่ screen reader ยังอ่านได้ ใช้เฉพาะเมื่อมีอย่างอื่นบนจอที่บอกอยู่แล้วว่าช่องนี้คืออะไร
  • inline วาง label ไว้หน้าช่องและข้อความไว้หลังช่องในแถวเดียว (inline text input ของ Carbon)
  • fluid หรือการครอบด้วยฟอร์มแบบ fluid ย้าย label เข้าไปในกล่องสูง 64px (ฟอร์มแบบ fluid)
  • readonly แสดงค่าและส่งค่าไปกับฟอร์ม แต่ผู้ใช้แก้ไม่ได้ ส่วน disabled ทำให้ช่องเป็นสีเทาและไม่ส่งค่า
HTMLReactReact + Tailwind<ui-text-input label="เลขที่สัญญา" value="C-2026-0142" inline></ui-text-input> <ui-text-input label="เลขที่สัญญา" value="C-2026-0142" readonly></ui-text-input> <ui-text-input label="สาขา" value="กรุงเทพฯ" disabled></ui-text-input> <UiTextInput label="เลขที่สัญญา" value="C-2026-0142" inline /> <UiTextInput label="เลขที่สัญญา" value="C-2026-0142" readonly /> <UiTextInput label="สาขา" value="กรุงเทพฯ" disabled /> <TextInput label="เลขที่สัญญา" defaultValue="C-2026-0142" inline /> <TextInput label="เลขที่สัญญา" defaultValue="C-2026-0142" readOnly /> <TextInput label="สาขา" defaultValue="กรุงเทพฯ" disabled />

ช่องแบบ fluid

ช่องแบบ fluid วาง label ไว้ในกล่องสูง 64px และแสดง error หรือคำเตือนในกล่องใต้เส้นคั่น ช่องแบบนี้ไม่มี helper text ตั้ง fluid ที่ช่อง หรือครอบช่องทั้งหมดด้วย <ui-fluid-form> (ดู ฟอร์มแบบ fluid)

HTMLReactReact + Tailwind<ui-text-input label="มูลค่าสัญญา" name="amount" prefix-text="฿" suffix-text="บาท" value="500,000" fluid></ui-text-input> <ui-text-input label="รหัสผ่าน" name="password" type="password" fluid></ui-text-input> <UiTextInput label="มูลค่าสัญญา" name="amount" prefixText="฿" suffixText="บาท" value="500,000" fluid /> <UiTextInput label="รหัสผ่าน" name="password" type="password" fluid /> <TextInput label="มูลค่าสัญญา" name="amount" prefixText="฿" suffixText="บาท" defaultValue="500,000" fluid /> <TextInput label="รหัสผ่าน" name="password" type="password" 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-text-input label="ชื่อ-นามสกุล" name="name" helper-text="ตามบัตรประชาชน"></ui-text-input> <ui-text-input label="รหัสผ่าน" name="password" type="password"></ui-text-input> </div> <div data-field-variant="outlined"> <UiTextInput label="ชื่อ-นามสกุล" name="name" helperText="ตามบัตรประชาชน" /> <UiTextInput label="รหัสผ่าน" name="password" type="password" /> </div> <div data-field-variant="outlined"> <TextInput label="ชื่อ-นามสกุล" name="name" helperText="ตามบัตรประชาชน" /> <TextInput label="รหัสผ่าน" name="password" type="password" /> </div>

ตรวจค่าในฟอร์ม

ใน <form> element ตรวจ required, type, pattern, minlength และตัวนับให้เหมือน input ปกติ ถ้ามีช่องที่ไม่ผ่าน ฟอร์มจะส่งไม่ได้ ทุกช่องที่ไม่ผ่านจะแสดง error และช่องแรกจะได้โฟกัส ลองกดส่งฟอร์มนี้โดยไม่กรอกอะไร

เพิ่มผู้ขาย

ส่งค่า:

HTMLReactReact + Tailwind<form id="text-input-form" method="dialog" style="display: grid; gap: 16px"> <ui-text-input label="ชื่อผู้ขาย" name="supplier" required></ui-text-input> <ui-text-input label="อีเมล" name="email" type="email" required></ui-text-input> <div><ui-button type="submit" size="md">เพิ่มผู้ขาย</ui-button></div> </form> <p>ส่งค่า: <output id="text-input-form-out"></output></p> <script type="module"> const form = document.getElementById('text-input-form'); form.addEventListener('submit', (event) => { event.preventDefault(); document.getElementById('text-input-form-out').textContent = [...new FormData(form)].map(([key, value]) => `${key}=${value}`).join(', '); }); </script> <form onSubmit={(e) => { e.preventDefault(); addSupplier(new FormData(e.currentTarget)); }}> <UiTextInput label="ชื่อผู้ขาย" name="supplier" required /> <UiTextInput label="อีเมล" name="email" type="email" required /> <UiButton type="submit" size="md">เพิ่มผู้ขาย</UiButton> </form> // การตรวจของแอปใส่ข้อความลงใน `errors` แบบ Carbon React <form onSubmit={(e) => { e.preventDefault(); addSupplier(new FormData(e.currentTarget)); }} noValidate> <TextInput label="ชื่อผู้ขาย" name="supplier" invalid={!!errors.supplier} invalidText={errors.supplier} /> <TextInput label="อีเมล" name="email" type="email" invalid={!!errors.email} invalidText={errors.email} /> <Button type="submit" size="md">เพิ่มผู้ขาย</Button> </form>

error จากการตรวจของคุณเอง

setCustomValidity(message) ทำให้ช่องไม่ผ่านพร้อมข้อความของคุณ เหมือน <input> ใช้กับการตรวจที่เบราว์เซอร์ทำเองไม่ได้ เช่น เลขที่สัญญาที่มีคนใช้แล้ว error จะขึ้นเมื่อผู้ใช้ออกจากช่อง และ setCustomValidity('') ล้าง error ดูเพิ่มที่ ฟอร์มและการตรวจค่า

HTMLReactReact + Tailwind<ui-text-input id="text-input-contract" label="เลขที่สัญญา" name="contractNo" helper-text="ลองพิมพ์ C-2026-0142 ซึ่งมีคนใช้แล้ว"></ui-text-input> <script type="module"> const contract = document.getElementById('text-input-contract'); contract.addEventListener('change', () => { contract.setCustomValidity(contract.value.trim() === 'C-2026-0142' ? 'เลขที่สัญญานี้มีคนใช้แล้ว' : ''); }); </script> <UiTextInput label="เลขที่สัญญา" name="contractNo" onChange={(e) => { const field = e.target as UiTextInputElement; field.setCustomValidity(isTaken(field.value) ? 'เลขที่สัญญานี้มีคนใช้แล้ว' : ''); }} /> <TextInput label="เลขที่สัญญา" name="contractNo" invalid={taken} invalidText="เลขที่สัญญานี้มีคนใช้แล้ว" onBlur={(e) => setTaken(isTaken(e.target.value))} />

Skeleton

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

HTMLReactReact + Tailwind<ui-text-input-skeleton></ui-text-input-skeleton> <ui-text-input-skeleton fluid></ui-text-input-skeleton> import { UiTextInputSkeleton } from '@purpurabog/ui/react'; <UiTextInputSkeleton /> <UiTextInputSkeleton fluid /> import { TextInputSkeleton } from '@/components/ui/react-tw/text-input'; <TextInputSkeleton /> <TextInputSkeleton fluid />

คีย์บอร์ด

ปุ่ม ผล
Enter ส่งฟอร์มที่ช่องนี้อยู่ เหมือน text input ปกติ
Enter หรือ Space ที่ปุ่มรหัสผ่าน แสดงหรือซ่อนรหัสผ่าน

Accessibility

  • label เป็น <label> จริงที่ผูกกับ <input> ข้างใน และบรรทัดข้อความใต้ช่องเป็น aria-describedby ของมัน aria-label ที่ตัว element ไปไม่ถึง input ให้ใช้ label แทน (คู่กับ hide-label ถ้าไม่อยากให้เห็น)
  • เมื่อมี error input จะมี aria-invalid ใน element error รอจนผู้ใช้ออกจากช่อง จะได้ไม่มีใครได้ยินคำว่า "ต้องกรอก" ระหว่างที่ยังพิมพ์อยู่
  • ปุ่มรหัสผ่านไม่มี aria-pressed ชื่อปุ่มเปลี่ยนตามสิ่งที่จะเกิดเมื่อกดครั้งถัดไป แบบ Carbon
  • prefix และ suffix ถูกอ่านเป็นส่วนหนึ่งของคำอธิบายช่อง ผู้ใช้ screen reader จึงไม่พลาด "฿" หรือ "กก."

ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Text input

API

สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)

<ui-text-input> · React UiTextInput

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 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.
invalid-text invalidText string '' Message shown when invalid. Falls back to the browser's validation message.
invalid invalid boolean false Force the error state (e.g. a server-side error).
warn warn boolean false Non-blocking warning; shown when there is no error.
warn-text warnText string '' Text of the warning shown while warn.
value value string ''
name name string '' Reflected: form-associated elements submit under the name attribute.
type type TextInputType 'text'
size size TextInputSize 'md' Field height: sm 32px, md 40px, lg 48px (tokens --ui-field-height-*).
placeholder placeholder string ''
autocomplete autocomplete string ''
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…).
pattern pattern string ''
prefix-text prefixText string '' Text inside the field before the value, e.g. a currency (฿). Part of the field's description for screen readers.
suffix-text suffixText string '' Text inside the field after the value, e.g. a unit (kg). Part of the field's description for screen readers.
mask mask string '' Formats the value as the user types: 0 a digit, a a letter, * either; any other character is a separator added for the user (\ makes a pattern character one). E.g. 0-0000-00000-00-0 (Thai citizen ID), 000-000-0000. value and the form get the formatted text, rawValue what was typed. Put the format in the helper text too (Carbon).
minlength minlength number | undefined
maxlength maxlength number | undefined
max-count maxCount number | undefined Soft limit with a visible counter (unlike maxlength, extra text isn't blocked).
too-long-text tooLongText string '' Error when over max-count; {max} is replaced with the limit.
near-limit-text nearLimitText string '' Announced once when the remaining characters drop to 10% or less.
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.
fluid fluid boolean false Carbon FluidTextInput: the label inside a 64px box, an error / warning inside it. <ui-fluid-form> sets it for every field inside.
required required boolean false Blocks form submission while the field is empty.
show-password-label showPasswordLabel string '' The password toggle's name and tooltip while the password is hidden (Carbon showPasswordLabel; default: the translation's "Show password").
hide-password-label hidePasswordLabel string '' The password toggle's name and tooltip while the password shows (Carbon hidePasswordLabel; default: the translation's "Hide password").
inline inline boolean false Carbon inline: the label before the field, the helper / error text after it (ignored while fluid).

Property

Property ชนิด คำอธิบาย
rawValue (อ่านอย่างเดียว) The characters typed, without the mask's separators (value when there is no mask).
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 onChange Re-dispatched from the native input (native change is not composed)
ui-toggle onUiToggle detail: { visible } when the password toggle shows or hides the password (Carbon onTogglePasswordVisibility)
input onInput Native input event (already composed, so it reaches the host)

Slot

Slot คำอธิบาย
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 field box around the input
label The label
input The native input
prefix The text before the value
suffix The text after the value
password-toggle The show / hide password button
password-tooltip The <ui-tooltip> around it (its name)
counter The character counter
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-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-field-readonly-background The box's fill when read-only (default transparent; Kanok: a faint ink tint, so read-only reads as set and never weaker than disabled)
--ui-fluid-background / --ui-fluid-gap - A fluid box's fill and the space under it (default the layer's fill, 0: boxes that touch; Kanok: transparent, 8px)
--ui-icon-control-radius Corners of its small icon buttons, their hover fill and ring (default 0; Kanok: round)