Combobox
A field that suggests as you type, and keeps one suggestion or several as chips, with a clear button and an empty panel.
Previews are drawn in the theme you made.
Edit itBasic #
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Combobox, ComboboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const cities = ['Kyiv', 'Lagos', 'Lima', 'Oslo', 'Osaka', 'Quito', 'Seoul', 'Zurich'] as const
const City = Combobox.one(cities)
// The city is the application's own field. The Combobox holds what is typed
// and highlighted, and reports a choice, which `City.picked` folds.
const App = Bundle.compose({ city: Schema.Option(Schema.Literals(cities)) }).pipe(
Bundle.withChild('box', Bundles.Combobox, {
args: { id: 'city', animated: true },
onOut: out => model => ({ model: { ...model, city: City.picked(model.city, out) } }),
}),
)
export default example({
title: 'Basic',
slots: ComboboxSlots,
recipe: Recipes.Combobox,
state: App.placements.runtime({ Model: App.Model, initial: { city: Option.none() } }),
view: ({ style, model, onMessage, h }) =>
Combobox.view(
{
model: model.box,
onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
label: 'City',
choice: City,
value: model.city,
filter: Combobox.contains,
placeholder: 'Find a city',
style,
mixins: Combobox.fluid,
},
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.
Several, as chips #
- Lima
- Seoul
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Combobox, ComboboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const cities = ['Kyiv', 'Lagos', 'Lima', 'Oslo', 'Osaka', 'Quito', 'Seoul', 'Zurich'] as const
const Cities = Combobox.many(cities)
// Several: the value is a list, each chosen city a chip in the field, and
// the list stays open as cities are chosen. `multiple` must agree with the choice.
const App = Bundle.compose({ stops: Schema.Array(Schema.Literals(cities)) }).pipe(
Bundle.withChild('box', Bundles.Combobox, {
args: { id: 'stops', multiple: true, animated: true },
onOut: out => model => ({ model: { ...model, stops: Cities.picked(model.stops, out) } }),
}),
)
export default example({
title: 'Several, as chips',
slots: ComboboxSlots,
recipe: Recipes.Combobox,
state: App.placements.runtime({ Model: App.Model, initial: { stops: ['Lima', 'Seoul'] } }),
view: ({ style, model, onMessage, h }) =>
Combobox.view(
{
model: model.box,
onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
label: 'Stops',
choice: Cities,
value: model.stops,
filter: Combobox.contains,
chips: true,
placeholder: 'Add a stop',
name: 'stops',
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.
Empty and loading #
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { modifyFields } from 'foldkit/struct'
import { Bundles, Button, Combobox, ComboboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'
const cities = ['Kyiv', 'Lagos', 'Lima', 'Oslo', 'Osaka', 'Quito', 'Seoul', 'Zurich'] as const
const City = Combobox.one(cities)
const LoadingStyle = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })
// Whether the cities are still being fetched is the application's fact. While
// it holds, no row is offered and `empty` says why; after, it says no city
// matches the text.
const App = Bundle.compose({
city: Schema.Option(Schema.Literals(cities)),
loading: Schema.Boolean,
}).pipe(
Bundle.withMessages({ ToggledLoading: {} }),
Bundle.withChild('box', Bundles.Combobox, {
args: { id: 'empty-city' },
onOut: out => model => ({ model: { ...model, city: City.picked(model.city, out) } }),
}),
)
export default example({
title: 'Empty and loading',
slots: ComboboxSlots,
recipe: Recipes.Combobox,
state: App.placements.runtime({
Model: App.Model,
initial: { city: Option.none(), loading: false },
update: App.placements.update(model => ({
model: modifyFields(model, { loading: loading => !loading }),
})),
}),
view: ({ style, model, onMessage, h }) =>
stack(h, [
Button.view(
{
label: 'Still loading',
pressed: model.loading,
onClick: onMessage(App.Message.ToggledLoading()),
style: LoadingStyle,
},
h,
),
Combobox.view(
{
model: model.box,
onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
label: 'City',
choice: City,
value: model.city,
items: model.loading ? [] : undefined,
filter: Combobox.contains,
placeholder: 'Find a city',
empty: h.span([], [model.loading ? 'Loading cities…' : 'No cities match']),
style,
mixins: Combobox.fluid,
},
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 clear button #
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Combobox, ComboboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const cities = ['Kyiv', 'Lagos', 'Lima', 'Oslo', 'Osaka', 'Quito', 'Seoul', 'Zurich'] as const
const City = Combobox.one(cities)
// `nullable` lets an emptied field clear the value, and `text` is what the
// field shows for the city chosen before the page drew.
const App = Bundle.compose({ city: Schema.Option(Schema.Literals(cities)) }).pipe(
Bundle.withChild('box', Bundles.Combobox, {
args: { id: 'clearable-city', nullable: true, text: 'Oslo' },
onOut: out => model => ({ model: { ...model, city: City.picked(model.city, out) } }),
}),
)
export default example({
title: 'A clear button',
slots: ComboboxSlots,
recipe: Recipes.Combobox,
state: App.placements.runtime({ Model: App.Model, initial: { city: Option.some('Oslo') } }),
view: ({ style, model, onMessage, h }) =>
Combobox.view(
{
model: model.box,
onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
label: 'City',
choice: City,
value: model.city,
filter: Combobox.contains,
clear: true,
words: { clear: 'Clear the 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.
Groups #
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Combobox, ComboboxSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { label } from '../../snippets/layout.js'
// Rows of one group are adjacent in the list; `option` gives each row more
// than its name, and `heading` draws a group's.
const places = {
Lagos: 'Africa',
Lima: 'Americas',
Quito: 'Americas',
Oslo: 'Europe',
Zurich: 'Europe',
}
const cities = ['Lagos', 'Lima', 'Quito', 'Oslo', 'Zurich'] as const
const City = Combobox.one(cities)
const App = Bundle.compose({ city: Schema.Option(Schema.Literals(cities)) }).pipe(
Bundle.withChild('box', Bundles.Combobox, {
args: { id: 'grouped-city' },
onOut: out => model => ({ model: { ...model, city: City.picked(model.city, out) } }),
}),
)
export default example({
title: 'Groups',
slots: ComboboxSlots,
recipe: Recipes.Combobox,
state: App.placements.runtime({ Model: App.Model, initial: { city: Option.none() } }),
view: ({ style, model, onMessage, h }) =>
Combobox.view(
{
model: model.box,
onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
label: 'City',
choice: City,
value: model.city,
filter: Combobox.contains,
groupBy: city => places[city],
heading: group => label(h, [group]),
option: city => ({ label: city, description: places[city] }),
placeholder: 'Find a 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.
Creatable #
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { modifyFields } from 'foldkit/struct'
import { Bundles, Combobox, ComboboxSlots, ChoiceChange, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const cities = ['Kyiv', 'Lagos', 'Lima', 'Oslo', 'Osaka', 'Quito', 'Seoul', 'Zurich']
// With no match, the typed words are the one row, offered as a new city. The
// cities the reader made are the application's field beside the choice, and
// the choice is read over both lists.
const App = Bundle.compose({
city: Schema.Option(Schema.String),
created: Schema.Array(Schema.String),
}).pipe(
Bundle.withChild('box', Bundles.Combobox, {
args: { id: 'creatable-city' },
onOut: out => model => {
const created = ChoiceChange.match(out, {
Selected: ({ value }) =>
[...cities, ...model.created].includes(value) ? model.created : [...model.created, value],
ClearedSelection: () => model.created,
Typed: () => model.created,
})
return {
model: modifyFields(model, {
created: () => created,
city: city => Combobox.one([...cities, ...created]).picked(city, out),
}),
}
},
}),
)
export default example({
title: 'Creatable',
slots: ComboboxSlots,
recipe: Recipes.Combobox,
state: App.placements.runtime({
Model: App.Model,
initial: { city: Option.none(), created: [] },
}),
view: ({ style, model, onMessage, h }) => {
const known = [...cities, ...model.created]
const typed = model.box.inputValue.trim()
const matches = known.filter(city => city.toLowerCase().includes(typed.toLowerCase()))
return Combobox.view(
{
model: model.box,
onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
label: 'City',
choice: Combobox.one(known),
value: model.city,
items: matches.length === 0 && typed !== '' ? [typed] : matches,
option: city => ({ label: known.includes(city) ? city : `Create “${city}”` }),
placeholder: 'Find or add a city',
style,
mixins: Combobox.fluid,
},
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 choice of your own: at most three #
- Kyiv
- Oslo
import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Combobox, ComboboxSlots, Recipes, type Choice } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
// A rule of the application's own, written against the public `Choice`
// interface: the shipped `many`, with a fourth pick refused.
const upToThree = <Item extends string>(
items: ReadonlyArray<Item>,
): Choice<Item, ReadonlyArray<Item>> => {
const many = Combobox.many(items)
return {
...many,
picked: (value, change) => {
const next = many.picked(value, change)
return next.length > 3 ? value : next
},
}
}
const cities = ['Kyiv', 'Lagos', 'Lima', 'Oslo', 'Osaka', 'Quito', 'Seoul', 'Zurich'] as const
const Stops = upToThree(cities)
const App = Bundle.compose({ stops: Schema.Array(Schema.Literals(cities)) }).pipe(
Bundle.withChild('box', Bundles.Combobox, {
args: { id: 'three-stops', multiple: true },
onOut: out => model => ({ model: { ...model, stops: Stops.picked(model.stops, out) } }),
}),
)
export default example({
title: 'A choice of your own: at most three',
slots: ComboboxSlots,
recipe: Recipes.Combobox,
state: App.placements.runtime({ Model: App.Model, initial: { stops: ['Kyiv', 'Oslo'] } }),
view: ({ style, model, onMessage, h }) =>
Combobox.view(
{
model: model.box,
onMessage: message => onMessage(App.Message.GotBoxMessage({ message })),
label: 'Stops, up to three',
choice: Stops,
value: model.stops,
filter: Combobox.contains,
chips: 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.
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.