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

กล่องโต้ตอบ

Dialog

A modal dialog for a decision, built on the native dialog element, so focus stays inside and Esc closes it. The title says what will happen. The actions say what each button does, such as Approve request.

ใช้เมื่อต้องได้คำตอบก่อนทำงานต่อ เช่น ยืนยันการอนุมัติ หรือกรอกเหตุผลที่ไม่อนุมัติ ถ้าแค่ดูรายละเอียดโดยไม่เสียตำแหน่งในรายการ ใช้ Drawer ถ้าแค่ยืนยันว่าทำเสร็จแล้ว ใช้ Toast

import { Dialog } from "@bsru/ui";

ตัวอย่าง

ยืนยันการกระทำ

หน้าจอเก็บ open ใน state ส่งชื่อเรื่องเป็นคำถาม รายละเอียดเป็น children และปุ่มใน actions โดยปุ่มหลักอยู่ท้ายสุด

const [open, setOpen] = useState(false);

<Button label="เปิดกล่องยืนยัน" onClick={() => setOpen(true)} />

<Dialog
  open={open}
  onClose={() => setOpen(false)}
  title="อนุมัติคำขอนี้หรือไม่"
  actions={
    <>
      <Button label="ยกเลิก" onClick={() => setOpen(false)} />
      <Button label="อนุมัติคำขอ" tone="primary" onClick={approve} />
    </>
  }
>
  <p className="mt-2">PR-2570-0001 ยังไม่ผูกพันงบ เมื่ออนุมัติแล้ว เจ้าหน้าที่พัสดุจะออกใบสั่งซื้อวงเงิน 150,000.00 บาทได้</p>
</Dialog>

กล่องที่มีช่องกรอก

ปุ่มที่ไม่อนุมัติหรือลบใช้ Button tone danger ช่องกรอกอยู่ใน children และป้ายกำกับต้องชี้ไปที่ id ของช่อง

const [open, setOpen] = useState(false);

<Dialog
  open={open}
  onClose={() => setOpen(false)}
  title="ไม่อนุมัติคำขอนี้หรือไม่"
  actions={
    <>
      <Button label="ยกเลิก" onClick={() => setOpen(false)} />
      <Button label="ไม่อนุมัติคำขอ" tone="danger" onClick={reject} />
    </>
  }
>
  <p className="mt-2">ผู้ขอจะเห็นเหตุผลที่คุณระบุ</p>
  <div className="mt-3">
    <CountedTextarea label="เหตุผล" required rows={3} maxLength={500} onValueChange={setReason} />
  </div>
</Dialog>

props ทั้งหมด

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

Dialog props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
open* จำเป็นboolean—Shows the dialog. The screen keeps this in its state.
onClose* จำเป็น() => void—Called on Esc or a click outside. Set open to false here.
title* จำเป็นReactNode—A question that says what will happen, such as Approve this request?
childrenReactNode—The details under the title, such as what the action changes, or a short form.
actionsReactNode—The buttons, the main one last.

ข้อควรปฏิบัติ

  • ใช้ dialog ของเบราว์เซอร์ โฟกัสจึงอยู่ในกล่อง Esc หรือคลิกนอกกล่องเรียก onClose และโฟกัสกลับไปที่ปุ่มที่เปิด
  • ชื่อเรื่องบอกสิ่งที่จะเกิดขึ้น และปุ่มบอกว่าทำอะไร เช่น อนุมัติคำขอ ไม่ใช่ ตกลง
  • ถ้าข้อมูลที่กรอกไม่ผ่าน ให้กล่องเปิดค้างไว้ แสดงข้อผิดพลาดใต้ช่อง และย้ายโฟกัสไปที่ช่องนั้น
  • ห้ามเปิดกล่องโต้ตอบซ้อนกัน

ซอร์สโค้ด: ui/src/dialog.tsx