ปุ่ม
Button
A button, or a link that looks like one with href. Label it with what will happen, such as Approve request. Use one primary button per step. busy shows a spinner and blocks a second press while the action runs.
ใช้สั่งงาน เช่น อนุมัติ บันทึก หรือส่ง ข้อความบนปุ่มบอกว่าจะเกิดอะไรขึ้น เช่น อนุมัติคำขอ ไม่ใช่ ตกลง ใช้ปุ่มหลัก (primary) ได้ปุ่มเดียวต่อหนึ่งขั้นตอน ปุ่มอื่นใช้ secondary ถ้าไม่มีที่สำหรับข้อความ ใช้ IconButton
import { Button } from "@bsru/ui";ตัวอย่าง
ระดับของปุ่ม
primary สำหรับงานหลักของขั้นตอน secondary สำหรับงานอื่น danger สำหรับงานที่ลบ และ quiet สำหรับงานรอง เช่น ยกเลิก
<Button tone="primary" label="อนุมัติคำขอ" />
<Button tone="secondary" label="บันทึกร่าง" />
<Button tone="danger" label="ลบคำขอ" />
<Button tone="quiet" label="ยกเลิก" />| Prop | ค่า 1 | ค่า 2 | ค่า 3 | ค่า 4 | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|---|---|---|
| tone | "primary" | "secondary" | "danger" | "quiet" | ButtonTone ค่าที่ใช้ได้: "primary""secondary""danger""quiet" | — |
| label* จำเป็น | "อนุมัติคำขอ" | "บันทึกร่าง" | "ลบคำขอ" | "ยกเลิก" | string | The button text, already in the screen language. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — | |||
พร้อมไอคอน
icon วางไอคอนหน้าข้อความ iconAfter วางไว้หลังข้อความ เช่น ลูกศรของปุ่มไปขั้นถัดไป ไอคอนประกอบข้อความ ไม่ใช่แทนข้อความ
<Button label="สร้างคำขอ" tone="primary" icon={Plus} />
<Button label="ขั้นถัดไป" iconAfter={ArrowRight} />| Prop | ค่า 1 | ค่า 2 | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|---|
| label* จำเป็น | "สร้างคำขอ" | "ขั้นถัดไป" | string | The button text, already in the screen language. |
| tone | "primary" | — | ButtonTone ค่าที่ใช้ได้: "primary""secondary""danger""quiet" | — |
| icon | Plus | — | LucideIcon | An icon before the label. |
| iconAfter | — | ArrowRight | LucideIcon | An icon after the label, such as an arrow for a step forward. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — | |
ขนาดเล็ก
size sm สำหรับแถบที่แน่น เช่น ปุ่มคำสั่งใน BulkBar ขนาดปกติสูง 44 พิกเซล เหมาะกับการแตะ
<Button label="ส่งออก" icon={Download} size="sm" />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "ส่งออก" | string | The button text, already in the screen language. |
| icon | Download | LucideIcon | An icon before the label. |
| size | "sm" | "md" | "sm" ค่าที่ใช้ได้: "md""sm" | sm for a compact bar, such as actions on chosen rows. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
ระหว่างทำงาน
ตั้ง busy เป็น true เมื่อเริ่ม และ false เมื่อเสร็จหรือผิดพลาด ปุ่มแสดงวงหมุนและกดซ้ำไม่ได้ ลองกด แล้วรอข้อความยืนยัน
const toast = useToast();
const [busy, setBusy] = useState(false);
async function send() {
setBusy(true);
try {
await sendRequest();
toast({ title: "ส่งคำขอแล้ว", body: "PR-2570-0003" });
} finally {
setBusy(false);
}
}
<Button label="ส่งคำขอ" tone="primary" icon={Send} busy={busy} onClick={send} />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "ส่งคำขอ" | string | The button text, already in the screen language. |
| tone | "primary" | ButtonTone ค่าที่ใช้ได้: "primary""secondary""danger""quiet" | — |
| icon | "Send" | LucideIcon | An icon before the label. |
| busy | false | boolean | The action is running. The button shows a spinner and can't be pressed. |
| onClick | send | (e: MouseEvent<HTMLElement>) => void | — |
กดไม่ได้
ใช้ disabled เมื่อยังทำไม่ได้ และบอกเหตุผลไว้ใกล้ ๆ เช่น ยังไม่ได้แนบใบเสนอราคา ระหว่างทำงานให้ใช้ busy แทน
<Button label="ส่งอนุมัติ" tone="primary" disabled />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "ส่งอนุมัติ" | string | The button text, already in the screen language. |
| tone | "primary" | ButtonTone ค่าที่ใช้ได้: "primary""secondary""danger""quiet" | — |
| disabled | true | boolean | — |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
เต็มความกว้าง
wide ให้ปุ่มเต็มความกว้างของกล่อง เช่น ปุ่มหลักบนหน้าจอมือถือ
<Button label="ยืนยันการลงเวลา" tone="primary" wide />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "ยืนยันการลงเวลา" | string | The button text, already in the screen language. |
| tone | "primary" | ButtonTone ค่าที่ใช้ได้: "primary""secondary""danger""quiet" | — |
| wide | true | boolean | Fills the width, such as a phone screen's main action. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
ลิงก์ที่ดูเหมือนปุ่ม
ส่ง href เมื่อปุ่มพาไปหน้าอื่น ปุ่มจะเป็นลิงก์ จึงเปิดในแท็บใหม่ได้ ถ้าเป็นการสั่งงานให้ใช้ onClick
<Button
label="ดูคำขอทั้งหมด"
iconAfter={ArrowRight}
href="#purchase-requests"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "ดูคำขอทั้งหมด" | string | The button text, already in the screen language. |
| iconAfter | ArrowRight | LucideIcon | An icon after the label, such as an arrow for a step forward. |
| href | "#purchase-requests" | string | Makes it a link to this URL, for an action that opens a page. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
ปุ่มส่งฟอร์มที่อยู่นอกฟอร์ม
type submit ส่งฟอร์มที่ปุ่มอยู่ข้างใน ถ้าปุ่มอยู่นอกฟอร์ม เช่น ใน footer ของ Drawer ให้ส่ง form เป็น id ของฟอร์ม ลองกดบันทึก
<form id="leave-note" onSubmit={saveNote}>
<TextInput label="หมายเหตุถึงผู้อนุมัติ" name="note" />
</form>
<Button label="บันทึกหมายเหตุ" tone="primary" type="submit" form="leave-note" />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "บันทึกหมายเหตุ" | string | The button text, already in the screen language. |
| tone | "primary" | ButtonTone ค่าที่ใช้ได้: "primary""secondary""danger""quiet" | — |
| type | "submit" | "button" | "submit" ค่าที่ใช้ได้: "button""submit" | submit sends the form it's in, or the form named by form. |
| form | "leave-note" | string | The id of the form a submit button sends, when it sits outside the form, such as in a dialog's footer. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
ข้อความต่างภาษา
lang บอกภาษาของข้อความบนปุ่มเมื่อต่างจากหน้า เช่น ปุ่ม English บนหน้าภาษาไทย เพื่อให้โปรแกรมอ่านหน้าจอออกเสียงถูก ปุ่ม submit ส่งฟอร์มที่อยู่ เช่น server action ของ Next.js
<form action={setLocale}>
<input type="hidden" name="locale" value="en" />
<Button label="English" type="submit" lang="en" />
</form>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "English" | string | The button text, already in the screen language. |
| type | "submit" | "button" | "submit" ค่าที่ใช้ได้: "button""submit" | submit sends the form it's in, or the form named by form. |
| lang | "en" | string | The label's language when it differs from the page, such as en for English on a Thai page, so screen readers pronounce it. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | string | — | The button text, already in the screen language. |
| tone | ButtonTone ค่าที่ใช้ได้: "primary""secondary""danger""quiet" | "secondary" | — |
| icon | LucideIcon | — | An icon before the label. |
| iconAfter | LucideIcon | — | An icon after the label, such as an arrow for a step forward. |
| size | "md" | "sm" | "md" | sm for a compact bar, such as actions on chosen rows. |
| type | "button" | "submit" | "button" | submit sends the form it's in, or the form named by form. |
| busy | boolean | false | The action is running. The button shows a spinner and can't be pressed. |
| disabled | boolean | false | — |
| wide | boolean | false | Fills the width, such as a phone screen's main action. |
| href | string | — | Makes it a link to this URL, for an action that opens a page. |
| form | string | — | The id of the form a submit button sends, when it sits outside the form, such as in a dialog's footer. |
| lang | string | — | The label's language when it differs from the page, such as en for English on a Thai page, so screen readers pronounce it. |
| onClick | (e: MouseEvent<HTMLElement>) => void | — | — |
ชนิดข้อมูล
ButtonTone
primary for the one main action of a step, secondary for the rest, danger for an action that deletes, quiet for a minor one.
type ButtonTone = "primary" | "secondary" | "danger" | "quiet";ข้อควรปฏิบัติ
- ส่ง label ในภาษาของหน้าจออยู่แล้ว เป็นคำกริยาที่บอกผล เช่น อนุมัติคำขอ บันทึกร่าง
- หนึ่งขั้นตอนมีปุ่ม primary ได้ปุ่มเดียว ในกล่องโต้ตอบปุ่มหลักอยู่ท้ายสุด
- งานที่ลบหรือย้อนกลับไม่ได้ใช้ tone danger และถามยืนยันใน Dialog ก่อน
- ใช้ busy ระหว่างรอ core API เพื่อกันการกดซ้ำ และบอกผลด้วย Toast เมื่อเสร็จ
- แป้นพิมพ์: Tab ไปที่ปุ่ม Enter หรือ Space กด ลิงก์ (href) กดด้วย Enter
ซอร์สโค้ด: ui/src/button.tsx