@inferencebureau/apps-sdk 0.12.0 → 0.14.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 -2
- package/dist/auto.d.ts +113 -0
- package/dist/auto.js +197 -0
- package/dist/index.d.ts +127 -0
- package/dist/index.js +113 -0
- package/dist/polaris.d.ts +30 -0
- package/dist/polaris.js +104 -0
- package/dist/react.d.ts +242 -0
- package/dist/react.js +488 -0
- package/dist/test.js +33 -3
- package/package.json +25 -2
package/dist/polaris.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
/**
|
|
4
|
+
* AutoTable and AutoForm in Polaris web components, for Shopify apps whose admin pages use them (layout.tsx loads
|
|
5
|
+
* Shopify's polaris.js): the table is the admin's own index table (<s-table>, a list on narrow screens), with search,
|
|
6
|
+
* sort, pages and bulk delete in its filters; the form's fields are Polaris fields with the API's refusals on them.
|
|
7
|
+
* They work out what to show the same way as the plain ones in `@inferencebureau/apps-sdk/react` (useAutoTable,
|
|
8
|
+
* useAutoForm), whose hooks and RecordsProvider they share.
|
|
9
|
+
*/
|
|
10
|
+
import { createElement, useEffect, useId, useRef, useState } from "react";
|
|
11
|
+
import { useAutoForm, useAutoTable } from "./react.js";
|
|
12
|
+
/** A Polaris element with its events listened to: each handler gets the element, to read `value`, `checked` or `files`. */
|
|
13
|
+
function El({ tag, on, ...props }) {
|
|
14
|
+
const ref = useRef(null);
|
|
15
|
+
const handlers = useRef(on);
|
|
16
|
+
handlers.current = on;
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const el = ref.current;
|
|
19
|
+
if (!el)
|
|
20
|
+
return;
|
|
21
|
+
const fns = Object.keys(handlers.current).map((name) => [name, (e) => handlers.current[name]?.(e.currentTarget)]);
|
|
22
|
+
for (const [name, fn] of fns)
|
|
23
|
+
el.addEventListener(name, fn);
|
|
24
|
+
return () => { for (const [name, fn] of fns)
|
|
25
|
+
el.removeEventListener(name, fn); };
|
|
26
|
+
}, [tag]);
|
|
27
|
+
return createElement(tag, { ...props, ref });
|
|
28
|
+
}
|
|
29
|
+
/** A cell in Polaris: badges for yes/no and choices, links for URLs and emails, a thumbnail for an image. */
|
|
30
|
+
export function PolarisCell({ value }) {
|
|
31
|
+
switch (value.kind) {
|
|
32
|
+
case "empty": return _jsx("s-text", { color: "subdued", children: "\u2014" });
|
|
33
|
+
case "boolean": return _jsx("s-badge", { tone: value.value ? "success" : "neutral", children: value.text });
|
|
34
|
+
case "tags": return _jsx("s-stack", { direction: "inline", gap: "small-200", children: value.tags.map((t) => _jsx("s-badge", { children: t }, t)) });
|
|
35
|
+
case "link": return _jsx("s-link", { href: value.href, target: value.href.startsWith("mailto:") ? "auto" : "_blank", children: value.text });
|
|
36
|
+
case "color": return (_jsxs("s-stack", { direction: "inline", gap: "small-200", alignItems: "center", children: [_jsx("span", { style: { display: "inline-block", width: 14, height: 14, borderRadius: 4, background: value.color, boxShadow: "inset 0 0 0 1px rgba(0,0,0,.15)" } }), _jsx("s-text", { children: value.color })] }));
|
|
37
|
+
case "file": return (_jsxs("s-stack", { direction: "inline", gap: "small-200", alignItems: "center", children: [value.image && _jsx("s-thumbnail", { size: "small-200", src: value.image, alt: "" }), value.href ? _jsx("s-link", { href: value.href, target: "_blank", children: value.text }) : _jsx("s-text", { children: value.text })] }));
|
|
38
|
+
default: return _jsx("s-text", { children: value.text });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A model's records as the admin's index table: search and sort in its filters, pages, a row opening its record
|
|
43
|
+
* (`rowHref` or `onRowClick`), and bulk delete when `selectable`.
|
|
44
|
+
*/
|
|
45
|
+
export function AutoTable({ model, rowHref, onRowClick, emptyText, searchable = true, heading, ...o }) {
|
|
46
|
+
const t = useAutoTable(model, o);
|
|
47
|
+
const [confirming, setConfirming] = useState(false);
|
|
48
|
+
const id = useId().replace(/:/g, "");
|
|
49
|
+
useEffect(() => { if (!t.selected.size)
|
|
50
|
+
setConfirming(false); }, [t.selected.size]);
|
|
51
|
+
const opens = !!(rowHref || onRowClick);
|
|
52
|
+
const listSlot = (i, kind) => (i === 0 ? "primary" : kind === "enum" || kind === "boolean" ? "inline" : "labeled");
|
|
53
|
+
const span = t.columns.length + (o.selectable ? 1 : 0);
|
|
54
|
+
return (_jsxs("s-section", { padding: "none", ...(heading ? { heading } : { accessibilityLabel: `${model} records` }), children: [t.error && _jsx("s-banner", { tone: "critical", children: t.error.message }), _jsxs(El, { tag: "s-table", on: { nextpage: t.nextPage, previouspage: t.previousPage }, paginate: t.hasNextPage || t.hasPreviousPage, hasNextPage: t.hasNextPage, hasPreviousPage: t.hasPreviousPage, loading: t.fetching, children: [_jsx("s-grid", { slot: "filters", gridTemplateColumns: t.selected.size > 0 ? "auto" : "1fr auto", gap: "small-200", alignItems: "center", children: t.selected.size > 0 ? (_jsxs("s-stack", { direction: "inline", gap: "small-200", alignItems: "center", children: [_jsxs("s-text", { children: [t.selected.size, " selected"] }), confirming ? (_jsxs(_Fragment, { children: [_jsx(El, { tag: "s-button", on: { click: () => void t.deleteSelected() }, tone: "critical", variant: "primary", loading: t.deleting, children: `Delete ${t.selected.size}` }), _jsx(El, { tag: "s-button", on: { click: () => setConfirming(false) }, variant: "tertiary", disabled: t.deleting, children: "Cancel" })] })) : (_jsx(El, { tag: "s-button", on: { click: () => setConfirming(true) }, tone: "critical", children: "Delete" }))] })) : (_jsxs(_Fragment, { children: [searchable ? _jsx(El, { tag: "s-search-field", on: { input: (el) => t.setSearch(String(el.value ?? "")) }, label: "Search", labelAccessibilityVisibility: "exclusive", placeholder: "Search", value: t.search }) : _jsx("span", {}), t.sortOptions.length > 1 && (_jsx(El, { tag: "s-select", on: { change: (el) => t.setSort(String(el.value ?? "")) }, label: "Sort", labelAccessibilityVisibility: "exclusive", value: t.sort, children: t.sortOptions.map((s) => _jsx("s-option", { value: s.value, children: s.label }, s.value)) }))] })) }), _jsxs("s-table-header-row", { children: [o.selectable && (_jsx("s-table-header", { children: _jsx(El, { tag: "s-checkbox", on: { change: t.toggleAll }, accessibilityLabel: "Choose every row", checked: t.allSelected }) })), t.columns.map((c, i) => _jsx("s-table-header", { listSlot: listSlot(i, c.field.type), format: c.numeric ? "numeric" : "base", children: c.label }, c.name))] }), _jsxs("s-table-body", { children: [t.rows.map((r) => {
|
|
55
|
+
const link = `${id}-${r.id}`;
|
|
56
|
+
return (_jsxs("s-table-row", { ...(opens ? { clickDelegate: link } : {}), children: [o.selectable && (_jsx("s-table-cell", { children: _jsx(El, { tag: "s-checkbox", on: { change: () => t.toggle(r.id) }, accessibilityLabel: "Choose this row", checked: t.selected.has(r.id) }) })), r.cells.map((cell, i) => (_jsx("s-table-cell", { children: i === 0 && opens ? _jsx(RowLink, { id: link, record: r.record, rowHref: rowHref, onRowClick: onRowClick, cell: cell }) : _jsx(PolarisCell, { value: cell }) }, t.columns[i].name)))] }, r.id));
|
|
57
|
+
}), !t.rows.length && !t.fetching && (_jsx("s-table-row", { children: Array.from({ length: span || 1 }, (_, i) => (_jsx("s-table-cell", { children: i === 0 ? _jsx("s-text", { color: "subdued", children: t.search ? "No records match." : emptyText ?? "No records yet." }) : null }, i))) }))] })] })] }));
|
|
58
|
+
}
|
|
59
|
+
/** The first cell's link, which the row's click goes to. */
|
|
60
|
+
function RowLink({ id, record, rowHref, onRowClick, cell }) {
|
|
61
|
+
const text = cell.kind === "empty" ? record.id : cell.kind === "boolean" || cell.kind === "link" || cell.kind === "file" ? cell.text : cell.kind === "tags" ? cell.tags.join(", ") : cell.kind === "color" ? cell.color : cell.text;
|
|
62
|
+
return _jsx(El, { tag: "s-link", on: onRowClick ? { click: () => onRowClick(record) } : {}, id: id, ...(rowHref ? { href: rowHref(record) } : {}), children: text });
|
|
63
|
+
}
|
|
64
|
+
/** One field's control in Polaris. */
|
|
65
|
+
export function PolarisInput({ field: f }) {
|
|
66
|
+
const text = typeof f.value === "string" ? f.value : "";
|
|
67
|
+
const base = { label: f.label, name: f.name, required: f.required, ...(f.error ? { error: f.error } : {}) };
|
|
68
|
+
const typed = (tag, extra = {}) => _jsx(El, { tag: tag, on: { input: (el) => f.set(String(el.value ?? "")) }, ...base, value: text, ...extra });
|
|
69
|
+
switch (f.kind) {
|
|
70
|
+
case "boolean": return _jsx(El, { tag: "s-checkbox", on: { change: (el) => f.set(el.checked === true) }, ...base, checked: f.value === true });
|
|
71
|
+
case "textarea": return typed("s-text-area", { rows: 4 });
|
|
72
|
+
case "json": return typed("s-text-area", { rows: 6, details: "JSON" });
|
|
73
|
+
case "number": return typed("s-number-field");
|
|
74
|
+
case "email": return typed("s-email-field");
|
|
75
|
+
case "url": return typed("s-url-field");
|
|
76
|
+
case "color": return typed("s-color-field");
|
|
77
|
+
case "password": return typed("s-password-field", { autocomplete: "new-password" });
|
|
78
|
+
case "list": return typed("s-text-field", { details: "Separate with commas" });
|
|
79
|
+
case "select":
|
|
80
|
+
case "record": {
|
|
81
|
+
const options = f.kind === "select" ? f.options.map((x) => ({ value: x, label: x })) : f.choices;
|
|
82
|
+
return (_jsxs(El, { tag: "s-select", on: { change: (el) => f.set(String(el.value ?? "")) }, ...base, value: text, children: [_jsx("s-option", { value: "", children: f.required ? "Choose…" : "None" }), options.map((x) => _jsx("s-option", { value: x.value, children: x.label }, x.value))] }));
|
|
83
|
+
}
|
|
84
|
+
case "multiselect": {
|
|
85
|
+
const chosen = Array.isArray(f.value) ? f.value : [];
|
|
86
|
+
return (_jsx(El, { tag: "s-choice-list", on: { change: (el) => f.set(Array.isArray(el.values) ? el.values : []) }, ...base, multiple: true, children: f.options.map((x) => _jsx("s-choice", { value: x, selected: chosen.includes(x), children: x }, x)) }));
|
|
87
|
+
}
|
|
88
|
+
case "dateTime": {
|
|
89
|
+
// Polaris has a date field and no time field: the time is typed beside it.
|
|
90
|
+
const [date = "", time = ""] = text.split("T");
|
|
91
|
+
const join = (d, t) => f.set(d ? `${d}T${t || "00:00"}` : "");
|
|
92
|
+
return (_jsxs("s-stack", { direction: "inline", gap: "small-200", children: [_jsx(El, { tag: "s-date-field", on: { change: (el) => join(String(el.value ?? ""), time) }, ...base, value: date }), _jsx(El, { tag: "s-text-field", on: { input: (el) => join(date, String(el.value ?? "")) }, label: "Time", placeholder: "14:30", value: time })] }));
|
|
93
|
+
}
|
|
94
|
+
case "file": return (_jsxs("s-stack", { gap: "small-200", children: [f.current && (f.current.url ? _jsx("s-link", { href: f.current.url, target: "_blank", children: f.current.fileName }) : _jsx("s-text", { children: f.current.fileName })), _jsx(El, { tag: "s-drop-zone", on: { change: (el) => { const files = el.files; f.set(files?.[0] ?? null); } }, ...base, accessibilityLabel: f.label }), f.value instanceof File && _jsx("s-text", { color: "subdued", children: f.value.name })] }));
|
|
95
|
+
default: return typed("s-text-field");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** A form for a model's record in Polaris fields, the API's refusals shown on the fields they're about. */
|
|
99
|
+
export function AutoForm({ model, submitLabel, heading, saveBar, ...o }) {
|
|
100
|
+
const f = useAutoForm(model, o);
|
|
101
|
+
if (f.loading)
|
|
102
|
+
return _jsx("s-section", { ...(heading ? { heading } : {}), children: _jsx("s-spinner", { accessibilityLabel: "Loading" }) });
|
|
103
|
+
return (_jsx("form", { onSubmit: (e) => void f.submit(e), onReset: (e) => { e.preventDefault(); f.reset(); }, noValidate: true, ...(saveBar ? { "data-save-bar": true } : {}), children: _jsx("s-section", { ...(heading ? { heading } : {}), children: _jsxs("s-stack", { gap: "base", children: [f.error && _jsx("s-banner", { tone: "critical", children: f.error }), f.fields.map((field) => _jsx(PolarisInput, { field: field }, field.name)), !saveBar && (_jsxs("s-stack", { direction: "inline", gap: "small-200", alignItems: "center", children: [_jsx("s-button", { type: "submit", variant: "primary", loading: f.submitting, children: submitLabel ?? (f.isNew && !o.action ? "Create" : "Save") }), f.saved && !f.dirty && _jsx("s-text", { color: "subdued", children: "Saved" })] }))] }) }) }));
|
|
104
|
+
}
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React for an app's pages: hooks that read and change records, and AutoTable and AutoForm, which draw a model's list
|
|
3
|
+
* and its create and edit forms from its schema. They call the app's records route (`serveRecords`, at
|
|
4
|
+
* /api/records unless the provider names another), which answers as whoever the request is from: the merchant in the
|
|
5
|
+
* Shopify admin (its session token goes with every request) or a signed-in user (their cookie does).
|
|
6
|
+
*
|
|
7
|
+
* The components here are the plain ones: standard elements with the starters' classes (`.btn`, `.auto-*` in
|
|
8
|
+
* globals.css) and their CSS variables, so they look like the rest of the app. `@inferencebureau/apps-sdk/polaris`
|
|
9
|
+
* draws the same tables and forms in Polaris web components. Both draw what `useAutoTable` and `useAutoForm` work out,
|
|
10
|
+
* which a page can use to draw its own.
|
|
11
|
+
*/
|
|
12
|
+
import { type FormEvent, type ReactNode } from "react";
|
|
13
|
+
import { ApiError, type AppRecord, type BulkDeleteResult, type Filter, type FindManyOptions, type ModelDescription, type RecordPage, type Sort } from "./index.js";
|
|
14
|
+
import { type Column, type ColumnOption, type Display, type FieldOption, type FormFieldSpec, type FormValue, type SortOption } from "./auto.js";
|
|
15
|
+
export { labelOf, recordLabel } from "./auto.js";
|
|
16
|
+
export type { Column, ColumnOption, Display, FieldOption, FormFieldSpec, FormValue, InputKind, SortOption } from "./auto.js";
|
|
17
|
+
/** A list's options as the records route takes them (`FindManyOptions`, its select any fields). */
|
|
18
|
+
export type ReadOptions = Omit<FindManyOptions, "select"> & {
|
|
19
|
+
select?: Record<string, unknown>;
|
|
20
|
+
};
|
|
21
|
+
export interface RecordsModel {
|
|
22
|
+
describe(): Promise<ModelDescription>;
|
|
23
|
+
findMany(options?: ReadOptions): Promise<RecordPage>;
|
|
24
|
+
findOne(id: string, o?: {
|
|
25
|
+
select?: Record<string, unknown>;
|
|
26
|
+
}): Promise<AppRecord | null>;
|
|
27
|
+
create(values: Record<string, unknown>): Promise<AppRecord>;
|
|
28
|
+
update(id: string, values: Record<string, unknown>): Promise<AppRecord>;
|
|
29
|
+
delete(id: string): Promise<void>;
|
|
30
|
+
bulkDelete(ids: string[]): Promise<BulkDeleteResult>;
|
|
31
|
+
/** Runs one of the model's actions: on a record when `id` is given. */
|
|
32
|
+
run(action: string, o?: {
|
|
33
|
+
id?: string;
|
|
34
|
+
params?: Record<string, unknown>;
|
|
35
|
+
}): Promise<{
|
|
36
|
+
record: AppRecord | null;
|
|
37
|
+
result?: unknown;
|
|
38
|
+
}>;
|
|
39
|
+
/** Uploads a file for a file field straight to storage; write the answer to the field. */
|
|
40
|
+
upload(field: string, file: Blob, o?: {
|
|
41
|
+
fileName?: string;
|
|
42
|
+
}): Promise<{
|
|
43
|
+
directUploadToken: string;
|
|
44
|
+
}>;
|
|
45
|
+
}
|
|
46
|
+
export interface RecordsClient {
|
|
47
|
+
model(name: string): RecordsModel;
|
|
48
|
+
/** Runs a global action the route allows (`serveRecords`'s `globalActions`). */
|
|
49
|
+
global(action: string, params?: Record<string, unknown>): Promise<{
|
|
50
|
+
result?: unknown;
|
|
51
|
+
}>;
|
|
52
|
+
/** Called with a model's name whenever a write through this client changed its records; returns the unsubscribe. */
|
|
53
|
+
subscribe(listener: (model: string) => void): () => void;
|
|
54
|
+
}
|
|
55
|
+
export interface RecordsClientOptions {
|
|
56
|
+
/** The app's records route; /api/records unless the app put it elsewhere. */
|
|
57
|
+
endpoint?: string;
|
|
58
|
+
/**
|
|
59
|
+
* How requests are sent. The browser helpers' `sessionFetch` by default: in the Shopify admin it adds the session
|
|
60
|
+
* token, and elsewhere it's a plain same-origin fetch, which carries the signed-in user's cookie.
|
|
61
|
+
*/
|
|
62
|
+
fetch?: (input: string, init: RequestInit) => Promise<Response>;
|
|
63
|
+
}
|
|
64
|
+
/** A client for the app's records route, for a page's own scripts. Most pages use the hooks, which make one. */
|
|
65
|
+
export declare function createRecordsClient(o?: RecordsClientOptions): RecordsClient;
|
|
66
|
+
/** Gives the hooks and components below it a client: one for another route, or with another fetch. Optional. */
|
|
67
|
+
export declare function RecordsProvider({ client, endpoint, children }: {
|
|
68
|
+
client?: RecordsClient;
|
|
69
|
+
endpoint?: string;
|
|
70
|
+
children: ReactNode;
|
|
71
|
+
}): import("react").JSX.Element;
|
|
72
|
+
/** The provider's client, or one for /api/records. */
|
|
73
|
+
export declare function useRecordsClient(): RecordsClient;
|
|
74
|
+
export interface Query<T> {
|
|
75
|
+
data: T | undefined;
|
|
76
|
+
fetching: boolean;
|
|
77
|
+
error: ApiError | null;
|
|
78
|
+
refetch(): void;
|
|
79
|
+
}
|
|
80
|
+
/** A page of a model's records. Pass `pause` to hold off (until an id is known, say). */
|
|
81
|
+
export declare function useFindMany(model: string, options?: ReadOptions, o?: {
|
|
82
|
+
pause?: boolean;
|
|
83
|
+
}): Query<RecordPage>;
|
|
84
|
+
/** One record by id, or null when there's none the caller can read. */
|
|
85
|
+
export declare function useFindOne(model: string, id: string | null | undefined, o?: {
|
|
86
|
+
select?: Record<string, unknown>;
|
|
87
|
+
pause?: boolean;
|
|
88
|
+
}): Query<AppRecord | null>;
|
|
89
|
+
/** The first record matching, or null. */
|
|
90
|
+
export declare function useFindFirst(model: string, options?: Omit<ReadOptions, "first" | "after">, o?: {
|
|
91
|
+
pause?: boolean;
|
|
92
|
+
}): Query<AppRecord | null>;
|
|
93
|
+
/** A model's description: its fields, which AutoTable and AutoForm draw from. */
|
|
94
|
+
export declare function useModel(model: string): Query<ModelDescription>;
|
|
95
|
+
export interface Mutation<T, A> {
|
|
96
|
+
data: T | undefined;
|
|
97
|
+
running: boolean;
|
|
98
|
+
error: ApiError | null;
|
|
99
|
+
run(args: A): Promise<T>;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* One of a model's actions: `create`, `update` and `delete` write the way the API does (the model's action when it has
|
|
103
|
+
* one), any other runs by name. Lists of the model refetch once it's done.
|
|
104
|
+
*
|
|
105
|
+
* const publish = useAction("post", "publish");
|
|
106
|
+
* await publish.run({ id: post.id });
|
|
107
|
+
*/
|
|
108
|
+
export declare function useAction(model: string, action: string): Mutation<{
|
|
109
|
+
record: AppRecord | null;
|
|
110
|
+
result?: unknown;
|
|
111
|
+
}, {
|
|
112
|
+
id?: string;
|
|
113
|
+
params?: Record<string, unknown>;
|
|
114
|
+
}>;
|
|
115
|
+
/** A global action the records route allows (`serveRecords`'s `globalActions`). */
|
|
116
|
+
export declare function useGlobalAction(action: string): Mutation<{
|
|
117
|
+
result?: unknown;
|
|
118
|
+
}, Record<string, unknown> | undefined>;
|
|
119
|
+
export interface AutoTableOptions {
|
|
120
|
+
/** Which columns, in order; the first six that read well in a cell, then Updated, when unset. */
|
|
121
|
+
columns?: ColumnOption[];
|
|
122
|
+
/** Records a page; 25 unless given (at most 250). */
|
|
123
|
+
pageSize?: number;
|
|
124
|
+
/** Only records matching, always (the search narrows further). */
|
|
125
|
+
filter?: Filter;
|
|
126
|
+
/** The order before the viewer picks one; newest first unless given. */
|
|
127
|
+
sort?: Sort;
|
|
128
|
+
/** Whether rows can be chosen for a bulk delete. */
|
|
129
|
+
selectable?: boolean;
|
|
130
|
+
}
|
|
131
|
+
export interface AutoTableRow {
|
|
132
|
+
id: string;
|
|
133
|
+
record: AppRecord;
|
|
134
|
+
cells: Display[];
|
|
135
|
+
}
|
|
136
|
+
export interface AutoTable {
|
|
137
|
+
description: ModelDescription | undefined;
|
|
138
|
+
columns: Column[];
|
|
139
|
+
rows: AutoTableRow[];
|
|
140
|
+
/** True until the first page is in, and while a page is being read. */
|
|
141
|
+
fetching: boolean;
|
|
142
|
+
error: ApiError | null;
|
|
143
|
+
search: string;
|
|
144
|
+
setSearch(search: string): void;
|
|
145
|
+
sortOptions: SortOption[];
|
|
146
|
+
/** The sort chosen, as a SortOption's value ("" is newest first, or the table's own `sort`). */
|
|
147
|
+
sort: string;
|
|
148
|
+
setSort(value: string): void;
|
|
149
|
+
/** Sorts by a column, turning it around when it's already the one. */
|
|
150
|
+
toggleSort(column: string): void;
|
|
151
|
+
page: number;
|
|
152
|
+
hasNextPage: boolean;
|
|
153
|
+
hasPreviousPage: boolean;
|
|
154
|
+
nextPage(): void;
|
|
155
|
+
previousPage(): void;
|
|
156
|
+
selected: Set<string>;
|
|
157
|
+
toggle(id: string): void;
|
|
158
|
+
/** Chooses every row on the page, or none once they all are. */
|
|
159
|
+
toggleAll(): void;
|
|
160
|
+
allSelected: boolean;
|
|
161
|
+
deleteSelected(): Promise<void>;
|
|
162
|
+
deleting: boolean;
|
|
163
|
+
refetch(): void;
|
|
164
|
+
}
|
|
165
|
+
/** What a table of the model shows and does, for a renderer to draw. */
|
|
166
|
+
export declare function useAutoTable(model: string, o?: AutoTableOptions): AutoTable;
|
|
167
|
+
export interface AutoFormOptions {
|
|
168
|
+
/** The record to edit; a new one is made without it. */
|
|
169
|
+
id?: string;
|
|
170
|
+
/**
|
|
171
|
+
* The action to run instead of creating or updating: one of the model's (on `id`'s record when given). Its params
|
|
172
|
+
* are the fields named in `fields`, so name them.
|
|
173
|
+
*/
|
|
174
|
+
action?: string;
|
|
175
|
+
/** Which fields, in order; every field a value can be written to when unset. */
|
|
176
|
+
fields?: FieldOption[];
|
|
177
|
+
/** Fields to leave out of the default set. */
|
|
178
|
+
exclude?: string[];
|
|
179
|
+
/** Values a new record starts with, over the fields' defaults. */
|
|
180
|
+
defaultValues?: Record<string, unknown>;
|
|
181
|
+
/** After a write succeeds, with the record as written. */
|
|
182
|
+
onSuccess?(record: AppRecord | null, result?: unknown): void;
|
|
183
|
+
}
|
|
184
|
+
export interface AutoFormField extends FormFieldSpec {
|
|
185
|
+
value: FormValue;
|
|
186
|
+
set(value: FormValue): void;
|
|
187
|
+
error: string | null;
|
|
188
|
+
/** A belongsTo's choices, once read. */
|
|
189
|
+
choices: {
|
|
190
|
+
value: string;
|
|
191
|
+
label: string;
|
|
192
|
+
}[];
|
|
193
|
+
/** A file field's current file, kept unless another is chosen. */
|
|
194
|
+
current: {
|
|
195
|
+
fileName: string;
|
|
196
|
+
url: string | null;
|
|
197
|
+
} | null;
|
|
198
|
+
}
|
|
199
|
+
export interface AutoForm {
|
|
200
|
+
description: ModelDescription | undefined;
|
|
201
|
+
fields: AutoFormField[];
|
|
202
|
+
/** True while the model, or the record being edited, is being read. */
|
|
203
|
+
loading: boolean;
|
|
204
|
+
submitting: boolean;
|
|
205
|
+
/** A refusal that isn't about one field, or why the form couldn't load. */
|
|
206
|
+
error: string | null;
|
|
207
|
+
/** True once a write has succeeded, until the next change. */
|
|
208
|
+
saved: boolean;
|
|
209
|
+
dirty: boolean;
|
|
210
|
+
submit(event?: FormEvent): Promise<void>;
|
|
211
|
+
reset(): void;
|
|
212
|
+
isNew: boolean;
|
|
213
|
+
}
|
|
214
|
+
/** What a form for the model shows and does, for a renderer to draw. */
|
|
215
|
+
export declare function useAutoForm(model: string, o?: AutoFormOptions): AutoForm;
|
|
216
|
+
/** A cell as standard elements, for the plain table. */
|
|
217
|
+
export declare function PlainCell({ value }: {
|
|
218
|
+
value: Display;
|
|
219
|
+
}): import("react").JSX.Element;
|
|
220
|
+
export interface AutoTableProps extends AutoTableOptions {
|
|
221
|
+
model: string;
|
|
222
|
+
/** What a row opens: a link to the record's page, say. */
|
|
223
|
+
rowHref?(record: AppRecord): string;
|
|
224
|
+
onRowClick?(record: AppRecord): void;
|
|
225
|
+
/** Shown when there are no records (none match, when searching). */
|
|
226
|
+
emptyText?: string;
|
|
227
|
+
/** Whether the search box shows; it does unless false. */
|
|
228
|
+
searchable?: boolean;
|
|
229
|
+
}
|
|
230
|
+
/** A model's records as a table: search, sort by a column's heading, pages, and bulk delete when `selectable`. */
|
|
231
|
+
export declare function AutoTable({ model, rowHref, onRowClick, emptyText, searchable, ...o }: AutoTableProps): import("react").JSX.Element;
|
|
232
|
+
/** One field's control as standard elements, for the plain form. */
|
|
233
|
+
export declare function PlainInput({ field: f }: {
|
|
234
|
+
field: AutoFormField;
|
|
235
|
+
}): import("react").JSX.Element;
|
|
236
|
+
export interface AutoFormProps extends AutoFormOptions {
|
|
237
|
+
model: string;
|
|
238
|
+
/** The submit button's words; Save, or Create for a new record, unless given. */
|
|
239
|
+
submitLabel?: string;
|
|
240
|
+
}
|
|
241
|
+
/** A form for a model's record: its fields from the schema, the API's refusals shown on the fields they're about. */
|
|
242
|
+
export declare function AutoForm({ model, submitLabel, ...o }: AutoFormProps): import("react").JSX.Element;
|