@larakit/react 0.0.0-stage → 0.5.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/package.json +30 -4
- package/src/Cell.tsx +35 -0
- package/src/Dashboard.tsx +79 -0
- package/src/Field.tsx +132 -0
- package/src/FileInput.tsx +76 -0
- package/src/Form.tsx +116 -0
- package/src/Layout.tsx +250 -0
- package/src/PanelPages.tsx +115 -0
- package/src/Repeater.tsx +46 -0
- package/src/ResourceIndex.tsx +271 -0
- package/src/action-upload.test.tsx +44 -0
- package/src/blocks.tsx +26 -0
- package/src/button-part.test.tsx +42 -0
- package/src/components.test.ts +22 -0
- package/src/components.tsx +435 -0
- package/src/confirm.test.tsx +68 -0
- package/src/context.tsx +19 -0
- package/src/custom-pages.test.tsx +177 -0
- package/src/errors.test.tsx +95 -0
- package/src/fixtures.ts +20 -0
- package/src/index.ts +29 -0
- package/src/kit/api.ts +305 -0
- package/src/kit/auth.tsx +109 -0
- package/src/kit/bridge.tsx +142 -0
- package/src/kit/button.tsx +396 -0
- package/src/kit/cell.tsx +260 -0
- package/src/kit/charts.tsx +638 -0
- package/src/kit/create.tsx +166 -0
- package/src/kit/dashboard.tsx +335 -0
- package/src/kit/dates.tsx +62 -0
- package/src/kit/entry.tsx +87 -0
- package/src/kit/field.tsx +449 -0
- package/src/kit/fields/CodeEditor.tsx +157 -0
- package/src/kit/fields/KeyValue.tsx +215 -0
- package/src/kit/fields/MarkdownEditor.tsx +380 -0
- package/src/kit/fields/OneTimeCode.tsx +192 -0
- package/src/kit/fields/Repeater.tsx +350 -0
- package/src/kit/fields/Slider.tsx +185 -0
- package/src/kit/fields/TagsInput.tsx +219 -0
- package/src/kit/filters.tsx +214 -0
- package/src/kit/form-shared.tsx +62 -0
- package/src/kit/form.tsx +253 -0
- package/src/kit/hooks.ts +119 -0
- package/src/kit/i18n.tsx +67 -0
- package/src/kit/icons.tsx +190 -0
- package/src/kit/infolist.tsx +452 -0
- package/src/kit/kit.test.tsx +2200 -0
- package/src/kit/layout.tsx +236 -0
- package/src/kit/loading.tsx +120 -0
- package/src/kit/notice.tsx +72 -0
- package/src/kit/page.tsx +103 -0
- package/src/kit/pageform.tsx +75 -0
- package/src/kit/picker.tsx +110 -0
- package/src/kit/plugins.tsx +206 -0
- package/src/kit/profile.tsx +86 -0
- package/src/kit/relations.tsx +146 -0
- package/src/kit/rich.tsx +276 -0
- package/src/kit/search.tsx +92 -0
- package/src/kit/select.tsx +244 -0
- package/src/kit/shell.tsx +401 -0
- package/src/kit/table-extras.tsx +457 -0
- package/src/kit/table.tsx +734 -0
- package/src/kit/theme.ts +57 -0
- package/src/layout-parts.test.tsx +159 -0
- package/src/pages/index.ts +7 -0
- package/src/part.test.tsx +56 -0
- package/src/plugins.test.tsx +159 -0
- package/src/plugins.tsx +240 -0
- package/src/preview.test.tsx +81 -0
- package/src/rail.test.tsx +56 -0
- package/src/repeater.test.tsx +48 -0
- package/src/resource-pages.test.tsx +119 -0
- package/src/screens.test.tsx +121 -0
- package/src/ssr.test.tsx +86 -0
- package/src/tenant.test.tsx +94 -0
- package/src/ui.tsx +100 -0
- package/theme.css +467 -0
- package/README.md +0 -3
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { Suspense, createContext, useCallback, useContext, useMemo, type ComponentType, type ReactNode } from 'react';
|
|
2
|
+
import { Head, Link, router } from '@inertiajs/react';
|
|
3
|
+
import { listSearch, type FormRecord, type IndexProps } from '@larakit/core';
|
|
4
|
+
import {
|
|
5
|
+
LayoutPart, Missing, component as appComponent, type ActionButtonProps, type BreadcrumbProps, type EmptyProps, type FiltersProps, type HeaderProps,
|
|
6
|
+
type PaginationProps, type PartOwn, type TableProps,
|
|
7
|
+
} from './components';
|
|
8
|
+
import { kitFor, useLarakit } from './context';
|
|
9
|
+
import { PanelLayout } from './Layout';
|
|
10
|
+
import { Button } from './ui';
|
|
11
|
+
import type { Layout, Resource } from './kit/api';
|
|
12
|
+
import { Form } from './kit/form';
|
|
13
|
+
import { say } from './kit/notice';
|
|
14
|
+
import { Table as KitTable, useActions } from './kit/table';
|
|
15
|
+
import { Guard } from './plugins';
|
|
16
|
+
|
|
17
|
+
/** A resource's own pages, which the server's PageComponent / `.page_component(...)` can give to the application. */
|
|
18
|
+
export type ResourcePageName = 'list' | 'create' | 'edit' | 'view';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* What the application's component for one of a resource's own pages gets: everything Larakit's page gets, and
|
|
22
|
+
* Default, Larakit's own page, to draw where it likes. Saving, validation errors, permissions and tenancy stay with
|
|
23
|
+
* Larakit's routes, so Default's form saves as it always does.
|
|
24
|
+
*
|
|
25
|
+
* ```tsx
|
|
26
|
+
* export default function Edit({ record, Default, run }: ResourcePageProps) {
|
|
27
|
+
* return <><p>Order {record?.id}</p><Default /><button onClick={() => run('ship', record?.id)}>Ship</button></>;
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export type ResourcePageProps<R = FormRecord> = {
|
|
32
|
+
/** Which page this is. */
|
|
33
|
+
page: ResourcePageName;
|
|
34
|
+
/** The resource's definition: its columns, fields, actions and the rest. */
|
|
35
|
+
screen: IndexProps['screen'];
|
|
36
|
+
/** The list's rows, page and totals, as the list came with them. */
|
|
37
|
+
table: IndexProps['table'];
|
|
38
|
+
/** The list's search, sort, filters and page. */
|
|
39
|
+
query: IndexProps['query'];
|
|
40
|
+
/** The record's id on edit and view; null on list and create. */
|
|
41
|
+
id: string | null;
|
|
42
|
+
/** The record on edit and view, with its values, version and what the visitor may do. */
|
|
43
|
+
record?: R;
|
|
44
|
+
/** Inertia's field errors after a save that failed. */
|
|
45
|
+
errors: IndexProps['errors'];
|
|
46
|
+
/** Where the panel lives, such as /admin. */
|
|
47
|
+
path: string;
|
|
48
|
+
/** An address of the resource: href() is its list, href('/7/edit') a record's form. */
|
|
49
|
+
href: (to?: string) => string;
|
|
50
|
+
/** Larakit's own page: the list with its table, or the record's form or view with its heading. */
|
|
51
|
+
Default: ComponentType;
|
|
52
|
+
/**
|
|
53
|
+
* Runs one of the resource's actions: a record's when id is given, a header action otherwise. It asks first and
|
|
54
|
+
* fills its form when the action says so, shows its message and loads the page again.
|
|
55
|
+
*/
|
|
56
|
+
run: (action: string, id?: string | null) => Promise<void>;
|
|
57
|
+
/** Loads the page's props again. */
|
|
58
|
+
reload: () => void;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The list of a screen, with everything the panel's list does: search, tabs, filters, sorting, pages, groups,
|
|
63
|
+
* summaries, columns to hide, bulk and header actions, inline edits, import, export and reordering. A record
|
|
64
|
+
* opens at its own address: over the list as a dialog or a drawer, or as a page of its own when the panel's
|
|
65
|
+
* forms are pages.
|
|
66
|
+
*/
|
|
67
|
+
export function ResourceIndex(props: IndexProps & { path: string }) {
|
|
68
|
+
const kit = useMemo(() => kitFor(props.larakit, props.path), [props.larakit, props.path]);
|
|
69
|
+
return (
|
|
70
|
+
<PanelLayout kit={kit} current={props.screen.name} title={props.screen.label}>
|
|
71
|
+
<Head title={props.screen.label} />
|
|
72
|
+
<EmptyContext value={props.screen.table?.empty ?? null}>
|
|
73
|
+
<Records {...props} />
|
|
74
|
+
</EmptyContext>
|
|
75
|
+
</PanelLayout>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function Records(props: IndexProps) {
|
|
80
|
+
const { screen, table, query, modal } = props;
|
|
81
|
+
const { panel, path } = useLarakit();
|
|
82
|
+
const resource = screen as unknown as Resource;
|
|
83
|
+
const form = (panel.layout?.form ?? 'dialog') as Layout['form'];
|
|
84
|
+
const open = modal ? { id: modal.mode === 'create' ? null : modal.record.id, view: modal.mode === 'view', record: modal.mode === 'create' ? undefined : modal.record } : null;
|
|
85
|
+
const home = panel.menu.find((m) => m.name === 'dashboard');
|
|
86
|
+
const search = listSearch(query).slice(1);
|
|
87
|
+
const own = screen.pageComponents ?? {};
|
|
88
|
+
// A record's form or view is a page of its own when the panel's forms are pages, and when the application draws it.
|
|
89
|
+
const page: ResourcePageName = modal && (form === 'page' || own[modal.mode]) ? modal.mode : 'list';
|
|
90
|
+
const key = (open?.id ?? '') + ':' + page;
|
|
91
|
+
const larakits = page === 'list'
|
|
92
|
+
? <KitTable resource={resource} form={form} inertia={{ page: table as never, query: search, open, home: { label: home?.label ?? panel.name, href: home?.href ?? undefined } }} />
|
|
93
|
+
: <RecordPage key={key} resource={resource} id={open?.id ?? null} view={page === 'view'} search={search} record={open?.record} />;
|
|
94
|
+
const name = own[page];
|
|
95
|
+
if (!name) return larakits;
|
|
96
|
+
return (
|
|
97
|
+
<DefaultPage value={larakits}>
|
|
98
|
+
<OwnPage key={key} name={name} page={page} props={props} id={open?.id ?? null} path={path} resource={resource} />
|
|
99
|
+
</DefaultPage>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Larakit's own page, for the application's component to draw as Default. It is the same component on every
|
|
104
|
+
// render, so the list keeps its state when the page's props change.
|
|
105
|
+
const DefaultPage = createContext<ReactNode>(null);
|
|
106
|
+
const Default = () => <>{useContext(DefaultPage)}</>;
|
|
107
|
+
|
|
108
|
+
/** One of a resource's own pages, drawn by the application's component, which gets Larakit's page as Default. */
|
|
109
|
+
function OwnPage({ name, page, props, id, path, resource }: {
|
|
110
|
+
name: string; page: ResourcePageName; props: IndexProps; id: string | null; path: string; resource: Resource;
|
|
111
|
+
}) {
|
|
112
|
+
const { t } = useLarakit();
|
|
113
|
+
const Drawn = appComponent(name) as ComponentType<ResourcePageProps> | null;
|
|
114
|
+
const reload = useCallback(() => router.reload(), []);
|
|
115
|
+
// A deleted record is gone: its list is shown.
|
|
116
|
+
const [press, asked] = useActions(resource, (_, action) => (action === 'delete' && page !== 'list' ? router.visit(`${path}/${resource.name}`) : reload()));
|
|
117
|
+
const run = useCallback(async (action: string, of?: string | null) => { await press(of ?? null, action); }, [press]);
|
|
118
|
+
const href = useCallback((to = '') => `${path}/${resource.name}${to}`, [path, resource.name]);
|
|
119
|
+
const problem = Drawn ? <p role="alert" className="danger">{t('page.wentWrong', 'Something went wrong.')}</p> : <Missing name={name} />;
|
|
120
|
+
return (
|
|
121
|
+
<div data-lk="own-page" data-page={page}>
|
|
122
|
+
{!Drawn ? problem : (
|
|
123
|
+
<Guard fallback={problem}>
|
|
124
|
+
<Suspense fallback={null}>
|
|
125
|
+
<Drawn page={page} screen={props.screen} table={props.table} query={props.query} id={id}
|
|
126
|
+
record={props.modal && props.modal.mode !== 'create' ? props.modal.record : undefined} errors={props.errors ?? {}}
|
|
127
|
+
path={path} href={href} Default={Default} run={run} reload={reload} />
|
|
128
|
+
</Suspense>
|
|
129
|
+
</Guard>
|
|
130
|
+
)}
|
|
131
|
+
{asked}
|
|
132
|
+
</div>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** A record's form or view as a page of its own: `/create`, `/{id}` or `/{id}/edit` when the panel's forms are pages. */
|
|
137
|
+
function RecordPage({ resource, id, view, search, record }: { resource: Resource; id: string | null; view: boolean; search: string; record?: unknown }) {
|
|
138
|
+
const { path, t } = useLarakit();
|
|
139
|
+
const base = `${path}/${resource.name}`, back = base + (search ? '?' + search : '');
|
|
140
|
+
const at = id === null ? '' : `${base}/${encodeURIComponent(id)}`;
|
|
141
|
+
// After an action the page shows what it changed; a deleted record is gone, so its list is shown.
|
|
142
|
+
const [run, asked] = useActions(resource, (_, name) => (name === 'delete' ? router.visit(back) : router.reload()));
|
|
143
|
+
return <>
|
|
144
|
+
<Form resource={resource} layout="page" id={id} view={view} record={record as never} saved={id === null ? () => {} : undefined}
|
|
145
|
+
edit={id === null ? undefined : () => router.visit(at + '/edit')}
|
|
146
|
+
show={id === null || !resource.view ? undefined : () => router.visit(at)}
|
|
147
|
+
run={id === null ? undefined : (name) => void run(id, name)}
|
|
148
|
+
close={(saved, made) => {
|
|
149
|
+
if (!saved) return router.visit(back);
|
|
150
|
+
say(id === null ? t('table.created', 'Created.') : t('form.saved', 'Saved.'));
|
|
151
|
+
// A new record opens for editing, as its own page. An edited one stays, with what was saved.
|
|
152
|
+
if (id === null) router.visit(made ? `${base}/${encodeURIComponent(made)}/edit` : back);
|
|
153
|
+
else router.reload();
|
|
154
|
+
}} />
|
|
155
|
+
{asked}
|
|
156
|
+
</>;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Larakit's table: the columns' headings, sortable ones as buttons, and a row for each record. */
|
|
160
|
+
export function Table({ columns, rows, sort, sortBy, Cell: CellHere, Actions }: PartOwn<TableProps>) {
|
|
161
|
+
return (
|
|
162
|
+
<div data-lk="table" className="overflow-x-auto rounded-(--lk-radius) bg-(--lk-surface) ring-1 ring-(--lk-border)">
|
|
163
|
+
<table className="min-w-full divide-y divide-(--lk-border) text-sm">
|
|
164
|
+
<thead>
|
|
165
|
+
<tr>
|
|
166
|
+
{columns.map((c) => (
|
|
167
|
+
<th key={c.key} scope="col" className={'px-4 py-2.5 text-left font-medium text-(--lk-muted) ' + (c.look?.align === 'end' ? 'text-right' : '')}
|
|
168
|
+
aria-sort={sort === c.key ? 'ascending' : sort === '-' + c.key ? 'descending' : undefined}>
|
|
169
|
+
{c.sortable ? (
|
|
170
|
+
<button type="button" className="inline-flex items-center gap-1 hover:text-(--lk-text)" onClick={() => sortBy(c.key)}>
|
|
171
|
+
{c.label}<span aria-hidden>{sort === c.key ? '▲' : sort === '-' + c.key ? '▼' : '↕'}</span>
|
|
172
|
+
</button>
|
|
173
|
+
) : c.label}
|
|
174
|
+
</th>
|
|
175
|
+
))}
|
|
176
|
+
<th className="w-0" />
|
|
177
|
+
</tr>
|
|
178
|
+
</thead>
|
|
179
|
+
<tbody className="divide-y divide-(--lk-border)">
|
|
180
|
+
{rows.length === 0 && (
|
|
181
|
+
<tr><td colSpan={columns.length + 1} className="px-4 py-10 text-center text-(--lk-muted)">
|
|
182
|
+
<EmptyTable />
|
|
183
|
+
</td></tr>
|
|
184
|
+
)}
|
|
185
|
+
{rows.map((row) => (
|
|
186
|
+
<tr key={row.id} data-id={row.id} className="hover:bg-black/2 dark:hover:bg-white/2">
|
|
187
|
+
{columns.map((c) => (
|
|
188
|
+
<td key={c.key} data-key={c.partKey ?? c.key} className={'px-4 py-2.5 align-top ' + (c.look?.align === 'end' ? 'text-right ' : '') + (c.look?.noWrap ? 'whitespace-nowrap' : '')}>
|
|
189
|
+
<CellHere column={c} row={row} />
|
|
190
|
+
</td>
|
|
191
|
+
))}
|
|
192
|
+
<td className="px-4 py-2 text-right whitespace-nowrap"><Actions row={row} /></td>
|
|
193
|
+
</tr>
|
|
194
|
+
))}
|
|
195
|
+
</tbody>
|
|
196
|
+
</table>
|
|
197
|
+
</div>
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** What a table without rows shows: `part("empty", ...)` can draw it. */
|
|
202
|
+
function EmptyTable() {
|
|
203
|
+
const { t } = useLarakit();
|
|
204
|
+
const text = useContext(EmptyContext) ?? t('table.empty', 'Nothing here yet.');
|
|
205
|
+
return <LayoutPart part="empty" Default={EmptyText} props={{ text }} />;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** The screen's own text for a table without rows, for whichever component draws the table. */
|
|
209
|
+
const EmptyContext = createContext<string | null>(null);
|
|
210
|
+
|
|
211
|
+
/** Larakit's breadcrumb: links to where the page is, then the page. */
|
|
212
|
+
export function Breadcrumb({ items }: PartOwn<BreadcrumbProps>) {
|
|
213
|
+
return (
|
|
214
|
+
<nav data-lk="breadcrumb" aria-label="Breadcrumb">
|
|
215
|
+
<ol className="flex flex-wrap items-center gap-1.5 text-xs text-(--lk-muted)">
|
|
216
|
+
{items.map((item, i) => {
|
|
217
|
+
const last = i === items.length - 1;
|
|
218
|
+
return (
|
|
219
|
+
<li key={i} className="flex items-center gap-1.5">
|
|
220
|
+
{item.href && !last ? <Link href={item.href} className="hover:text-(--lk-text)">{item.label}</Link>
|
|
221
|
+
: <span aria-current={last ? 'page' : undefined}>{item.label}</span>}
|
|
222
|
+
{!last && <span aria-hidden>/</span>}
|
|
223
|
+
</li>
|
|
224
|
+
);
|
|
225
|
+
})}
|
|
226
|
+
</ol>
|
|
227
|
+
</nav>
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Larakit's filters: each one's input, side by side. */
|
|
232
|
+
export function Filters({ filters, Filter }: PartOwn<FiltersProps>) {
|
|
233
|
+
return <div data-lk="filters" className="flex flex-wrap items-end gap-3">{filters.map((f) => <Filter key={f.key} filter={f} />)}</div>;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
/** Larakit's button of a row action. */
|
|
238
|
+
export function ActionButton({ action, run, partKey }: Pick<ActionButtonProps, 'action' | 'run'> & { partKey?: string }) {
|
|
239
|
+
return <Button data-key={partKey} tone={action.danger ? 'danger' : 'plain'} onClick={run}>{action.label}</Button>;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Larakit's heading of a screen, with its buttons. */
|
|
243
|
+
export function PageHeader({ title, actions }: PartOwn<HeaderProps>) {
|
|
244
|
+
return (
|
|
245
|
+
<header data-lk="header" className="flex flex-wrap items-center justify-between gap-3">
|
|
246
|
+
<h1 className="text-xl font-semibold">{title}</h1>
|
|
247
|
+
{actions}
|
|
248
|
+
</header>
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Larakit's text for a table without rows. */
|
|
253
|
+
export function EmptyText({ text }: PartOwn<EmptyProps>) {
|
|
254
|
+
return <span data-lk="empty">{text}</span>;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** Larakit's pages under a table. */
|
|
258
|
+
export function Pages({ page, lastPage, total, from, to, go }: PartOwn<PaginationProps>) {
|
|
259
|
+
const { t } = useLarakit();
|
|
260
|
+
return (
|
|
261
|
+
<footer data-lk="pagination" className="flex items-center justify-between text-sm text-(--lk-muted)">
|
|
262
|
+
<span>{t('table.showing', 'Showing {from} to {to} of {total}', { from, to, total })}</span>
|
|
263
|
+
<div className="flex items-center gap-2">
|
|
264
|
+
<Button aria-label={t('table.prevPage', 'Previous page')} disabled={page <= 1} onClick={() => go(page - 1)}>‹</Button>
|
|
265
|
+
<span>{page} / {Math.max(lastPage, 1)}</span>
|
|
266
|
+
<Button aria-label={t('table.nextPage', 'Next page')} disabled={page >= lastPage} onClick={() => go(page + 1)}>›</Button>
|
|
267
|
+
</div>
|
|
268
|
+
</footer>
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// A file field in an action's form (LA-321): the file goes to the action's own address, `uploads/{field}`, and the
|
|
2
|
+
// form sends the stored path as JSON. An application's own field component gets the action's address as `api`.
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
import { act, cleanup, fireEvent, render, screen as find, waitFor } from '@testing-library/react';
|
|
5
|
+
import { larakit } from './components';
|
|
6
|
+
import { Ask } from './kit/create';
|
|
7
|
+
import { setPanelPath, type Field } from './kit/api';
|
|
8
|
+
|
|
9
|
+
afterEach(() => { cleanup(); vi.unstubAllGlobals(); });
|
|
10
|
+
|
|
11
|
+
const proof = { key: 'proof', label: 'Proof', type: 'file', file: { maxSize: 2048, image: true } } as unknown as Field;
|
|
12
|
+
const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
|
13
|
+
|
|
14
|
+
describe("a file field in an action's form", () => {
|
|
15
|
+
it('uploads to the action, shows the file from there and sends its path', async () => {
|
|
16
|
+
setPanelPath('/admin');
|
|
17
|
+
const sent: { url: string; init?: RequestInit }[] = [];
|
|
18
|
+
vi.stubGlobal('fetch', vi.fn(async (url: string, init?: RequestInit) => {
|
|
19
|
+
sent.push({ url: String(url), init });
|
|
20
|
+
return String(url).endsWith('/uploads/proof') ? json({ value: 'actions/attach/x.png', name: 'a.png', size: 3 }, 201) : json({ message: 'Attached' });
|
|
21
|
+
}));
|
|
22
|
+
const close = vi.fn();
|
|
23
|
+
render(<Ask url="/posts/3/actions/attach" fields={[proof]} title="Attach" button="Attach" close={close} />);
|
|
24
|
+
const input = document.querySelector('input[type=file]') as HTMLInputElement;
|
|
25
|
+
await act(async () => { fireEvent.change(input, { target: { files: [new File(['png'], 'a.png', { type: 'image/png' })] } }); });
|
|
26
|
+
await find.findByText('a.png');
|
|
27
|
+
expect(sent[0].url).toBe('/admin/api/posts/3/actions/attach/uploads/proof');
|
|
28
|
+
expect(sent[0].init?.body).toBeInstanceOf(FormData);
|
|
29
|
+
expect(document.querySelector('[data-lk="file"] img')?.getAttribute('src')).toBe('/admin/api/posts/3/actions/attach/files/proof?path=actions%2Fattach%2Fx.png');
|
|
30
|
+
await act(async () => { fireEvent.click(find.getByRole('button', { name: 'Attach' })); });
|
|
31
|
+
await waitFor(() => expect(close).toHaveBeenCalledWith({ message: 'Attached' }));
|
|
32
|
+
expect(sent[1].url).toBe('/admin/api/posts/3/actions/attach');
|
|
33
|
+
expect(JSON.parse(String(sent[1].init?.body))).toEqual({ proof: 'actions/attach/x.png' });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("gives an application's own field component the action's address", async () => {
|
|
37
|
+
setPanelPath('/admin');
|
|
38
|
+
let got: unknown;
|
|
39
|
+
larakit({ components: { './larakit/components/Pick.tsx': { default: ({ api }: { api?: string }) => { got = api; return <span data-own-pick="" />; } } } });
|
|
40
|
+
render(<Ask url="/topbar/actions/logo" fields={[{ ...proof, component: 'Pick' } as Field]} title="Logo" button="Logo" close={() => {}} />);
|
|
41
|
+
await waitFor(() => expect(document.querySelector('[data-own-pick]')).not.toBeNull());
|
|
42
|
+
expect(got).toBe('/admin/api/topbar/actions/logo');
|
|
43
|
+
});
|
|
44
|
+
});
|
package/src/blocks.tsx
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Larakit's building blocks, for a component that draws one of a resource's own pages (ResourcePageProps). They take
|
|
2
|
+
// the page's own `screen`, as the server sent it.
|
|
3
|
+
import type { ComponentProps } from 'react';
|
|
4
|
+
import type { IndexProps } from '@larakit/core';
|
|
5
|
+
import { Table } from './kit/table';
|
|
6
|
+
import { Form } from './kit/form';
|
|
7
|
+
import { InfolistView as View } from './kit/infolist';
|
|
8
|
+
|
|
9
|
+
/** The resource as a page of it has it: ResourcePageProps' `screen`. */
|
|
10
|
+
type Screen = IndexProps['screen'];
|
|
11
|
+
type Infolist = NonNullable<Screen['infolist']>;
|
|
12
|
+
|
|
13
|
+
/** The resource's table, with its search, filters, actions and forms. `form` is how a record opens: dialog by default. */
|
|
14
|
+
export function ResourceTable({ resource, form = 'dialog', ...rest }: Omit<ComponentProps<typeof Table>, 'resource' | 'form'> & { resource: Screen; form?: 'dialog' | 'drawer' | 'page' }) {
|
|
15
|
+
return <Table {...rest} resource={resource as never} form={form} />;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A record's form, or its view with `view`: a new one when `id` is null. */
|
|
19
|
+
export function RecordForm({ resource, ...rest }: Omit<ComponentProps<typeof Form>, 'resource'> & { resource: Screen }) {
|
|
20
|
+
return <Form {...rest} resource={resource as never} />;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A record's infolist: the screen's, or one of your own. */
|
|
24
|
+
export function InfolistView({ resource, infolist, ...rest }: Omit<ComponentProps<typeof View>, 'resource' | 'infolist'> & { resource: Screen; infolist: Infolist }) {
|
|
25
|
+
return <View {...rest} resource={resource as never} infolist={infolist as never} />;
|
|
26
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// The application's own button part is told what Larakit's own button knows: its color, style, size, icon and
|
|
2
|
+
// which button it is.
|
|
3
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
4
|
+
import { act, cleanup, render } from '@testing-library/react';
|
|
5
|
+
import { createInertiaApp } from '@inertiajs/react';
|
|
6
|
+
import type { IndexProps } from '@larakit/core';
|
|
7
|
+
import { larakit, type ButtonProps } from './components';
|
|
8
|
+
import { panel, screen } from './fixtures';
|
|
9
|
+
import { ResourceIndex } from './pages';
|
|
10
|
+
|
|
11
|
+
afterEach(() => { cleanup(); localStorage.clear(); });
|
|
12
|
+
|
|
13
|
+
const seen: ButtonProps[] = [];
|
|
14
|
+
|
|
15
|
+
function Button(props: ButtonProps) {
|
|
16
|
+
seen.push(props);
|
|
17
|
+
return <button type={props.type} onClick={props.onClick} data-lk={props['data-lk']} data-tone={props.tone}>{props.children}</button>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('the button part', () => {
|
|
21
|
+
it('gets the color, style, size, icon and name of the button', async () => {
|
|
22
|
+
larakit({ components: { './larakit/components/parts/Button.tsx': { default: Button } } });
|
|
23
|
+
const props = {
|
|
24
|
+
path: '/admin', errors: {},
|
|
25
|
+
larakit: { ...panel('Shop', [{ name: 'orders', label: 'Orders', icon: '', group: '', href: '/admin/orders' }]), parts: { button: 'parts/Button' } },
|
|
26
|
+
screen: screen({ name: 'orders', label: 'Orders', canCreate: true, columns: [{ key: 'number', label: 'Number', type: 'text', sortable: false, searchable: false }] }),
|
|
27
|
+
table: { data: [], total: 0, page: 1, lastPage: 1 },
|
|
28
|
+
query: { filters: {} },
|
|
29
|
+
modal: null,
|
|
30
|
+
} as unknown as IndexProps;
|
|
31
|
+
await act(async () => {
|
|
32
|
+
await createInertiaApp({
|
|
33
|
+
page: { component: 'larakit/Resource/Index', props, url: '/admin/orders', version: '1', flash: {} } as never,
|
|
34
|
+
resolve: () => ResourceIndex as never, setup: ({ App, props }) => { render(<App {...props} />); },
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
const create = seen.find((b) => b['data-lk'] === 'new');
|
|
38
|
+
expect(create, JSON.stringify(seen.map((b) => b['data-lk']))).toBeTruthy();
|
|
39
|
+
expect(create).toMatchObject({ tone: 'primary', variant: 'solid', color: 'primary', size: 'md', icon: 'plus' });
|
|
40
|
+
expect(document.querySelector('[data-lk="new"]')).toBeTruthy();
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { component, componentName, larakit } from './components';
|
|
3
|
+
|
|
4
|
+
describe('the application components', () => {
|
|
5
|
+
it('names a file as .component(...) does', () => {
|
|
6
|
+
expect(componentName('./larakit/components/OrderSection.tsx')).toBe('OrderSection');
|
|
7
|
+
expect(componentName('../js/larakit/components/orders/Header.jsx')).toBe('orders/Header');
|
|
8
|
+
expect(componentName('./Other.tsx')).toBe('Other');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('finds what app.tsx gave, eager or lazy', () => {
|
|
12
|
+
const Eager = () => null;
|
|
13
|
+
larakit({ components: {
|
|
14
|
+
'./larakit/components/Eager.tsx': { default: Eager },
|
|
15
|
+
'./larakit/components/orders/Lazy.tsx': () => Promise.resolve({ default: Eager }),
|
|
16
|
+
} });
|
|
17
|
+
expect(component('Eager')).toBe(Eager);
|
|
18
|
+
expect(component('orders/Lazy')).not.toBeNull();
|
|
19
|
+
expect(component('orders/Lazy')).toBe(component('orders/Lazy'));
|
|
20
|
+
expect(component('Missing')).toBeNull();
|
|
21
|
+
});
|
|
22
|
+
});
|