oneslash-design-system 2.19.0 → 2.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/DESIGN.md +62 -5
- package/dist/cases/about-page/profile.d.ts +1 -0
- package/dist/cases/about-page/profile.jsx +64 -0
- package/dist/cases/about-page.md +114 -0
- package/dist/cases/admin-inbox/list.d.ts +1 -0
- package/dist/cases/admin-inbox/list.jsx +104 -0
- package/dist/cases/admin-inbox/split.d.ts +1 -0
- package/dist/cases/admin-inbox/split.jsx +68 -0
- package/dist/cases/admin-inbox.md +254 -0
- package/dist/cases/api-keys.md +1 -1
- package/dist/cases/billing-page/billed-elsewhere.d.ts +1 -0
- package/dist/cases/billing-page/billed-elsewhere.jsx +28 -0
- package/dist/cases/billing-page/self-serve.d.ts +1 -0
- package/dist/cases/billing-page/self-serve.jsx +134 -0
- package/dist/cases/billing-page.md +247 -0
- package/dist/cases/blog-index/cards.d.ts +1 -0
- package/dist/cases/blog-index/cards.jsx +45 -0
- package/dist/cases/blog-index/list.d.ts +1 -0
- package/dist/cases/blog-index/list.jsx +46 -0
- package/dist/cases/blog-index.md +160 -0
- package/dist/cases/breakdown-list/bars.d.ts +1 -0
- package/dist/cases/breakdown-list/bars.jsx +47 -0
- package/dist/cases/breakdown-list/table.d.ts +1 -0
- package/dist/cases/breakdown-list/table.jsx +69 -0
- package/dist/cases/breakdown-list.md +158 -0
- package/dist/cases/changelog/by-month.d.ts +1 -0
- package/dist/cases/changelog/by-month.jsx +70 -0
- package/dist/cases/changelog/one-page.d.ts +1 -0
- package/dist/cases/changelog/one-page.jsx +70 -0
- package/dist/cases/changelog.md +200 -0
- package/dist/cases/code-sample/block.d.ts +1 -0
- package/dist/cases/code-sample/block.jsx +19 -0
- package/dist/cases/code-sample/command.d.ts +1 -0
- package/dist/cases/code-sample/command.jsx +17 -0
- package/dist/cases/code-sample.md +96 -0
- package/dist/cases/composer-limit/notice-with-action.jsx +3 -3
- package/dist/cases/composer-limit/notice.jsx +3 -3
- package/dist/cases/composer-limit.md +8 -8
- package/dist/cases/contact-page/form.d.ts +1 -0
- package/dist/cases/contact-page/form.jsx +83 -0
- package/dist/cases/contact-page/links.d.ts +1 -0
- package/dist/cases/contact-page/links.jsx +19 -0
- package/dist/cases/contact-page.md +142 -0
- package/dist/cases/dashboard/numbers-first.d.ts +1 -0
- package/dist/cases/dashboard/numbers-first.jsx +165 -0
- package/dist/cases/dashboard/work-first.d.ts +1 -0
- package/dist/cases/dashboard/work-first.jsx +165 -0
- package/dist/cases/dashboard.md +507 -0
- package/dist/cases/data-table-page/all-rows.d.ts +1 -0
- package/dist/cases/data-table-page/all-rows.jsx +226 -0
- package/dist/cases/data-table-page/paged.d.ts +1 -0
- package/dist/cases/data-table-page/paged.jsx +284 -0
- package/dist/cases/data-table-page.md +771 -0
- package/dist/cases/docs-component-page/reference.d.ts +1 -0
- package/dist/cases/docs-component-page/reference.jsx +93 -0
- package/dist/cases/docs-component-page.md +144 -0
- package/dist/cases/faq/accordion.d.ts +1 -0
- package/dist/cases/faq/accordion.jsx +25 -0
- package/dist/cases/faq/open-list.d.ts +1 -0
- package/dist/cases/faq/open-list.jsx +27 -0
- package/dist/cases/faq.md +110 -0
- package/dist/cases/feature-grid/cards.d.ts +1 -0
- package/dist/cases/feature-grid/cards.jsx +33 -0
- package/dist/cases/feature-grid/rows.d.ts +1 -0
- package/dist/cases/feature-grid/rows.jsx +30 -0
- package/dist/cases/feature-grid.md +132 -0
- package/dist/cases/image-viewer/gallery.d.ts +1 -0
- package/dist/cases/image-viewer/gallery.jsx +32 -0
- package/dist/cases/image-viewer/single.d.ts +1 -0
- package/dist/cases/image-viewer/single.jsx +25 -0
- package/dist/cases/image-viewer.md +123 -0
- package/dist/cases/invoice-list/table.d.ts +1 -0
- package/dist/cases/invoice-list/table.jsx +64 -0
- package/dist/cases/invoice-list/with-upcoming.d.ts +1 -0
- package/dist/cases/invoice-list/with-upcoming.jsx +75 -0
- package/dist/cases/invoice-list.md +237 -0
- package/dist/cases/landing-hero/centered.d.ts +1 -0
- package/dist/cases/landing-hero/centered.jsx +22 -0
- package/dist/cases/landing-hero/split.d.ts +1 -0
- package/dist/cases/landing-hero/split.jsx +30 -0
- package/dist/cases/landing-hero.md +114 -0
- package/dist/cases/legal-page/sections.d.ts +1 -0
- package/dist/cases/legal-page/sections.jsx +61 -0
- package/dist/cases/legal-page.md +96 -0
- package/dist/cases/load-failure/inline.jsx +2 -2
- package/dist/cases/load-failure.md +2 -2
- package/dist/cases/long-form-page/article.d.ts +1 -0
- package/dist/cases/long-form-page/article.jsx +58 -0
- package/dist/cases/long-form-page/with-contents.d.ts +1 -0
- package/dist/cases/long-form-page/with-contents.jsx +41 -0
- package/dist/cases/long-form-page.md +170 -0
- package/dist/cases/members-list/rows.jsx +2 -1
- package/dist/cases/members-list/table.jsx +8 -9
- package/dist/cases/members-list.md +12 -13
- package/dist/cases/message-actions/hover-toolbar.jsx +11 -8
- package/dist/cases/message-actions/menu.jsx +10 -7
- package/dist/cases/message-actions.md +19 -13
- package/dist/cases/message-attachments/in-composer.jsx +2 -2
- package/dist/cases/message-attachments/in-message.jsx +16 -11
- package/dist/cases/message-attachments.md +29 -17
- package/dist/cases/number-format.md +36 -0
- package/dist/cases/one-main-action.md +33 -0
- package/dist/cases/overflow-menu/in-a-header.jsx +9 -6
- package/dist/cases/overflow-menu/on-a-row.jsx +9 -6
- package/dist/cases/overflow-menu.md +16 -10
- package/dist/cases/page-end/cta-band.d.ts +1 -0
- package/dist/cases/page-end/cta-band.jsx +24 -0
- package/dist/cases/page-end/next-link.d.ts +1 -0
- package/dist/cases/page-end/next-link.jsx +18 -0
- package/dist/cases/page-end.md +88 -0
- package/dist/cases/paged-card/pager.d.ts +1 -0
- package/dist/cases/paged-card/pager.jsx +41 -0
- package/dist/cases/paged-card/see-all.d.ts +1 -0
- package/dist/cases/paged-card/see-all.jsx +34 -0
- package/dist/cases/paged-card.md +147 -0
- package/dist/cases/password-gate/inline.d.ts +1 -0
- package/dist/cases/password-gate/inline.jsx +94 -0
- package/dist/cases/password-gate/modal.d.ts +1 -0
- package/dist/cases/password-gate/modal.jsx +81 -0
- package/dist/cases/password-gate/page.d.ts +1 -0
- package/dist/cases/password-gate/page.jsx +82 -0
- package/dist/cases/password-gate.md +351 -0
- package/dist/cases/payment-method/none.d.ts +1 -0
- package/dist/cases/payment-method/none.jsx +17 -0
- package/dist/cases/payment-method/on-file.d.ts +1 -0
- package/dist/cases/payment-method/on-file.jsx +92 -0
- package/dist/cases/payment-method.md +182 -0
- package/dist/cases/plan-change-confirm/blocked.d.ts +1 -0
- package/dist/cases/plan-change-confirm/blocked.jsx +48 -0
- package/dist/cases/plan-change-confirm/downgrade.d.ts +1 -0
- package/dist/cases/plan-change-confirm/downgrade.jsx +52 -0
- package/dist/cases/plan-change-confirm/upgrade.d.ts +1 -0
- package/dist/cases/plan-change-confirm/upgrade.jsx +51 -0
- package/dist/cases/plan-change-confirm.md +261 -0
- package/dist/cases/plan-picker/cards.d.ts +1 -0
- package/dist/cases/plan-picker/cards.jsx +97 -0
- package/dist/cases/plan-picker/list.d.ts +1 -0
- package/dist/cases/plan-picker/list.jsx +103 -0
- package/dist/cases/plan-picker.md +297 -0
- package/dist/cases/pricing-section/cards.d.ts +1 -0
- package/dist/cases/pricing-section/cards.jsx +40 -0
- package/dist/cases/pricing-section.md +85 -0
- package/dist/cases/product-cards/grid.d.ts +1 -0
- package/dist/cases/product-cards/grid.jsx +20 -0
- package/dist/cases/product-cards.md +71 -0
- package/dist/cases/range-switch/segmented.jsx +11 -7
- package/dist/cases/range-switch.md +16 -11
- package/dist/cases/reply-feedback/thumbs.jsx +11 -8
- package/dist/cases/reply-feedback/with-reason.jsx +11 -8
- package/dist/cases/reply-feedback.md +20 -14
- package/dist/cases/section-index/cards.d.ts +1 -0
- package/dist/cases/section-index/cards.jsx +25 -0
- package/dist/cases/section-index/list.d.ts +1 -0
- package/dist/cases/section-index/list.jsx +56 -0
- package/dist/cases/section-index.md +140 -0
- package/dist/cases/settings-page/top-tabs.jsx +4 -3
- package/dist/cases/settings-page.md +5 -4
- package/dist/cases/site-header.md +1 -1
- package/dist/cases/site-search/dialog.d.ts +1 -0
- package/dist/cases/site-search/dialog.jsx +131 -0
- package/dist/cases/site-search.md +176 -0
- package/dist/cases/stat-row/featured.d.ts +1 -0
- package/dist/cases/stat-row/featured.jsx +17 -0
- package/dist/cases/stat-row/tiles.d.ts +1 -0
- package/dist/cases/stat-row/tiles.jsx +34 -0
- package/dist/cases/stat-row.md +104 -0
- package/dist/cases/status-label/dot.d.ts +1 -0
- package/dist/cases/status-label/dot.jsx +39 -0
- package/dist/cases/status-label/text.d.ts +1 -0
- package/dist/cases/status-label/text.jsx +44 -0
- package/dist/cases/status-label.md +143 -0
- package/dist/cases/tabbed-panels/tabs.d.ts +1 -0
- package/dist/cases/tabbed-panels/tabs.jsx +40 -0
- package/dist/cases/tabbed-panels.md +81 -0
- package/dist/cases/upgrade-prompt/inline.jsx +4 -2
- package/dist/cases/upgrade-prompt/modal.jsx +2 -1
- package/dist/cases/upgrade-prompt.md +10 -6
- package/dist/cases/usage-meters/by-person.d.ts +1 -0
- package/dist/cases/usage-meters/by-person.jsx +72 -0
- package/dist/cases/usage-meters/meters.d.ts +1 -0
- package/dist/cases/usage-meters/meters.jsx +49 -0
- package/dist/cases/usage-meters/with-cap.d.ts +1 -0
- package/dist/cases/usage-meters/with-cap.jsx +73 -0
- package/dist/cases/usage-meters.md +303 -0
- package/dist/cases.json +2178 -97
- package/dist/components/accordion.d.ts +31 -0
- package/dist/components/accordion.jsx +103 -0
- package/dist/components/button.d.ts +1 -1
- package/dist/components/button.jsx +11 -5
- package/dist/components/codeBlock.d.ts +26 -0
- package/dist/components/codeBlock.jsx +101 -0
- package/dist/components/formatNumber.d.ts +25 -0
- package/dist/components/formatNumber.js +125 -0
- package/dist/components/iconButton.jsx +12 -7
- package/dist/components/lightbox.d.ts +31 -0
- package/dist/components/lightbox.jsx +161 -0
- package/dist/components/linkCard.d.ts +10 -1
- package/dist/components/linkCard.jsx +32 -6
- package/dist/components/modal.d.ts +10 -2
- package/dist/components/modal.jsx +17 -12
- package/dist/components/pageHeader.jsx +1 -1
- package/dist/components/pagination.d.ts +28 -0
- package/dist/components/pagination.jsx +89 -0
- package/dist/components/progress.d.ts +33 -0
- package/dist/components/progress.jsx +50 -0
- package/dist/components/searchDialog.d.ts +44 -0
- package/dist/components/searchDialog.jsx +152 -0
- package/dist/components/skeleton.d.ts +21 -0
- package/dist/components/skeleton.jsx +41 -0
- package/dist/components/statTile.d.ts +32 -0
- package/dist/components/statTile.jsx +61 -0
- package/dist/components/tab.d.ts +5 -1
- package/dist/components/tab.jsx +2 -2
- package/dist/components/tabPanel.d.ts +16 -0
- package/dist/components/tabPanel.jsx +12 -0
- package/dist/components/tableContainer.d.ts +5 -1
- package/dist/components/tableContainer.jsx +9 -1
- package/dist/components/tableHeaderCell.d.ts +3 -1
- package/dist/components/tableHeaderCell.jsx +2 -2
- package/dist/components/tabsContainer.d.ts +5 -1
- package/dist/components/tabsContainer.jsx +2 -2
- package/dist/components/titleBar.d.ts +3 -1
- package/dist/components/titleBar.jsx +4 -3
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/internal/accordion.d.ts +4 -0
- package/dist/internal/accordion.js +53 -0
- package/dist/internal/combobox.d.ts +20 -0
- package/dist/internal/combobox.js +28 -0
- package/dist/internal/highlight.d.ts +5 -0
- package/dist/internal/highlight.js +18 -0
- package/dist/native-mapping.json +57 -3
- package/dist/stories/Accordion.stories.d.ts +11 -0
- package/dist/stories/Accordion.stories.jsx +93 -0
- package/dist/stories/Button.stories.d.ts +1 -0
- package/dist/stories/Button.stories.jsx +19 -1
- package/dist/stories/Cases.generated.stories.d.ts +60 -0
- package/dist/stories/Cases.generated.stories.jsx +120 -0
- package/dist/stories/CodeBlock.stories.d.ts +12 -0
- package/dist/stories/CodeBlock.stories.jsx +65 -0
- package/dist/stories/Lightbox.stories.d.ts +8 -0
- package/dist/stories/Lightbox.stories.jsx +40 -0
- package/dist/stories/LinkCard.stories.d.ts +6 -0
- package/dist/stories/LinkCard.stories.jsx +17 -0
- package/dist/stories/PageHeader.stories.d.ts +2 -0
- package/dist/stories/PageHeader.stories.jsx +13 -0
- package/dist/stories/Pagination.stories.d.ts +9 -0
- package/dist/stories/Pagination.stories.jsx +75 -0
- package/dist/stories/Progress.stories.d.ts +9 -0
- package/dist/stories/Progress.stories.jsx +43 -0
- package/dist/stories/SearchDialog.stories.d.ts +17 -0
- package/dist/stories/SearchDialog.stories.jsx +111 -0
- package/dist/stories/Skeleton.stories.d.ts +7 -0
- package/dist/stories/Skeleton.stories.jsx +27 -0
- package/dist/stories/StatTile.stories.d.ts +10 -0
- package/dist/stories/StatTile.stories.jsx +55 -0
- package/dist/stories/TabPanel.stories.d.ts +7 -0
- package/dist/stories/TabPanel.stories.jsx +31 -0
- package/dist/stories/Table.stories.d.ts +1 -0
- package/dist/stories/Table.stories.jsx +49 -1
- package/dist/stories/TitleBar.stories.d.ts +1 -0
- package/dist/stories/TitleBar.stories.jsx +7 -0
- package/package.json +5 -1
|
@@ -0,0 +1,771 @@
|
|
|
1
|
+
# Data table page
|
|
2
|
+
|
|
3
|
+
A page is one long table of records, such as invoices or customers, that a person searches, sorts and acts on.
|
|
4
|
+
|
|
5
|
+
## Do
|
|
6
|
+
|
|
7
|
+
- Follow the rule 'A number shows its unit and never shows 0 for unknown'.
|
|
8
|
+
- Put the page in `AppShell` as the case 'App shell' says, with a `TitleBar` named for the records: 'Invoices'. Under it say the total and its unit: '290 invoices'.
|
|
9
|
+
- Put search and a filter above the table, in a row at the end. Use the case 'Collapsible search' for the search, so the row fits a phone, and the case 'Filter menu' for the filter.
|
|
10
|
+
- Say what search and filter leave in the line under the title: '43 of 290 invoices match "north", past due.'
|
|
11
|
+
- Wait a moment after the last key before you ask the server for a search.
|
|
12
|
+
- Sort a column with the Button in its `TableHeaderCell`, as the case 'Members' says. Set `sort` on the header cell of the sorted column only, and leave it unset on the others. The Button is named by its label alone, such as 'Date'.
|
|
13
|
+
- Right-align a number column and its header, and show the exact amount: '$1,240.00'.
|
|
14
|
+
- Give each row one More menu, named for the row: 'More actions for INV-1290'. Follow the case 'More menu'. Keep every action in the menu, not as Buttons in the row.
|
|
15
|
+
- Show the status as the case 'Status in a row' says.
|
|
16
|
+
- Name the table with `TableContainer aria-label="Invoices"` and give it a `minWidth`. A phone scrolls the table inside its named region, never the page.
|
|
17
|
+
- Show 25 rows a page when the pages come from the server, with `Pagination` named for the table below it: 'Invoice pages'. Pass `scrollTargetId` so a press of Next scrolls to the top of the page. Focus stays on Next.
|
|
18
|
+
- Pass ``getHref={(p) => '/invoices?page=' + p}`` when each page has its own address.
|
|
19
|
+
- Go back to page 1 after a sort, a search or a filter, and let the pager follow. With all the rows on the page, show no pager and no loading state.
|
|
20
|
+
- Keep the header and the pager while a page loads, and show 10 `Skeleton` rows. Give the first Skeleton a `label` such as 'Loading invoices', so a screen reader hears one status. Show a `Skeleton` in place of the count too.
|
|
21
|
+
- Show the case 'Empty list' in the table's place when there are no rows at all: 'No invoices yet', with 'Your first invoice appears here after your first payment.'
|
|
22
|
+
- Show the case 'Empty search' with the words searched for when no row matches. Its Button is 'Clear Search' when only words are on, and 'Clear Filter' when a filter is on too. It clears both. Focus goes to the search button.
|
|
23
|
+
- Show the case 'Load failure' in the table's place when a page fails to load. 'Try Again' is `loading` while it asks again and keeps its label. Keep the title and the pager.
|
|
24
|
+
- Reach the page from a card's 'See All' link, as the case 'Short list on a dashboard' says.
|
|
25
|
+
|
|
26
|
+
## Don't
|
|
27
|
+
|
|
28
|
+
- Do not replace the page with a spinner while a page loads. Keep the header and the pager.
|
|
29
|
+
- Do not show 0 for a total that is loading or failed.
|
|
30
|
+
- Do not show page 4 of the old list after a new sort or filter. Go back to page 1.
|
|
31
|
+
- Do not show 'No results' with no words, or the list's own empty text when a search found nothing.
|
|
32
|
+
- Do not put a Button for every action in each row. Use one More menu.
|
|
33
|
+
- Do not leave a table that can scroll sideways without a name.
|
|
34
|
+
- Do not put `sort` on more than one header cell.
|
|
35
|
+
- Do not number the pages as a row of buttons. A phone has no room, and 500 pages have no end.
|
|
36
|
+
- Do not page a list of under 50 rows.
|
|
37
|
+
|
|
38
|
+
## Option: Pages from the server
|
|
39
|
+
|
|
40
|
+
Use when: There are more rows than fit on a page, they come from the server, and the page holds 25 of them.
|
|
41
|
+
|
|
42
|
+
Uses: `Button`, `Center`, `Cluster`, `EmptyBox`, `IconButton`, `Line`, `Menu`, `MenuItem`, `Pagination`, `Paragraph`, `Popover`, `Skeleton`, `Stack`, `Surface`, `TableCell`, `TableContainer`, `TableHeader`, `TableHeaderCell`, `TableRow`, `Tag`, `TextField`, `TitleBar`, `Toaster`, `Tooltip`, `dismissToast`, `formatCount`, `formatMoney`, `toast`
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
'use client';
|
|
46
|
+
import { useEffect, useRef, useState } from 'react';
|
|
47
|
+
import Toaster, { toast, dismissToast } from 'oneslash-design-system/components/toaster';
|
|
48
|
+
import TitleBar from 'oneslash-design-system/components/titleBar';
|
|
49
|
+
import Paragraph from 'oneslash-design-system/components/paragraph';
|
|
50
|
+
import Skeleton from 'oneslash-design-system/components/skeleton';
|
|
51
|
+
import Surface from 'oneslash-design-system/components/surface';
|
|
52
|
+
import EmptyBox from 'oneslash-design-system/components/emptyBox';
|
|
53
|
+
import Button from 'oneslash-design-system/components/button';
|
|
54
|
+
import IconButton from 'oneslash-design-system/components/iconButton';
|
|
55
|
+
import Tooltip from 'oneslash-design-system/components/tooltip';
|
|
56
|
+
import TextField from 'oneslash-design-system/components/textField';
|
|
57
|
+
import Popover from 'oneslash-design-system/components/popover';
|
|
58
|
+
import Menu from 'oneslash-design-system/components/menu';
|
|
59
|
+
import MenuItem from 'oneslash-design-system/components/menuItem';
|
|
60
|
+
import Line from 'oneslash-design-system/components/line';
|
|
61
|
+
import Tag from 'oneslash-design-system/components/tag';
|
|
62
|
+
import Pagination from 'oneslash-design-system/components/pagination';
|
|
63
|
+
import TableContainer from 'oneslash-design-system/components/tableContainer';
|
|
64
|
+
import TableHeader from 'oneslash-design-system/components/tableHeader';
|
|
65
|
+
import TableHeaderCell from 'oneslash-design-system/components/tableHeaderCell';
|
|
66
|
+
import TableRow from 'oneslash-design-system/components/tableRow';
|
|
67
|
+
import TableCell from 'oneslash-design-system/components/tableCell';
|
|
68
|
+
import { formatCount, formatMoney } from 'oneslash-design-system/components/formatNumber';
|
|
69
|
+
import { Center, Cluster, Stack } from 'oneslash-design-system/components/layout';
|
|
70
|
+
|
|
71
|
+
type Status = 'Paid' | 'Past due' | 'Refunded';
|
|
72
|
+
type Invoice = { id: string; number: string; customer: string; day: number; date: string; cents: number; status: Status };
|
|
73
|
+
type SortBy = 'customer' | 'date' | 'amount';
|
|
74
|
+
type Focusable = HTMLButtonElement | HTMLAnchorElement | null;
|
|
75
|
+
|
|
76
|
+
const customers = ['Northwind', 'Harbor Studio', 'Lumen Labs', 'Pine and Co', 'Cedar Works', 'Bright Dental', 'Maple Design', 'Atlas Freight', 'Juniper Foods', 'Orchard Legal'];
|
|
77
|
+
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
78
|
+
|
|
79
|
+
// A real app asks the server for one page. This makes the 290 invoices the server would hold, newest first.
|
|
80
|
+
const all: Invoice[] = Array.from({ length: 290 }, (_, i) => {
|
|
81
|
+
const day = new Date(Date.UTC(2026, 10, 28 - i));
|
|
82
|
+
return {
|
|
83
|
+
id: `inv-${i}`,
|
|
84
|
+
number: `INV-${1290 - i}`,
|
|
85
|
+
customer: customers[(i * 7 + 3) % customers.length],
|
|
86
|
+
day: i,
|
|
87
|
+
date: `${monthNames[day.getUTCMonth()]} ${day.getUTCDate()}, ${day.getUTCFullYear()}`,
|
|
88
|
+
cents: 2400 + ((i * 5303) % 40000),
|
|
89
|
+
status: i % 11 === 4 ? 'Past due' : i % 9 === 5 ? 'Refunded' : 'Paid',
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const statuses: Status[] = ['Paid', 'Past due', 'Refunded'];
|
|
94
|
+
const PAGE_SIZE = 25;
|
|
95
|
+
|
|
96
|
+
export default function DataTablePagePaged() {
|
|
97
|
+
const [query, setQuery] = useState('');
|
|
98
|
+
const [status, setStatus] = useState<Status | null>(null);
|
|
99
|
+
// Opens newest first.
|
|
100
|
+
const [sort, setSort] = useState<{ by: SortBy; dir: 'ascending' | 'descending' }>({ by: 'date', dir: 'descending' });
|
|
101
|
+
const [page, setPage] = useState(1);
|
|
102
|
+
const [loading, setLoading] = useState(false);
|
|
103
|
+
const [failed, setFailed] = useState(false);
|
|
104
|
+
|
|
105
|
+
const [searchAnchor, setSearchAnchor] = useState<HTMLElement | null>(null);
|
|
106
|
+
const [searchOpen, setSearchOpen] = useState(false);
|
|
107
|
+
const [filterAnchor, setFilterAnchor] = useState<HTMLElement | null>(null);
|
|
108
|
+
const [filterOpen, setFilterOpen] = useState(false);
|
|
109
|
+
const [menuRow, setMenuRow] = useState<Invoice | null>(null);
|
|
110
|
+
const [menuAnchor, setMenuAnchor] = useState<HTMLElement | null>(null);
|
|
111
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
112
|
+
|
|
113
|
+
const timer = useRef<ReturnType<typeof setTimeout>>();
|
|
114
|
+
const failedOnce = useRef(false);
|
|
115
|
+
const mounted = useRef(true);
|
|
116
|
+
const toastIds = useRef<string[]>([]);
|
|
117
|
+
const searchButton = useRef<Focusable>(null);
|
|
118
|
+
|
|
119
|
+
// On unmount it stops the request and closes every toast it opened.
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
mounted.current = true;
|
|
122
|
+
return () => {
|
|
123
|
+
mounted.current = false;
|
|
124
|
+
clearTimeout(timer.current);
|
|
125
|
+
toastIds.current.forEach((id) => dismissToast(id));
|
|
126
|
+
toastIds.current = [];
|
|
127
|
+
};
|
|
128
|
+
}, []);
|
|
129
|
+
|
|
130
|
+
// The server sorts and filters, so the page asks it again for every change. The header and the pager stay while it answers.
|
|
131
|
+
function request(forPage: number) {
|
|
132
|
+
clearTimeout(timer.current);
|
|
133
|
+
setLoading(true);
|
|
134
|
+
timer.current = setTimeout(() => {
|
|
135
|
+
setLoading(false);
|
|
136
|
+
// The failure stays on screen, with its Button, until the new answer arrives.
|
|
137
|
+
setFailed(false);
|
|
138
|
+
// Stands in for a network failure, once: page 3 fails the first time it is asked for.
|
|
139
|
+
if (forPage === 3 && !failedOnce.current) {
|
|
140
|
+
failedOnce.current = true;
|
|
141
|
+
setFailed(true);
|
|
142
|
+
}
|
|
143
|
+
}, 600);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// A sort, a search or a filter goes back to page 1: page 4 of the old list means nothing in the new one.
|
|
147
|
+
// A real app waits a moment after the last key before it asks the server for a search.
|
|
148
|
+
function change(next: { query?: string; status?: Status | null; sort?: { by: SortBy; dir: 'ascending' | 'descending' } }) {
|
|
149
|
+
if (next.query !== undefined) setQuery(next.query);
|
|
150
|
+
if (next.status !== undefined) setStatus(next.status);
|
|
151
|
+
if (next.sort !== undefined) setSort(next.sort);
|
|
152
|
+
setPage(1);
|
|
153
|
+
request(1);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function goToPage(next: number) {
|
|
157
|
+
setPage(next);
|
|
158
|
+
request(next);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// The first press on a column sorts it A to Z, and the next one reverses it.
|
|
162
|
+
function handleSort(by: SortBy) {
|
|
163
|
+
change({ sort: { by, dir: sort.by === by && sort.dir === 'ascending' ? 'descending' : 'ascending' } });
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const words = query.trim();
|
|
167
|
+
const found = all
|
|
168
|
+
.filter((inv) => (status === null || inv.status === status) && (words === '' || `${inv.number} ${inv.customer}`.toLowerCase().includes(words.toLowerCase())))
|
|
169
|
+
.sort((a, b) => {
|
|
170
|
+
const order = sort.by === 'customer' ? a.customer.localeCompare(b.customer) : sort.by === 'amount' ? a.cents - b.cents : b.day - a.day;
|
|
171
|
+
return (sort.dir === 'ascending' ? order : -order) || a.day - b.day;
|
|
172
|
+
});
|
|
173
|
+
const pageCount = Math.ceil(found.length / PAGE_SIZE);
|
|
174
|
+
const rows = found.slice((page - 1) * PAGE_SIZE, page * PAGE_SIZE);
|
|
175
|
+
const filtering = words !== '' || status !== null;
|
|
176
|
+
|
|
177
|
+
// The line under the title says the total, and what the search and the filter leave.
|
|
178
|
+
const matching = [words !== '' ? `"${words}"` : null, status !== null ? status.toLowerCase() : null].filter(Boolean).join(', ');
|
|
179
|
+
const countLine = filtering ? `${formatCount(found.length)} of ${formatCount(all.length)} invoices match ${matching}.` : `${formatCount(all.length)} ${all.length === 1 ? 'invoice' : 'invoices'}`;
|
|
180
|
+
|
|
181
|
+
// The Button clears the words and the filter. Focus goes to the search button, because the field is in the closed Popover.
|
|
182
|
+
function clearFilter() {
|
|
183
|
+
change({ query: '', status: null });
|
|
184
|
+
searchButton.current?.focus();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function openMenu(inv: Invoice, anchor: HTMLElement) {
|
|
188
|
+
if (menuOpen && menuRow?.id === inv.id) {
|
|
189
|
+
setMenuOpen(false);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
setMenuRow(inv);
|
|
193
|
+
setMenuAnchor(anchor);
|
|
194
|
+
setMenuOpen(true);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function choose(action: (inv: Invoice) => void) {
|
|
198
|
+
if (!menuRow) return;
|
|
199
|
+
const inv = menuRow;
|
|
200
|
+
setMenuOpen(false);
|
|
201
|
+
action(inv);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function sendReceipt(inv: Invoice) {
|
|
205
|
+
toastIds.current.push(toast({ type: 'success', message: `Receipt for ${inv.number} sent to ${inv.customer}.` }));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Only reads 'copied' when the number reached the clipboard.
|
|
209
|
+
function copyNumber(inv: Invoice) {
|
|
210
|
+
const failedCopy = () => {
|
|
211
|
+
if (!mounted.current) return;
|
|
212
|
+
toastIds.current.push(toast({ type: 'error', message: `Copying is blocked here. Select the number and copy it: ${inv.number}` }));
|
|
213
|
+
};
|
|
214
|
+
if (!navigator.clipboard) return failedCopy();
|
|
215
|
+
navigator.clipboard.writeText(inv.number).then(() => {
|
|
216
|
+
if (!mounted.current) return;
|
|
217
|
+
toastIds.current.push(toast({ type: 'success', message: 'Invoice number copied.' }));
|
|
218
|
+
}, failedCopy);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// The sorted column has `sort`; the others leave it unset, so exactly one header carries `aria-sort`.
|
|
222
|
+
function sortHeader(by: SortBy, label: string, width?: string, align?: 'right') {
|
|
223
|
+
const active = sort.by === by;
|
|
224
|
+
const button = (
|
|
225
|
+
<Button
|
|
226
|
+
size="small"
|
|
227
|
+
type="tertiary"
|
|
228
|
+
state={active ? 'selected' : 'enabled'}
|
|
229
|
+
label={label}
|
|
230
|
+
decoIcon={active ? (sort.dir === 'ascending' ? 'ArrowUp' : 'ArrowDown') : undefined}
|
|
231
|
+
onClickButton={() => handleSort(by)}
|
|
232
|
+
/>
|
|
233
|
+
);
|
|
234
|
+
return (
|
|
235
|
+
<TableHeaderCell width={width} align={align} sort={active ? sort.dir : undefined}>
|
|
236
|
+
{align === 'right' ? <Cluster justify="end">{button}</Cluster> : button}
|
|
237
|
+
</TableHeaderCell>
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function statusTag(s: Status) {
|
|
242
|
+
// The case 'Status in a row': a dot and a color only for a state that needs action.
|
|
243
|
+
return s === 'Past due' ? <Tag variant="dot" size="small" color="error" label={s} /> : <Tag variant="textOnly" size="small" label={s} />;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return (
|
|
247
|
+
<Center width="page">
|
|
248
|
+
{/* A real app mounts one Toaster near its root. */}
|
|
249
|
+
<Toaster />
|
|
250
|
+
<Stack gap="large">
|
|
251
|
+
<Stack gap="small">
|
|
252
|
+
<TitleBar title="Invoices" id="data-table-page-paged-top" />
|
|
253
|
+
{loading ? <Skeleton variant="text" width="medium" /> : <Paragraph tone="secondary" size="body2" measure="none">{countLine}</Paragraph>}
|
|
254
|
+
</Stack>
|
|
255
|
+
|
|
256
|
+
{/* Search and the filter sit above the table, as the cases 'Collapsible search' and 'Filter menu' say. */}
|
|
257
|
+
<Cluster gap="small" justify="end">
|
|
258
|
+
<Tooltip title={words !== '' ? `Search: ${words}` : 'Search'}>
|
|
259
|
+
<IconButton
|
|
260
|
+
ref={searchButton}
|
|
261
|
+
color={words !== '' ? 'secondary' : 'iconOnly'}
|
|
262
|
+
state={searchOpen ? 'selected' : 'enabled'}
|
|
263
|
+
size="small"
|
|
264
|
+
iconName="Search"
|
|
265
|
+
ariaLabel={words !== '' ? `Search: ${words}` : 'Search'}
|
|
266
|
+
aria-haspopup="dialog"
|
|
267
|
+
aria-expanded={searchOpen}
|
|
268
|
+
onClick={(e: { currentTarget: HTMLElement }) => {
|
|
269
|
+
setSearchAnchor(e.currentTarget);
|
|
270
|
+
setSearchOpen((o) => !o);
|
|
271
|
+
}}
|
|
272
|
+
/>
|
|
273
|
+
</Tooltip>
|
|
274
|
+
<Tooltip title={status ? status : 'Filter by status'}>
|
|
275
|
+
<IconButton
|
|
276
|
+
color={status ? 'secondary' : 'iconOnly'}
|
|
277
|
+
state={filterOpen ? 'selected' : 'enabled'}
|
|
278
|
+
size="small"
|
|
279
|
+
iconName="ListFilter"
|
|
280
|
+
ariaLabel={status ? `Filter by status: ${status}` : 'Filter by status'}
|
|
281
|
+
aria-haspopup="menu"
|
|
282
|
+
aria-expanded={filterOpen}
|
|
283
|
+
onClick={(e: { currentTarget: HTMLElement }) => {
|
|
284
|
+
setFilterAnchor(e.currentTarget);
|
|
285
|
+
setFilterOpen((o) => !o);
|
|
286
|
+
}}
|
|
287
|
+
/>
|
|
288
|
+
</Tooltip>
|
|
289
|
+
</Cluster>
|
|
290
|
+
|
|
291
|
+
{all.length === 0 ? (
|
|
292
|
+
// A new workspace has none. The case 'Empty list' says what to write.
|
|
293
|
+
<Surface minHeight="tall" align="center">
|
|
294
|
+
<EmptyBox size="large" text="No invoices yet" secondText="Your first invoice appears here after your first payment." />
|
|
295
|
+
</Surface>
|
|
296
|
+
) : failed ? (
|
|
297
|
+
// The table's place: the header goes too, because nothing is left to sort. The pager stays.
|
|
298
|
+
<Surface minHeight="tall" align="center">
|
|
299
|
+
<EmptyBox
|
|
300
|
+
size="large"
|
|
301
|
+
icon="CloudOff"
|
|
302
|
+
text="Could not load invoices"
|
|
303
|
+
secondText="Check your connection and try again."
|
|
304
|
+
action={<Button size="medium" type="primary" state="enabled" label="Try Again" loading={loading} onClickButton={() => request(page)} />}
|
|
305
|
+
/>
|
|
306
|
+
</Surface>
|
|
307
|
+
) : !loading && found.length === 0 ? (
|
|
308
|
+
<Surface minHeight="tall" align="center">
|
|
309
|
+
<EmptyBox
|
|
310
|
+
size="large"
|
|
311
|
+
icon="Search"
|
|
312
|
+
text={words !== '' ? `No ${status ? `${status.toLowerCase()} ` : ''}invoices match "${words}"` : `No ${status?.toLowerCase()} invoices`}
|
|
313
|
+
secondText={words !== '' ? 'Check the spelling, or search for fewer words.' : 'Choose another status, or clear the filter.'}
|
|
314
|
+
action={<Button size="medium" type="secondary" state="enabled" label={status !== null ? 'Clear Filter' : 'Clear Search'} onClickButton={clearFilter} />}
|
|
315
|
+
/>
|
|
316
|
+
</Surface>
|
|
317
|
+
) : (
|
|
318
|
+
<TableContainer aria-label="Invoices" minWidth="640px">
|
|
319
|
+
<TableHeader>
|
|
320
|
+
<TableRow>
|
|
321
|
+
<TableHeaderCell width="110px">Invoice</TableHeaderCell>
|
|
322
|
+
{sortHeader('customer', 'Customer')}
|
|
323
|
+
{sortHeader('date', 'Date', '140px')}
|
|
324
|
+
{sortHeader('amount', 'Amount', '130px', 'right')}
|
|
325
|
+
<TableHeaderCell width="120px">Status</TableHeaderCell>
|
|
326
|
+
<TableHeaderCell width="90px">Actions</TableHeaderCell>
|
|
327
|
+
</TableRow>
|
|
328
|
+
</TableHeader>
|
|
329
|
+
{loading
|
|
330
|
+
? Array.from({ length: 10 }, (_, i) => (
|
|
331
|
+
<TableRow key={i}>
|
|
332
|
+
{/* One status for the ten rows: the Skeleton with a label is announced once. */}
|
|
333
|
+
<TableCell width="110px">{i === 0 ? <Skeleton label="Loading invoices" /> : <Skeleton />}</TableCell>
|
|
334
|
+
<TableCell><Skeleton width="large" /></TableCell>
|
|
335
|
+
<TableCell width="140px"><Skeleton /></TableCell>
|
|
336
|
+
<TableCell width="130px"><Skeleton /></TableCell>
|
|
337
|
+
<TableCell width="120px"><Skeleton width="large" /></TableCell>
|
|
338
|
+
<TableCell width="90px"><Skeleton width="small" /></TableCell>
|
|
339
|
+
</TableRow>
|
|
340
|
+
))
|
|
341
|
+
: rows.map((inv) => (
|
|
342
|
+
<TableRow key={inv.id}>
|
|
343
|
+
<TableCell width="110px">{inv.number}</TableCell>
|
|
344
|
+
<TableCell>{inv.customer}</TableCell>
|
|
345
|
+
<TableCell width="140px">{inv.date}</TableCell>
|
|
346
|
+
{/* A table shows the exact amount, right-aligned. */}
|
|
347
|
+
<TableCell width="130px" align="right">{formatMoney(inv.cents / 100)}</TableCell>
|
|
348
|
+
<TableCell width="120px">{statusTag(inv.status)}</TableCell>
|
|
349
|
+
<TableCell width="90px">
|
|
350
|
+
<Tooltip title="More">
|
|
351
|
+
<IconButton
|
|
352
|
+
color="iconOnly"
|
|
353
|
+
state={menuOpen && menuRow?.id === inv.id ? 'selected' : 'enabled'}
|
|
354
|
+
size="small"
|
|
355
|
+
iconName="Ellipsis"
|
|
356
|
+
ariaLabel={`More actions for ${inv.number}`}
|
|
357
|
+
aria-haspopup="menu"
|
|
358
|
+
aria-expanded={menuOpen && menuRow?.id === inv.id}
|
|
359
|
+
onClick={(e: { currentTarget: HTMLElement }) => openMenu(inv, e.currentTarget)}
|
|
360
|
+
/>
|
|
361
|
+
</Tooltip>
|
|
362
|
+
</TableCell>
|
|
363
|
+
</TableRow>
|
|
364
|
+
))}
|
|
365
|
+
</TableContainer>
|
|
366
|
+
)}
|
|
367
|
+
|
|
368
|
+
{/* It shows nothing with one page. A real app also passes `getHref`, so each page has an address. */}
|
|
369
|
+
<Pagination aria-label="Invoice pages" page={page} pageCount={pageCount} onPageChange={goToPage} scrollTargetId="data-table-page-paged-top" />
|
|
370
|
+
</Stack>
|
|
371
|
+
|
|
372
|
+
<Popover open={searchOpen} anchorEl={searchAnchor} onClose={() => setSearchOpen(false)} returnFocus>
|
|
373
|
+
<Menu width={288}>
|
|
374
|
+
<TextField
|
|
375
|
+
id="data-table-page-paged-search"
|
|
376
|
+
type="search"
|
|
377
|
+
size="small"
|
|
378
|
+
autoFocus
|
|
379
|
+
aria-label="Search invoices"
|
|
380
|
+
placeholder="Search invoices"
|
|
381
|
+
value={query}
|
|
382
|
+
onChange={(e) => change({ query: e.target.value })}
|
|
383
|
+
onKeyDown={(e) => {
|
|
384
|
+
// Escape clears the search and closes. Enter closes and keeps it.
|
|
385
|
+
if (e.key === 'Escape') {
|
|
386
|
+
e.preventDefault();
|
|
387
|
+
change({ query: '' });
|
|
388
|
+
setSearchOpen(false);
|
|
389
|
+
} else if (e.key === 'Enter') {
|
|
390
|
+
e.preventDefault();
|
|
391
|
+
setSearchOpen(false);
|
|
392
|
+
}
|
|
393
|
+
}}
|
|
394
|
+
/>
|
|
395
|
+
</Menu>
|
|
396
|
+
</Popover>
|
|
397
|
+
<Popover open={filterOpen} anchorEl={filterAnchor} onClose={() => setFilterOpen(false)} returnFocus>
|
|
398
|
+
<Menu navigable aria-label="Filter by status" width={200}>
|
|
399
|
+
{statuses.map((s) => (
|
|
400
|
+
<MenuItem
|
|
401
|
+
key={s}
|
|
402
|
+
size="small"
|
|
403
|
+
label={s === 'Past due' ? 'Past Due' : s}
|
|
404
|
+
isSelected={s === status}
|
|
405
|
+
role="menuitemradio"
|
|
406
|
+
onClick={() => {
|
|
407
|
+
setFilterOpen(false);
|
|
408
|
+
change({ status: s });
|
|
409
|
+
}}
|
|
410
|
+
/>
|
|
411
|
+
))}
|
|
412
|
+
{status && <Line />}
|
|
413
|
+
{status && (
|
|
414
|
+
<MenuItem
|
|
415
|
+
size="small"
|
|
416
|
+
iconName="X"
|
|
417
|
+
label="Clear Filter"
|
|
418
|
+
onClick={() => {
|
|
419
|
+
setFilterOpen(false);
|
|
420
|
+
change({ status: null });
|
|
421
|
+
}}
|
|
422
|
+
/>
|
|
423
|
+
)}
|
|
424
|
+
</Menu>
|
|
425
|
+
</Popover>
|
|
426
|
+
<Popover open={menuOpen} anchorEl={menuAnchor} onClose={() => setMenuOpen(false)} returnFocus>
|
|
427
|
+
<Menu navigable aria-label={menuRow ? `Actions for ${menuRow.number}` : 'Invoice actions'} width={220}>
|
|
428
|
+
<MenuItem size="small" iconName="Copy" label="Copy Invoice Number" onClick={() => choose(copyNumber)} />
|
|
429
|
+
<MenuItem size="small" iconName="Mail" label="Send Receipt" onClick={() => choose(sendReceipt)} />
|
|
430
|
+
</Menu>
|
|
431
|
+
</Popover>
|
|
432
|
+
</Center>
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
## Option: All rows, sorted in place
|
|
438
|
+
|
|
439
|
+
Use when: There are under about 50 rows, so the page holds them all and sorts and filters them at once.
|
|
440
|
+
|
|
441
|
+
Uses: `Button`, `Center`, `Cluster`, `EmptyBox`, `IconButton`, `Line`, `Menu`, `MenuItem`, `Paragraph`, `Popover`, `Stack`, `Surface`, `TableCell`, `TableContainer`, `TableHeader`, `TableHeaderCell`, `TableRow`, `Tag`, `TextField`, `TitleBar`, `Toaster`, `Tooltip`, `dismissToast`, `formatCount`, `formatMoney`, `toast`
|
|
442
|
+
|
|
443
|
+
```tsx
|
|
444
|
+
'use client';
|
|
445
|
+
import { useEffect, useRef, useState } from 'react';
|
|
446
|
+
import Toaster, { toast, dismissToast } from 'oneslash-design-system/components/toaster';
|
|
447
|
+
import TitleBar from 'oneslash-design-system/components/titleBar';
|
|
448
|
+
import Paragraph from 'oneslash-design-system/components/paragraph';
|
|
449
|
+
import Surface from 'oneslash-design-system/components/surface';
|
|
450
|
+
import EmptyBox from 'oneslash-design-system/components/emptyBox';
|
|
451
|
+
import Button from 'oneslash-design-system/components/button';
|
|
452
|
+
import IconButton from 'oneslash-design-system/components/iconButton';
|
|
453
|
+
import Tooltip from 'oneslash-design-system/components/tooltip';
|
|
454
|
+
import TextField from 'oneslash-design-system/components/textField';
|
|
455
|
+
import Popover from 'oneslash-design-system/components/popover';
|
|
456
|
+
import Menu from 'oneslash-design-system/components/menu';
|
|
457
|
+
import MenuItem from 'oneslash-design-system/components/menuItem';
|
|
458
|
+
import Line from 'oneslash-design-system/components/line';
|
|
459
|
+
import Tag from 'oneslash-design-system/components/tag';
|
|
460
|
+
import TableContainer from 'oneslash-design-system/components/tableContainer';
|
|
461
|
+
import TableHeader from 'oneslash-design-system/components/tableHeader';
|
|
462
|
+
import TableHeaderCell from 'oneslash-design-system/components/tableHeaderCell';
|
|
463
|
+
import TableRow from 'oneslash-design-system/components/tableRow';
|
|
464
|
+
import TableCell from 'oneslash-design-system/components/tableCell';
|
|
465
|
+
import { formatCount, formatMoney } from 'oneslash-design-system/components/formatNumber';
|
|
466
|
+
import { Center, Cluster, Stack } from 'oneslash-design-system/components/layout';
|
|
467
|
+
|
|
468
|
+
type Status = 'Paid' | 'Past due' | 'Refunded';
|
|
469
|
+
type Invoice = { id: string; number: string; customer: string; day: number; date: string; cents: number; status: Status };
|
|
470
|
+
type SortBy = 'customer' | 'date' | 'amount';
|
|
471
|
+
type Focusable = HTMLButtonElement | HTMLAnchorElement | null;
|
|
472
|
+
|
|
473
|
+
const customers = ['Northwind', 'Harbor Studio', 'Lumen Labs', 'Pine and Co', 'Cedar Works', 'Bright Dental', 'Maple Design', 'Atlas Freight', 'Juniper Foods', 'Orchard Legal'];
|
|
474
|
+
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
475
|
+
|
|
476
|
+
// A real app reads all of them in one request: 42 is few enough to hold, sort and filter in the page.
|
|
477
|
+
const all: Invoice[] = Array.from({ length: 42 }, (_, i) => {
|
|
478
|
+
const day = new Date(Date.UTC(2026, 10, 28 - i));
|
|
479
|
+
return {
|
|
480
|
+
id: `inv-${i}`,
|
|
481
|
+
number: `INV-${1042 - i}`,
|
|
482
|
+
customer: customers[(i * 7 + 3) % customers.length],
|
|
483
|
+
day: i,
|
|
484
|
+
date: `${monthNames[day.getUTCMonth()]} ${day.getUTCDate()}, ${day.getUTCFullYear()}`,
|
|
485
|
+
cents: 2400 + ((i * 5303) % 40000),
|
|
486
|
+
status: i % 11 === 4 ? 'Past due' : i % 9 === 5 ? 'Refunded' : 'Paid',
|
|
487
|
+
};
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
const statuses: Status[] = ['Paid', 'Past due', 'Refunded'];
|
|
491
|
+
|
|
492
|
+
export default function DataTablePageAllRows() {
|
|
493
|
+
const [query, setQuery] = useState('');
|
|
494
|
+
const [status, setStatus] = useState<Status | null>(null);
|
|
495
|
+
// Opens newest first.
|
|
496
|
+
const [sort, setSort] = useState<{ by: SortBy; dir: 'ascending' | 'descending' }>({ by: 'date', dir: 'descending' });
|
|
497
|
+
|
|
498
|
+
const [searchAnchor, setSearchAnchor] = useState<HTMLElement | null>(null);
|
|
499
|
+
const [searchOpen, setSearchOpen] = useState(false);
|
|
500
|
+
const [filterAnchor, setFilterAnchor] = useState<HTMLElement | null>(null);
|
|
501
|
+
const [filterOpen, setFilterOpen] = useState(false);
|
|
502
|
+
const [menuRow, setMenuRow] = useState<Invoice | null>(null);
|
|
503
|
+
const [menuAnchor, setMenuAnchor] = useState<HTMLElement | null>(null);
|
|
504
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
505
|
+
|
|
506
|
+
const mounted = useRef(true);
|
|
507
|
+
const toastIds = useRef<string[]>([]);
|
|
508
|
+
const searchButton = useRef<Focusable>(null);
|
|
509
|
+
|
|
510
|
+
// On unmount it closes every toast it opened.
|
|
511
|
+
useEffect(() => {
|
|
512
|
+
mounted.current = true;
|
|
513
|
+
return () => {
|
|
514
|
+
mounted.current = false;
|
|
515
|
+
toastIds.current.forEach((id) => dismissToast(id));
|
|
516
|
+
toastIds.current = [];
|
|
517
|
+
};
|
|
518
|
+
}, []);
|
|
519
|
+
|
|
520
|
+
// The first press on a column sorts it A to Z, and the next one reverses it. The rows are here, so it answers at once.
|
|
521
|
+
function handleSort(by: SortBy) {
|
|
522
|
+
setSort((current) => ({ by, dir: current.by === by && current.dir === 'ascending' ? 'descending' : 'ascending' }));
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const words = query.trim();
|
|
526
|
+
const rows = all
|
|
527
|
+
.filter((inv) => (status === null || inv.status === status) && (words === '' || `${inv.number} ${inv.customer}`.toLowerCase().includes(words.toLowerCase())))
|
|
528
|
+
.sort((a, b) => {
|
|
529
|
+
const order = sort.by === 'customer' ? a.customer.localeCompare(b.customer) : sort.by === 'amount' ? a.cents - b.cents : b.day - a.day;
|
|
530
|
+
return (sort.dir === 'ascending' ? order : -order) || a.day - b.day;
|
|
531
|
+
});
|
|
532
|
+
const filtering = words !== '' || status !== null;
|
|
533
|
+
|
|
534
|
+
// The line under the title says the total, and what the search and the filter leave.
|
|
535
|
+
const matching = [words !== '' ? `"${words}"` : null, status !== null ? status.toLowerCase() : null].filter(Boolean).join(', ');
|
|
536
|
+
const countLine = filtering ? `${formatCount(rows.length)} of ${formatCount(all.length)} invoices match ${matching}.` : `${formatCount(all.length)} ${all.length === 1 ? 'invoice' : 'invoices'}`;
|
|
537
|
+
|
|
538
|
+
// The Button clears the words and the filter. Focus goes to the search button, because the field is in the closed Popover.
|
|
539
|
+
function clearFilter() {
|
|
540
|
+
setQuery('');
|
|
541
|
+
setStatus(null);
|
|
542
|
+
searchButton.current?.focus();
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function openMenu(inv: Invoice, anchor: HTMLElement) {
|
|
546
|
+
if (menuOpen && menuRow?.id === inv.id) {
|
|
547
|
+
setMenuOpen(false);
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
setMenuRow(inv);
|
|
551
|
+
setMenuAnchor(anchor);
|
|
552
|
+
setMenuOpen(true);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function choose(action: (inv: Invoice) => void) {
|
|
556
|
+
if (!menuRow) return;
|
|
557
|
+
const inv = menuRow;
|
|
558
|
+
setMenuOpen(false);
|
|
559
|
+
action(inv);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function sendReceipt(inv: Invoice) {
|
|
563
|
+
toastIds.current.push(toast({ type: 'success', message: `Receipt for ${inv.number} sent to ${inv.customer}.` }));
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// Only reads 'copied' when the number reached the clipboard.
|
|
567
|
+
function copyNumber(inv: Invoice) {
|
|
568
|
+
const failedCopy = () => {
|
|
569
|
+
if (!mounted.current) return;
|
|
570
|
+
toastIds.current.push(toast({ type: 'error', message: `Copying is blocked here. Select the number and copy it: ${inv.number}` }));
|
|
571
|
+
};
|
|
572
|
+
if (!navigator.clipboard) return failedCopy();
|
|
573
|
+
navigator.clipboard.writeText(inv.number).then(() => {
|
|
574
|
+
if (!mounted.current) return;
|
|
575
|
+
toastIds.current.push(toast({ type: 'success', message: 'Invoice number copied.' }));
|
|
576
|
+
}, failedCopy);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// The sorted column has `sort`; the others leave it unset, so exactly one header carries `aria-sort`.
|
|
580
|
+
function sortHeader(by: SortBy, label: string, width?: string, align?: 'right') {
|
|
581
|
+
const active = sort.by === by;
|
|
582
|
+
const button = (
|
|
583
|
+
<Button
|
|
584
|
+
size="small"
|
|
585
|
+
type="tertiary"
|
|
586
|
+
state={active ? 'selected' : 'enabled'}
|
|
587
|
+
label={label}
|
|
588
|
+
decoIcon={active ? (sort.dir === 'ascending' ? 'ArrowUp' : 'ArrowDown') : undefined}
|
|
589
|
+
onClickButton={() => handleSort(by)}
|
|
590
|
+
/>
|
|
591
|
+
);
|
|
592
|
+
return (
|
|
593
|
+
<TableHeaderCell width={width} align={align} sort={active ? sort.dir : undefined}>
|
|
594
|
+
{align === 'right' ? <Cluster justify="end">{button}</Cluster> : button}
|
|
595
|
+
</TableHeaderCell>
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function statusTag(s: Status) {
|
|
600
|
+
// The case 'Status in a row': a dot and a color only for a state that needs action.
|
|
601
|
+
return s === 'Past due' ? <Tag variant="dot" size="small" color="error" label={s} /> : <Tag variant="textOnly" size="small" label={s} />;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
return (
|
|
605
|
+
<Center width="page">
|
|
606
|
+
{/* A real app mounts one Toaster near its root. */}
|
|
607
|
+
<Toaster />
|
|
608
|
+
<Stack gap="large">
|
|
609
|
+
<Stack gap="small">
|
|
610
|
+
<TitleBar title="Invoices" />
|
|
611
|
+
<Paragraph tone="secondary" size="body2" measure="none">{countLine}</Paragraph>
|
|
612
|
+
</Stack>
|
|
613
|
+
|
|
614
|
+
{/* Search and the filter sit above the table, as the cases 'Collapsible search' and 'Filter menu' say. */}
|
|
615
|
+
<Cluster gap="small" justify="end">
|
|
616
|
+
<Tooltip title={words !== '' ? `Search: ${words}` : 'Search'}>
|
|
617
|
+
<IconButton
|
|
618
|
+
ref={searchButton}
|
|
619
|
+
color={words !== '' ? 'secondary' : 'iconOnly'}
|
|
620
|
+
state={searchOpen ? 'selected' : 'enabled'}
|
|
621
|
+
size="small"
|
|
622
|
+
iconName="Search"
|
|
623
|
+
ariaLabel={words !== '' ? `Search: ${words}` : 'Search'}
|
|
624
|
+
aria-haspopup="dialog"
|
|
625
|
+
aria-expanded={searchOpen}
|
|
626
|
+
onClick={(e: { currentTarget: HTMLElement }) => {
|
|
627
|
+
setSearchAnchor(e.currentTarget);
|
|
628
|
+
setSearchOpen((o) => !o);
|
|
629
|
+
}}
|
|
630
|
+
/>
|
|
631
|
+
</Tooltip>
|
|
632
|
+
<Tooltip title={status ? status : 'Filter by status'}>
|
|
633
|
+
<IconButton
|
|
634
|
+
color={status ? 'secondary' : 'iconOnly'}
|
|
635
|
+
state={filterOpen ? 'selected' : 'enabled'}
|
|
636
|
+
size="small"
|
|
637
|
+
iconName="ListFilter"
|
|
638
|
+
ariaLabel={status ? `Filter by status: ${status}` : 'Filter by status'}
|
|
639
|
+
aria-haspopup="menu"
|
|
640
|
+
aria-expanded={filterOpen}
|
|
641
|
+
onClick={(e: { currentTarget: HTMLElement }) => {
|
|
642
|
+
setFilterAnchor(e.currentTarget);
|
|
643
|
+
setFilterOpen((o) => !o);
|
|
644
|
+
}}
|
|
645
|
+
/>
|
|
646
|
+
</Tooltip>
|
|
647
|
+
</Cluster>
|
|
648
|
+
|
|
649
|
+
{all.length === 0 ? (
|
|
650
|
+
// A new workspace has none. The case 'Empty list' says what to write.
|
|
651
|
+
<Surface minHeight="tall" align="center">
|
|
652
|
+
<EmptyBox size="large" text="No invoices yet" secondText="Your first invoice appears here after your first payment." />
|
|
653
|
+
</Surface>
|
|
654
|
+
) : rows.length === 0 ? (
|
|
655
|
+
<Surface minHeight="tall" align="center">
|
|
656
|
+
<EmptyBox
|
|
657
|
+
size="large"
|
|
658
|
+
icon="Search"
|
|
659
|
+
text={words !== '' ? `No ${status ? `${status.toLowerCase()} ` : ''}invoices match "${words}"` : `No ${status?.toLowerCase()} invoices`}
|
|
660
|
+
secondText={words !== '' ? 'Check the spelling, or search for fewer words.' : 'Choose another status, or clear the filter.'}
|
|
661
|
+
action={<Button size="medium" type="secondary" state="enabled" label={status !== null ? 'Clear Filter' : 'Clear Search'} onClickButton={clearFilter} />}
|
|
662
|
+
/>
|
|
663
|
+
</Surface>
|
|
664
|
+
) : (
|
|
665
|
+
<TableContainer aria-label="Invoices" minWidth="640px">
|
|
666
|
+
<TableHeader>
|
|
667
|
+
<TableRow>
|
|
668
|
+
<TableHeaderCell width="110px">Invoice</TableHeaderCell>
|
|
669
|
+
{sortHeader('customer', 'Customer')}
|
|
670
|
+
{sortHeader('date', 'Date', '140px')}
|
|
671
|
+
{sortHeader('amount', 'Amount', '130px', 'right')}
|
|
672
|
+
<TableHeaderCell width="120px">Status</TableHeaderCell>
|
|
673
|
+
<TableHeaderCell width="90px">Actions</TableHeaderCell>
|
|
674
|
+
</TableRow>
|
|
675
|
+
</TableHeader>
|
|
676
|
+
{rows.map((inv) => (
|
|
677
|
+
<TableRow key={inv.id}>
|
|
678
|
+
<TableCell width="110px">{inv.number}</TableCell>
|
|
679
|
+
<TableCell>{inv.customer}</TableCell>
|
|
680
|
+
<TableCell width="140px">{inv.date}</TableCell>
|
|
681
|
+
{/* A table shows the exact amount, right-aligned. */}
|
|
682
|
+
<TableCell width="130px" align="right">{formatMoney(inv.cents / 100)}</TableCell>
|
|
683
|
+
<TableCell width="120px">{statusTag(inv.status)}</TableCell>
|
|
684
|
+
<TableCell width="90px">
|
|
685
|
+
<Tooltip title="More">
|
|
686
|
+
<IconButton
|
|
687
|
+
color="iconOnly"
|
|
688
|
+
state={menuOpen && menuRow?.id === inv.id ? 'selected' : 'enabled'}
|
|
689
|
+
size="small"
|
|
690
|
+
iconName="Ellipsis"
|
|
691
|
+
ariaLabel={`More actions for ${inv.number}`}
|
|
692
|
+
aria-haspopup="menu"
|
|
693
|
+
aria-expanded={menuOpen && menuRow?.id === inv.id}
|
|
694
|
+
onClick={(e: { currentTarget: HTMLElement }) => openMenu(inv, e.currentTarget)}
|
|
695
|
+
/>
|
|
696
|
+
</Tooltip>
|
|
697
|
+
</TableCell>
|
|
698
|
+
</TableRow>
|
|
699
|
+
))}
|
|
700
|
+
</TableContainer>
|
|
701
|
+
)}
|
|
702
|
+
</Stack>
|
|
703
|
+
|
|
704
|
+
<Popover open={searchOpen} anchorEl={searchAnchor} onClose={() => setSearchOpen(false)} returnFocus>
|
|
705
|
+
<Menu width={288}>
|
|
706
|
+
<TextField
|
|
707
|
+
id="data-table-page-all-rows-search"
|
|
708
|
+
type="search"
|
|
709
|
+
size="small"
|
|
710
|
+
autoFocus
|
|
711
|
+
aria-label="Search invoices"
|
|
712
|
+
placeholder="Search invoices"
|
|
713
|
+
value={query}
|
|
714
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
715
|
+
onKeyDown={(e) => {
|
|
716
|
+
// Escape clears the search and closes. Enter closes and keeps it.
|
|
717
|
+
if (e.key === 'Escape') {
|
|
718
|
+
e.preventDefault();
|
|
719
|
+
setQuery('');
|
|
720
|
+
setSearchOpen(false);
|
|
721
|
+
} else if (e.key === 'Enter') {
|
|
722
|
+
e.preventDefault();
|
|
723
|
+
setSearchOpen(false);
|
|
724
|
+
}
|
|
725
|
+
}}
|
|
726
|
+
/>
|
|
727
|
+
</Menu>
|
|
728
|
+
</Popover>
|
|
729
|
+
<Popover open={filterOpen} anchorEl={filterAnchor} onClose={() => setFilterOpen(false)} returnFocus>
|
|
730
|
+
<Menu navigable aria-label="Filter by status" width={200}>
|
|
731
|
+
{statuses.map((s) => (
|
|
732
|
+
<MenuItem
|
|
733
|
+
key={s}
|
|
734
|
+
size="small"
|
|
735
|
+
label={s === 'Past due' ? 'Past Due' : s}
|
|
736
|
+
isSelected={s === status}
|
|
737
|
+
role="menuitemradio"
|
|
738
|
+
onClick={() => {
|
|
739
|
+
setFilterOpen(false);
|
|
740
|
+
setStatus(s);
|
|
741
|
+
}}
|
|
742
|
+
/>
|
|
743
|
+
))}
|
|
744
|
+
{status && <Line />}
|
|
745
|
+
{status && (
|
|
746
|
+
<MenuItem
|
|
747
|
+
size="small"
|
|
748
|
+
iconName="X"
|
|
749
|
+
label="Clear Filter"
|
|
750
|
+
onClick={() => {
|
|
751
|
+
setFilterOpen(false);
|
|
752
|
+
setStatus(null);
|
|
753
|
+
}}
|
|
754
|
+
/>
|
|
755
|
+
)}
|
|
756
|
+
</Menu>
|
|
757
|
+
</Popover>
|
|
758
|
+
<Popover open={menuOpen} anchorEl={menuAnchor} onClose={() => setMenuOpen(false)} returnFocus>
|
|
759
|
+
<Menu navigable aria-label={menuRow ? `Actions for ${menuRow.number}` : 'Invoice actions'} width={220}>
|
|
760
|
+
<MenuItem size="small" iconName="Copy" label="Copy Invoice Number" onClick={() => choose(copyNumber)} />
|
|
761
|
+
<MenuItem size="small" iconName="Mail" label="Send Receipt" onClick={() => choose(sendReceipt)} />
|
|
762
|
+
</Menu>
|
|
763
|
+
</Popover>
|
|
764
|
+
</Center>
|
|
765
|
+
);
|
|
766
|
+
}
|
|
767
|
+
```
|
|
768
|
+
|
|
769
|
+
## Native
|
|
770
|
+
|
|
771
|
+
On Apple use a `Table` with `KeyPathComparator` for the sort columns, `.searchable` for search, and a `Menu` in the toolbar for the filter. On Android use a `LazyColumn` of rows with a `SearchBar`, and load the next page when the end comes into view.
|