Number input
ช่องกรอกตัวเลข เช่น จำนวน ยอดเงิน หรือเปอร์เซ็นต์ มีปุ่ม − และ + แบบ Carbon อยู่ท้ายช่อง และกด ↑ / ↓ เพื่อเพิ่มลดค่าจากคีย์บอร์ดได้
ติดตั้ง
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
- ค่าเป็นข้อความ ช่องจึงว่างได้:
"12","0.5"หรือ""ส่วนvalueAsNumberของ element ให้ค่าเป็นตัวเลข (NaNเมื่อช่องว่าง) - element ยิง
inputทุกครั้งที่พิมพ์หรือกดเพิ่มลด และchangeเมื่อค่าถูกยืนยัน (ออกจากช่อง กด Enter หรือกดเพิ่มลด) wrapper ของ React จึงมีonInputกับonChange onChangeของ React + Tailwind ได้ค่าใหม่เป็นข้อความ ไม่ใช่ event
ระยะเพิ่มลด ค่าต่ำสุด และค่าสูงสุด
step (ค่าเริ่มต้น 1) คือระยะที่ − / + และ ↑ / ↓ เพิ่มหรือลด ค่าจะอยู่ในช่วง min ถึง max เสมอ และปุ่มที่ปลายช่วงจะกดไม่ได้ ถ้าพิมพ์ค่านอกช่วงจะเป็น error
ถ้าช่องว่าง การกดครั้งแรกจะไปที่ step-start-value ถ้าตั้งไว้ ถ้าไม่ได้ตั้งและมีแค่ min จะไปที่ min นอกนั้นไปที่ 1 หรือ −1 แบบ Carbon
ทศนิยม
การเพิ่มลดจะปัดตามจำนวนตำแหน่งทศนิยมของค่าหรือของ step แล้วแต่อันไหนมากกว่า 0.1 + 0.2 จึงได้ 0.3 ไม่ใช่ 0.30000000000000004 ใน type="number" (ค่าเริ่มต้น) step ยังเป็นความละเอียดที่เบราว์เซอร์ยอมรับด้วย ถ้า step="0.01" ค่า 12.345 จะเป็น error
สกุลเงิน หน่วย และรูปแบบตัวเลข
prefix-text และ suffix-text ใส่ข้อความไว้ในช่อง หน้าหรือหลังตัวเลข เช่น ฿, กก., บาท/เดือน ข้อความนี้อยู่ให้เห็นตลอดระหว่างพิมพ์และไม่เป็นส่วนของค่า Carbon ไม่มีสิ่งนี้
ถ้าต้องการช่องจัดรูปแบบแบบ Carbon ให้ตั้ง type="text" คู่กับ format-options ซึ่งรับ option ของ Intl.NumberFormat ได้ทั้งหมด เช่น ตัวคั่นหลักพัน สกุลเงิน เปอร์เซ็นต์ หน่วย ระหว่างพิมพ์ผู้ใช้พิมพ์อะไรก็ได้ แล้วช่องจะจัดรูปแบบให้เมื่อออกจากช่อง ค่ายังคงเป็นตัวเลขธรรมดา: ฿1,234.50 ส่งค่า 1234.5 และรูปแบบเปอร์เซ็นต์ให้ค่าเป็นเศษส่วน (0.5 สำหรับ 50%) locale ใช้ภาษาของหน้า หรือกำหนดด้วย locale
เปอร์เซ็นต์และหน่วย
เมื่อใช้ format-options เครื่องหมายเปอร์เซ็นต์หรือหน่วยจะเป็นส่วนหนึ่งของข้อความที่จัดรูปแบบ ซึ่งเป็นวิธีของ Carbon รูปแบบเปอร์เซ็นต์เก็บค่าเป็นเศษส่วน: 7.5% คือ 0.075 จึงต้องใส่ step, min และ max เป็นเศษส่วนด้วย ใช้ locale เมื่อต้องการรูปแบบตัวเลขที่ไม่ตามภาษาของหน้า
ช่องว่าง
แบบ Carbon ช่องตัวเลขที่ว่างถือว่ายังไม่ได้กรอก แม้ไม่ได้ตั้ง required ฟอร์มจะส่งไม่ได้จนกว่าจะมีตัวเลข ช่องที่ปล่อยว่างได้ให้ตั้ง allow-empty
ปุ่มเพิ่มลดและล้อเมาส์
hide-steppersเอาปุ่ม − / + ออก แต่ ↑ / ↓ ยังใช้ได้disable-wheelไม่ให้ล้อเมาส์เปลี่ยนค่าในช่องที่โฟกัสอยู่ ซึ่งเกิดขึ้นง่ายเวลาเลื่อนดูฟอร์มยาว ๆdecrement-labelและincrement-labelเปลี่ยนชื่อปุ่มที่ screen reader อ่าน (ค่าเริ่มต้นคือ "ลดค่า" และ "เพิ่มค่า" ตามภาษาของหน้า)
ขนาด ข้อความ อ่านอย่างเดียว และปิดใช้งาน
size:sm32px,md40px (ค่าเริ่มต้น),lg48pxhelper-text,warn+warn-textและinvalid+invalid-textทำงานเหมือน text input ค่าที่อยู่นอกmin/maxจะได้ข้อความของเบราว์เซอร์หรือข้อความในตัว เว้นแต่ตั้งinvalid-textไว้ ดูเพิ่มที่ ฟอร์มและการตรวจค่าreadonlyแสดงและส่งค่าไปกับฟอร์ม และปุ่มเพิ่มลดจะกดไม่ได้ ส่วนdisabledทำให้ช่องเป็นสีเทาและไม่ส่งค่า
แบบ fluid
fluid (FluidNumberInput ของ Carbon) หรือการครอบด้วย <ui-fluid-form> ย้าย label เข้าไปในกล่องสูง 64px error หรือคำเตือนแสดงในกล่อง และไม่มี helper text ดู ฟอร์มแบบ fluid
แบบมีขอบ (ดีไซน์ Kanok)
ในดีไซน์ Kanok ช่องกรอกเป็นแบบ filled โดยปกติ คือพื้นเทาอ่อน ไม่มีขอบ ใส่ data-field-variant="outlined" ที่ element ที่ครอบช่อง (เช่น <form> หรือ <html> ถ้าต้องการทั้งแอป) จะเปลี่ยนเป็นขอบ 1px บนพื้นใส ส่วน focus ยังเป็นวงแสงสีฟ้าเหมือนเดิม attribute นี้ไม่มีผลในดีไซน์ Carbon ตัวอย่างด้านล่างจึงครอบด้วย data-design="kanok" ดูรายละเอียดทั้งหมดที่ ดีไซน์ Kanok
คำนวณยอดรวม
ฟัง input เพื่ออัปเดตยอดรวมทุกครั้งที่พิมพ์หรือกดเพิ่มลด valueAsNumber ให้ค่าเป็นตัวเลข หรือ NaN เมื่อช่องว่าง
ยอดรวม:
ใช้ในฟอร์ม
เมื่อใส่ name ช่องจะส่งค่าเป็นตัวเลขธรรมดา: ค่าเช่าที่แสดงเป็น ฿18,500.00 ด้านล่างส่งค่า 18500 ค่าที่อยู่นอก min / max หรือช่องว่างที่ไม่ได้ตั้ง allow-empty จะทำให้ส่งฟอร์มไม่ได้และแสดง error ลองใส่ 72 เดือน
ส่งค่า:
Skeleton
ระหว่างที่ข้อมูลของฟอร์มกำลังโหลด <ui-number-input-skeleton> จองที่ของช่องไว้ เป็นแถบ label กับแถบช่องสูงตาม size หรือเป็นกล่องแบบ fluid เมื่อใส่ fluid screen reader ข้ามแถบนี้ จึงต้องบอกว่า "กำลังโหลด" ที่ฟอร์มแทน (เช่น aria-busy="true")
คีย์บอร์ด
| ปุ่ม | ผล |
|---|---|
| ↑ / ↓ | เพิ่มหรือลดหนึ่ง step ภายในช่วง min / max |
| Enter | ยืนยันค่าและส่งฟอร์มที่ช่องนี้อยู่ |
ปุ่ม − / + ไม่อยู่ในลำดับ Tab แบบ Carbon เพราะลูกศรในช่องทำงานเดียวกันได้
Accessibility
- label เป็น
<label>จริงที่ผูกกับ<input>ข้างใน ส่วน prefix, suffix และบรรทัดข้อความใต้ช่องเป็นคำอธิบายของมัน - ปุ่ม − / + มีชื่อ ("ลดค่า", "เพิ่มค่า") และกดไม่ได้เมื่อถึงปลายช่วง
inputmode="decimal"เปิดคีย์บอร์ดที่มีจุดทศนิยมบนมือถือ
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Number input
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-number-input> · React UiNumberInput
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 |
'' |
The field text; valueAsNumber gives the parsed number (NaN while empty or invalid). |
name |
name |
string |
'' |
Reflected: form-associated elements submit under the name attribute. |
placeholder |
placeholder |
string |
'' |
|
prefix-text |
prefixText |
string |
'' |
Text inside the field before the value, e.g. a currency (฿); not part of value. 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); not part of value. Part of the field's description for screen readers. |
min |
min |
number | undefined |
||
max |
max |
number | undefined |
||
step |
step |
number |
1 |
Step of the ± buttons and ↑ / ↓; with type="number" also the allowed precision. |
step-start-value |
stepStartValue |
number |
0 |
Where the steppers start from an empty field (Carbon stepStartValue); 0 = Carbon's rule: ±1, or min when only a minimum is set. |
type |
type |
NumberInputType |
'number' |
number: a native number field. text: a text field formatted for locale with format-options (Carbon type="text"). |
locale |
locale |
string |
'' |
BCP 47 locale of the formatting with type="text"; empty = the page language (lang on the element, an ancestor or <html>), as ui-date-picker. |
format-options |
formatOptions |
Intl.NumberFormatOptions |
{} |
Intl.NumberFormat options with type="text", e.g. {"style":"currency","currency":"THB"} or {"maximumFractionDigits":2}. |
allow-empty |
allowEmpty |
boolean |
false |
An empty field is valid (Carbon allowEmpty); otherwise empty counts as missing. |
hide-steppers |
hideSteppers |
boolean |
false |
Remove the − / + buttons (keyboard stepping still works). |
disable-wheel |
disableWheel |
boolean |
false |
Ignore mouse-wheel changes while focused (Carbon disableWheel). |
size |
size |
NumberInputSize |
'md' |
Field height: sm 32px, md 40px, lg 48px (tokens --ui-field-height-*). |
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 FluidNumberInput: 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. |
decrement-label |
decrementLabel |
string |
'' |
Accessible name of the − button. |
increment-label |
incrementLabel |
string |
'' |
Accessible name of the + button. |
Property
| Property | ชนิด | คำอธิบาย |
|---|---|---|
valueAsNumber |
(อ่านอย่างเดียว) The parsed value, NaN while empty or not a number. | |
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 |
After a committed change (blur / Enter / stepper; with type="text", on blur when the number changed) |
input |
onInput |
On every keystroke or stepper click |
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 |
label |
The label |
input |
The native input |
prefix |
The text before the value |
suffix |
The text after the value |
controls |
The stepper group |
decrement-button |
The − button |
increment-button |
The + button |
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) |