ข้ามไปยังเนื้อหา

ปุ่ม

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="ยกเลิก" />

พร้อมไอคอน

icon วางไอคอนหน้าข้อความ iconAfter วางไว้หลังข้อความ เช่น ลูกศรของปุ่มไปขั้นถัดไป ไอคอนประกอบข้อความ ไม่ใช่แทนข้อความ

<Button label="สร้างคำขอ" tone="primary" icon={Plus} />
<Button label="ขั้นถัดไป" iconAfter={ArrowRight} />

ขนาดเล็ก

size sm สำหรับแถบที่แน่น เช่น ปุ่มคำสั่งใน BulkBar ขนาดปกติสูง 44 พิกเซล เหมาะกับการแตะ

<Button label="ส่งออก" icon={Download} size="sm" />

ระหว่างทำงาน

ตั้ง 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} />

กดไม่ได้

ใช้ disabled เมื่อยังทำไม่ได้ และบอกเหตุผลไว้ใกล้ ๆ เช่น ยังไม่ได้แนบใบเสนอราคา ระหว่างทำงานให้ใช้ busy แทน

<Button label="ส่งอนุมัติ" tone="primary" disabled />

เต็มความกว้าง

wide ให้ปุ่มเต็มความกว้างของกล่อง เช่น ปุ่มหลักบนหน้าจอมือถือ

<Button label="ยืนยันการลงเวลา" tone="primary" wide />

ลิงก์ที่ดูเหมือนปุ่ม

ส่ง href เมื่อปุ่มพาไปหน้าอื่น ปุ่มจะเป็นลิงก์ จึงเปิดในแท็บใหม่ได้ ถ้าเป็นการสั่งงานให้ใช้ onClick

<Button
  label="ดูคำขอทั้งหมด"
  iconAfter={ArrowRight}
  href="#purchase-requests"
/>

ปุ่มส่งฟอร์มที่อยู่นอกฟอร์ม

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" />

ข้อความต่างภาษา

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>

props ทั้งหมด

ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้

Button props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
label* จำเป็นstring—The button text, already in the screen language.
toneButtonTone
ค่าที่ใช้ได้:"primary""secondary""danger""quiet"
"secondary"—
iconLucideIcon—An icon before the label.
iconAfterLucideIcon—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.
busybooleanfalseThe action is running. The button shows a spinner and can't be pressed.
disabledbooleanfalse—
widebooleanfalseFills the width, such as a phone screen's main action.
hrefstring—Makes it a link to this URL, for an action that opens a page.
formstring—The id of the form a submit button sends, when it sits outside the form, such as in a dialog's footer.
langstring—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