Data grid / foldkit-data-grid
Editing
A column with edit is editable. The grid owns the edit session (which
cell, the draft, an error); the application owns the value, and hears of a
commit as the grid's OutMessage. The column's schema says what its text
means, from the text typed to the value: a draft is committed only when it
decodes, and the failure's message is the cell's error. A schema that
decodes to a date (Schema.DateFromString) is CellEditor.Date and edits in
a date field. The draft the field opens on is the local calendar day,
YYYY-MM-DD. toISOString is the UTC day, so a local midnight used to open
the field on the wrong one. A column draft that returns a string still
wins. Committing decodes the field's text through the schema.
import { Schema, SchemaGetter } from 'effect'
// Dollars as typed, with at most two decimals, and the number they mean.
const Price = Schema.String.check(
Schema.isPattern(/^\d+(\.\d{1,2})?$/, { message: 'A price, like 4.99' }),
).pipe(
Schema.decodeTo(Schema.Number, {
decode: SchemaGetter.transform(text => Number(text)),
encode: SchemaGetter.transform(price => price.toFixed(2)),
}),
)
const priced = Columns.define<Product>()({
sku: { header: 'SKU', value: product => product.sku },
price: {
header: 'Price',
value: product => product.price,
edit: { schema: Price, draft: product => Schema.encodeSync(Price)(product.price) },
},
})
const Prices = DataGrid.make({ id: 'prices', columns: priced })
// Edited({ row, column, text }) | Pasted({ accepted, refused }) | Copied({ rows, columns })
// | Filled({ source, to }) | UndoRequested | RedoRequested | RowsRequested({ from, to })
type Out = typeof Prices.Out.Type
// In `onOut`: the committed text read back as its column's value, a number here.
const repriced = (cell: Extract<Out, { _tag: 'Edited' }>) =>
Prices.matchEdit(cell, { price: ({ row, value }) => ({ row, price: value }) })EditStarted({ address, draft })opens an edit on an editable cell;EditChangedkeeps the draft;EditCancelleddrops it.EditCommitted({ next, reveal })decodes the draft with the column'sschema(any text, without one). A draft it refuses keeps the edit with the error, and nothing is reported. One it accepts ends the edit, moves focus tonext, and the update returns the OutMessageOut.Edited({ row, column, text }), unless it is unchanged: the text the edit began from, or text that decodes to the same value (09.0for9.00). An unchanged edit closes as a changed one does and reports nothing. It is judged against the text the edit began from, not the cell as it is now, so leaving an editor as it opened never writes over a change another device made meanwhile.Grid.matchEdit(cell, handlers)reads a reported cell's value back: one handler per editable column, each given its column's decoded type, so the application parses nothing itself. The OutMessage stays text, which a parent can store or send; a cell that does not decode, or names a column that does not edit, was not the grid's, and throws.- A key typed on the grid is
EditTyped({ address, text, from }): it starts the edit fromfrom, the text the cell showed, or adds to the one open on that cell, so keys faster than the editor opens all count. On a choice column it finds the next option the key starts instead.EditStepped({ by })moves a choice's draft through its options (next,previous,first,last,pageNext,pagePrevious), andEditChosen({ draft })keeps one a pointer chose. The grid's Messages carry what the user did andupdateworks out what it means, for the reason in Messages carry intent. - Place the grid with
onOut. Every placement handles the OutMessage, as any Bundle's must; a grid that edits nothing passesonOut: Bundle.ignore. - A click on another cell commits first; a refused draft keeps the edit and the click waits. Hiding the edited column ends the edit.
Checkboxes
A column whose schema decodes to a boolean is CellEditor.Checkbox: its
cells are checkboxes drawn from the column's value, toggled in place, and no
draft opens. Columns.checkbox is that schema for the text true and
false, which is what a boolean shows as; any schema from text to a boolean
works the same way.
interface Task {
readonly id: string
readonly title: string
readonly done: boolean
}
const tasks = Columns.define<Task>()({
title: { header: 'Title', value: task => task.title },
done: { header: 'Done', value: task => task.done, edit: { schema: Columns.checkbox } },
})
const Tasks = DataGrid.make({ id: 'tasks', columns: tasks })
// In `onOut`, as any edit: the text read back as a boolean.
const checked = (cell: Extract<typeof Tasks.Out.Type, { _tag: 'Edited' }>) =>
Tasks.matchEdit(cell, { done: ({ row, value }) => ({ row, done: value }) })EditToggled({ cell, checked, by })commits the column's text for the other value at once and reportsOut.Edited, as a commit does; the application writes the row.cellis the cell's DOM id andcheckedthe value as drawn. The view sends it for a press on the box (by: Grid.Toggle.Press()) and for Space or Enter on the cell holding the grid's tab stop (by: Grid.Toggle.Key({ drawn }),drawnthe focus that frame was drawn from). Before anything is focused the tab stop is the first cell, so a keyboard user who tabs in can toggle it at once.updaterefuses it, changing nothing, on a cell whose column does not edit or is no checkbox, while an edit is open, and for a key whosedrawnis no longer the Model's focus: the key's frame was drawn before focus moved, and it would toggle the cell focus left.EditStartedandEditTypedon a checkbox change nothing.- The value must be a boolean. The checkbox is drawn from it, so
Columns.definerefuses a column withColumns.checkboxwhosevaluereturns anything else, at thevalue.GridCrud.columnsrefuses a checkbox outright: a listed member's value is its text. - Paste and fill judge its text by the schema, as on any column:
trueandfalsepass. A cut or Delete leaves a checkbox as it is, since it has no empty value, and clears the other cells in its range. - Every toggle sends the opposite of the value drawn, so until the application's write of the last one is drawn, each further press or key sends the same value again. Where the row changes only when a server answers, every toggle before the answer asks for the same value, and a second press does not take the first one back.