The lines of an invoice or quote typed in place, with quantity, price and tax, and totals that follow as you type.
- 6.0 kB JS 5.0 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 10 tests
Invoice INV-2041
- Subtotal
- €7,202.00
- Tax 7% on €460.00
- €32.20
- Tax 19% on €6,700.00
- €1,273.00
- Total
- €8,507.20
Enter on the last line starts a new one.
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { LineItems } from "./ui/uicontext/components/LineItems.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ wrap: { inlineSize: "min(100%, 52rem)" } })// a studio's invoice in euros: services at the standard rate, a printed book at the reduced oneexport const StudioInvoice = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.wrap)).children( LineItems(h, { key: "invoice", props: { label: "Invoice INV-2041", currency: "EUR", locale: "en-IE", taxRates: [0, 0.07, 0.19], defaultTaxRate: 0.19, value: [ { description: "Brand workshop, two days", quantity: 2, unitPrice: 1450, taxRate: 0.19 }, { description: "Logo and type system", quantity: 1, unitPrice: 3800, taxRate: 0.19 }, { description: "Printed brand book", quantity: 25, unitPrice: 18.4, taxRate: 0.07 }, { description: "Courier to Lisbon", quantity: 1, unitPrice: 42, taxRate: 0 }, ], }, }), )When to use
The lines of an invoice or quote typed in place, with quantity, price and tax, and totals that follow as you type. Use Order Summary instead when the lines are settled and only read, as on a receipt.
Usage
import { LineItems, type LineItem } from "./ui/uicontext/components/LineItems.ts"export const InvoiceLines = <M>(h: Html.H<M>, lines: ReadonlyArray<LineItem>, onChange: (lines: ReadonlyArray<LineItem>, total: number) => M) => LineItems(h, { props: { label: "Invoice lines", value: lines, currency: "EUR", taxRates: [0, 0.07, 0.19] }, onEmit: { RequestedChange: ({ value, total }) => onChange(value, total) }, })Examples
Starting empty
No taxRates, so no tax column. Type a line and press Enter for the next.
Quote for Harbor Street Bakery
- Subtotal
- $0.00
- Total
- $0.00
Enter on the last line starts a new one.
Nothing to send yet.
This example also uses Button. Install it too:
npx shadcn@latest add checkfu/uicontext/buttonimport { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { formatMoney } from "./ui/uicontext/components/domain/Format.ts"import { LineItem, LineItems } from "./ui/uicontext/components/LineItems.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ wrap: { display: "grid", gap: space.lg, inlineSize: "min(100%, 44rem)" }, foot: { display: "flex", flexWrap: "wrap", alignItems: "center", justifyContent: "space-between", gap: space.md, fontSize: font.sm, color: color.muted },})const Message = UiSchema.TaggedEnum({ Edited: { lines: Schema.Array(LineItem), total: Schema.Number } })// a quote started empty: type a line, press Enter, type the next; the app keeps the lines and totalexport const NewQuote = Html.component({ name: "NewQuote", model: Schema.Struct({ lines: Schema.Array(LineItem), total: Schema.Number }), message: Message,}).make({ init: () => ({ lines: [], total: 0 }), update: { Edited: ($, { lines, total }) => $.evolve({ lines, total }) }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( LineItems(h, { key: "quote", props: { id: "quote", label: "Quote for Harbor Street Bakery", value: [] }, onEmit: { RequestedChange: ({ value, total }) => Message.Edited({ lines: value, total }) }, }), h.div.attrs(sx(styles.foot)).children( h.span.children(model.lines.length === 0 ? "Nothing to send yet." : `Ready to send: ${formatMoney(model.total)}`), Button(h, { tone: "primary" }).disabled(model.lines.length === 0).children("Send quote"), ), ),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | What the lines belong to: "Invoice INV-2041". |
valuerequired | LineItem[] | — | The lines to start with. |
currency | string | "USD" | ISO 4217 code; "USD" by default. |
taxRates | number[] | — | Tax rates a line can take, as fractions. Without them there's no tax column. |
defaultTaxRate | number | taxRates | The rate a new line starts with; the first of taxRates by default. |
id | string | "lines" | Starts the ids inside it, unique on the page; "lines" by default. |
locale | string | — | For amounts and percents; the browser's by default. |
Events
| Event | Payload | Description |
|---|---|---|
RequestedChange | { value: LineItem[]; total: number } | The lines changed: every line with a description or an amount, in order, and the total. |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/line-items