เลขหน้า
Pagination
Previous and next buttons with page numbers between them, and an ellipsis for each gap. Empty with one page.
ใช้ใต้ตารางข้อมูลเพื่อเปลี่ยนหน้า ส่งหน้าปัจจุบันและจำนวนหน้าทั้งหมด หน้าจอเก็บหน้าปัจจุบันไว้ใน state ถ้าเป็นขั้นตอนของฟอร์มหลายหน้า ใช้ Steps แทน ตอนนี้หน้าจอยังต้องเขียนข้อความช่วงรายการ (แสดง 1-10 จาก 47 รายการ) และตัวเลือกจำนวนต่อหน้าเอง
import { Pagination } from "@bsru/ui";ตัวอย่าง
ไม่กี่หน้า
ทุกหน้ามีปุ่มเลข ปุ่มก่อนหน้าปิดอยู่เมื่ออยู่หน้าแรก
const [page, setPage] = useState(1);
<Pagination page={page} pages={5} onPage={setPage} />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| page* จำเป็น | page | number | The current page, from 1. |
| pages* จำเป็น | 5 | number | How many pages there are, worked out from the total the core API returns. |
| onPage* จำเป็น | setPage | (next: number) => void | Called with the page to show, from a number or the previous and next buttons. |
หลายหน้า
แสดงหน้าแรก หน้าสุดท้าย และหน้าปัจจุบันกับหน้าที่อยู่ติดกัน ช่องว่างระหว่างนั้นแสดงเป็น …
const [page, setPage] = useState(12);
<Pagination page={page} pages={24} onPage={setPage} />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| page* จำเป็น | page | number | The current page, from 1. |
| pages* จำเป็น | 24 | number | How many pages there are, worked out from the total the core API returns. |
| onPage* จำเป็น | setPage | (next: number) => void | Called with the page to show, from a number or the previous and next buttons. |
หน้าเดียว
เมื่อมีหน้าเดียว ตัวแบ่งหน้าว่างเปล่า ไม่ต้องซ่อนเอง แต่ตอนไม่พบรายการ กำลังโหลด หรือโหลดไม่สำเร็จ ให้ไม่แสดงตัวแบ่งหน้าเลย
<Pagination page={1} pages={1} onPage={handlePage} />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| page* จำเป็น | 1 | number | The current page, from 1. |
| pages* จำเป็น | 1 | number | How many pages there are, worked out from the total the core API returns. |
| onPage* จำเป็น | handlePage | (next: number) => void | Called with the page to show, from a number or the previous and next buttons. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| page* จำเป็น | number | — | The current page, from 1. |
| pages* จำเป็น | number | — | How many pages there are, worked out from the total the core API returns. |
| onPage* จำเป็น | (next: number) => void | — | Called with the page to show, from a number or the previous and next buttons. |
| className | string | "flex items-center gap-1" | Classes for the nav. |
ข้อควรปฏิบัติ
- คำนวณจำนวนหน้าจากจำนวนรายการทั้งหมดที่ core API ส่งมา และขนาดหน้า
- เมื่อคำค้นหา ตัวกรอง การเรียง หรือขนาดหน้าเปลี่ยน ให้กลับไปหน้า 1
- ตัวแบ่งหน้าเป็น nav ที่มีชื่อ เลขหน้า ปุ่มหน้าปัจจุบันมี aria-current="page" และทุกปุ่มมีชื่อให้โปรแกรมอ่านหน้าจอ
- pageNumbers(page, pages) คืนเลขหน้าที่แสดง ใช้ได้ถ้าต้องการทดสอบ
ซอร์สโค้ด: ui/src/data-table.tsx