Filter, sort and group the records a view shows.
Sort
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Outcome } from "./ui/uicontext/components/domain/Outcome.ts"import { SavedView, View, viewOf } from "./ui/uicontext/components/domain/Record.ts"import { Board } from "./ui/uicontext/components/kit/Board.ts"import { filterItems, ViewToolbar } from "./ui/uicontext/components/kit/Filter.ts"import { List } from "./ui/uicontext/components/kit/List.ts"import { Item, Value } from "./ui/uicontext/components/kit/Property.ts"import { Table } from "./ui/uicontext/components/kit/Table.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { applyEdit, issueFields, issues } from "./issues.ts"// One View in the model, passed to the toolbar and to the record view under it. Every record view// takes the same `records`, `fields` and `view`, so switching the layout is swapping the component.const savedViews: ReadonlyArray<SavedView> = [ { id: "triage", label: "Triage", shared: true, view: viewOf({ groupBy: "status", sort: [{ key: "priority", direction: "asc" }] }, "list") }, { id: "board", label: "By status", shared: true, view: viewOf({ groupBy: "status", hidden: ["customer", "impact"] }, "board") }, { id: "customers", label: "Customer asks", view: viewOf( { filter: { match: "all", conditions: [{ key: "customer", op: "isNotEmpty", value: null }] }, sort: [{ key: "impact", direction: "desc" }], }, "table", ), },]const styles = stylex.create({ stack: { display: "grid", gridTemplateColumns: "minmax(0, 1fr)", gap: 12 }, frame: { blockSize: 420, inlineSize: "100%" },})const Message = UiSchema.TaggedEnum({ RequestedEditIssue: { request: Schema.String, item: Schema.String, key: Schema.String, value: Value }, RequestedIssueView: { view: View }, SelectedSavedView: { view: Schema.String }, RequestedSaveIssueView: { view: Schema.NullOr(Schema.String), label: Schema.String, config: View }, RequestedRenameIssueView: { view: Schema.String, label: Schema.String }, RequestedShareIssueView: { view: Schema.String, shared: Schema.Boolean }, RequestedDeleteIssueView: { view: Schema.String },})export const IssueViews = Html.component({ name: "IssueViews", model: Schema.Struct({ records: Schema.Array(Item), outcomes: Schema.Array(Outcome), views: Schema.Array(SavedView), active: Schema.NullOr(Schema.String), view: View, }), message: Message,}).make({ init: () => ({ records: issues, outcomes: [], views: savedViews, active: "triage", view: savedViews[0]!.view }), update: { RequestedEditIssue: ($, { request, item, key, value }) => $.evolve({ records: (records) => records.map((r) => (r.id === item ? applyEdit(r, key, value) : r)), outcomes: (o) => [...o.slice(-10), { _tag: "Accepted", request }], }), // the toolbar and the view under it both ask for changes the same way: the whole next view RequestedIssueView: ($, { view }) => $.evolve({ view }), SelectedSavedView: ($, { view }) => $.evolve({ active: view, view: $.model.views.find((v) => v.id === view)?.view ?? $.model.view }), RequestedSaveIssueView: ($, { view, label, config }) => { if (view !== null) return $.evolve({ views: (all) => all.map((v) => (v.id === view ? { ...v, view: config } : v)) }) const id = `view-${$.model.views.length + 1}` return $.evolve({ views: (all) => [...all, { id, label, view: config }], active: id, view: config }) }, RequestedRenameIssueView: ($, { view, label }) => $.evolve({ views: (all) => all.map((v) => (v.id === view ? { ...v, label } : v)) }), RequestedShareIssueView: ($, { view, shared }) => $.evolve({ views: (all) => all.map((v) => (v.id === view ? { ...v, shared } : v)) }), // deleting the view on screen moves to the first one left RequestedDeleteIssueView: ($, { view }) => { const views = $.model.views.filter((v) => v.id !== view) const next = $.model.active === view ? (views[0] ?? null) : undefined return $.evolve({ views, ...(next === undefined ? {} : { active: next?.id ?? null, view: next?.view ?? $.model.view }), }) }, }, view: (h, { records, outcomes, views, active, view }) => { const props = { label: "Issues", records, fields: issueFields, view, outcomes } const shared = { RequestedEdit: (edit: { request: string; item: string; key: string; value: Value }) => Message.RequestedEditIssue(edit), RequestedView: ({ view }: { view: View }) => Message.RequestedIssueView({ view }), RequestedOpen: () => undefined, RequestedAction: () => undefined, RequestedCreate: () => undefined, RequestedReorder: () => undefined, Selected: () => undefined, } return h.div.attrs(sx(styles.stack)).children( ViewToolbar(h, { props: { label: "Issues", fields: issueFields, view, views, active, layouts: ["list", "board", "table"], count: { shown: filterItems(records, view.filter, issueFields).length, total: records.length }, can: ["shareViews"], }, onEmit: { RequestedView: ({ view }) => Message.RequestedIssueView({ view }), SelectedView: ({ view }) => Message.SelectedSavedView({ view }), RequestedSaveView: ({ view, label, config }) => Message.RequestedSaveIssueView({ view, label, config }), RequestedDeleteView: ({ view }) => Message.RequestedDeleteIssueView({ view }), RequestedRenameView: ({ view, label }) => Message.RequestedRenameIssueView({ view, label }), RequestedShareView: ({ view, shared }) => Message.RequestedShareIssueView({ view, shared }), RequestedEditField: () => undefined, }, }), h.div.attrs(sx(styles.frame)).children( view.layout === "board" ? Board(h, { key: "board", props, onEmit: shared }) : view.layout === "table" ? Table(h, { key: "table", props: { ...props, title: { label: "Issue", width: 300 }, resizable: true }, onEmit: { ...shared, RequestedCellAction: () => undefined, RequestedAddColumn: () => undefined, RequestedColumnAction: () => undefined, }, }) : List(h, { key: "list", props: { ...props, columns: [ { key: "priority", at: "start", compact: true }, { key: "status", at: "start", compact: true }, { key: "labels" }, { key: "impact" }, { key: "due" }, { key: "assignee", compact: true }, ], }, onEmit: shared, }), ), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/filterUsage
import { type Property, View } from "./ui/uicontext/components/domain/Record.ts"import { ViewToolbar } from "./ui/uicontext/components/kit/Filter.ts"const Message = UiSchema.TaggedEnum({ ChangedView: { view: View } })// Keep one View in your model and pass it to the toolbar and to the List, Board or Data Grid under it.export const TaskToolbar = (h: Html.H<typeof Message.Type>, fields: ReadonlyArray<Property>, view: View) => ViewToolbar(h, { props: { label: "Tasks", fields, view, layouts: ["list", "board", "table"] }, onEmit: { RequestedView: ({ view }) => Message.ChangedView({ view }), SelectedView: () => undefined, RequestedSaveView: () => undefined, RequestedDeleteView: () => undefined, RequestedRenameView: () => undefined, RequestedShareView: () => undefined, RequestedEditField: () => undefined, }, })Examples
Filter groups and sort stacks
Ana's deals that are won or worth $50,000 or more, sorted by stage, then amount. Add a group from Filter, and more sort levels from Sort.
Owner
Stage
Amount
Sort
2 rows· 6 filtered out· This example only filters and sorts.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { View, viewOf } from "./ui/uicontext/components/domain/Record.ts"import { filterItems, ViewToolbar } from "./ui/uicontext/components/kit/Filter.ts"import { Table } from "./ui/uicontext/components/kit/Table.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { dealFields, deals } from "./deals.ts"// A filter with a group: Ana's deals that are won or worth at least $50,000. A group counts as one// condition, with its own "all of" or "any of". The sort stack orders by stage, then by amount.const styles = stylex.create({ stack: { display: "grid", gridTemplateColumns: "minmax(0, 1fr)", gap: 12 }, frame: { blockSize: 320, inlineSize: "100%" },})const Message = UiSchema.TaggedEnum({ RequestedPipelineView: { view: View } })export const BigDeals = Html.component({ name: "BigDeals", model: Schema.Struct({ view: View }), message: Message,}).make({ init: () => ({ view: viewOf( { filter: { match: "all", conditions: [{ key: "owner", op: "is", value: ["ana"] }], groups: [ { match: "any", conditions: [ { key: "stage", op: "is", value: ["won"] }, { key: "amount", op: "gte", value: 50000 }, ], }, ], }, sort: [ { key: "stage", direction: "asc" }, { key: "amount", direction: "desc" }, ], }, "table", ), }), update: { RequestedPipelineView: ($, { view }) => $.evolve({ view }) }, view: (h, { view }) => h.div.attrs(sx(styles.stack)).children( ViewToolbar(h, { props: { label: "Deals", agentId: "deals-grouped", fields: dealFields, view, layouts: ["table"], count: { shown: filterItems(deals, view.filter, dealFields).length, total: deals.length }, locale: "en-US", }, onEmit: { RequestedView: ({ view }) => Message.RequestedPipelineView({ view }), SelectedView: () => undefined, RequestedSaveView: () => undefined, RequestedDeleteView: () => undefined, RequestedRenameView: () => undefined, RequestedShareView: () => undefined, RequestedEditField: () => undefined, }, }), h.div.attrs(sx(styles.frame)).children( Table(h, { props: { label: "Deals", agentId: "deals-grouped-grid", records: deals, fields: dealFields, view, title: { label: "Deal", width: 260 }, readOnly: "This example only filters and sorts.", selectable: false, locale: "en-US", }, onEmit: { RequestedView: ({ view }) => Message.RequestedPipelineView({ view }), RequestedEdit: () => undefined, RequestedOpen: () => undefined, RequestedCreate: () => undefined, RequestedAction: () => undefined, RequestedReorder: () => undefined, Selected: () => undefined, RequestedCellAction: () => undefined, RequestedAddColumn: () => undefined, RequestedColumnAction: () => undefined, }, }), ), ),})Filter bar
The filter on its own, over a list. It starts on bugs; add a condition for priority or assignee.
Labels
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Filter } from "./ui/uicontext/components/domain/Record.ts"import { FilterBar, filterItems } from "./ui/uicontext/components/kit/Filter.ts"import { List } from "./ui/uicontext/components/kit/List.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { issueFields, issues } from "./issues.ts"// Just the filter: FilterBar edits one Filter, and `filterItems` applies it the way every record// view does. Use it where the layout, sort and saved views are fixed.const styles = stylex.create({ stack: { display: "grid", gridTemplateColumns: "minmax(0, 1fr)", gap: 12, inlineSize: "100%" }, frame: { blockSize: 320 },})const Message = UiSchema.TaggedEnum({ ChangedFilter: { filter: Filter } })const filterable = issueFields.filter((field) => ["status", "priority", "assignee", "labels", "due"].includes(field.key))export const BugFilter = Html.component({ name: "BugFilter", model: Schema.Struct({ filter: Filter }), message: Message,}).make({ init: () => ({ filter: { match: "all", conditions: [{ key: "labels", op: "hasAny", value: ["bug"] }] }, }), update: { ChangedFilter: ($, { filter }) => $.evolve({ filter }) }, view: (h, { filter }) => { const shown = filterItems(issues, filter, issueFields) return h.div.attrs(sx(styles.stack)).children( FilterBar(h, { props: { label: "Issues", properties: filterable, filter, saveAs: false, count: { shown: shown.length, total: issues.length } }, onEmit: { ChangedFilter: ({ filter }) => Message.ChangedFilter({ filter }), SelectedView: () => undefined, RequestedSaveView: () => undefined, RequestedDeleteView: () => undefined, }, }), h.div.attrs(sx(styles.frame)).children( List(h, { props: { label: "Filtered issues", records: shown, fields: issueFields, columns: [{ key: "priority", at: "start", compact: true }, { key: "status", at: "start", compact: true }, { key: "labels" }, { key: "assignee", compact: true }], selectable: false, empty: { title: "No issues match", hint: "Remove a condition to see more." }, }, onEmit: { RequestedEdit: () => undefined, RequestedOpen: () => undefined, RequestedView: () => undefined, RequestedAction: () => undefined, RequestedReorder: () => undefined, RequestedCreate: () => undefined, Selected: () => undefined, }, }), ), ) },})Locked views
A guest filters and sorts for themselves; saving, renaming and deleting stay with members.
Only members can change views
Stage
Sort
Records show in the order they were added. Add a field to sort by.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import { Schema } from "effect"import { type SavedView, View, viewOf } from "./ui/uicontext/components/domain/Record.ts"import { filterItems, ViewToolbar } from "./ui/uicontext/components/kit/Filter.ts"import { dealFields, deals } from "./deals.ts"// A guest can still filter, sort and switch views for themselves, but can't save, rename or delete// the team's views: `locked` says why, and the save and view menus don't show.const views: ReadonlyArray<SavedView> = [ { id: "open", label: "Open pipeline", shared: true, view: viewOf({ filter: { match: "any", conditions: [{ key: "stage", op: "isNot", value: ["won", "lost"] }] } }, "table") }, { id: "closing", label: "Closing this month", shared: true, view: viewOf({ sort: [{ key: "close", direction: "asc" }] }, "table") },]const Message = UiSchema.TaggedEnum({ RequestedGuestView: { view: View }, SelectedGuestView: { view: Schema.String },})export const GuestToolbar = Html.component({ name: "GuestToolbar", model: Schema.Struct({ active: Schema.String, view: View }), message: Message,}).make({ init: () => ({ active: "open", view: views[0]!.view }), update: { RequestedGuestView: ($, { view }) => $.evolve({ view }), SelectedGuestView: ($, { view }) => $.evolve({ active: view, view: views.find((v) => v.id === view)?.view ?? $.model.view }), }, view: (h, { active, view }) => ViewToolbar(h, { props: { label: "Deals", agentId: "deals-guest", fields: dealFields, view, views, active, layouts: ["table", "board"], locked: "Only members can change views", count: { shown: filterItems(deals, view.filter, dealFields).length, total: deals.length }, locale: "en-US", }, onEmit: { RequestedView: ({ view }) => Message.RequestedGuestView({ view }), SelectedView: ({ view }) => Message.SelectedGuestView({ view }), RequestedSaveView: () => undefined, RequestedDeleteView: () => undefined, RequestedRenameView: () => undefined, RequestedShareView: () => undefined, RequestedEditField: () => undefined, }, }),})API reference
FilterBarComponent
Props
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | What the bar filters, for its accessible name: "Issues" |
agentId | string | filter:<label> | This bar's id for agents, unique on the page; filter:<label> by default |
propertiesrequired | Property[] | — | The properties a person can filter on |
filterrequired | Filter | — | The conditions applied now; every change comes back as ChangedFilter |
current | ViewInput | — | The rest of the view this filter belongs to (layout, sort, grouping); a saved view keeps all of it |
views | SavedView[] | — | The saved views the bar lists to switch between |
view | string | null | — | The active view's id |
saveAs | boolean | true | Whether the bar offers "Save as view"; true by default (the View Toolbar saves whole views itself) |
locked | string | — | Why views can't be changed here: "Views are locked" |
outcome | Outcome | null | — | Pass null before saving, then return the outcome for each new view request. |
count | { shown, total } | — | How many items pass, of how many: "12 of 340" |
locale | string | — | The locale numbers and dates are formatted in; the browser's by default |
Events
| Event | Payload | Description |
|---|---|---|
ChangedFilter | { filter: Filter } | The person changed the filter here; keep it as view state and pass it back as filter |
SelectedView | { view: string } | Show this saved view; pass its id back as view |
RequestedSaveView | { view: string | null; label: string; config: View; request?: string } | Save config (the current view with this filter) to the view (null: a new view named label) |
RequestedDeleteView | { view: string } | Delete this saved view |
Agent actions
| Action | Description |
|---|---|
SetFilter | Replace the whole filter. Each condition is { key, op, value }: op from the field's ops; value an option value list for choices, text, a number, an ISO date, true/false |
AddedCondition | Add one condition: key, and optionally op, value, and group (a group's index, to add inside it) |
RemovedCondition | Remove the condition at this index (in the group at group, when given) |
AddedGroup | Add an empty group: its conditions count as one, joined by its own match (any by default) |
RemovedGroup | Remove the group at this index, with its conditions |
ClearedFilter | Remove every condition and group |
ClickedView | Switch to a saved view by id |
ClickedSaveView | Save the current filter to the active view (shared views change for everyone) |
SubmittedNewView | Save the current filter as a new view named label |
ClickedDeleteViewasks first | Delete a saved view |
ViewToolbarComponent
Props
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | What the view shows, for accessible names: "Deals" |
agentId | string | view:<label> | This toolbar's id for agents, unique on the page; view:<label> by default |
fieldsrequired | Property[] | — | The fields the view can filter, sort, group and show |
viewrequired | ViewInput | — | The view as shown now; every change comes back as RequestedView |
views | SavedView[] | — | The saved views the switcher lists |
active | string | null | — | The saved view being shown, by id; null when the view is unsaved |
layouts | ViewLayout[] | — | The layouts the switcher offers, in order; every layout by default |
count | { shown, total } | — | How many records pass the filter, of how many |
locked | string | — | Why saved views can't be changed here: "Views are locked" |
outcome | Outcome | null | — | Pass null before saving, then return the outcome for each new view request. |
locale | string | — | The locale numbers and dates are formatted in; the browser's by default |
can | Can(["editFields", "shareViews"]) | — | "editFields" offers New field and Edit field in the Fields menu; "shareViews" offers sharing a saved view with everyone, and making a shared one private again |
Events
| Event | Payload | Description |
|---|---|---|
RequestedView | { view: View } | The person changed the view (layout, filter, sort, grouping, fields); pass it back as view |
SelectedView | { view: string } | Show this saved view |
RequestedSaveView | { view: string | null; label: string; config: View; request?: string } | Save config to the view (null: a new view named label) |
RequestedDeleteView | { view: string } | Delete this saved view |
RequestedRenameView | { view: string; label: string } | Give this saved view a new name; pass the renamed view back in views |
RequestedShareView | { view: string; shared: boolean } | Share this saved view with everyone (true), or make it the person's own again (false) |
RequestedEditField | { key: string | null } | Open the field editor on this field, or on a new one (null) |
Agent actions
| Action | Description |
|---|---|
RequestedView | Change parts of the view; the rest stays. view: { layout?, filter?: { conditions: [{ key, op, value }], match }, sort?: [{ key, direction: asc|desc }], groupBy?: key|null, hidden?: [keys] } |
PickedLayout | Show the records in another layout |
ClickedView | Switch to a saved view by id |
ClickedSaveView | Save the view as it is now to the active saved view (shared views change for everyone) |
SubmittedNewView | Save the view as it is now as a new view named label |
ClickedResetView | Undo the unsaved changes to the active view |
ClickedDeleteViewasks first | Delete a saved view |
SubmittedRename | Rename a saved view: { view: its id, label: the new name } |
ClickedDuplicateView | Copy a saved view as a new view named after it |
ClickedShareViewasks first | Share a saved view with everyone (shared: true), or make it private again (false) |
PickedEditField | Open the field editor on a field by key, or on a new field (null) |
Accessibility
- The filter bar is a
searchlandmark named for its records; saved views are anavof links with the open one markedaria-current, and an unsaved view says so. - Each filter chip is a button named for the whole condition ("Edit filter: Status is Done") with a Remove button beside it; a rating value is a radio group.
- The count of matching records is a status, so it's announced as filters change. Sort, group and fields are menus.
- Agents add and remove conditions, switch and save views, and get the reason when a field doesn't exist or can't group.