Text area
ช่องกรอกข้อความหลายบรรทัด เช่น หมายเหตุ คำอธิบาย หรือเหตุผล มี label, helper text และบรรทัด error เหมือน text input และเพิ่มตัวนับตัวอักษรหรือนับคำ กับช่องที่ขยายตามข้อความได้
ติดตั้ง
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
- element ยิง event
inputแบบ native ทุกครั้งที่พิมพ์ และchangeเมื่อผู้ใช้ออกจากช่อง wrapper ของ React จึงมีonInputกับonChangeส่วนonChangeของ React + Tailwind เป็นของ React เอง (ยิงทุกครั้งที่พิมพ์)
ความสูง
rows กำหนดความสูงเริ่มต้นเป็นจำนวนบรรทัด ค่าเริ่มต้นคือ 4 แบบ Carbon ผู้ใช้ลากมุมช่องให้สูงขึ้นได้ (resize: vertical) ช่องที่ปิดใช้งานจะย่อขยายไม่ได้
ขยายตามข้อความ
auto-grow เริ่มที่ rows แล้วสูงขึ้นทีละบรรทัดตามที่ผู้ใช้พิมพ์ และไม่มีมุมสำหรับลาก max-rows จำกัดความสูงสูงสุด เกินจากนั้นช่องจะเลื่อนดูได้ Carbon ไม่มี auto-grow
นับตัวอักษรหรือนับคำ
max-count แสดงตัวนับไว้เหนือช่อง เป็นขีดจำกัดแบบไม่บังคับเหมือน text input: ถ้าเกิน ตัวนับจะเป็นสีแดง error ขึ้นทันที และฟอร์มส่งไม่ได้ แต่ไม่มีข้อความไหนถูกตัดทิ้ง screen reader จะอ่านเตือนเมื่อใกล้ครบ
counter-mode="word" นับเป็นคำแทน (counterMode ของ Carbon) ภาษาไทยไม่เว้นวรรคระหว่างคำ ช่องจึงใช้ตัวตัดคำของเบราว์เซอร์ตามภาษาของช่อง (lang ที่ช่องหรือที่หน้า): "สวัสดีครับ ผมชื่อสมชาย" นับได้ 5 คำ ส่วนวิธีนับของ Carbon จะตัดคำไทยทุกครั้งที่เจอสระหรือวรรณยุกต์ ตัวเลขไม่นับเป็นคำ
เปลี่ยนข้อความได้ด้วย too-long-text ({max} จะถูกแทนด้วยจำนวนที่กำหนด) และ near-limit-text ถ้าต้องการขีดจำกัดที่พิมพ์เกินไม่ได้เลย ให้ใช้ maxlength รายละเอียดเพิ่มเติมที่ ฟอร์มและการตรวจค่า
เขียนข้อความของตัวนับเอง
too-long-text คือ error เมื่อข้อความเกินจำนวนที่กำหนด ส่วน near-limit-text คือข้อความที่ screen reader อ่านครั้งเดียวเมื่อเหลือไม่เกิน 10% เขียนให้เข้ากับช่อง ผู้ใช้จะได้รู้ว่าต้องตัดอะไร ข้อความในตัวอย่างนี้ยาวเกินอยู่แล้ว error จึงขึ้นทันที
ข้อความช่วย คำเตือน และ error
เหมือน text input: ใต้ช่องแสดงได้ทีละบรรทัด error มาก่อนคำเตือน และคำเตือนมาก่อน helper text ไอคอน error และคำเตือนอยู่ที่มุมขวาบนของช่อง
ใน element required, minlength และ maxlength ทำงานเหมือน <textarea> ส่วนใน React + Tailwind การตรวจค่าเป็นหน้าที่ของแอป ให้ส่ง invalid และ invalidText มา เรื่องอื่น ๆ ของ error ดูที่ ฟอร์มและการตรวจค่า
label และข้อความช่วยที่มี markup
ถ้า label หรือข้อความมี markup ให้ใช้ slot label และ helper-text (warn-text กับ invalid-text ก็ใช้แบบเดียวกัน) ช่องของ React + Tailwind รับ markup ทาง label และ helperText
อ่านอย่างเดียว ปิดใช้งาน และ fluid
readonlyแสดงข้อความและส่งค่าไปกับฟอร์ม แต่ผู้ใช้แก้ไม่ได้ ส่วนdisabledทำให้ช่องเป็นสีเทาและไม่ส่งค่าfluidหรือการครอบด้วยฟอร์มแบบ fluid ย้าย label เข้าไปในกล่อง (FluidTextAreaของ Carbon ดู ฟอร์มแบบ fluid)
แบบมีขอบ (ดีไซน์ Kanok)
ในดีไซน์ Kanok ช่องกรอกเป็นแบบ filled โดยปกติ คือพื้นเทาอ่อน ไม่มีขอบ ใส่ data-field-variant="outlined" ที่ element ที่ครอบช่อง (เช่น <form> หรือ <html> ถ้าต้องการทั้งแอป) จะเปลี่ยนเป็นขอบ 1px บนพื้นใส ส่วน focus ยังเป็นวงแสงสีฟ้าเหมือนเดิม attribute นี้ไม่มีผลในดีไซน์ Carbon ตัวอย่างด้านล่างจึงครอบด้วย data-design="kanok" ดูรายละเอียดทั้งหมดที่ ดีไซน์ Kanok
ตรวจค่าในฟอร์ม
ใน <form> required และ minlength จะไม่ให้ส่งฟอร์มจนกว่าข้อความจะยาวพอ และช่องจะแสดง error ลองส่งฟอร์มนี้ด้วยเหตุผลสั้น ๆ
ส่งค่า:
บันทึกร่างเมื่อผู้ใช้ออกจากช่อง
change ยิงครั้งเดียวเมื่อผู้ใช้แก้ข้อความแล้วออกจากช่อง จึงเป็นจังหวะที่ดีสำหรับบันทึกร่าง โดยไม่ต้องส่ง request ทุกครั้งที่พิมพ์
ร่าง:
Skeleton
ระหว่างที่ข้อมูลของฟอร์มกำลังโหลด <ui-text-area-skeleton> จองที่ของช่องไว้ เป็นแถบ label กับแถบช่องสูง 100px หรือเป็นกล่องแบบ fluid เมื่อใส่ fluid screen reader ข้ามแถบนี้ จึงต้องบอกว่า "กำลังโหลด" ที่ฟอร์มแทน (เช่น aria-busy="true")
Accessibility
- label เป็น
<label>จริงที่ผูกกับ<textarea>ข้างใน และบรรทัดข้อความใต้ช่องเป็นaria-describedbyของมันaria-labelที่ตัว element ไปไม่ถึง textarea ให้ใช้labelแทน (คู่กับhide-labelถ้าไม่อยากให้เห็น) - Enter ขึ้นบรรทัดใหม่เหมือน text area ทั่วไป ไม่ส่งฟอร์ม
- ตัวนับไม่ถูกอ่านทุกครั้งที่พิมพ์ screen reader จะอ่านครั้งเดียวเมื่อใกล้ครบ และเมื่อเกินจำนวนที่กำหนด
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Text area
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-text-area> · React UiTextArea
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 |
'' |
Error text shown while invalid or while the field fails validation. |
invalid |
invalid |
boolean |
false |
Forces the error state, e.g. after server-side validation. |
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. |
placeholder |
placeholder |
string |
'' |
|
rows |
rows |
number |
4 |
Initial number of text rows (Carbon default 4); the user can still resize. |
minlength |
minlength |
number | undefined |
||
maxlength |
maxlength |
number | undefined |
||
autocomplete |
autocomplete |
string |
'' |
Passed to the textarea (off, or an autofill token). |
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…). |
max-count |
maxCount |
number | undefined |
Soft limit with a visible counter (unlike maxlength, extra text isn't blocked). | |
counter-mode |
counterMode |
CounterMode |
'character' |
What max-count counts: character (default) or word (Carbon counterMode). |
auto-grow |
autoGrow |
boolean |
false |
Grows with the text from rows (no Carbon counterpart); max-rows caps it, then it scrolls. |
max-rows |
maxRows |
number | undefined |
The most lines auto-grow shows before scrolling (default: no cap). |
|
too-long-text |
tooLongText |
string |
'' |
Error when over max-count; {max} is replaced with the limit (default: the translation's, for characters or words). |
near-limit-text |
nearLimitText |
string |
'' |
Announced once when what is left of the limit drops 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 FluidTextArea: 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. |
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 (and is shown as its error once touched) until cleared with ''. |
Event
| Event | React | คำอธิบาย |
|---|---|---|
change |
onChange |
Re-dispatched from the native textarea (native change is not composed) |
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 textarea |
label |
The label |
input |
The native textarea |
counter |
The character (or word) 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) |