Data grid / foldkit-data-grid
Filtering from a column
A column with filter draws a filter form on its header
(foldkit-mixins-data-grid). The grid keeps only the open form's draft, sending
each valid change as Out.Filtered({ change }) for your application Model to
store and apply—either over local rows with Grid.keeps or inside a Remote
query's input:
ts
import { Schema } from 'effect'
import { CellEditor } from 'foldkit-data-grid'
interface Job {
readonly id: string
readonly title: string
readonly status: 'Open' | 'Done'
readonly hours: number
}
const Status = Schema.Literals(['Open', 'Done'])
const jobColumns = Columns.define<Job>()({
title: { header: 'Title', value: job => job.title, filter: true },
status: { header: 'Status', value: job => job.status, edit: { schema: Status }, filter: true },
// Shown as text, filtered as a number: a range.
hours: { header: 'Hours', value: job => job.hours.toFixed(1), filter: CellEditor.Number() },
})
const JobGrid = DataGrid.make({ id: 'jobs', columns: jobColumns })
// The application's value, stored as `JobGrid.Filters` (`JobGrid.noFilters` to start).
type JobFilters = typeof JobGrid.Filters.Type
// In `onOut`: one column's change written in.
const filtered = (filters: JobFilters, out: typeof JobGrid.Out.Type) =>
JobGrid.Out.match(out, {
Filtered: ({ change }) => JobGrid.setFilter(filters, change),
Edited: () => filters,
Pasted: () => filters,
Copied: () => filters,
UndoRequested: () => filters,
RedoRequested: () => filters,
Filled: () => filters,
RowsRequested: () => filters,
})
// Held rows: what the filters keep, read from each row's own field.
const shown = (all: ReadonlyArray<Job>, filters: JobFilters) =>
all.filter(
JobGrid.keeps(filters, {
title: job => job.title,
status: job => job.status,
hours: job => job.hours,
}),
)filter: truereads the kind fromedit.schema's decoded side (filterKindOf): a boolean is a checkbox, aDatea range of days, a number a range, text literals or a text enum a choice of them, a number enum a number, anything else text (a branded text, a template literal, a mixed enum). A column with no schema filters text; aCellEditornames the kind for one that does not edit.ColumnFilteris the value, one variant per kind:Contains,Range,OneOf,Is,Between. A filter that keeps every row (empty text, no bound, nothing ticked, any) is reported as none, never an empty variant. Each column's is typed by its spec (FilterOf), soJobGrid.Filters.Type['status']isOption<OneOf<'Open' | 'Done'>>.Out.Filtered({ change })carries one column's new filter or none;setFilterwrites it in.hasFilters(filters)andfilterCount(filters)say whether any column is filtered and how many, sinceFiltersis a record of differently typed Options that no oneOption.isSomereads. Only a grid with a filtering column has this arm, so a grid that filters nothing matches noFiltered.keeps(filters, read)is a predicate over held rows.readnames each filtering column's own field, typed by its variant, never the text a cell shows:hours: job => job.hours.toFixed(1)does not compile.Containsfolds ASCII case only, asExpr.containsdoes on every backend; aBetweencompares aDateby its local day, so a server applying the same filter must store or compare local days too to agree with it. Build it once per filters value: the predicate is a function of the filters alone.Filtersis the stored form:nullfor none, and each column only its own variant, never empty, a choice only of its literals each once, bounds finite numbers orYYYY-MM-DDdays. A key no filtering column has is refused whatever the decode is given, so a URL or a saved view naming a removed column fails to decode rather than being dropped silently.- Over a Remote list, the application maps its filters onto the
query's input, and the query's body filters what is given
(
Expr.whenGiven(input.statuses, statuses => Expr.in(fields.status, statuses)),Expr.whenGiven(input.min, min => Expr.gte(fields.cents, min))), with null for a column not filtered.examples/registryfilters statuses and prices that way, its prices typed in dollars and rounded inward to cents so the server keeps what the range says.
The form's update:
FilterOpened({ column, drawn })opens the form seeded with the application's filter as the view drew it; it is refused for a column that does not filter or is hidden, or adrawnits column refuses. Opening focuses the header and closes the menu, asMenuOpeneddoes.- A field's change (
FilterTyped,FilterOptionToggled,FilterChecked) is applied to the form as it is now, so keys faster than a frame add up. It is reported when the form reads as a valid filter other than the last reported. A bound that is no number (1x,0x10, or 309 digits, past every finite number) is not reported, so the rows stay filtered by the last filter that held; crossed bounds are reported, and keep nothing. - The form's draft is the grid's while it is open. The application may
store each
Out.Filteredchange whenever it likes (in the same update, on aUrlChanged, debounced) and may clean it (trim a text): the form keeps what was typed. The grid remembers every filter the form opened with or reported since it was last seeded, and the application's filter is the form's own echo while it is one of these, aContainscompared with its text trimmed (isEcho). Anything else is a write from outside the form (a saved view, a URL navigation, a value the application changed beyond trimming, such as a clampedRange): the view draws that frame stale, showing the written filter, and every change it sends (Clear included) carries the form's generation when it was drawn. The first such change seeds the form from the written filter before it applies; the reseed and each report move the generation on, so the frame's later changes add to the first rather than seeding again. An outside write that equals a filter the form already sent cannot be told from its echo, so it leaves the form as typed. - While the form is open, a paste, a fill, an undo and a redo are refused, as during an edit; whatever focuses a cell or opens an edit, the menu or a fill closes the form, and so does hiding its column (a column hidden by a write to the Model closes it on the next Message).
Limits
- Column pinning uses the column menu rather than cross-region header dragging, and cell selection supports one rectangular range at a time.
- Every row shares one fixed height.
- Filters support one condition per column combined with logical AND across five
operators (
Contains,Range,OneOf,Is,Between); choice options read literal schemas rather than scanning row data, and hidden columns retain active filters without a visible header indicator.
Next
- Render the virtualized WAI-ARIA grid UI with keyboard, pointer, and styling
Slots:
foldkit-mixins-data-grid. - Explore a 100,000-row product registry built with both packages:
examples/data-grid. - Sync grid edits across clients through Remote and Sync:
examples/registry.