ข้อความชั่วคราว
ToastProvider
Shows toasts at the bottom right. A toast confirms a finished action. A decision that needs an answer uses a dialog.
ใช้ยืนยันว่าการกระทำเสร็จแล้ว เช่น บันทึกร่างแล้ว ข้อความหายไปเอง ถ้าต้องได้คำตอบก่อนทำต่อ ใช้ Dialog ถ้าข้อความต้องอยู่ในหน้าจนกว่าจะแก้ไข เช่น งบไม่พอ ใช้ Alert
import { ToastProvider } from "@bsru/ui";ตัวอย่าง
ตั้งค่าครั้งเดียวที่ราก
ใส่ ToastProvider ไว้ใน LangProvider ครั้งเดียวทั้งแอป แล้วหน้าจอใดก็เรียก useToast ได้
เว็บนี้ครอบด้วย ToastProvider แบบเดียวกัน ตัวอย่างถัดไปจึงเรียก useToast ได้
<LangProvider>
<ToastProvider>{children}</ToastProvider>
</LangProvider>ข้อความแต่ละแบบ
useToast คืนฟังก์ชัน ส่งข้อมูลของข้อความเป็นอ็อบเจกต์ ไม่ส่ง tone จะเป็น success
const toast = useToast();
toast({ title: "บันทึกร่างแล้ว", body: "PR-2570-0003" });
toast({
tone: "warning",
title: "งบคงเหลือน้อยกว่า 10%",
body: "BG-2570-ข-002",
});
toast({
tone: "danger",
title: "ส่งใบลาไม่สำเร็จ",
body: "วันที่เลือกทับกับคำขอเดิม เลือกวันอื่น",
});
toast({ tone: "info", title: "มีคำขอรออนุมัติ 3 รายการ" });| Prop | ค่า 1 | ค่า 2 | ค่า 3 | ค่า 4 | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|---|---|---|
| title* จำเป็น | "บันทึกร่างแล้ว" | "งบคงเหลือน้อยกว่า 10%" | "ส่งใบลาไม่สำเร็จ" | "มีคำขอรออนุมัติ 3 รายการ" | string | What happened, in a few words, such as Draft saved. |
| body | "PR-2570-0003" | "BG-2570-ข-002" | "วันที่เลือกทับกับคำขอเดิม เลือกวันอื่น" | — | string | A second line, such as the document number. |
| tone | — | "warning" | "danger" | "info" | ToastTone ค่าที่ใช้ได้: "success""danger""warning""info" | success by default. |
| onAction | ไม่ได้ส่งในตัวอย่างนี้ | () => void | Called when the action is pressed. The toast closes first. | |||
พร้อมปุ่มเลิกทำ
action คือข้อความบนปุ่ม onAction ทำงานหลังข้อความปิด ใช้กับการกระทำที่ย้อนกลับได้
const toast = useToast();
toast({
title: "ลบรายการแล้ว",
body: "PR-2570-0004",
action: "เลิกทำ",
onAction: () => restore("PR-2570-0004"),
});| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| title* จำเป็น | "ลบรายการแล้ว" | string | What happened, in a few words, such as Draft saved. |
| body | "PR-2570-0004" | string | A second line, such as the document number. |
| action | "เลิกทำ" | string | A short action, such as Undo. |
| onAction | ไม่ได้ส่งในตัวอย่างนี้ | () => void | Called when the action is pressed. The toast closes first. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| children* จำเป็น | ReactNode | — | The app. Every screen that calls useToast must be inside it. |
ชนิดข้อมูล
ToastOptions
What a toast says.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| tone | ToastTone ค่าที่ใช้ได้: "success""danger""warning""info" | — | success by default. |
| title* จำเป็น | string | — | What happened, in a few words, such as Draft saved. |
| body | string | — | A second line, such as the document number. |
| action | string | — | A short action, such as Undo. |
| onAction | () => void | — | Called when the action is pressed. The toast closes first. |
| ms | number | — | How long it stays, in milliseconds. 5000 by default. |
ToastTone
The kind of message, which sets the icon.
type ToastTone = "success" | "danger" | "warning" | "info";ข้อควรปฏิบัติ
- ข้อความประกาศผ่าน role status โปรแกรมอ่านหน้าจอจึงอ่านโดยไม่ย้ายโฟกัส
- เวลาหยุดเมื่อชี้หรือโฟกัสที่ข้อความ ค่าเริ่มต้น 5 วินาที ข้อความที่มีปุ่มควรให้เวลามากขึ้นด้วย ms
- ชื่อเรื่องสั้น บอกสิ่งที่เกิดขึ้น และส่งมาในภาษาของหน้าจออยู่แล้ว
- ห้ามใช้ Toast แจ้งข้อผิดพลาดของช่องกรอก ให้แสดงใต้ช่องนั้น
ซอร์สโค้ด: ui/src/toast.tsx