หน้าตัวอย่าง
ฟอร์มและตาราง
ใช้เป็นแบบของหน้ารายการและหน้าฟอร์มทุกหน้าในแอป เช่น รายการใบขอซื้อ และหน้าสร้างใบขอซื้อ กฎข้อแรก: ฟอร์ม ช่องกรอก และตาราง อยู่ใน Card ก่อนเสมอ ห้ามวางลงบนพื้นหน้าสีเทาของ AppShell โดยตรง คำถามสั้น ๆ ข้อเดียว เช่น เหตุผลที่ไม่อนุมัติ ใช้ Dialog แทนหน้าแยก
คอมโพเนนต์ที่ใช้
- PageHeaderThe top of a page: a short line saying where it belongs, the page's title as its one level 1 heading, a sentence on what it's for, and the page's actions.
- CardA card for one piece of a page, such as a dashboard section: a header with a title, a line under it, an icon and an action in its corner; the content; and a footer for actions or a note.
- DashedContainerA dashed outline around one block inside a Card, such as a DataTable, a preview or a group of figures, so the block reads as its own area while the card around it stays white.
- DataTableA complete data table from columns and rows: a filter bar with search, filter chips, sortable headings, paging, row selection with bulk actions, a total row, and the loading, error, first-use and no-results states.
- TextInputA one-line text field, with an optional leading icon.
- SelectA dropdown list on the ARIA listbox pattern.
- DateInputA field that picks a day, a day and time, a month, a week or a range of days from the guide's own calendar, instead of the browser's.
- ComboboxA searchable field on the ARIA combobox pattern, for long lists such as vendors or budget codes.
- MoneyInputAn amount field.
- RadioCardsOne choice from a few, shown as cards so each can carry a detail line, such as a leave type and the days left.
- CountedTextareaA textarea with a count of characters used against the limit, below it on the right.
- FileDropA drop zone that is also a file picker.
- SegmentedA small set of choices in one bar, such as Day, Month and Year.
- ButtonA button, or a link that looks like one with href.
- AppShellThe application frame: the left menu, which collapses to icons on wide screens and slides in on phones, and the top bar with the breadcrumb, global search, language switch, notifications and account menu.
import { PageHeader, Card, DashedContainer, DataTable, TextInput, Select, DateInput, Combobox, MoneyInput, RadioCards, CountedTextarea, FileDrop, Segmented, Button, AppShell } from "@bsru/ui";กฎของหน้านี้
Card ก่อนเสมอ
- ห้ามวางฟอร์ม ช่องกรอก หรือตาราง ลงบนพื้นหน้าของ AppShell (shell-page) โดยตรง ทุกอย่างอยู่ใน Card ก่อน
- สิ่งที่วางบนพื้นหน้าได้โดยตรงมีเพียง PageHeader, KpiTiles แบบการ์ด, Alert ที่เกี่ยวกับทั้งหน้า, แถวปุ่มของฟอร์ม และ grid หรือ flex ที่จัดวางการ์ด
- การ์ดแต่ละใบมีส่วนหัว: title, description สั้น ๆ และ icon แบบเส้นจาก lucide-react ที่ตรงกับเนื้อหา และปุ่มที่มุมเมื่อจำเป็น Panel เลิกใช้แล้วตั้งแต่ 0.40.0 ให้ใช้ Card แทน
- ตารางที่อยู่ใน Card อยู่ใน DashedContainer เสมอ
หน้ารายการ
- PageHeader ก่อน มีปุ่มหลักหนึ่งปุ่ม เช่น สร้างใบขอซื้อ ถ้าผู้ใช้มีสิทธิ์สร้าง
- ตารางอยู่ใน Card ที่มีส่วนหัว และ DashedContainer ตัวกรองอยู่บนแถบตัวกรองของตารางผ่าน filters.panel ไม่ทำเป็นฟอร์มค้นหาแยกเหนือตาราง
- แถวเปิดไปหน้าของเอกสารด้วย openHref
หน้าฟอร์ม
- แบ่งช่องกรอกเป็นกลุ่มตามเรื่อง หนึ่งกลุ่มต่อหนึ่ง Card จัดเป็นสองคอลัมน์บนจอกว้าง (md:grid-cols-2) และคอลัมน์เดียวบนโทรศัพท์ ช่องยาว เช่น เรื่อง ตัวเลือกแบบการ์ด ข้อความยาว และไฟล์แนบ กว้างเต็มแถว
- ทุกช่องมี label ช่องที่ต้องกรอกใส่ required และ error บอกว่าผิดอะไรและแก้อย่างไร ข้อผิดพลาดมาจาก core API แล้วส่งให้ช่องของมัน
- รายการของเอกสาร เช่น สินค้าในใบขอซื้อ เป็น DataTable paged={false} ใน Card และ DashedContainer ยอดรวมมาจาก core API หน้าจอไม่คำนวณเงินเอง
- ปุ่มของฟอร์มอยู่แถวเดียวหลังส่วนสุดท้าย ชิดขวา: ยกเลิก บันทึกร่าง แล้วปุ่มหลักหนึ่งปุ่มที่บอกว่าจะเกิดอะไร เช่น ส่งขออนุมัติ
ในแอป
- AppShell มาจาก layout หน้าแสดงเฉพาะเนื้อหาของมัน: PageHeader แล้วตามด้วยการ์ด
- ข้อความทุกตัวมาจากไฟล์ข้อความของแอป ในภาษาของหน้าจอ
- ตรวจข้อมูลซ้ำที่ server เสมอ การซ่อนหรือปิดช่องบนหน้าจอไม่ใช่การควบคุมสิทธิ์
ตัวอย่างทั้งหน้า
แต่ละตัวอย่างเป็นหน้าเต็ม แสดงในกรอบจอคอมพิวเตอร์ เลือกโทรศัพท์หรือแท็บเล็ตที่แถบบนเพื่อดูบนจอนั้น ข้อมูลทั้งหมดเป็นข้อมูลสมมติ
ERP: รายการใบขอซื้อ
PageHeader แล้วตามด้วย Card หนึ่งใบที่มีตาราง ช่องค้นหา และตัวกรองสถานะกับหน่วยงานบนแถบตัวกรอง ไม่มีอะไรวางบนพื้นหน้าโดยตรง
// app/(erp)/purchase-requests/page.tsx: AppShell comes from the layout, so the page renders only its content.
// PageHeader, then the table in a Card and a DashedContainer. The table's filters go on its own filter bar.
import { getTranslations } from "next-intl/server";
import { listPurchaseRequests } from "@/server/core";
import { RequestTable } from "./request-table";
export default async function PurchaseRequests() {
const t = await getTranslations("purchaseRequests");
// One call to the core API: only the requests this person may see.
const requests = await listPurchaseRequests();
return (
<div className="flex flex-col gap-6 p-4 lg:p-6">
<PageHeader
kicker={t("kicker")}
title={t("title")}
description={t("intro")}
actions={<NewRequestButton label={t("new")} />}
/>
<RequestTable rows={requests} />
</div>
);
}
// request-table.tsx
"use client";
import { Card, DashedContainer, DataTable, Segmented, Select, type FilterChip } from "@bsru/ui";
import { ClipboardList } from "lucide-react";
export function RequestTable({ rows }: { rows: PurchaseRequest[] }) {
const t = useTranslations("purchaseRequests");
const [status, setStatus] = useState<Status>("all");
const [unit, setUnit] = useState<string | null>(null);
const scoped = rows.filter((r) => !unit || r.unit === unit);
const shown = status === "all" ? scoped : scoped.filter((r) => r.status === status);
const chips: FilterChip[] = unit ? [{ key: "unit", label: unit, onRemove: () => setUnit(null) }] : [];
return (
<Card title={t("table.title")} description={t("table.year", { year: 2570 })} icon={ClipboardList}>
<DashedContainer>
<DataTable
caption={t("table.title")}
columns={columns(t)}
rows={shown}
rowKey="no"
search={{ placeholder: t("table.search") }}
openHref={(r) => `/purchase-requests/${r.no}`}
filters={{
chips,
active: status !== "all",
onClearAll: () => {
setStatus("all");
setUnit(null);
},
panel: (
<>
<Segmented label={t("filter.status")} value={status} options={statusChoices(t, scoped)} onChange={setStatus} />
<Select hideLabel label={t("filter.unit")} placeholder={t("filter.allUnits")} options={unitOptions} value={unit} onChange={setUnit} />
</>
),
}}
noun={t("noun")}
/>
</DashedContainer>
</Card>
);
}| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| rows* จำเป็น | รายการ 5 รายการ[
{
no: "PR-2570-0001",
item: "ครุภัณฑ์คอมพิวเตอร์โน้ตบุ๊ก 5 เครื่อง",
unit: "หน่วยงาน ก",
amount: 150000,
status: "pending",
},
{
no: "PR-2570-0002",
item: "จ้างซ่อมเครื่องปรับอากาศ",
unit: "หน่วยงาน ข",
amount: 43500,
status: "approved",
},
{
no: "PR-2570-0003",
item: "วัสดุสำนักงาน",
unit: "หน่วยงาน ค",
amount: 19500,
status: "approved",
},
{
no: "PR-2570-0004",
item: "โต๊ะและเก้าอี้ห้องเรียน",
unit: "หน่วยงาน ก",
amount: 88000,
status: "draft",
},
{
no: "PR-2570-0005",
item: "ค่าเช่าซอฟต์แวร์",
unit: "หน่วยงาน ข",
amount: 64000,
status: "rejected",
},
] | readonly R[] | All rows. With total set, only the rows of the current page. |
| units | รายการ 3 รายการ[
{ value: "หน่วยงาน ก", label: "หน่วยงาน ก" },
{ value: "หน่วยงาน ข", label: "หน่วยงาน ข" },
{ value: "หน่วยงาน ค", label: "หน่วยงาน ค" },
] | array | — |
| search | { placeholder: "ค้นหาเลขที่หรือรายการ" } | TableSearch | false | The search box. Without total, the table searches the searchable columns itself, and shows the box even when this is left out, with ค้นหา (Search) as its placeholder. With total the core API pages the list, so pass a search with onChange to get a box. false hides it. A table with paged false shows one only when this is passed. |
| onSortChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: TableSort) => void | Hears every sort change. Go back to page 1. |
| onPageChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: number) => void | — |
| onPageSizeChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: number) => void | — |
| onOpen | ไม่ได้ส่งในตัวอย่างนี้ | (row: R) => void | Opens a row, from the value in an opens column and a quick-view button. Use for a drawer. |
| openHref | (r) => `/purchase-requests/${r.no}` | (row: R) => string | The URL of a row's record page. The value in an opens column becomes a link. |
| openLabel | ไม่ได้ส่งในตัวอย่างนี้ | (row: R) => string | The quick-view button's accessible name for a row. ดูรายละเอียด and the row key by default. |
ERP: สร้างใบขอซื้อ
ช่องกรอกอยู่ใน Card ข้อมูลคำขอ รายการอยู่ใน Card ที่มีตารางใน DashedContainer เหตุผลและไฟล์แนบอยู่ใน Card ใบที่สาม มีเพียงแถวปุ่มที่วางบนพื้นหน้า
// app/(erp)/purchase-requests/new/request-form.tsx: the page renders <RequestForm />, and AppShell comes from the layout.
// PageHeader, then a Card for each group of fields, and the lines in a Card and a DashedContainer.
// Only the buttons sit on the page, after the last section. Fields and tables never sit straight on the page.
"use client";
import { Button, Card, Combobox, CountedTextarea, DashedContainer, DataTable, DateInput, FileDrop, MoneyInput, PageHeader, RadioCards, Select, TextInput } from "@bsru/ui";
import { FileText, ListChecks, Paperclip, Plus, Send } from "lucide-react";
import { useTranslations } from "next-intl";
import { useActionState, useState } from "react";
import { saveRequest } from "./actions"; // A server action that calls the core API, which validates and works out the money.
export function RequestForm({ units, budgetCodes, vendors, methods, draft }: RequestFormProps) {
const t = useTranslations("purchaseRequestForm");
// The core API's answer: each field's error, already in the screen language, and the lines with their totals.
const [result, submit, saving] = useActionState(saveRequest, { errors: {}, lines: draft.lines, total: draft.total });
const [unit, setUnit] = useState<string | null>(draft.unit);
const [code, setCode] = useState<string | null>(draft.budgetCode);
const [vendor, setVendor] = useState<ComboItem | null>(draft.vendor);
const [method, setMethod] = useState<string | null>(draft.method);
const e = result.errors;
return (
<form action={submit} className="flex flex-col gap-6 p-4 lg:p-6">
<PageHeader kicker={t("kicker")} title={t("title")} description={t("intro")} />
<Card title={t("details.title")} description={t("details.description")} icon={FileText}>
<div className="grid gap-4 md:grid-cols-2">
<div className="md:col-span-2">
<TextInput label={t("subject")} required name="subject" defaultValue={draft.subject} error={e.subject} />
</div>
<Select label={t("unit")} required placeholder={t("unitPick")} options={units} value={unit} onChange={setUnit} error={e.unit} />
<DateInput label={t("neededBy")} required name="neededBy" defaultValue={draft.neededBy} error={e.neededBy} />
<Select label={t("budgetCode")} required placeholder={t("budgetCodePick")} options={budgetCodes} value={code} onChange={setCode} hint={t("budgetCodeHint")} error={e.budgetCode} />
<Combobox label={t("vendor")} placeholder={t("vendorPick")} items={vendors} value={vendor} onChange={setVendor} error={e.vendor} />
<MoneyInput label={t("amount")} required defaultValue={draft.amount} hint={t("amountHint")} error={e.amount} />
<div className="md:col-span-2">
<RadioCards label={t("method")} required options={methods} value={method} onValueChange={setMethod} columns={3} error={e.method} />
</div>
</div>
</Card>
<Card title={t("lines.title")} description={t("lines.description")} icon={ListChecks} action={<Button size="sm" icon={Plus} label={t("lines.add")} onClick={openLineDialog} />}>
<DashedContainer>
<DataTable caption={t("lines.title")} columns={lineColumns(t)} rows={result.lines} rowKey="key" paged={false} totals={{ amount: result.total }} totalLabel={t("lines.total")} />
</DashedContainer>
</Card>
<Card title={t("reason.title")} description={t("reason.description")} icon={Paperclip}>
<div className="grid gap-4">
<CountedTextarea label={t("reason.label")} required rows={3} maxLength={500} defaultValue={draft.reason} error={e.reason} />
<FileDrop label={t("files.label")} title={t("files.drop")} hint={t("files.hint")} multiple accept="application/pdf" error={e.files} />
</div>
</Card>
<div className="flex flex-wrap justify-end gap-3">
<Button tone="quiet" label={t("cancel")} href="/purchase-requests" />
<Button label={t("saveDraft")} type="submit" busy={saving} />
<Button tone="primary" icon={Send} label={t("submit")} type="submit" busy={saving} />
</div>
</form>
);
}| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| units | รายการ 3 รายการ[
{ value: "หน่วยงาน ก", label: "หน่วยงาน ก" },
{ value: "หน่วยงาน ข", label: "หน่วยงาน ข" },
{ value: "หน่วยงาน ค", label: "หน่วยงาน ค" },
] | array | — |
| budgetCodes | รายการ 3 รายการ[
{
value: "2570-01-001",
label: "2570-01-001 ครุภัณฑ์คอมพิวเตอร์",
note: "คงเหลือ 350,000.00 บาท",
},
{
value: "2570-01-002",
label: "2570-01-002 วัสดุสำนักงาน",
note: "คงเหลือ 48,500.00 บาท",
},
{
value: "2570-02-001",
label: "2570-02-001 ค่าซ่อมแซมครุภัณฑ์",
note: "คงเหลือ 120,000.00 บาท",
},
] | array | — |
| vendors | รายการ 3 รายการ[
{
code: "V-0001",
th: "บริษัท ทดสอบคอมพิวเตอร์ จำกัด",
en: "Test Computer Co., Ltd.",
},
{
code: "V-0002",
th: "ห้างหุ้นส่วนจำกัด ตัวอย่างพาณิชย์",
en: "Sample Trading Ltd. Part.",
},
{ code: "V-0003", th: "ร้านสมมติการค้า", en: "Imaginary Store" },
] | array | — |
| methods | รายการ 3 รายการ[
{
value: "specific",
label: "เฉพาะเจาะจง",
detail: "วงเงินไม่เกิน 500,000 บาท",
},
{
value: "selection",
label: "คัดเลือก",
detail: "เชิญผู้ค้าอย่างน้อย 3 ราย",
},
{
value: "e-bidding",
label: "ประกวดราคาอิเล็กทรอนิกส์",
detail: "วงเงินเกิน 500,000 บาท",
},
] | array | — |
| lines | รายการ 3 รายการ[
{
key: "1",
description: "คอมพิวเตอร์โน้ตบุ๊ก",
quantity: 5,
unit: "เครื่อง",
unitPrice: 27000,
amount: 135000,
},
{
key: "2",
description: "กระเป๋าใส่โน้ตบุ๊ก",
quantity: 5,
unit: "ใบ",
unitPrice: 1000,
amount: 5000,
},
{
key: "3",
description: "ติดตั้งและตั้งค่าซอฟต์แวร์",
quantity: 1,
unit: "งาน",
unitPrice: 10000,
amount: 10000,
},
] | array | — |
| total | 150000 | number | How many rows match on the server, for a list the core API pages. The table then shows rows as given. |
| onChange* จำเป็น | setUnit | (next: V) => void | — |