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

Headless Text input

TextInput คือพฤติกรรมของ Text input แบบมี style ในรูป React part ที่ไม่มี style: มี <input> ปุ่มแสดง / ซ่อนรหัสผ่าน และ mask ที่จัดรูปแบบค่าระหว่างพิมพ์

markup เป็นของคุณเอง ทั้ง label, helper text, ข้อความ error, prefix หรือ suffix, ขนาด และ style ทั้งหมด part จะตั้ง attribute data-* ไว้ให้แต่ง style

ติดตั้ง

ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):

npm install @purpurabog/ui import { TextInput } from '@purpurabog/ui/headless';

วิธีใช้

import { useState } from 'react'; import { TextInput } from '@purpurabog/ui/headless'; export default function Example() { const [name, setName] = useState(''); return ( <div className="grid max-w-xs gap-2"> <TextInput.Root value={name}> <label htmlFor="name" className="text-xs text-ink-muted">ชื่อ-นามสกุล</label> <TextInput.Input id="name" name="name" onChange={(e) => setName(e.target.value)} className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" /> </TextInput.Root> <p className="text-xs text-ink-muted">ค่า: {name}</p> </div> ); }

type, value, defaultValue และ disabled ใส่ที่ Root ไม่ใช่ที่ Input

โครงสร้าง

<TextInput.Root> <TextInput.Input /> <TextInput.PasswordToggle /> </TextInput.Root>

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

label และบรรทัดข้อความใต้ช่องเป็น element ของคุณเอง ผูกกับ input ด้วย htmlFor และ aria-describedby

import { TextInput } from '@purpurabog/ui/headless'; export default function Example() { return ( <div className="grid max-w-xs gap-2"> <TextInput.Root> <label htmlFor="full-name" className="text-xs text-ink-muted">ชื่อ-นามสกุล</label> <TextInput.Input id="full-name" name="name" aria-describedby="full-name-help" className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" /> <p id="full-name-help" className="text-xs text-ink-helper">ตามบัตรประชาชน</p> </TextInput.Root> </div> ); }

รหัสผ่าน

เมื่อ type="password" ปุ่ม PasswordToggle จะแสดงหรือซ่อนข้อความ ตั้งชื่อปุ่มตามสิ่งที่จะเกิดเมื่อกดครั้งถัดไป ("แสดงรหัสผ่าน" แล้วเปลี่ยนเป็น "ซ่อนรหัสผ่าน") ปุ่มนี้ไม่มี aria-pressed อ่านค่า visible ได้จาก useTextInputContext() และ onVisibleChange ถูกเรียกทุกครั้งที่กด พร้อมสถานะใหม่

import { useState } from 'react'; import { TextInput } from '@purpurabog/ui/headless'; function PasswordToggle() { // ตั้งชื่อปุ่มตามสิ่งที่จะเกิดเมื่อกด const { visible } = TextInput.useTextInputContext(); return ( <TextInput.PasswordToggle className="shrink-0 px-4 text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"> {visible ? 'ซ่อนรหัสผ่าน' : 'แสดงรหัสผ่าน'} </TextInput.PasswordToggle> ); } export default function Example() { const [last, setLast] = useState('ยังไม่ได้กด'); return ( <div className="grid max-w-sm gap-2"> <TextInput.Root type="password" onVisibleChange={(visible) => setLast(visible ? 'แสดง' : 'ซ่อน')}> <label htmlFor="password" className="text-xs text-ink-muted">รหัสผ่าน</label> <div className="flex border-b border-border-strong-1 bg-field-1"> <TextInput.Input id="password" name="password" autoComplete="current-password" className="h-10 min-w-0 flex-1 bg-transparent px-4 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" /> <PasswordToggle /> </div> </TextInput.Root> <p className="text-xs text-ink-muted">onVisibleChange: {last}</p> </div> ); }

ถ้าต้องการคุมสถานะเอง ให้ส่ง visible และอัปเดตค่าใน onVisibleChange

Mask

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

import { useState } from 'react'; import { TextInput } from '@purpurabog/ui/headless'; export default function Example() { const [citizenId, setCitizenId] = useState(''); return ( <div className="grid max-w-xs gap-2"> {/* prefix หรือ suffix เป็น markup ของคุณเองข้าง input */} <TextInput.Root mask="0-0000-00000-00-0"> <label htmlFor="citizen-id" className="text-xs text-ink-muted">เลขประจำตัวประชาชน</label> <TextInput.Input id="citizen-id" name="citizenId" aria-describedby="citizen-id-help" onChange={(_event, raw) => setCitizenId(raw)} className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" /> <p id="citizen-id-help" className="text-xs text-ink-helper">ตัวเลข 13 หลัก: 0-0000-00000-00-0</p> </TextInput.Root> <p className="text-xs text-ink-muted">ค่าที่ไม่มีตัวคั่น: {citizenId}</p> </div> ); }
  • input และค่าที่ส่งไปกับฟอร์มเป็นข้อความที่จัดรูปแบบแล้ว (1-2345-67890-12-3) ส่วน onChange ของ Input ได้ค่าที่ไม่มีตัวคั่น (1234567890123) เป็น argument ตัวที่สอง หลังจากช่องจัดรูปแบบเสร็จแล้ว
  • การแก้ไขถูกจัดรูปแบบในที่เดิม และเคอร์เซอร์อยู่ตำแหน่งเดิม ระหว่างที่ input method ยังประกอบคำอยู่ จะยังไม่จัดรูปแบบ
  • value หรือ defaultValue ที่โค้ดของคุณตั้งให้ จะมีหรือไม่มีตัวคั่นก็ได้ ช่องจะแสดงแบบจัดรูปแบบแล้ว
  • mask ที่มีแต่ตัวเลขจะตั้ง inputMode="numeric" ให้ เว้นแต่คุณส่ง inputMode เอง

TextInput ไม่มีตัวนับตัวอักษร ถ้าต้องการให้ใช้ part Counter ของ Headless Text area

แต่ง style

import { useState } from 'react'; import { TextInput } from '@purpurabog/ui/headless'; function PasswordToggle({ className }: { className?: string }) { const { visible } = TextInput.useTextInputContext(); return <TextInput.PasswordToggle className={className}>{visible ? 'ซ่อนรหัสผ่าน' : 'แสดงรหัสผ่าน'}</TextInput.PasswordToggle>; } export default function Example() { const [disabled, setDisabled] = useState(false); return ( <div className="grid max-w-sm gap-2"> <TextInput.Root type="password" disabled={disabled}> <label htmlFor="style-password" className="text-xs text-ink-muted">รหัสผ่าน</label> <div className="flex border-b border-border-strong-1 bg-field-1"> <TextInput.Input id="style-password" className="h-10 min-w-0 flex-1 bg-transparent px-4 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-disabled:cursor-not-allowed data-disabled:text-ink-disabled" /> <PasswordToggle className="shrink-0 px-4 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-disabled:cursor-not-allowed data-disabled:text-ink-disabled data-visible:text-link" /> </div> </TextInput.Root> <label className="flex items-center gap-2 text-sm text-ink"> <input type="checkbox" checked={disabled} onChange={(e) => setDisabled(e.target.checked)} /> ปิดใช้งาน </label> </div> ); } input[data-disabled] { color: #a8a8a8; cursor: not-allowed; } button[data-visible] { color: #0f62fe; } button[data-disabled] { cursor: not-allowed; }

Parts

TextInput.Root

ไม่ render element ใด ๆ เก็บสถานะไว้ให้ part อื่น

Prop Type ค่าเริ่มต้น
type string 'text' type ของ input ถ้าเป็น password ข้อความจะซ่อนจนกว่า PasswordToggle จะแสดง
value string | number | readonly string[] — ค่า (controlled)
defaultValue string | number | readonly string[] — ค่าเริ่มต้น (uncontrolled)
mask string — รูปแบบที่ให้พิมพ์ตาม
disabled boolean false ปิดทั้ง input และปุ่ม
visible boolean — รหัสผ่านแสดงอยู่หรือไม่ (controlled)
defaultVisible boolean false รหัสผ่านแสดงตั้งแต่แรกหรือไม่
onVisibleChange (visible: boolean, event) => void — ทุกครั้งที่กด PasswordToggle พร้อมสถานะใหม่

TextInput.Input

render <input> รับ prop ของ <input> ได้ทุกตัว ยกเว้น type, value, defaultValue และ disabled ซึ่งมาจาก Root

Prop Type ค่าเริ่มต้น
onChange (event, raw: string) => void — การเปลี่ยนค่า หลังจาก mask จัดรูปแบบแล้ว raw คือค่าที่ไม่มีตัวคั่น (ถ้าไม่มี mask ก็คือค่าเดิม)
Attribute เมื่อ
data-visible type เป็น password และรหัสผ่านแสดงอยู่
data-disabled Root ถูกปิดใช้งาน

TextInput.PasswordToggle

render <button type="button"> ไม่มี prop เพิ่ม ปุ่มจะกดไม่ได้เมื่อ Root ถูกปิดใช้งาน หรือเมื่อคุณส่ง disabled

Attribute เมื่อ
data-visible รหัสผ่านแสดงอยู่
data-disabled ปุ่มกดไม่ได้

TextInput.useTextInputContext()

คืนสถานะของ Root ที่ครอบอยู่: visible, value (ค่าที่แสดง จัดรูปแบบแล้ว), type, mask และ disabled เรียกใน component ที่อยู่ข้างใน Root

คีย์บอร์ดและ accessibility

  • Input เป็น <input> จริง กด Enter แล้วส่งฟอร์ม
  • PasswordToggle เป็นปุ่มจริง กด Enter หรือ Space ได้ และเป็น type="button" จึงไม่ส่งฟอร์ม
  • สิ่งที่คุณต้องใส่เอง: <label>, aria-describedby ที่ชี้ไปยัง helper text หรือข้อความ error และ aria-invalid เมื่อค่าผิด
  • ตั้งชื่อปุ่มรหัสผ่านให้บอกว่ากดครั้งถัดไปจะเกิดอะไร เป็นข้อความในปุ่มหรือ aria-label
  • เขียนรูปแบบของ mask ไว้ใน helper text ผู้ใช้จะได้รู้ก่อนพิมพ์