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 area

TextArea คือพฤติกรรมของ Text area แบบมี style ในรูป React part ที่ไม่มี style: มี <textarea> ที่ขยายตามข้อความได้ และตัวนับตัวอักษรหรือนับคำแบบไม่บังคับ ช่องแบบ uncontrolled และตัวนับจะกลับไปที่ defaultValue เมื่อฟอร์ม reset

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

ติดตั้ง

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

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

วิธีใช้

import { useState } from 'react'; import { TextArea } from '@purpurabog/ui/headless'; export default function Example() { const [notes, setNotes] = useState(''); return ( <div className="grid max-w-sm gap-2"> <TextArea.Root value={notes}> <label htmlFor="notes" className="text-xs text-ink-muted">หมายเหตุ</label> <TextArea.Input id="notes" name="notes" onChange={(e) => setNotes(e.target.value)} className="border-b border-border-strong-1 bg-field-1 px-4 py-3 text-sm leading-5 text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" /> </TextArea.Root> <p className="whitespace-pre-wrap text-xs text-ink-muted">ค่า: {notes}</p> </div> ); }

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

โครงสร้าง

<TextArea.Root> <TextArea.Counter /> <TextArea.Input /> </TextArea.Root>

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

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

import { TextArea } from '@purpurabog/ui/headless'; export default function Example() { return ( <div className="grid max-w-sm gap-2"> <TextArea.Root> <label htmlFor="legal-notes" className="text-xs text-ink-muted">หมายเหตุ</label> <TextArea.Input id="legal-notes" name="notes" rows={4} aria-describedby="legal-notes-help" className="border-b border-border-strong-1 bg-field-1 px-4 py-3 text-sm leading-5 text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" /> <p id="legal-notes-help" className="text-xs text-ink-helper">สิ่งที่ฝ่ายกฎหมายควรรู้</p> </TextArea.Root> </div> ); }

ความสูงและการขยายตามข้อความ

rows เป็น attribute ปกติของ <textarea> คือความสูงเริ่มต้นเป็นจำนวนบรรทัด autoGrow ที่ Input เริ่มที่ rows แล้วสูงขึ้นทีละบรรทัดตามข้อความ maxRows จำกัดความสูงสูงสุด เกินจากนั้นจะเลื่อนดูได้ ช่องจะคำนวณความสูงใหม่หลังทุก render และเมื่อความกว้างเปลี่ยน

import { TextArea } from '@purpurabog/ui/headless'; export default function Example() { return ( <div className="grid max-w-sm gap-2"> <TextArea.Root> <label htmlFor="description" className="text-xs text-ink-muted">รายละเอียด</label> <TextArea.Input id="description" name="description" rows={2} autoGrow maxRows={6} className="resize-none border-b border-border-strong-1 bg-field-1 px-4 py-3 text-sm leading-5 text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" /> </TextArea.Root> </div> ); }
  • autoGrow ตั้ง style height และ overflow-y แบบ inline ส่วนมุมสำหรับลากเป็นเรื่องของคุณ ถ้าไม่ต้องการให้ตั้ง resize: none
  • maxRows วัดจาก line-height ที่คำนวณได้ จึงต้องตั้ง line-height ที่ไม่ใช่ normal

นับตัวอักษรหรือนับคำ

maxCount ที่ Root เปิดตัวนับ เป็นขีดจำกัดแบบไม่บังคับ พิมพ์เกินได้ เมื่อเกิน Input และ Counter จะมี data-over และเมื่ออยู่ใน 10% สุดท้ายก่อนครบ Counter จะมี data-near

counterMode="word" นับเป็นคำแทน คำถูกนับด้วยตัวตัดคำของเบราว์เซอร์ตาม lang (ค่าเริ่มต้น en) คำไทยจึงนับเป็นคำทั้งคำ ตัวเลขไม่นับเป็นคำ

import { TextArea } from '@purpurabog/ui/headless'; export default function Example() { return ( <div className="grid max-w-sm gap-2"> {/* Counter แสดง "5/50" และมี data-near กับ data-over ให้แต่ง style */} <TextArea.Root maxCount={50} counterMode="word" lang="th" defaultValue="สวัสดีครับ ผมชื่อสมชาย"> <div className="flex justify-between text-xs text-ink-muted"> <label htmlFor="summary">สรุป</label> <TextArea.Counter aria-hidden="true" className="data-near:font-semibold data-near:text-ink data-over:text-error-text" /> </div> <TextArea.Input id="summary" name="summary" className="border-b border-border-strong-1 bg-field-1 px-4 py-3 text-sm leading-5 text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-over:outline-2 data-over:-outline-offset-2 data-over:outline-error" /> </TextArea.Root> </div> ); }

ตัวนับแค่นับให้ ข้อความเตือนเมื่อใกล้ครบหรือเกิน และสิ่งที่ screen reader อ่าน เป็นหน้าที่ของคุณ ถ้าต้องการขีดจำกัดที่พิมพ์เกินไม่ได้เลย ให้ใช้ maxLength ของ <textarea>

แต่ง style

import { TextArea } from '@purpurabog/ui/headless'; export default function Example() { return ( <div className="grid max-w-sm gap-2"> <TextArea.Root maxCount={40}> <div className="flex justify-between text-xs text-ink-muted"> <label htmlFor="style-notes">หมายเหตุ</label> <TextArea.Counter aria-hidden="true" className="data-near:font-semibold data-near:text-ink data-over:text-error-text" /> </div> <TextArea.Input id="style-notes" autoGrow maxRows={8} className="w-full resize-none border-b border-border-strong-1 bg-field-1 px-4 py-3 text-sm leading-5 text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-over:outline-2 data-over:-outline-offset-2 data-over:outline-error" /> </TextArea.Root> </div> ); } textarea { line-height: 1.25rem; resize: none; } textarea[data-over] { outline: 2px solid #da1e28; } .counter[data-near] { color: #8e6a00; } .counter[data-over] { color: #da1e28; }

Parts

TextArea.Root

ไม่ render element ใด ๆ เก็บข้อความไว้ให้ตัวนับ

Prop Type ค่าเริ่มต้น
value string | number | readonly string[] — ข้อความ (controlled)
defaultValue string | number | readonly string[] — ข้อความเริ่มต้น (uncontrolled)
maxCount number — ขีดจำกัดของตัวนับ ถ้าไม่ใส่ก็ไม่มีตัวนับ
counterMode 'character' | 'word' 'character' สิ่งที่ maxCount นับ
lang string 'en' ภาษา (BCP 47) ที่ใช้ตัดคำ

TextArea.Input

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

Prop Type ค่าเริ่มต้น
autoGrow boolean false ขยายตามข้อความ เริ่มจาก rows
maxRows number — (ไม่จำกัด) จำนวนบรรทัดมากสุดที่ autoGrow แสดงก่อนเลื่อนดู
Attribute เมื่อ
data-over จำนวนเกิน maxCount

TextArea.Counter

render <span> ที่แสดง length/maxCount (12/100) หรือ children ของคุณแทน ถ้าไม่มี maxCount จะไม่ render อะไร ไม่มี prop เพิ่ม

Attribute เมื่อ
data-over จำนวนเกิน maxCount
data-near จำนวนอยู่ใน 10% สุดท้ายก่อนครบ maxCount และยังไม่เกิน

TextArea.useTextAreaContext()

คืนสถานะของ Root ที่ครอบอยู่: length, maxCount, over และ near ใช้เขียนข้อความตัวนับหรือข้อความเตือนเอง

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

  • Input เป็น <textarea> จริง Enter ขึ้นบรรทัดใหม่ ไม่ส่งฟอร์ม
  • สิ่งที่คุณต้องใส่เอง: <label>, aria-describedby ที่ชี้ไปยัง helper text หรือข้อความ error และ aria-invalid เมื่อข้อความยาวเกิน
  • ซ่อนตัวนับจาก screen reader (aria-hidden="true") แล้วแจ้งเรื่องขีดจำกัดเอง เช่น แจ้งครั้งเดียวใน live region เมื่อ near หรือ over เปิด ไม่ใช่ทุกครั้งที่พิมพ์