Components

Cascade select

One value chosen as a path through nested options, from a column per level or one level at a time, by the pointer or the arrows.

Previews are drawn in the theme you made.

Edit it
size
trigger
width
scheme
direction

Columns #

import { Bundles, CascadeSelect, CascadeSelectSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Places } from '../../snippets/places.js'

// A city chosen through its country and region: a column per level, the
// arrows within and between them. The chosen path of ids is the widget's own
// value, held in its state (`model.value`); `Places` is given to the state
// and to the view alike.
export default example({
  title: 'Columns',
  slots: CascadeSelectSlots,
  recipe: Recipes.CascadeSelect,
  state: Bundles.CascadeSelect.with({ id: 'city', config: Places }),
  view: ({ style, model, onMessage, h }) =>
    CascadeSelect.view(
      {
        model,
        config: Places,
        onMessage,
        label: 'City',
        placeholder: 'Choose a city',
        levels: ['Country', 'Region', 'City'],
        clear: true,
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

One level at a time #

import { Bundles, CascadeSelect, CascadeSelectSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Places } from '../../snippets/places.js'

// One level at a time with a way back: a phone's width holds one column.
// Columns that would not fit the window drill in on their own as well.
export default example({
  title: 'One level at a time',
  slots: CascadeSelectSlots,
  recipe: Recipes.CascadeSelect,
  state: Bundles.CascadeSelect.with({ id: 'city-drill', config: Places }),
  view: ({ style, model, onMessage, h }) =>
    CascadeSelect.view(
      { model, config: Places, onMessage, label: 'City', layout: 'drill', style },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

In German #

import { Bundles, CascadeSelect, CascadeSelectSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Places } from '../../snippets/places.js'

// The fixed text is `words`, with `{label}` filled where a word names a row:
// one object says it in another language. The levels' names, the placeholder
// and the separator are data of the use.
export default example({
  title: 'In German',
  slots: CascadeSelectSlots,
  recipe: Recipes.CascadeSelect,
  state: Bundles.CascadeSelect.with({ id: 'stadt', config: Places, value: ['ca', 'on', 'tor'] }),
  view: ({ style, model, onMessage, h }) =>
    CascadeSelect.view(
      {
        model,
        config: Places,
        onMessage,
        label: 'Stadt',
        levels: ['Land', 'Region', 'Stadt'],
        separator: ' › ',
        layout: 'drill',
        clear: true,
        words: { placeholder: 'Stadt wählen', clear: 'Auswahl löschen', back: 'Zurück zu {label}' },
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Rows with icons, descriptions and a trailing part #

import { Bundles, CascadeSelect, CascadeSelectSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Places } from '../../snippets/places.js'

const flags = new Map([
  ['ca', '🇨🇦'],
  ['jp', '🇯🇵'],
  ['sg', '🇸🇬'],
])
const notes = new Map([
  ['ca', 'Two regions'],
  ['jp', 'Two regions'],
  ['sg', 'A city-state'],
])

// A row's `RowContent`: an icon before the label, a line under it and a part
// at its end. `label` still names the row and the typeahead's key; whether a
// row can be chosen is the node's `disabled`, which `update` reads too.
export default example({
  title: 'Rows with icons, descriptions and a trailing part',
  slots: CascadeSelectSlots,
  recipe: Recipes.CascadeSelect,
  state: Bundles.CascadeSelect.with({ id: 'city-rows', config: Places }),
  view: ({ style, model, onMessage, h }) =>
    CascadeSelect.view(
      {
        model,
        config: Places,
        onMessage,
        label: 'City',
        option: node => {
          const flag = flags.get(node.id)
          const note = notes.get(node.id)
          return {
            label: node.label,
            ...(flag === undefined ? {} : { icon: h.span([], [flag]) }),
            ...(note === undefined ? {} : { description: note }),
            ...(node.children === undefined
              ? {}
              : { trailing: h.span([], [String(node.children.length)]) }),
          }
        },
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

A long path, collapsed #

import { Bundles, CascadeSelect, CascadeSelectSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Places } from '../../snippets/places.js'

// `triggerText` says what the trigger shows for the labels of the chosen
// path: here a long path is collapsed to its two ends.
export default example({
  title: 'A long path, collapsed',
  slots: CascadeSelectSlots,
  recipe: Recipes.CascadeSelect,
  state: Bundles.CascadeSelect.with({
    id: 'city-collapsed',
    config: Places,
    value: ['ca', 'on', 'tor'],
  }),
  view: ({ style, model, onMessage, h }) =>
    CascadeSelect.view(
      {
        model,
        config: Places,
        onMessage,
        label: 'City',
        triggerText: labels =>
          labels.length > 2 ? `${labels[0]} / … / ${labels.at(-1)}` : labels.join(' / '),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

In place, with no popup #

Singapore
import { Bundles, CascadeSelect, CascadeSelectSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Places } from '../../snippets/places.js'

// `inline` is an option of the config, given to the state and the view alike:
// the tree is drawn where it stands, always open, with no trigger, and Tab
// brings focus to it. Nothing closes it, so a choice leaves it as it is.
const Inline = { ...Places, inline: true }

export default example({
  title: 'In place, with no popup',
  slots: CascadeSelectSlots,
  recipe: Recipes.CascadeSelect,
  state: Bundles.CascadeSelect.with({ id: 'city-inline', config: Inline }),
  view: ({ style, model, onMessage, h }) =>
    CascadeSelect.view({ model, config: Inline, onMessage, label: 'City', style }, h),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Set from outside #

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, CascadeSelect, CascadeSelectSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'
import { Places } from '../../snippets/places.js'

const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })

// The path is the widget's own value, and `helpers.set` is how the parent
// writes it: buttons of the application's set it, or clear it with an empty
// path. A path that cannot be chosen is ignored, and nothing is reported.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Set: { path: Schema.Array(Schema.String) } }),
  Bundle.withChild('box', Bundles.CascadeSelect, {
    args: { id: 'city-external', config: Places },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Set from outside',
  slots: CascadeSelectSlots,
  recipe: Recipes.CascadeSelect,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update((model, { path }) => App.children.box.helpers.set(path)(model)),
  }),
  view: ({ style, model, onMessage, h }) =>
    row(h, [
      CascadeSelect.view(
        {
          model: model.box,
          config: Places,
          onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
          label: 'City',
          style,
        },
        h,
      ),
      Button.view(
        {
          label: 'Tokyo',
          onClick: onMessage(App.Message.Set({ path: ['jp', 'kanto', 'tyo'] })),
          style: Quiet,
        },
        h,
      ),
      Button.view(
        { label: 'Reset', onClick: onMessage(App.Message.Set({ path: [] })), style: Quiet },
        h,
      ),
    ]),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

From rows with a parent #

import { Bundles, CascadeSelect, CascadeSelectSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

interface Category {
  readonly id: string
  readonly label: string
  readonly parent?: string
}

// Rows with a parent pointer, as a database holds them.
const categories: ReadonlyArray<Category> = [
  { id: 'tools', label: 'Tools' },
  { id: 'hand', label: 'Hand tools', parent: 'tools' },
  { id: 'saw', label: 'Saws', parent: 'hand' },
  { id: 'power', label: 'Power tools', parent: 'tools' },
  { id: 'garden', label: 'Garden' },
  { id: 'soil', label: 'Soil', parent: 'garden' },
]

// The tree the select takes is the application's to build, once.
const childrenOf = (parent: string | undefined): ReadonlyArray<CascadeSelect.Node> =>
  categories
    .filter(category => category.parent === parent)
    .map(({ id, label }) => {
      const children = childrenOf(id)
      return children.length === 0 ? { id, label } : { id, label, children }
    })

const Catalog: CascadeSelect.Config = { nodes: childrenOf(undefined) }

export default example({
  title: 'From rows with a parent',
  slots: CascadeSelectSlots,
  recipe: Recipes.CascadeSelect,
  state: Bundles.CascadeSelect.with({ id: 'category', config: Catalog }),
  view: ({ style, model, onMessage, h }) =>
    CascadeSelect.view(
      {
        model,
        config: Catalog,
        onMessage,
        label: 'Category',
        placeholder: 'Choose a category',
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Configure once #

import { Bundles, CascadeSelect, CascadeSelectSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Places } from '../../snippets/places.js'

// Stated once, at module scope: what every cascade of the application says
// and how it lays out, and a named preset over it.
const AppCascade = CascadeSelect.configure({
  base: { clear: true, separator: ' › ', words: { placeholder: 'Choose', clear: 'Reset' } },
  presets: { drilled: { layout: 'drill', levels: ['Country', 'Region', 'City'] } },
})

export default example({
  title: 'Configure once',
  slots: CascadeSelectSlots,
  recipe: Recipes.CascadeSelect,
  state: Bundles.CascadeSelect.with({ id: 'city-configured', config: Places, value: ['sg'] }),
  view: ({ style, model, onMessage, h }) =>
    AppCascade.view(
      { model, config: Places, onMessage, label: 'City', preset: 'drilled', style },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Usage

Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

The recipe ↗This code ↗

  • Keys of your own Add the application’s keys with the routes option, beside the ones the widget handles; the widget’s conflict(routes) judges a candidate set without drawing it.