Text input
ช่องกรอกข้อความบรรทัดเดียว เช่น ชื่อ อีเมล รหัสผ่าน หรือเลขประจำตัว ถ้าต้องกรอกหลายบรรทัดให้ใช้ text area ถ้าเป็นจำนวนหรือยอดเงินให้ใช้ number input
ติดตั้ง
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
- element ยิง event
inputแบบ native ทุกครั้งที่พิมพ์ และchangeเมื่อผู้ใช้ยืนยันค่า wrapper ของ React จึงมีonInputกับonChange onChangeของ React + Tailwind เป็นของ React เอง (ยิงทุกครั้งที่พิมพ์) argument ตัวที่สองคือค่าที่ไม่มีตัวคั่นของ mask
ขนาด
size: sm 32px, md 40px (ค่าเริ่มต้น), lg 48px
รหัสผ่าน
type="password" เพิ่มปุ่มแสดง / ซ่อนรหัสผ่านแบบ Carbon ที่ท้ายช่อง tooltip ของปุ่มเป็นชื่อปุ่มด้วย และบอกว่ากดแล้วจะเกิดอะไร: "แสดงรหัสผ่าน" แล้วเปลี่ยนเป็น "ซ่อนรหัสผ่าน" เปลี่ยนข้อความได้ด้วย show-password-label และ hide-password-label ทุกครั้งที่กดจะยิง ui-toggle พร้อม detail.visible
แสดงรหัสผ่านอยู่:
อีเมล เบอร์โทร และเว็บไซต์
type รับ email, tel, url, search และ number ได้ด้วย เหมือน <input> ถ้าต้องการแค่เปลี่ยนคีย์บอร์ดบนมือถือ ให้ตั้ง inputmode (และ enterkeyhint สำหรับปุ่ม Enter ของคีย์บอร์ด)
ใส่ autocomplete ด้วย เบราว์เซอร์จะกรอกให้ได้ เบราว์เซอร์ตรวจรูปแบบอีเมลให้ ส่วน mask ช่วยให้อ่านเบอร์โทรได้ง่าย
สกุลเงิน หน่วย และ mask
prefix-text และ suffix-text ใส่ข้อความไว้ในช่อง หน้าหรือหลังค่า เช่น สกุลเงินหรือหน่วย ข้อความนี้ไม่เป็นส่วนของค่า และ screen reader อ่านเป็นส่วนหนึ่งของคำอธิบายช่อง
mask จัดรูปแบบค่าระหว่างพิมพ์: 0 คือตัวเลข a คือตัวอักษร * คืออย่างใดอย่างหนึ่ง ส่วนตัวอักษรอื่นเป็นตัวคั่นที่ช่องใส่ให้เอง mask ที่มีแต่ตัวเลขจะเปิดคีย์บอร์ดตัวเลขบนมือถือ ควรเขียนรูปแบบไว้ใน helper text ด้วย ผู้ใช้จะได้รู้ก่อนพิมพ์
valueของ element และค่าที่ส่งไปกับฟอร์มเป็นข้อความที่จัดรูปแบบแล้ว (1-2345-67890-12-3) ส่วน propertyrawValueคือสิ่งที่พิมพ์โดยไม่มีตัวคั่น (1234567890123) ใน React + Tailwind ค่านี้มาเป็น argument ตัวที่สองของonChange- ค่าที่โค้ดของคุณตั้งให้ จะมีหรือไม่มีตัวคั่นก็ได้ ช่องจะแสดงแบบจัดรูปแบบแล้วเสมอ
- ทั้งสามอย่างนี้ไม่มีใน Carbon (Carbon ให้เขียนหน่วยหรือรูปแบบไว้ใน label หรือ helper text) เราทำตาม USWDS
อ่านค่า
ฟัง input เพื่อทำงานทุกครั้งที่พิมพ์ หรือฟัง change เมื่อผู้ใช้กรอกเสร็จ ถ้ามี mask value คือข้อความที่ช่องแสดง ส่วน rawValue คือสิ่งที่ผู้ใช้พิมพ์
value:
rawValue:
ตัวนับตัวอักษร
max-count แสดงตัวนับไว้เหนือช่อง เป็นขีดจำกัดแบบไม่บังคับ ผู้ใช้วางข้อความยาว ๆ แล้วค่อยตัดทีหลังได้ ถ้าเกิน ตัวนับจะเป็นสีแดง error ขึ้นทันที และฟอร์มส่งไม่ได้ screen reader จะอ่านเตือนเมื่อใกล้ครบ ไม่ได้อ่านทุกครั้งที่พิมพ์
เปลี่ยนข้อความได้ด้วย too-long-text ({max} จะถูกแทนด้วยจำนวนที่กำหนด) และ near-limit-text ถ้าต้องการขีดจำกัดที่พิมพ์เกินไม่ได้เลย ให้ใช้ maxlength รายละเอียดเพิ่มเติมที่ ฟอร์มและการตรวจค่า
ข้อความช่วย คำเตือน และ error
ใต้ช่องแสดงได้ทีละบรรทัด: error มาก่อนคำเตือน และคำเตือนมาก่อน helper text
- ใน 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
ตำแหน่ง label, อ่านอย่างเดียว และปิดใช้งาน
hide-labelซ่อน label จากสายตาแต่ screen reader ยังอ่านได้ ใช้เฉพาะเมื่อมีอย่างอื่นบนจอที่บอกอยู่แล้วว่าช่องนี้คืออะไรinlineวาง label ไว้หน้าช่องและข้อความไว้หลังช่องในแถวเดียว (inline text input ของ Carbon)fluidหรือการครอบด้วยฟอร์มแบบ fluid ย้าย label เข้าไปในกล่องสูง 64px (ฟอร์มแบบ fluid)readonlyแสดงค่าและส่งค่าไปกับฟอร์ม แต่ผู้ใช้แก้ไม่ได้ ส่วนdisabledทำให้ช่องเป็นสีเทาและไม่ส่งค่า
ช่องแบบ fluid
ช่องแบบ fluid วาง label ไว้ในกล่องสูง 64px และแสดง error หรือคำเตือนในกล่องใต้เส้นคั่น ช่องแบบนี้ไม่มี helper text ตั้ง fluid ที่ช่อง หรือครอบช่องทั้งหมดด้วย <ui-fluid-form> (ดู ฟอร์มแบบ fluid)
แบบมีขอบ (ดีไซน์ Kanok)
ในดีไซน์ Kanok ช่องกรอกเป็นแบบ filled โดยปกติ คือพื้นเทาอ่อน ไม่มีขอบ ใส่ data-field-variant="outlined" ที่ element ที่ครอบช่อง (เช่น <form> หรือ <html> ถ้าต้องการทั้งแอป) จะเปลี่ยนเป็นขอบ 1px บนพื้นใส ส่วน focus ยังเป็นวงแสงสีฟ้าเหมือนเดิม attribute นี้ไม่มีผลในดีไซน์ Carbon ตัวอย่างด้านล่างจึงครอบด้วย data-design="kanok" ดูรายละเอียดทั้งหมดที่ ดีไซน์ Kanok
ตรวจค่าในฟอร์ม
ใน <form> element ตรวจ required, type, pattern, minlength และตัวนับให้เหมือน input ปกติ ถ้ามีช่องที่ไม่ผ่าน ฟอร์มจะส่งไม่ได้ ทุกช่องที่ไม่ผ่านจะแสดง error และช่องแรกจะได้โฟกัส ลองกดส่งฟอร์มนี้โดยไม่กรอกอะไร
ส่งค่า:
error จากการตรวจของคุณเอง
setCustomValidity(message) ทำให้ช่องไม่ผ่านพร้อมข้อความของคุณ เหมือน <input> ใช้กับการตรวจที่เบราว์เซอร์ทำเองไม่ได้ เช่น เลขที่สัญญาที่มีคนใช้แล้ว error จะขึ้นเมื่อผู้ใช้ออกจากช่อง และ setCustomValidity('') ล้าง error ดูเพิ่มที่ ฟอร์มและการตรวจค่า
Skeleton
ระหว่างที่ข้อมูลของฟอร์มกำลังโหลด <ui-text-input-skeleton> จองที่ของช่องไว้ เป็นแถบ label กับแถบช่องสูงตาม size หรือเป็นกล่องแบบ fluid เมื่อใส่ fluid screen reader ข้ามแถบนี้ จึงต้องบอกว่า "กำลังโหลด" ที่ฟอร์มแทน (เช่น aria-busy="true")
คีย์บอร์ด
| ปุ่ม | ผล |
|---|---|
| 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) |