@oaiz/app-kit 0.0.0-stage → 0.1.4130
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/LICENSE +21 -0
- package/README.md +149 -2
- package/THIRD_PARTY_NOTICES.md +76 -0
- package/package.json +139 -4
- package/registry.json +1847 -0
- package/src/components/activity-timeline.tsx +227 -0
- package/src/components/app-shell.tsx +622 -0
- package/src/components/bar-list.tsx +47 -0
- package/src/components/board.tsx +700 -0
- package/src/components/calendar-view.tsx +1388 -0
- package/src/components/card-grid.tsx +462 -0
- package/src/components/charts.tsx +701 -0
- package/src/components/chat-markdown.tsx +230 -0
- package/src/components/chat-panel.tsx +760 -0
- package/src/components/code-editor.tsx +276 -0
- package/src/components/collection-controls.tsx +226 -0
- package/src/components/command-palette.tsx +403 -0
- package/src/components/confirm-action.tsx +123 -0
- package/src/components/data-table.tsx +821 -0
- package/src/components/date-input.tsx +429 -0
- package/src/components/empty-state.tsx +31 -0
- package/src/components/error-state.tsx +37 -0
- package/src/components/file-upload.tsx +595 -0
- package/src/components/filter-bar.tsx +415 -0
- package/src/components/grouped-picker.tsx +250 -0
- package/src/components/line-items.tsx +601 -0
- package/src/components/list-view.tsx +491 -0
- package/src/components/metrics.tsx +53 -0
- package/src/components/page-header.tsx +33 -0
- package/src/components/property-picker.tsx +546 -0
- package/src/components/record-detail.tsx +416 -0
- package/src/components/record-form.tsx +295 -0
- package/src/components/record-name.tsx +85 -0
- package/src/components/save-bar.tsx +142 -0
- package/src/components/sign-in.tsx +179 -0
- package/src/components/split-view.tsx +128 -0
- package/src/components/status-dot.tsx +56 -0
- package/src/components/stepper.tsx +153 -0
- package/src/components/text-diff-model.ts +111 -0
- package/src/components/text-diff.tsx +98 -0
- package/src/components/time-zone-picker.tsx +69 -0
- package/src/components/ui/accordion.tsx +138 -0
- package/src/components/ui/alert-dialog.tsx +67 -0
- package/src/components/ui/alert.tsx +65 -0
- package/src/components/ui/avatar-group.tsx +119 -0
- package/src/components/ui/avatar.tsx +55 -0
- package/src/components/ui/badge.tsx +59 -0
- package/src/components/ui/bottom-navigation.tsx +102 -0
- package/src/components/ui/breadcrumb.tsx +117 -0
- package/src/components/ui/button.tsx +71 -0
- package/src/components/ui/card.tsx +95 -0
- package/src/components/ui/checkbox.tsx +31 -0
- package/src/components/ui/collapsible.tsx +27 -0
- package/src/components/ui/context-menu.tsx +270 -0
- package/src/components/ui/count-up.tsx +136 -0
- package/src/components/ui/detail-disclosure-section.tsx +109 -0
- package/src/components/ui/dialog.tsx +157 -0
- package/src/components/ui/dropdown-menu.tsx +273 -0
- package/src/components/ui/form-field.tsx +155 -0
- package/src/components/ui/heading.tsx +126 -0
- package/src/components/ui/hover-card.tsx +45 -0
- package/src/components/ui/input.tsx +50 -0
- package/src/components/ui/label.tsx +30 -0
- package/src/components/ui/orb-spinner.tsx +238 -0
- package/src/components/ui/popover.tsx +59 -0
- package/src/components/ui/progress-ring.tsx +125 -0
- package/src/components/ui/progress.tsx +38 -0
- package/src/components/ui/radio-group.tsx +124 -0
- package/src/components/ui/resizable.tsx +48 -0
- package/src/components/ui/scroll-fade.tsx +98 -0
- package/src/components/ui/segmented-control.tsx +70 -0
- package/src/components/ui/select.tsx +230 -0
- package/src/components/ui/separator.tsx +29 -0
- package/src/components/ui/sheet.tsx +170 -0
- package/src/components/ui/shimmer-text.tsx +87 -0
- package/src/components/ui/shortcut.tsx +39 -0
- package/src/components/ui/skeleton.tsx +15 -0
- package/src/components/ui/slider.tsx +48 -0
- package/src/components/ui/switch.tsx +44 -0
- package/src/components/ui/table.tsx +119 -0
- package/src/components/ui/tabs.tsx +80 -0
- package/src/components/ui/text.tsx +93 -0
- package/src/components/ui/textarea.tsx +51 -0
- package/src/components/ui/toast.tsx +141 -0
- package/src/components/ui/toaster.tsx +40 -0
- package/src/components/ui/tooltip.tsx +38 -0
- package/src/components/ui/top-banner.tsx +108 -0
- package/src/components/ui/tree-view.tsx +215 -0
- package/src/components/ui/use-toast.tsx +184 -0
- package/src/components/ui/voice-orb.tsx +252 -0
- package/src/hooks/oaiz-chat.tsx +530 -0
- package/src/hooks/oaiz-data.tsx +617 -0
- package/src/hooks/oaiz-session.tsx +63 -0
- package/src/hooks/use-arrivals.ts +19 -0
- package/src/hooks/use-breakpoint.ts +100 -0
- package/src/hooks/use-media-query.ts +36 -0
- package/src/hooks/use-theme.ts +67 -0
- package/src/hooks/use-url-state.ts +77 -0
- package/src/lib/breakpoints.ts +81 -0
- package/src/lib/csp-nonce.ts +15 -0
- package/src/lib/format.ts +291 -0
- package/src/lib/speech-level.ts +118 -0
- package/src/lib/utils.ts +109 -0
- package/src/styles/oaiz/app.css +21 -0
- package/src/styles/oaiz/scrollbars.css +20 -0
- package/src/styles/oaiz/tokens.css +952 -0
- package/src/styles/oaiz/voice-orb.css +39 -0
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell } from './ui/table';
|
|
2
|
+
import { Fragment, useEffect, useId, useRef, useState, type ComponentProps, type KeyboardEvent, type ReactNode } from 'react';
|
|
3
|
+
import { Plus, Trash2 } from 'lucide-react';
|
|
4
|
+
|
|
5
|
+
import { useIsMobile } from '../hooks/use-breakpoint';
|
|
6
|
+
import { formatCurrency, formatPercent } from '../lib/format';
|
|
7
|
+
import { cn } from '../lib/utils';
|
|
8
|
+
import { Button } from './ui/button';
|
|
9
|
+
import { Input } from './ui/input';
|
|
10
|
+
import { Label } from './ui/label';
|
|
11
|
+
import { Skeleton } from './ui/skeleton';
|
|
12
|
+
import { Text } from './ui/text';
|
|
13
|
+
|
|
14
|
+
export type LineItem = {
|
|
15
|
+
id: string | number;
|
|
16
|
+
description: string;
|
|
17
|
+
/** How many; up to three decimals, such as 2.5 hours. `NaN` while the cell holds no number. */
|
|
18
|
+
quantity: number;
|
|
19
|
+
/** The price of one, in major units such as euros. It rounds to the currency's minor unit. */
|
|
20
|
+
unitPrice: number;
|
|
21
|
+
/** Percent: 25 is 25%. */
|
|
22
|
+
taxRate: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type LineItemField = 'description' | 'quantity' | 'unitPrice' | 'taxRate';
|
|
26
|
+
export type LineItemErrors = Partial<Record<LineItemField, string>>;
|
|
27
|
+
|
|
28
|
+
/** The amounts of lines, in whole minor units such as cents: divide by 10^digits for `formatCurrency`. */
|
|
29
|
+
export type LineItemTotals = {
|
|
30
|
+
/** Each line's amounts, in the order of the lines. */
|
|
31
|
+
lines: { net: number; tax: number; total: number }[];
|
|
32
|
+
subtotal: number;
|
|
33
|
+
tax: number;
|
|
34
|
+
total: number;
|
|
35
|
+
/** The tax of each rate, highest rate first. */
|
|
36
|
+
taxes: { rate: number; amount: number }[];
|
|
37
|
+
/** The currency's minor-unit digits: 2 for EUR, 0 for JPY. */
|
|
38
|
+
digits: number;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const QUANTITY_DIGITS = 3;
|
|
42
|
+
const RATE_DIGITS = 2;
|
|
43
|
+
const digitsCache = new Map<string, number>();
|
|
44
|
+
|
|
45
|
+
/** The minor-unit digits of `currency` (ISO 4217): 2 for EUR, 0 for JPY. */
|
|
46
|
+
export function currencyDigits(currency: string): number {
|
|
47
|
+
let digits = digitsCache.get(currency);
|
|
48
|
+
if (digits === undefined) {
|
|
49
|
+
digits = new Intl.NumberFormat('en', { style: 'currency', currency }).resolvedOptions().maximumFractionDigits ?? 2;
|
|
50
|
+
digitsCache.set(currency, digits);
|
|
51
|
+
}
|
|
52
|
+
return digits;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `amount` as a whole number of minor units, rounded half up. It shifts the decimal digits as
|
|
57
|
+
* written, so 1.005 at 2 digits is 101, where `1.005 * 100` gives 100.49999….
|
|
58
|
+
*/
|
|
59
|
+
export function toMinorUnits(amount: number, digits: number): number {
|
|
60
|
+
if (!Number.isFinite(amount)) return 0;
|
|
61
|
+
const [mantissa, exponent = '0'] = String(amount).split('e');
|
|
62
|
+
return Math.round(Number(`${mantissa}e${Number(exponent) + digits}`));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Minor units back to an amount for `formatCurrency`. */
|
|
66
|
+
export function fromMinorUnits(minor: number, digits: number): number {
|
|
67
|
+
return minor / 10 ** digits;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** What is wrong with each cell of a line; empty when the line can be saved. */
|
|
71
|
+
export function lineItemErrors(line: LineItem): LineItemErrors {
|
|
72
|
+
const errors: LineItemErrors = {};
|
|
73
|
+
if (line.description.trim() === '') errors.description = 'Enter a description.';
|
|
74
|
+
if (!Number.isFinite(line.quantity) || line.quantity <= 0) errors.quantity = 'Enter a quantity above 0.';
|
|
75
|
+
if (!Number.isFinite(line.unitPrice) || line.unitPrice < 0) errors.unitPrice = 'Enter a price of 0 or more.';
|
|
76
|
+
if (!Number.isFinite(line.taxRate) || line.taxRate < 0 || line.taxRate > 100) errors.taxRate = 'Enter a tax rate from 0 to 100.';
|
|
77
|
+
return errors;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** True when every line can be saved. */
|
|
81
|
+
export function lineItemsValid(lines: LineItem[]): boolean {
|
|
82
|
+
return lines.every((line) => Object.keys(lineItemErrors(line)).length === 0);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The amounts of `lines` in `currency`, worked out in whole minor units so no float drift creeps
|
|
87
|
+
* in: 3 × 19.99 is exactly 59.97. A line's amount and its tax each round half up to the minor unit,
|
|
88
|
+
* as an invoice prints them, and the totals add the rounded lines. A line with an invalid number
|
|
89
|
+
* counts as nothing.
|
|
90
|
+
*/
|
|
91
|
+
export function lineItemTotals(lines: LineItem[], currency: string): LineItemTotals {
|
|
92
|
+
const digits = currencyDigits(currency);
|
|
93
|
+
const byRate = new Map<number, number>();
|
|
94
|
+
const amounts = lines.map((line) => {
|
|
95
|
+
const errors = lineItemErrors(line);
|
|
96
|
+
if (errors.quantity || errors.unitPrice || errors.taxRate) return { net: 0, tax: 0, total: 0 };
|
|
97
|
+
const quantity = toMinorUnits(line.quantity, QUANTITY_DIGITS);
|
|
98
|
+
const price = toMinorUnits(line.unitPrice, digits);
|
|
99
|
+
const rate = toMinorUnits(line.taxRate, RATE_DIGITS);
|
|
100
|
+
// Each product of whole numbers is exact; the one division rounds once.
|
|
101
|
+
const net = Math.round((quantity * price) / 10 ** QUANTITY_DIGITS);
|
|
102
|
+
const tax = Math.round((net * rate) / (100 * 10 ** RATE_DIGITS));
|
|
103
|
+
if (tax > 0 || rate > 0) byRate.set(line.taxRate, (byRate.get(line.taxRate) ?? 0) + tax);
|
|
104
|
+
return { net, tax, total: net + tax };
|
|
105
|
+
});
|
|
106
|
+
const subtotal = amounts.reduce((sum, line) => sum + line.net, 0);
|
|
107
|
+
const tax = amounts.reduce((sum, line) => sum + line.tax, 0);
|
|
108
|
+
return {
|
|
109
|
+
lines: amounts,
|
|
110
|
+
subtotal,
|
|
111
|
+
tax,
|
|
112
|
+
total: subtotal + tax,
|
|
113
|
+
taxes: [...byRate].map(([rate, amount]) => ({ rate, amount })).sort((a, b) => b.rate - a.rate),
|
|
114
|
+
digits,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** A number as typed: a comma or a point for decimals, spaces ignored. `NaN` for anything else. */
|
|
119
|
+
function parseNumber(text: string): number {
|
|
120
|
+
const cleaned = text.replace(/\s/g, '').replace(',', '.');
|
|
121
|
+
return /^-?(\d+\.?\d*|\.\d+)$/.test(cleaned) ? Number(cleaned) : Number.NaN;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type LineItemsProps = {
|
|
125
|
+
lines: LineItem[];
|
|
126
|
+
/** Every edit, add and remove; keep the lines in state and save them when the member saves. */
|
|
127
|
+
onChange?: (lines: LineItem[]) => void;
|
|
128
|
+
/** ISO 4217, such as "EUR": the amounts' currency and minor unit. */
|
|
129
|
+
currency: string;
|
|
130
|
+
/** Plain text and no add or remove, as for a confirmed order. */
|
|
131
|
+
readOnly?: boolean;
|
|
132
|
+
/** Inputs that cannot change, as while a save is on its way. */
|
|
133
|
+
disabled?: boolean;
|
|
134
|
+
/** Shows every cell's error, not only those the member has left; set it when a save is refused. */
|
|
135
|
+
showErrors?: boolean;
|
|
136
|
+
/** Skeleton lines while the lines load. */
|
|
137
|
+
loading?: boolean;
|
|
138
|
+
/** The tax rate of the first line added to an empty list; later lines take the last line's rate. */
|
|
139
|
+
defaultTaxRate?: number;
|
|
140
|
+
/** More buttons beside Add line, such as a product picker that adds a line. */
|
|
141
|
+
actions?: ReactNode;
|
|
142
|
+
/** The accessible name of the lines. */
|
|
143
|
+
label?: string;
|
|
144
|
+
className?: string;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
type Focus = { index: number; field: LineItemField | 'remove' } | 'add' | null;
|
|
148
|
+
|
|
149
|
+
const NUMBER_INPUT = 'text-right tabular-nums';
|
|
150
|
+
/** Cells read as text until hovered or focused, so a table of inputs stays quiet. */
|
|
151
|
+
const CELL_INPUT = 'h-9 px-2 border-transparent hover:border-input focus-visible:border-input disabled:hover:border-transparent';
|
|
152
|
+
const PHONE_TARGET = 'max-mobile:h-11';
|
|
153
|
+
const FIELD_NAMES: Record<LineItemField, string> = {
|
|
154
|
+
description: 'Description',
|
|
155
|
+
quantity: 'Quantity',
|
|
156
|
+
unitPrice: 'Unit price',
|
|
157
|
+
taxRate: 'Tax %',
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Lines of an order, quote or invoice: description, quantity, unit price and tax, each line's
|
|
162
|
+
* amount, and the subtotal, tax and total under them. Tab moves through the cells; Enter in a
|
|
163
|
+
* line's last cell goes to the next line, or adds one after the last. A removed line can be put
|
|
164
|
+
* back from the notice that replaces it. An error shows once the member leaves a cell, or at once
|
|
165
|
+
* with `showErrors`. On a phone each line is a card.
|
|
166
|
+
*/
|
|
167
|
+
export function LineItems({
|
|
168
|
+
lines,
|
|
169
|
+
onChange,
|
|
170
|
+
currency,
|
|
171
|
+
readOnly = false,
|
|
172
|
+
disabled = false,
|
|
173
|
+
showErrors = false,
|
|
174
|
+
loading = false,
|
|
175
|
+
defaultTaxRate = 0,
|
|
176
|
+
actions,
|
|
177
|
+
label = 'Line items',
|
|
178
|
+
className,
|
|
179
|
+
}: LineItemsProps) {
|
|
180
|
+
const baseId = useId();
|
|
181
|
+
const phone = useIsMobile();
|
|
182
|
+
const root = useRef<HTMLDivElement>(null);
|
|
183
|
+
const addButton = useRef<HTMLButtonElement>(null);
|
|
184
|
+
const nextId = useRef(0);
|
|
185
|
+
const focusAfter = useRef<Focus>(null);
|
|
186
|
+
const [touched, setTouched] = useState<ReadonlySet<string>>(new Set());
|
|
187
|
+
const [removed, setRemoved] = useState<{ line: LineItem; index: number } | null>(null);
|
|
188
|
+
const totals = lineItemTotals(lines, currency);
|
|
189
|
+
const money = (minor: number) => formatCurrency(fromMinorUnits(minor, totals.digits), currency);
|
|
190
|
+
const editable = !readOnly && onChange !== undefined;
|
|
191
|
+
|
|
192
|
+
useEffect(() => {
|
|
193
|
+
const target = focusAfter.current;
|
|
194
|
+
if (target === null) return;
|
|
195
|
+
focusAfter.current = null;
|
|
196
|
+
if (target === 'add') {
|
|
197
|
+
addButton.current?.focus();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
root.current
|
|
201
|
+
?.querySelector<HTMLElement>(`[data-line-index="${target.index}"][data-field="${target.field}"]`)
|
|
202
|
+
?.focus();
|
|
203
|
+
}, [lines]);
|
|
204
|
+
|
|
205
|
+
function change(next: LineItem[]) {
|
|
206
|
+
setRemoved(null);
|
|
207
|
+
onChange?.(next);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function edit(index: number, values: Partial<LineItem>) {
|
|
211
|
+
change(lines.map((line, at) => (at === index ? { ...line, ...values } : line)));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function add() {
|
|
215
|
+
nextId.current += 1;
|
|
216
|
+
const last = lines[lines.length - 1];
|
|
217
|
+
focusAfter.current = { index: lines.length, field: 'description' };
|
|
218
|
+
change([
|
|
219
|
+
...lines,
|
|
220
|
+
{
|
|
221
|
+
id: `${baseId}new${nextId.current}`,
|
|
222
|
+
description: '',
|
|
223
|
+
quantity: 1,
|
|
224
|
+
unitPrice: Number.NaN,
|
|
225
|
+
taxRate: last && Number.isFinite(last.taxRate) ? last.taxRate : defaultTaxRate,
|
|
226
|
+
},
|
|
227
|
+
]);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function remove(index: number) {
|
|
231
|
+
focusAfter.current = lines.length > 1 ? { index: Math.min(index, lines.length - 2), field: 'remove' } : 'add';
|
|
232
|
+
onChange?.(lines.filter((_, at) => at !== index));
|
|
233
|
+
setRemoved({ line: lines[index]!, index });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function undo() {
|
|
237
|
+
if (!removed) return;
|
|
238
|
+
const index = Math.min(removed.index, lines.length);
|
|
239
|
+
focusAfter.current = { index, field: 'description' };
|
|
240
|
+
change([...lines.slice(0, index), removed.line, ...lines.slice(index)]);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function onLastCellKey(event: KeyboardEvent<HTMLInputElement>, index: number) {
|
|
244
|
+
if (event.key !== 'Enter') return;
|
|
245
|
+
event.preventDefault();
|
|
246
|
+
if (index === lines.length - 1) add();
|
|
247
|
+
else root.current?.querySelector<HTMLElement>(`[data-line-index="${index + 1}"][data-field="description"]`)?.focus();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const touch = (line: LineItem, field: LineItemField) => {
|
|
251
|
+
const key = `${line.id}:${field}`;
|
|
252
|
+
if (!touched.has(key)) setTouched(new Set(touched).add(key));
|
|
253
|
+
};
|
|
254
|
+
const shownErrors = (line: LineItem): LineItemErrors => {
|
|
255
|
+
const errors = lineItemErrors(line);
|
|
256
|
+
if (showErrors) return errors;
|
|
257
|
+
return Object.fromEntries(
|
|
258
|
+
Object.entries(errors).filter(([field]) => touched.has(`${line.id}:${field}`)),
|
|
259
|
+
) as LineItemErrors;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const cell = (line: LineItem, index: number, field: LineItemField, errors: LineItemErrors) => {
|
|
263
|
+
const error = errors[field];
|
|
264
|
+
const props = {
|
|
265
|
+
id: `${baseId}-${index}-${field}`,
|
|
266
|
+
'data-line-index': index,
|
|
267
|
+
'data-field': field,
|
|
268
|
+
disabled,
|
|
269
|
+
'aria-invalid': error ? true : undefined,
|
|
270
|
+
'aria-describedby': error ? `${baseId}-${index}-${field}-error` : undefined,
|
|
271
|
+
onBlur: () => touch(line, field),
|
|
272
|
+
className: cn(phone ? PHONE_TARGET : CELL_INPUT, field !== 'description' && NUMBER_INPUT),
|
|
273
|
+
};
|
|
274
|
+
if (field === 'description') {
|
|
275
|
+
return (
|
|
276
|
+
<Input
|
|
277
|
+
{...props}
|
|
278
|
+
autoComplete="off"
|
|
279
|
+
aria-label={phone ? undefined : `Description, line ${index + 1}`}
|
|
280
|
+
value={line.description}
|
|
281
|
+
onChange={(event) => edit(index, { description: event.target.value })}
|
|
282
|
+
/>
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return (
|
|
286
|
+
<NumberInput
|
|
287
|
+
{...props}
|
|
288
|
+
aria-label={phone ? undefined : `${FIELD_NAMES[field]}, line ${index + 1}`}
|
|
289
|
+
value={line[field]}
|
|
290
|
+
format={(value) =>
|
|
291
|
+
field === 'unitPrice'
|
|
292
|
+
? fromMinorUnits(toMinorUnits(value, totals.digits), totals.digits).toFixed(totals.digits)
|
|
293
|
+
: String(value)
|
|
294
|
+
}
|
|
295
|
+
onValue={(value) => edit(index, { [field]: value } as Partial<LineItem>)}
|
|
296
|
+
onKeyDown={field === 'taxRate' ? (event) => onLastCellKey(event, index) : undefined}
|
|
297
|
+
/>
|
|
298
|
+
);
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const errorText = (index: number, field: LineItemField, message: string | undefined) =>
|
|
302
|
+
message && (
|
|
303
|
+
<Text key={field} id={`${baseId}-${index}-${field}-error`} as="span" size="xs" variant="destructive">
|
|
304
|
+
{message}
|
|
305
|
+
</Text>
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
const removeButton = (line: LineItem, index: number) => (
|
|
309
|
+
<Button
|
|
310
|
+
type="button"
|
|
311
|
+
variant="ghost"
|
|
312
|
+
size="icon"
|
|
313
|
+
data-line-index={index}
|
|
314
|
+
data-field="remove"
|
|
315
|
+
disabled={disabled}
|
|
316
|
+
aria-label={`Remove ${line.description.trim() || `line ${index + 1}`}`}
|
|
317
|
+
onClick={() => remove(index)}
|
|
318
|
+
className={cn('text-muted-foreground hover:text-destructive-text', phone ? 'size-11' : 'size-8')}
|
|
319
|
+
>
|
|
320
|
+
<Trash2 />
|
|
321
|
+
</Button>
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
const fields: LineItemField[] = ['description', 'quantity', 'unitPrice', 'taxRate'];
|
|
325
|
+
const noLines = !loading && lines.length === 0;
|
|
326
|
+
|
|
327
|
+
return (
|
|
328
|
+
<div ref={root} className={cn('flex flex-col gap-stack', className)}>
|
|
329
|
+
{phone ? (
|
|
330
|
+
<ol aria-label={label} className="flex flex-col gap-inline">
|
|
331
|
+
{loading &&
|
|
332
|
+
[0, 1].map((row) => (
|
|
333
|
+
<li key={row} className="flex flex-col gap-inline rounded-lg border p-inset">
|
|
334
|
+
<Skeleton className="h-4 w-2/3" />
|
|
335
|
+
<Skeleton className="h-4 w-1/3" />
|
|
336
|
+
</li>
|
|
337
|
+
))}
|
|
338
|
+
{noLines && (
|
|
339
|
+
<li className="rounded-lg border border-dashed p-inset text-center">
|
|
340
|
+
<Text size="sm" variant="muted">
|
|
341
|
+
{editable ? 'No lines yet.' : 'No lines.'}
|
|
342
|
+
</Text>
|
|
343
|
+
</li>
|
|
344
|
+
)}
|
|
345
|
+
{!loading &&
|
|
346
|
+
lines.map((line, index) => {
|
|
347
|
+
const amount = money(totals.lines[index]!.net);
|
|
348
|
+
if (!editable) {
|
|
349
|
+
return (
|
|
350
|
+
<li key={line.id} className="flex items-start justify-between gap-stack rounded-lg border p-inset">
|
|
351
|
+
<div className="flex min-w-0 flex-col gap-0.5">
|
|
352
|
+
<Text size="sm" weight="medium" className="break-words">
|
|
353
|
+
{line.description}
|
|
354
|
+
</Text>
|
|
355
|
+
<Text size="xs" variant="muted" className="tabular-nums">
|
|
356
|
+
{line.quantity} × {money(toMinorUnits(line.unitPrice, totals.digits))} ·{' '}
|
|
357
|
+
{formatPercent(line.taxRate / 100, { maximumFractionDigits: 2 })} tax
|
|
358
|
+
</Text>
|
|
359
|
+
</div>
|
|
360
|
+
<Text size="sm" weight="medium" className="shrink-0 tabular-nums">
|
|
361
|
+
{amount}
|
|
362
|
+
</Text>
|
|
363
|
+
</li>
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
const errors = shownErrors(line);
|
|
367
|
+
return (
|
|
368
|
+
<li key={line.id} className="flex flex-col gap-inset rounded-lg border p-inset">
|
|
369
|
+
{(['description', 'numbers'] as const).map((group) => (
|
|
370
|
+
<div key={group} className={group === 'numbers' ? 'grid grid-cols-3 gap-inline' : 'contents'}>
|
|
371
|
+
{fields
|
|
372
|
+
.filter((field) => (field === 'description') === (group === 'description'))
|
|
373
|
+
.map((field) => (
|
|
374
|
+
<div key={field} className="flex min-w-0 flex-col gap-1.5">
|
|
375
|
+
<Label htmlFor={`${baseId}-${index}-${field}`}>
|
|
376
|
+
{FIELD_NAMES[field]}
|
|
377
|
+
<span className="sr-only">, line {index + 1}</span>
|
|
378
|
+
</Label>
|
|
379
|
+
{cell(line, index, field, errors)}
|
|
380
|
+
{errorText(index, field, errors[field])}
|
|
381
|
+
</div>
|
|
382
|
+
))}
|
|
383
|
+
</div>
|
|
384
|
+
))}
|
|
385
|
+
<div className="flex items-center justify-between gap-inline">
|
|
386
|
+
<Text size="sm" variant="muted">
|
|
387
|
+
Amount <span className="font-medium text-foreground tabular-nums">{amount}</span>
|
|
388
|
+
</Text>
|
|
389
|
+
{removeButton(line, index)}
|
|
390
|
+
</div>
|
|
391
|
+
</li>
|
|
392
|
+
);
|
|
393
|
+
})}
|
|
394
|
+
</ol>
|
|
395
|
+
) : (
|
|
396
|
+
<div className="overflow-x-auto">
|
|
397
|
+
<Table containerClassName="overflow-visible" aria-label={label} className="w-full min-w-[34rem] table-fixed border-collapse text-sm">
|
|
398
|
+
<colgroup>
|
|
399
|
+
<col />
|
|
400
|
+
<col className="w-24" />
|
|
401
|
+
<col className="w-32" />
|
|
402
|
+
<col className="w-24" />
|
|
403
|
+
<col className="w-32" />
|
|
404
|
+
{editable && <col className="w-12" />}
|
|
405
|
+
</colgroup>
|
|
406
|
+
<TableHeader>
|
|
407
|
+
<TableRow className="border-b">
|
|
408
|
+
<Head>Description</Head>
|
|
409
|
+
<Head number>Quantity</Head>
|
|
410
|
+
<Head number>Unit price</Head>
|
|
411
|
+
<Head number>Tax %</Head>
|
|
412
|
+
<Head number>Amount</Head>
|
|
413
|
+
{editable && (
|
|
414
|
+
<TableHead scope="col">
|
|
415
|
+
<span className="sr-only">Remove</span>
|
|
416
|
+
</TableHead>
|
|
417
|
+
)}
|
|
418
|
+
</TableRow>
|
|
419
|
+
</TableHeader>
|
|
420
|
+
<TableBody>
|
|
421
|
+
{loading &&
|
|
422
|
+
[0, 1, 2].map((row) => (
|
|
423
|
+
<TableRow key={row} className="border-b">
|
|
424
|
+
{[0, 1, 2, 3, 4].map((column) => (
|
|
425
|
+
<TableCell key={column} className="px-inline py-inset">
|
|
426
|
+
<Skeleton className="h-4 w-full" />
|
|
427
|
+
</TableCell>
|
|
428
|
+
))}
|
|
429
|
+
{editable && <TableCell />}
|
|
430
|
+
</TableRow>
|
|
431
|
+
))}
|
|
432
|
+
{noLines && (
|
|
433
|
+
<TableRow className="border-b">
|
|
434
|
+
<TableCell colSpan={editable ? 6 : 5} className="px-inline py-stack text-center">
|
|
435
|
+
<Text size="sm" variant="muted">
|
|
436
|
+
{editable ? 'No lines yet.' : 'No lines.'}
|
|
437
|
+
</Text>
|
|
438
|
+
</TableCell>
|
|
439
|
+
</TableRow>
|
|
440
|
+
)}
|
|
441
|
+
{!loading &&
|
|
442
|
+
lines.map((line, index) => {
|
|
443
|
+
const amount = money(totals.lines[index]!.net);
|
|
444
|
+
if (!editable) {
|
|
445
|
+
return (
|
|
446
|
+
<TableRow key={line.id} className="border-b align-top">
|
|
447
|
+
<TableCell className="px-inline py-inset break-words">{line.description}</TableCell>
|
|
448
|
+
<TableCell className="px-inline py-inset text-right tabular-nums">{line.quantity}</TableCell>
|
|
449
|
+
<TableCell className="px-inline py-inset text-right tabular-nums">
|
|
450
|
+
{money(toMinorUnits(line.unitPrice, totals.digits))}
|
|
451
|
+
</TableCell>
|
|
452
|
+
<TableCell className="px-inline py-inset text-right tabular-nums">
|
|
453
|
+
{formatPercent(line.taxRate / 100, { maximumFractionDigits: 2 })}
|
|
454
|
+
</TableCell>
|
|
455
|
+
<TableCell className="px-inline py-inset text-right tabular-nums">{amount}</TableCell>
|
|
456
|
+
</TableRow>
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
const errors = shownErrors(line);
|
|
460
|
+
const messages = fields.map((field) => errorText(index, field, errors[field])).filter(Boolean);
|
|
461
|
+
return [
|
|
462
|
+
<TableRow key={line.id} className={cn(messages.length === 0 && 'border-b')}>
|
|
463
|
+
{fields.map((field) => (
|
|
464
|
+
<TableCell key={field} className="p-1">
|
|
465
|
+
{cell(line, index, field, errors)}
|
|
466
|
+
</TableCell>
|
|
467
|
+
))}
|
|
468
|
+
<TableCell className="px-inline py-1 text-right tabular-nums">{amount}</TableCell>
|
|
469
|
+
<TableCell className="p-1 text-center">{removeButton(line, index)}</TableCell>
|
|
470
|
+
</TableRow>,
|
|
471
|
+
messages.length > 0 && (
|
|
472
|
+
<TableRow key={`${line.id}-errors`} className="border-b">
|
|
473
|
+
<TableCell colSpan={6} className="px-inline pb-inline">
|
|
474
|
+
<span className="flex flex-wrap gap-x-stack gap-y-0.5">{messages}</span>
|
|
475
|
+
</TableCell>
|
|
476
|
+
</TableRow>
|
|
477
|
+
),
|
|
478
|
+
];
|
|
479
|
+
})}
|
|
480
|
+
</TableBody>
|
|
481
|
+
</Table>
|
|
482
|
+
</div>
|
|
483
|
+
)}
|
|
484
|
+
|
|
485
|
+
<div role="status" className={cn(!removed && 'sr-only')}>
|
|
486
|
+
{removed && (
|
|
487
|
+
<span className="flex items-center gap-inline rounded-md bg-muted px-inset py-1">
|
|
488
|
+
<Text as="span" size="sm" className="min-w-0 flex-1 truncate">
|
|
489
|
+
Removed {removed.line.description.trim() ? `“${removed.line.description.trim()}”` : 'a line'}.
|
|
490
|
+
</Text>
|
|
491
|
+
<Button type="button" variant="ghost" size="xs" onClick={undo} className={PHONE_TARGET}>
|
|
492
|
+
Undo
|
|
493
|
+
</Button>
|
|
494
|
+
</span>
|
|
495
|
+
)}
|
|
496
|
+
</div>
|
|
497
|
+
|
|
498
|
+
<div className="flex flex-col gap-stack mobile:flex-row mobile:items-start mobile:justify-between">
|
|
499
|
+
{editable && (
|
|
500
|
+
<div className="flex flex-wrap items-center gap-inline">
|
|
501
|
+
<Button ref={addButton} type="button" variant="outline" size="sm" onClick={add} disabled={disabled} className={PHONE_TARGET}>
|
|
502
|
+
<Plus />
|
|
503
|
+
Add line
|
|
504
|
+
</Button>
|
|
505
|
+
{actions}
|
|
506
|
+
</div>
|
|
507
|
+
)}
|
|
508
|
+
<div role="group" aria-label="Totals" className="w-full mobile:ml-auto mobile:w-auto mobile:min-w-64">
|
|
509
|
+
<dl className="grid grid-cols-[1fr_auto] gap-y-inline">
|
|
510
|
+
{loading ? (
|
|
511
|
+
<Skeleton className="col-span-2 h-16 w-full" />
|
|
512
|
+
) : (
|
|
513
|
+
<>
|
|
514
|
+
<Text asChild size="sm" variant="muted">
|
|
515
|
+
<dt>Subtotal</dt>
|
|
516
|
+
</Text>
|
|
517
|
+
<Text asChild size="sm" className="pl-section text-right tabular-nums">
|
|
518
|
+
<dd>{money(totals.subtotal)}</dd>
|
|
519
|
+
</Text>
|
|
520
|
+
{totals.taxes.length === 0 ? (
|
|
521
|
+
<>
|
|
522
|
+
<Text asChild size="sm" variant="muted">
|
|
523
|
+
<dt>Tax</dt>
|
|
524
|
+
</Text>
|
|
525
|
+
<Text asChild size="sm" className="pl-section text-right tabular-nums">
|
|
526
|
+
<dd>{money(0)}</dd>
|
|
527
|
+
</Text>
|
|
528
|
+
</>
|
|
529
|
+
) : (
|
|
530
|
+
totals.taxes.map((tax) => (
|
|
531
|
+
<Fragment key={tax.rate}>
|
|
532
|
+
<Text asChild size="sm" variant="muted">
|
|
533
|
+
<dt>Tax {formatPercent(tax.rate / 100, { maximumFractionDigits: 2 })}</dt>
|
|
534
|
+
</Text>
|
|
535
|
+
<Text asChild size="sm" className="pl-section text-right tabular-nums">
|
|
536
|
+
<dd>{money(tax.amount)}</dd>
|
|
537
|
+
</Text>
|
|
538
|
+
</Fragment>
|
|
539
|
+
))
|
|
540
|
+
)}
|
|
541
|
+
<Text asChild size="md" weight="semibold" className="border-t pt-inline">
|
|
542
|
+
<dt>Total</dt>
|
|
543
|
+
</Text>
|
|
544
|
+
<Text asChild size="md" weight="semibold" className="border-t pl-section pt-inline text-right tabular-nums">
|
|
545
|
+
<dd>{money(totals.total)}</dd>
|
|
546
|
+
</Text>
|
|
547
|
+
</>
|
|
548
|
+
)}
|
|
549
|
+
</dl>
|
|
550
|
+
</div>
|
|
551
|
+
</div>
|
|
552
|
+
</div>
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function Head({ children, number = false }: { children: ReactNode; number?: boolean }) {
|
|
557
|
+
return (
|
|
558
|
+
<Text asChild size="xs" weight="medium" variant="muted">
|
|
559
|
+
<TableHead scope="col" className={cn('px-inline py-inline text-left', number && 'text-right')}>
|
|
560
|
+
{children}
|
|
561
|
+
</TableHead>
|
|
562
|
+
</Text>
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* A number cell that keeps the text as typed, so "2." or "abc" stays on screen while `onValue`
|
|
568
|
+
* reports `NaN` or the number so far. A new value from outside replaces text it no longer matches.
|
|
569
|
+
*/
|
|
570
|
+
function NumberInput({
|
|
571
|
+
value,
|
|
572
|
+
format,
|
|
573
|
+
onValue,
|
|
574
|
+
onBlur,
|
|
575
|
+
...props
|
|
576
|
+
}: Omit<ComponentProps<typeof Input>, 'value' | 'onChange'> & {
|
|
577
|
+
value: number;
|
|
578
|
+
format: (value: number) => string;
|
|
579
|
+
onValue: (value: number) => void;
|
|
580
|
+
}) {
|
|
581
|
+
const [text, setText] = useState<string | null>(null);
|
|
582
|
+
const current = text !== null && Object.is(parseNumber(text), value) ? text : null;
|
|
583
|
+
return (
|
|
584
|
+
<Input
|
|
585
|
+
{...props}
|
|
586
|
+
type="text"
|
|
587
|
+
inputMode="decimal"
|
|
588
|
+
autoComplete="off"
|
|
589
|
+
value={current ?? (Number.isFinite(value) ? format(value) : '')}
|
|
590
|
+
onFocus={(event) => event.currentTarget.select()}
|
|
591
|
+
onChange={(event) => {
|
|
592
|
+
setText(event.target.value);
|
|
593
|
+
onValue(parseNumber(event.target.value));
|
|
594
|
+
}}
|
|
595
|
+
onBlur={(event) => {
|
|
596
|
+
if (current !== null && Number.isFinite(parseNumber(current))) setText(null);
|
|
597
|
+
onBlur?.(event);
|
|
598
|
+
}}
|
|
599
|
+
/>
|
|
600
|
+
);
|
|
601
|
+
}
|