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 Number input

NumberInput คือพฤติกรรมของ Number input แบบมี style ในรูป React part ที่ไม่มี style: มี <input> กับปุ่ม − และ + และกด ↑ / ↓ ในช่องเพื่อเพิ่มลดได้ ค่าที่เพิ่มลดจะอยู่ในช่วง min ถึง max และถูกปัดเศษ ปุ่มแต่ละปุ่มจะกดไม่ได้เมื่อถึงปลายช่วงของมัน เมื่อ type="text" ช่องจะแสดงตัวเลขตามรูปแบบของ locale แต่ค่ายังเป็นตัวเลขธรรมดา ช่องแบบ uncontrolled จะกลับไปที่ defaultValue เมื่อฟอร์ม reset

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

ติดตั้ง

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

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

วิธีใช้

import { useState } from 'react'; import { NumberInput } from '@purpurabog/ui/headless'; export default function Example() { const [units, setUnits] = useState('1'); return ( <div className="grid max-w-xs gap-2"> <NumberInput.Root name="units" min={1} max={20} value={units} onValueChange={setUnits}> <label htmlFor="units" className="text-xs text-ink-muted">จำนวนห้อง</label> <div className="flex border-b border-border-strong-1 bg-field-1"> <NumberInput.Input id="units" className="h-10 min-w-0 flex-1 bg-transparent px-4 text-sm text-ink [appearance:textfield] focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus [&::-webkit-inner-spin-button]:appearance-none" /> <NumberInput.Decrement aria-label="ลดค่า" className="size-10 text-ink hover:bg-field-hover-1 data-disabled:cursor-not-allowed data-disabled:text-ink-disabled">−</NumberInput.Decrement> <NumberInput.Increment aria-label="เพิ่มค่า" className="size-10 text-ink hover:bg-field-hover-1 data-disabled:cursor-not-allowed data-disabled:text-ink-disabled">+</NumberInput.Increment> </div> </NumberInput.Root> <p className="text-xs text-ink-muted">ค่า: {JSON.stringify(units)}</p> </div> ); }
  • ค่าเป็นข้อความ ช่องจึงว่างได้: "12", "0.5" หรือ ""
  • onValueChange ได้ค่าใหม่เป็นข้อความ ไม่ใช่ event ถูกเรียกหลังพิมพ์ หลังกดเพิ่มลด หรือหลังออกจากช่องแล้วค่าถูกปัดเศษ
  • name, min, max, step, disabled และ readOnly ใส่ที่ Root ไม่ใช่ที่ Input

โครงสร้าง

<NumberInput.Root> <NumberInput.Input /> <NumberInput.Decrement /> <NumberInput.Increment /> <NumberInput.HiddenInput /> </NumberInput.Root>

ระยะเพิ่มลด ค่าต่ำสุด และค่าสูงสุด

step (ค่าเริ่มต้น 1) คือระยะที่ − / + และ ↑ / ↓ เพิ่มหรือลด ค่าจะอยู่ในช่วง min ถึง max และปุ่มที่ปลายช่วงจะกดไม่ได้

ถ้าช่องว่างอยู่ การกดครั้งแรกจะไปที่ stepStartValue ถ้าตั้งไว้ ถ้าไม่ได้ตั้งจะไปที่ min เมื่อกำหนดแค่ค่าต่ำสุด นอกนั้นไปที่ 1 หรือ −1 เหมือน Carbon

import { useState } from 'react'; import { NumberInput } from '@purpurabog/ui/headless'; export default function Example() { const [floor, setFloor] = useState(''); return ( <div className="grid max-w-xs gap-2"> <NumberInput.Root name="floor" min={1} stepStartValue={10} onValueChange={setFloor}> <label htmlFor="floor" className="text-xs text-ink-muted">ชั้น</label> <div className="flex border-b border-border-strong-1 bg-field-1"> <NumberInput.Input id="floor" className="h-10 min-w-0 flex-1 bg-transparent px-4 text-sm text-ink [appearance:textfield] focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus [&::-webkit-inner-spin-button]:appearance-none" /> <NumberInput.Decrement aria-label="ลดค่า" className="size-10 text-ink hover:bg-field-hover-1 data-disabled:cursor-not-allowed data-disabled:text-ink-disabled">−</NumberInput.Decrement> <NumberInput.Increment aria-label="เพิ่มค่า" className="size-10 text-ink hover:bg-field-hover-1 data-disabled:cursor-not-allowed data-disabled:text-ink-disabled">+</NumberInput.Increment> </div> </NumberInput.Root> <p className="text-xs text-ink-muted">ค่า: {JSON.stringify(floor)}</p> </div> ); }

ทศนิยม

การเพิ่มลดจะปัดเศษตามจำนวนทศนิยมของค่าหรือของ step แล้วแต่อันไหนมากกว่า 0.1 + 0.2 จึงได้ 0.3 เมื่อเป็น type="number" (ค่าเริ่มต้น) step ยังเป็นความละเอียดที่เบราว์เซอร์ยอมรับด้วย

import { useState } from 'react'; import { NumberInput } from '@purpurabog/ui/headless'; export default function Example() { const [rate, setRate] = useState('2.75'); return ( <div className="grid max-w-xs gap-2"> <NumberInput.Root name="rate" defaultValue="2.75" step={0.01} min={0} onValueChange={setRate}> <label htmlFor="rate" className="text-xs text-ink-muted">อัตราดอกเบี้ย (% ต่อปี)</label> <NumberInput.Input id="rate" aria-describedby="rate-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="rate-help" className="text-xs text-ink-helper">↑ / ↓ เพิ่มลดทีละ 0.01</p> </NumberInput.Root> <p className="text-xs text-ink-muted">ค่า: {JSON.stringify(rate)}</p> </div> ); }

สกุลเงิน หน่วย และรูปแบบตัวเลข

type="text" คู่กับ formatOptions จัดรูปแบบตัวเลขด้วย Intl.NumberFormat เช่น ตัวคั่นหลักพัน สกุลเงิน เปอร์เซ็นต์ หน่วย ระหว่างพิมพ์ผู้ใช้พิมพ์อะไรก็ได้ แล้วช่องจะจัดรูปแบบให้เมื่อออกจากช่อง ข้อความที่ไม่ใช่ตัวเลขยังอยู่ในช่องและถูกทำเครื่องหมายว่าไม่ถูกต้อง ให้ผู้ใช้แก้ ค่ายังเป็นตัวเลขธรรมดา: ฿1,234.50 คือ 1234.5 และรูปแบบเปอร์เซ็นต์ให้ค่าเป็นเศษส่วน (0.5 สำหรับ 50%)

import { useState } from 'react'; import { NumberInput } from '@purpurabog/ui/headless'; export default function Example() { const [sent, setSent] = useState<Record<string, FormDataEntryValue>>(); return ( <form className="grid max-w-xs gap-2" onSubmit={(e) => { e.preventDefault(); setSent(Object.fromEntries(new FormData(e.currentTarget))); }} > {/* type="text": HiddenInput ส่งค่าตัวเลขธรรมดา ถ้าไม่ใส่ locale จะใช้ <html lang> */} <NumberInput.Root name="rent" type="text" locale="th-TH" defaultValue={18500} formatOptions={{ style: 'currency', currency: 'THB' }}> <label htmlFor="rent" className="text-xs text-ink-muted">ค่าเช่าต่อเดือน</label> <NumberInput.Input id="rent" 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" /> <NumberInput.HiddenInput /> </NumberInput.Root> <button type="submit" className="h-10 justify-self-start bg-interactive px-4 text-sm text-on-primary hover:bg-blue-hover focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus" > ส่ง </button> <p className="text-xs text-ink-muted">ส่งแล้ว: {sent ? JSON.stringify(sent) : 'ยังไม่มี'}</p> </form> ); }
  • ถ้าไม่ใส่ locale ช่อง type="text" จะใช้ภาษาของหน้า (<html lang> หรือภาษาของ browser)
  • locale หรือ formatOptions ที่ไม่ถูกต้องจะกลับไปใช้รูปแบบภาษาอังกฤษธรรมดา และเตือนใน console
  • prefix หรือ suffix (฿, กก.) เป็น markup ของคุณเองข้าง input

ช่องว่างและ error

ไม่มีกฎเรื่องช่องว่างในตัว ให้ตรวจค่าเอง การตรวจของเบราว์เซอร์ที่ input (required และ min, max, step เมื่อเป็น type="number") ยังทำงานตอนส่งฟอร์ม แต่จะไม่มีกล่องข้อความ error ของเบราว์เซอร์ขึ้นมา ช่องจะได้ data-touched แทน และช่องแรกของฟอร์มที่ค่าไม่ถูกต้องจะได้โฟกัส คุณจึงแสดงข้อความของคุณเองได้

ปุ่มเพิ่มลดและล้อเมาส์

ถ้าไม่ต้องการปุ่ม ก็ไม่ต้องใส่ Decrement และ Increment ↑ / ↓ ยังใช้ได้ disableWheel ที่ Input ไม่ให้ล้อเมาส์เปลี่ยนค่าในช่องที่โฟกัสอยู่

import { useState } from 'react'; import { NumberInput } from '@purpurabog/ui/headless'; export default function Example() { const [year, setYear] = useState('2019'); return ( <div className="grid max-w-xs gap-2"> {/* ไม่ต้องใส่ Decrement / Increment และใส่ disableWheel ที่ Input */} <NumberInput.Root name="year" defaultValue={2019} min={1900} max={2026} onValueChange={setYear}> <label htmlFor="year" className="text-xs text-ink-muted">ปีที่สร้าง</label> <NumberInput.Input id="year" disableWheel 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" /> </NumberInput.Root> <p className="text-xs text-ink-muted">ค่า: {JSON.stringify(year)}</p> </div> ); }

แต่ง style

import { NumberInput } from '@purpurabog/ui/headless'; export default function Example() { return ( <form className="grid max-w-xs gap-2" onSubmit={(e) => e.preventDefault()}> <NumberInput.Root min={0} max={10}> <label htmlFor="qty" className="text-xs text-ink-muted">จำนวน</label> <div className="flex border-b border-border-strong-1 bg-field-1"> <NumberInput.Input id="qty" required className="h-10 min-w-0 flex-1 bg-transparent px-4 text-sm text-ink [appearance:textfield] focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-touched:invalid:outline-2 data-touched:invalid:-outline-offset-2 data-touched:invalid:outline-error [&::-webkit-inner-spin-button]:appearance-none" /> <NumberInput.Decrement aria-label="ลดค่า" className="w-10 text-ink hover:bg-field-hover-1 data-disabled:cursor-not-allowed data-disabled:text-ink-disabled">−</NumberInput.Decrement> <NumberInput.Increment aria-label="เพิ่มค่า" className="w-10 text-ink hover:bg-field-hover-1 data-disabled:cursor-not-allowed data-disabled:text-ink-disabled">+</NumberInput.Increment> </div> </NumberInput.Root> <button type="submit" className="h-10 justify-self-start bg-interactive px-4 text-sm text-on-primary hover:bg-blue-hover focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus" > ส่ง </button> </form> ); } input[data-touched]:invalid { outline: 2px solid #da1e28; } button[data-disabled] { color: #a8a8a8; cursor: not-allowed; }

Parts

NumberInput.Root

ไม่ render element ใด ๆ เก็บค่าและขอบเขตไว้ให้ part อื่น

Prop Type ค่าเริ่มต้น
value string | number — ค่าเป็นข้อความตัวเลขธรรมดา ('' คือว่าง) แบบ controlled
defaultValue string | number — ค่าเริ่มต้น (uncontrolled)
onValueChange (value: string) => void — ค่าใหม่หลังพิมพ์ หลังเพิ่มลด หรือหลังออกจากช่องแล้วค่าถูกปัดเศษ
min number — ค่าต่ำสุดที่การเพิ่มลดไปถึง
max number — ค่าสูงสุดที่การเพิ่มลดไปถึง
step number 1 ระยะของปุ่มและ ↑ / ↓ เมื่อ type="number" ยังเป็นความละเอียดของค่าด้วย
stepStartValue number 0 จุดเริ่มเมื่อกดเพิ่มลดจากช่องว่าง 0 คือ ±1 หรือ min เมื่อกำหนดแค่ค่าต่ำสุด
type 'number' | 'text' 'number' text จัดรูปแบบตัวเลขตาม locale
locale string <html lang> หรือภาษาของ browser locale (BCP 47) ของ type="text"
formatOptions Intl.NumberFormatOptions — รูปแบบของ type="text"
disabled boolean false ปิดทั้งช่องและปุ่ม
readOnly boolean false คงค่าไว้ ปุ่มและ ↑ / ↓ ไม่ทำอะไร
name string — ชื่อที่ส่งไปกับฟอร์ม: อยู่ที่ Input เมื่อ type="number" และที่ HiddenInput เมื่อ type="text"

NumberInput.Input

render <input> ที่มี inputMode="decimal" เป็นช่องตัวเลขของเบราว์เซอร์เมื่อ type="number" และเป็นช่องข้อความเมื่อ type="text" รับ prop ของ <input> ได้ทุกตัว ยกเว้น type, value, defaultValue, min, max, step และ name ซึ่งมาจาก Root onBlur ของคุณถูกเรียกหลังค่าลงตัวแล้ว และ ↑ / ↓ จะไม่เพิ่มลดถ้า onKeyDown ของคุณเรียก preventDefault()

Prop Type ค่าเริ่มต้น
disableWheel boolean false ไม่สนใจล้อเมาส์ระหว่างที่โฟกัสอยู่
Attribute เมื่อ
data-disabled Root ถูกปิดใช้งาน
data-readonly Root เป็นแบบอ่านอย่างเดียว
data-touched ผู้ใช้ออกจากช่องหรือกดเพิ่มลดแล้ว หรือการส่งฟอร์มพบว่าค่าไม่ถูกต้อง

NumberInput.Decrement และ NumberInput.Increment

แต่ละตัว render <button type="button"> ที่ลดหรือเพิ่มค่าหนึ่งระยะ ไม่มี prop เพิ่ม ปุ่มจะกดไม่ได้เมื่อถึงปลายช่วงของมัน เมื่อช่องถูกปิดใช้งานหรืออ่านอย่างเดียว และเมื่อคุณส่ง disabled ปุ่มอยู่นอกลำดับ Tab เว้นแต่คุณส่ง tabIndex

Attribute เมื่อ
data-disabled ปุ่มกดไม่ได้

NumberInput.HiddenInput

render <input type="hidden"> ที่มี name ของ Root และค่าตัวเลขธรรมดา เฉพาะเมื่อ type="text" และมี name นอกนั้นไม่ render อะไร เมื่อ Root ถูกปิดใช้งาน ช่องนี้ก็ถูกปิด (ไม่ส่งค่า)

NumberInput.useNumberInputContext()

คืนสถานะของ Root ที่ครอบอยู่ เช่น value, number (NaN เมื่อช่องว่าง), isEmpty, badText (ข้อความของ type="text" ที่ไม่ใช่ตัวเลข), touched, atMin, atMax และ stepBy(1 | -1)

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

ปุ่ม ผล
↑ / ↓ เพิ่มหรือลดหนึ่งระยะ ภายใน min / max
Enter ส่งฟอร์มที่ช่องนี้อยู่ เหมือน input ปกติ
  • ปุ่ม − / + อยู่นอกลำดับ Tab เหมือน Carbon เพราะปุ่มลูกศรในช่องทำแบบเดียวกันได้ ปุ่มเป็นปุ่มจริงที่มี disabled เมื่อถึงปลายช่วง
  • สิ่งที่คุณต้องใส่เอง: <label>, ชื่อของปุ่ม (aria-label), aria-describedby ที่ชี้ไปยัง helper text หรือข้อความ error และ aria-invalid คู่กับข้อความ error ของคุณ