Components

Select

The native select, styled like the fields beside it: options, groups, a placeholder, and the states.

Previews are drawn in the theme you made.

Edit it
size
variant
scheme
direction

Basic #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Select, SelectSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ region: Schema.String })
const Message = defineMessageUnion({ Chose: { region: Schema.String } })

// The native `select`: the browser owns the list, the keys and the form
// value; the options are data, and the chosen value is the application's.
export default example({
  title: 'Basic',
  slots: SelectSlots,
  recipe: Recipes.Select,
  state: {
    Model,
    Message,
    init: () => ({ model: { region: 'europe' } }),
    update: (model, { region }) => ({ model: model.region === region ? model : { region } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Select.view(
      {
        id: 'region',
        label: 'Region',
        options: [
          { value: 'europe', label: 'Europe' },
          { value: 'americas', label: 'Americas' },
          { value: 'apac', label: 'Asia Pacific' },
        ],
        value: model.region,
        onChange: region => onMessage(Message.Chose({ region })),
        name: 'region',
        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.

Placeholder and description #

Where your data is kept.

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Select, SelectSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ region: Schema.String })
const Message = defineMessageUnion({ Chose: { region: Schema.String } })

// An empty first option that names what to choose, muted while it is the
// chosen one and not a choice itself. The value is empty until one is made.
export default example({
  title: 'Placeholder and description',
  slots: SelectSlots,
  recipe: Recipes.Select,
  state: {
    Model,
    Message,
    init: () => ({ model: { region: '' } }),
    update: (model, { region }) => ({ model: model.region === region ? model : { region } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Select.view(
      {
        id: 'region-placeholder',
        label: 'Region',
        placeholder: 'Choose a region',
        description: 'Where your data is kept.',
        options: [
          { value: 'europe', label: 'Europe' },
          { value: 'americas', label: 'Americas' },
        ],
        value: model.region,
        onChange: region => onMessage(Message.Chose({ region })),
        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.

Groups #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Select, SelectSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Model = Schema.Struct({ country: Schema.String })
const Message = defineMessageUnion({ Chose: { country: Schema.String } })

// Options in a row with the same `group` sit under one heading, an
// `optgroup`; one with no group stands outside any. `disabled` shows an
// option and refuses it.
export default example({
  title: 'Groups',
  slots: SelectSlots,
  recipe: Recipes.Select,
  state: {
    Model,
    Message,
    init: () => ({ model: { country: 'fr' } }),
    update: (model, { country }) => ({ model: model.country === country ? model : { country } }),
  },
  view: ({ style, model, onMessage, h }) =>
    Select.view(
      {
        id: 'country',
        label: 'Country',
        options: [
          { value: 'us', label: 'United States', group: 'Americas' },
          { value: 'ca', label: 'Canada', group: 'Americas' },
          { value: 'fr', label: 'France', group: 'Europe' },
          { value: 'de', label: 'Germany', group: 'Europe', disabled: true },
          { value: 'other', label: 'Somewhere else' },
        ],
        value: model.country,
        onChange: country => onMessage(Message.Chose({ country })),
        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.

Disabled and invalid #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Select, SelectSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'

const Model = Schema.Struct({ region: Schema.String })
const Message = defineMessageUnion({ Chose: { region: Schema.String } })

const options = [
  { value: 'europe', label: 'Europe' },
  { value: 'americas', label: 'Americas' },
]

// `disabled` refuses the control; `invalid` shows the error edge and says so
// (`aria-invalid`).
export default example({
  title: 'Disabled and invalid',
  slots: SelectSlots,
  recipe: Recipes.Select,
  state: {
    Model,
    Message,
    init: () => ({ model: { region: 'europe' } }),
    update: (model, { region }) => ({ model: model.region === region ? model : { region } }),
  },
  view: ({ style, model, onMessage, h }) => {
    const common = {
      options,
      value: model.region,
      onChange: (region: string) => onMessage(Message.Chose({ region })),
      style,
    }
    return stack(h, [
      Select.view({ id: 'off', label: 'Disabled', disabled: true, ...common }, h),
      Select.view({ id: 'bad', label: 'Invalid', invalid: true, ...common }, 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 { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Select, SelectSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Filled = Recipes.Select.style({ variant: 'filled', size: 'sm' })

// Every select of the application is a small filled one with the same
// placeholder; a use gives its options and its value.
const AppSelect = Select.configure({
  base: { style: Filled, placeholder: 'Choose one' },
})

const Model = Schema.Struct({ plan: Schema.String })
const Message = defineMessageUnion({ Chose: { plan: Schema.String } })

export default example({
  title: 'Configure once',
  slots: SelectSlots,
  recipe: Recipes.Select,
  state: {
    Model,
    Message,
    init: () => ({ model: { plan: '' } }),
    update: (model, { plan }) => ({ model: model.plan === plan ? model : { plan } }),
  },
  view: ({ model, onMessage, h }) =>
    AppSelect.view(
      {
        id: 'plan',
        label: 'Plan',
        options: [
          { value: 'free', label: 'Free' },
          { value: 'team', label: 'Team' },
        ],
        value: model.plan,
        onChange: plan => onMessage(Message.Chose({ plan })),
      },
      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 ↗