Skip to content

Commit 5b3e3ad

Browse files
authored
feat(grid): add embeddable select grid component (#89)
* feat(grid): add embeddable select grid component Expose `SelectGrid` on the grid capability: the driven grid as a component a host plugin renders inside a render session of its own. On mount it reads the dialogs capability, failing that render with an error naming the count when there is not exactly one provider, and maps its request with `selectRequest` once. It renders the dialogs' embeddable `Select` with the host's `width`, `height`, and `isActive`, and maps a submitted choice back through `selection`, so `onSubmit` receives exactly what `select` resolves with. It is created once at registration so every read yields the same component. The contract gains `SelectGridProps<T, A>`, built over `EmbeddedProps` imported type-only from `@fx/tx/dialogs`, and the `SelectGrid` member, typed as returning `unknown`. The demo tests' stub providers gain the member, and the boundary test's exact list of contract type names gains `SelectGridProps`. * test(grid): drive the embeddable select grid from a host session Render `grid.SelectGrid`, read from the registry, inside a host session on a keyboard and screen of its own while the command's streams stay captured. Cover acting on a row, a row offering no actions, cancelling with Escape, an inactive grid taking no keys until activated, the host's height and width bounding what it draws, and the render failing with the provider count when the dialogs provider is absent or repeated. * test(grid): type the embeddable select grid through the published contract The packed consumer fixture types `SelectGridProps`, which resolves the grid contract through the dialogs contract in the tarball, and builds a `grid.SelectGrid` element with the injected React, cast at `createElement` as the contract requires of a host. * docs: document the embeddable select grid component Document `SelectGrid` in the plugin guide's grid section: the shared embedded props it takes, the select it is drawn as, what `onSubmit` receives, the dialogs lookup on mount, and the cast at `createElement`, with a host example. Mark the grid spec's embeddable selection implemented, add a changelog row, tick the second task of change 0038, and set the change complete in the docs index. * docs: record the select grid pull request on change 0038
1 parent 95e27cf commit 5b3e3ad

13 files changed

Lines changed: 641 additions & 19 deletions

‎docs/changes/0038-add-embeddable-grid-components.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Expose the grid as two components a plugin renders inside its own render session: `Grid`, the printed lines as an element laid out for a given width, and `SelectGrid`, the driven grid over the dialogs' embeddable `Select`. `print` and `select` stay as they are.
66

77
**Spec:** [Grid](../specs/grid/)
8-
**Status:** draft
8+
**Status:** complete
99
**Depends On:** 0029, 0030, 0036
1010

1111
## Motivation
@@ -78,13 +78,13 @@ Skipping or weakening any of these rules to land the PR MUST be treated as a bug
7878
- [x] `test/plugin-consumer.test.ts`: the consumer fixture types `Grid` through the published contract; `test/plugin-boundary.test.ts` keeps passing
7979
- [x] [`docs/manual/plugins.md`](../manual/plugins.md#use-the-bundled-grid-capability): document `Grid`
8080
- [x] [Grid](../specs/grid/) spec: mark [Embeddable Grid](../specs/grid/index.md#embeddable-grid) implemented and add a changelog row
81-
- [ ] Add the embeddable `SelectGrid` component (after [Change 0036](./0036-add-embeddable-dialog-components.md))
82-
- [ ] `plugins/grid/contract.ts`: `SelectGridProps<T, A>` importing `EmbeddedProps` type-only from `@fx/tx/dialogs`, and the `SelectGrid` member
83-
- [ ] `plugins/grid/index.ts` and `plugins/grid/select.ts`: the component over the dialogs' `Select`, mapping the request on mount and the answer on submit
84-
- [ ] `test/grid-embedded.test.ts`: every scenario in [Grid: Embeddable Selection](../specs/grid/index.md#embeddable-selection) — acting on a row, cancelling, and the absent dialogs provider — plus `isActive`, `height`, and a row offering no actions
85-
- [ ] `test/plugin-consumer.test.ts`: the consumer fixture types `SelectGrid`, resolving the grid contract through the dialogs contract in the packed tarball
86-
- [ ] [`docs/manual/plugins.md`](../manual/plugins.md#use-the-bundled-grid-capability): document `SelectGrid`
87-
- [ ] [Grid](../specs/grid/) spec: mark [Embeddable Selection](../specs/grid/index.md#embeddable-selection) implemented and add a changelog row; set this change `complete` in `docs/index.yml` and `docs/index.md`
81+
- [x] Add the embeddable `SelectGrid` component (after [Change 0036](./0036-add-embeddable-dialog-components.md)) (PR #89)
82+
- [x] `plugins/grid/contract.ts`: `SelectGridProps<T, A>` importing `EmbeddedProps` type-only from `@fx/tx/dialogs`, and the `SelectGrid` member
83+
- [x] `plugins/grid/index.ts` and `plugins/grid/select.ts`: the component over the dialogs' `Select`, mapping the request on mount and the answer on submit
84+
- [x] `test/grid-embedded.test.ts`: every scenario in [Grid: Embeddable Selection](../specs/grid/index.md#embeddable-selection) — acting on a row, cancelling, and the absent dialogs provider — plus `isActive`, `height`, and a row offering no actions
85+
- [x] `test/plugin-consumer.test.ts`: the consumer fixture types `SelectGrid`, resolving the grid contract through the dialogs contract in the packed tarball
86+
- [x] [`docs/manual/plugins.md`](../manual/plugins.md#use-the-bundled-grid-capability): document `SelectGrid`
87+
- [x] [Grid](../specs/grid/) spec: mark [Embeddable Selection](../specs/grid/index.md#embeddable-selection) implemented and add a changelog row; set this change `complete` in `docs/index.yml` and `docs/index.md`
8888

8989
## References
9090

‎docs/index.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@
5353
| 0035 | [Production React and Cached Text Widths](changes/0035-production-react-and-cached-widths.md) | [Architecture](specs/architecture/) | complete | 0033 |
5454
| 0036 | [Add Embeddable Dialog Components](changes/0036-add-embeddable-dialog-components.md) | [Dialogs](specs/dialogs/) | complete | 0017, 0023, 0031, 0033 |
5555
| 0037 | [Add Multi-Select](changes/0037-add-multi-select.md) | [Dialogs](specs/dialogs/) | draft | 0027, 0036 |
56-
| 0038 | [Add Embeddable Grid Components](changes/0038-add-embeddable-grid-components.md) | [Grid](specs/grid/) | draft | 0029, 0030, 0036 |
56+
| 0038 | [Add Embeddable Grid Components](changes/0038-add-embeddable-grid-components.md) | [Grid](specs/grid/) | complete | 0029, 0030, 0036 |

‎docs/index.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ changes:
337337
path: changes/0038-add-embeddable-grid-components.md
338338
description: Expose the grid as a Grid component drawing the printed lines for a given width and a SelectGrid component driving rows over the dialogs' embeddable Select, leaving print and select unchanged.
339339
spec: grid
340-
status: draft
340+
status: complete
341341
depends_on:
342342
- "0029"
343343
- "0030"

‎docs/manual/plugins.md‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,11 @@ import type {
646646
GridSelectRow,
647647
OutputStream,
648648
Row,
649+
SelectGridProps,
649650
} from "@fx/tx/grid";
650651
```
651652

652-
`Grid` is what the key carries: `print(request)` writing a `GridRequest` — a `stream`, an optional `layout` of `"table"` or `"flow"`, optional `headers`, the `rows`, an optional `empty` message, and an optional `summary` — and `select(request)` driving a `GridSelectRequest` of a `message`, optional `headers`, and `GridSelectRow`s, each carrying its `cells`, the `value` identifying it, and the `GridAction`s it offers, resolving to a `GridSelection` of the chosen `value` and `action` or to `undefined`. It also carries `Grid`, the printed grid as a component taking `GridProps`, described below. A `Row` is a list of `Cell`s or bare strings, and an `OutputStream` is anything that can be written to and may report its `columns` and `isTTY`.
653+
`Grid` is what the key carries: `print(request)` writing a `GridRequest` — a `stream`, an optional `layout` of `"table"` or `"flow"`, optional `headers`, the `rows`, an optional `empty` message, and an optional `summary` — and `select(request)` driving a `GridSelectRequest` of a `message`, optional `headers`, and `GridSelectRow`s, each carrying its `cells`, the `value` identifying it, and the `GridAction`s it offers, resolving to a `GridSelection` of the chosen `value` and `action` or to `undefined`. It also carries `Grid`, the printed grid as a component taking `GridProps`, and `SelectGrid`, the driven grid as a component taking `SelectGridProps`, both described below. A `Row` is a list of `Cell`s or bare strings, and an `OutputStream` is anything that can be written to and may report its `columns` and `isTTY`.
653654

654655
A consumer reads `registrations<Grid>("@fx/tx/grid")` inside its command action, after initialization has committed every provider. The contract is types only, and it declares a cell's `variable` as [Theming](#use-the-bundled-theme-capability)'s own `ThemeVariable` by importing `@fx/tx/theme` rather than restating that vocabulary, so a variable renamed there is renamed in every cell that can name one. The grid resolves a theme for the stream it is printing to through the theme capability above, so a `tx` composed without exactly one theme provider fails the call with an error naming the count rather than choosing an appearance of its own. It uses the injected React and Ink instances and never reaches for the process's own streams.
655656

@@ -766,6 +767,45 @@ const plugin: Plugin = ({ command, context, dependencies, registrations }) => {
766767
};
767768
```
768769

770+
`SelectGrid` is `select` as a component, for the same plugin. It takes `SelectGridProps<T, A>`: the `GridSelectRequest` you would pass to `select` as its `request`, and the `width`, `height`, `isActive`, `onSubmit`, and `onCancel` every embeddable component that takes keys shares — the `EmbeddedProps` of [Embed a dialog in your own interface](#embed-a-dialog-in-your-own-interface), which the grid contract imports from `@fx/tx/dialogs` rather than restating, so they mean exactly what they mean for `dialogs.Select`. It draws its request as that embedded `Select`, given your `width`, `height`, and `isActive`, so every rule above about selecting — the padded rows, the dropped `variable` and `align`, a row's actions as the column it opens, a row with no actions taken on its own — holds for it, and so does every rule that section states for an embedded `Select`: it is one-shot, it reads your callbacks on every call, it presents the request it was mounted with, and it answers keys only while it is active. `onSubmit` receives exactly the `GridSelection` `select` would resolve with, its `action` key absent where the row offered none, and `onCancel` is called where `select` would resolve `undefined`.
771+
772+
It reads the dialogs capability when it mounts, while your command runs, and fails that render — which your session's error handling sees like any other render failure — with an error naming the count where a `tx` is composed without exactly one dialogs provider, rather than falling back to anything. It writes to no stream and needs no interactive one, so the handover guarantee above is `select`'s alone: a process you start after `onSubmit` inherits a terminal your session holds, and handing it over is yours. Every read of the capability yields the same `SelectGrid`. Cast it where you hand it to `createElement`, exactly as `Grid`:
773+
774+
```ts
775+
import type { Grid, GridSelection, SelectGridProps } from "@fx/tx/grid";
776+
import type { ReactNode } from "react";
777+
778+
// Inside your command action, with `grid`, `react`, and `ink` in hand.
779+
const ServicePicker = grid.SelectGrid as (
780+
props: SelectGridProps<Service, "connect" | "logs">,
781+
) => ReactNode;
782+
783+
function Picker({ onPicked }: {
784+
readonly onPicked: (chosen?: GridSelection<Service, "connect" | "logs">) => void;
785+
}) {
786+
const { columns, rows } = ink.useWindowSize();
787+
return react.createElement(ServicePicker, {
788+
request: {
789+
message: "Pick a service",
790+
headers: ["SERVICE", "STATE"],
791+
rows: services.map((service) => ({
792+
cells: [service.name, service.state],
793+
value: service,
794+
actions: [
795+
{ label: "connect", value: "connect" as const },
796+
{ label: "open logs", value: "logs" as const },
797+
],
798+
})),
799+
},
800+
width: columns,
801+
height: rows - 1,
802+
isActive: true,
803+
onSubmit: (chosen) => onPicked(chosen),
804+
onCancel: () => onPicked(undefined),
805+
});
806+
}
807+
```
808+
769809
Sorting, grouping, filtering, and paginating the supplied rows are out of scope, as are machine-readable output, borders and box drawing, spanning and wrapped cells, caller-specified widths, multi-row selection, in-place cell editing, column reordering, and value formatting of every kind — a duration, a timestamp, and a byte count are the consumer's to render into a string before it hands one over.
770810

771811
## One namespace per plugin

‎docs/specs/grid/index.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ The grid capability lays text out in aligned cells and either prints it once or
66

77
A grid is not only a table. A table — a header row over columns of equal meaning — is one layout; a flow, where a list of short items fills as many columns as the terminal affords, is another. Both are the same cells measured the same way, so both belong to one capability.
88

9-
[Change 0030](../../changes/0030-publish-bundled-capability-contracts.md) publishes this contract at `@fx/tx/grid` and renames the capability's registry key to that same specifier, so a consumer imports the contract rather than restating it, under [Plugin System: Published Capability Contracts](../plugin-system/index.md#published-capability-contracts). That change has landed, so every requirement below describes current behavior except those the next paragraph names.
9+
[Change 0030](../../changes/0030-publish-bundled-capability-contracts.md) publishes this contract at `@fx/tx/grid` and renames the capability's registry key to that same specifier, so a consumer imports the contract rather than restating it, under [Plugin System: Published Capability Contracts](../plugin-system/index.md#published-capability-contracts). That change has landed.
1010

11-
[Change 0038](../../changes/0038-add-embeddable-grid-components.md) specifies [Embeddable Grid](#embeddable-grid) and [Embeddable Selection](#embeddable-selection) — a printed grid and a driven one rendered inside a plugin's own render session — together with the members they add to [Grid Capability](#grid-capability). [Embeddable Grid](#embeddable-grid) and the `Grid` member it adds are implemented. [Embeddable Selection](#embeddable-selection) and the `SelectGrid` member it adds are planned and not yet implemented: today the capability exposes `print`, `select`, and `Grid`.
11+
[Change 0038](../../changes/0038-add-embeddable-grid-components.md) adds [Embeddable Grid](#embeddable-grid) and [Embeddable Selection](#embeddable-selection) — a printed grid and a driven one rendered inside a plugin's own render session — together with the `Grid` and `SelectGrid` members they add to [Grid Capability](#grid-capability). That change has landed too, so every requirement below describes current behavior: the capability exposes `print`, `select`, `Grid`, and `SelectGrid`.
1212

1313
## Background
1414

@@ -436,3 +436,4 @@ They share every measurement decision and differ only in what drives them. Split
436436
| 2026-09-06 | Required the grid contract to be published at `@fx/tx/grid` as a types-only import, replacing the local structural copy every consumer maintained | [0030-publish-bundled-capability-contracts](../../changes/0030-publish-bundled-capability-contracts.md) |
437437
| 2026-10-18 | Desired embeddable `Grid` and `SelectGrid` components, sized by their host under the props Dialogs defines for every embeddable component | [0038-add-embeddable-grid-components](../../changes/0038-add-embeddable-grid-components.md) |
438438
| 2026-10-18 | Implemented the embeddable `Grid` component: the lines `print` would write, laid out against the host's `width` rather than a stream's, themed for the injected standard error, and handed to the host's session whole, a cell's own trailing spaces included | [0038-add-embeddable-grid-components](../../changes/0038-add-embeddable-grid-components.md) |
439+
| 2026-10-18 | Implemented the embeddable `SelectGrid` component: the request `select` presents, drawn as the dialogs' embeddable `Select` in the host's `width`, `height`, and focus, mapped once on mount and answered through `onSubmit` with exactly the selection `select` resolves with, reading the dialogs capability when it mounts and failing that render naming the count when there is not exactly one provider | [0038-add-embeddable-grid-components](../../changes/0038-add-embeddable-grid-components.md) |

‎plugins/grid/contract.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* restating it learns about a contract that moved when it builds rather than
99
* when its command reaches for a member that is no longer there.
1010
*
11-
* It declares types alone — one type-only import, no statements, nothing that
11+
* It declares types alone — type-only imports, no statements, nothing that
1212
* survives compilation — which is what lets it be published under a `types`
1313
* condition with no runtime condition beside it. The capability's value comes
1414
* from the registry and must keep coming from there: nothing here prints,
@@ -20,9 +20,13 @@
2020
* one runtime module graph; the published path shares no graph at all, since
2121
* it is erased. It is the mechanism `@fx/tx/plugin` already uses between the
2222
* same two directories, and it means a variable renamed in one place is
23-
* renamed everywhere a cell can name one.
23+
* renamed everywhere a cell can name one. The props every embeddable component
24+
* taking keys shares arrive through `@fx/tx/dialogs` for the same reason, so a
25+
* host learns them once and the selecting grid cannot drift from the select it
26+
* is drawn as.
2427
*/
2528

29+
import type { EmbeddedProps } from "@fx/tx/dialogs";
2630
import type { ThemeVariable } from "@fx/tx/theme";
2731

2832
/** One cell of a grid. `variable` and `align` describe a *printed* cell; the
@@ -136,6 +140,21 @@ export type GridProps = {
136140
readonly width: number;
137141
};
138142

143+
/** What a host hands the embeddable `SelectGrid`: the selecting request it
144+
* would pass to `select`, the props every embeddable component taking keys
145+
* shares, and the rows it may draw in.
146+
*
147+
* `onSubmit` receives exactly the selection `select` resolves with, and
148+
* `onCancel` is called where `select` resolves with nothing. The request is the
149+
* one the component was mounted with: a host presenting different rows mounts
150+
* a new component, keyed differently, rather than passing a new request. */
151+
export type SelectGridProps<T, A> = EmbeddedProps<GridSelection<T, A>> & {
152+
readonly request: GridSelectRequest<T, A>;
153+
/** The terminal rows the grid may draw in, as the select it is drawn as
154+
* reads them. */
155+
readonly height: number;
156+
};
157+
139158
/** The value registered under `@fx/tx/grid`: cells printed once, or driven. A
140159
* grid is one or the other in a call, never both. */
141160
export type Grid = {
@@ -151,4 +170,12 @@ export type Grid = {
151170
* React type: a host casts it where it passes it to `createElement`. The
152171
* same value on every read of the capability. */
153172
readonly Grid: (props: GridProps) => unknown;
173+
/** The driven grid as a component a host renders inside a session it owns:
174+
* the request `select` would present, drawn as the dialogs' embeddable
175+
* `Select` in the host's `width` and `height` and answering keys only while
176+
* `isActive`. It writes to no stream and needs no interactive one. It reads
177+
* the dialogs capability when it mounts, and fails that render when it finds
178+
* other than exactly one provider. Typed as returning `unknown` for the
179+
* reason `Grid` is; the same value on every read of the capability. */
180+
readonly SelectGrid: <T, A>(props: SelectGridProps<T, A>) => unknown;
154181
};

‎plugins/grid/index.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from "./contract.ts";
88
import { requireDialogsCapability } from "./dialogs.ts";
99
import { embeddedGrid, printGrid } from "./render.ts";
10-
import { selection, selectRequest } from "./select.ts";
10+
import { embeddedSelectGrid, selection, selectRequest } from "./select.ts";
1111
import { requireThemeCapability } from "./theme.ts";
1212

1313
/**
@@ -56,6 +56,15 @@ const definition: PluginDefinition = Object.freeze({
5656
requireThemeCapability(registrations).theme(context.stderr),
5757
);
5858

59+
// Created once for the same reason. The dialogs capability is read when
60+
// it mounts rather than here, for the reasons `select` reads it while
61+
// the command runs: a plugin reading during its own initialization sees
62+
// only what committed before it, and the provider is composed ahead of
63+
// its consumers.
64+
const SelectGrid = embeddedSelectGrid(react, () =>
65+
requireDialogsCapability(registrations),
66+
);
67+
5968
// Frozen, like every value a capability hands out: a consumer holds the
6069
// same object every other consumer in the process holds.
6170
const grid: Grid = Object.freeze({
@@ -108,6 +117,7 @@ const definition: PluginDefinition = Object.freeze({
108117
},
109118

110119
Grid,
120+
SelectGrid,
111121
});
112122

113123
register<Grid>(gridKey, grid);

0 commit comments

Comments
 (0)