Search field
A search input with a clear button, Escape to clear, and keys of your own beside it.
Previews are drawn in the theme you made.
Edit itBasic #
import { Schema } from 'effect'
import * as Command from 'foldkit/command'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Recipes, SearchField, SearchFieldSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ query: Schema.String })
const Message = defineMessageUnion({
Typed: { text: Schema.String },
Cleared: {},
Focused: {},
})
// The text is the application's. `clear` draws the button, and Escape sends
// the same Message; its answer puts focus back, since the pressed button
// takes its focus with it when it goes.
export default example({
title: 'Basic',
slots: SearchFieldSlots,
recipe: Recipes.SearchField,
state: {
Model,
Message,
init: () => ({ model: { query: '' } }),
update: (model, message) =>
Message.match(message, {
Typed: ({ text }) => ({ model: modifyFields(model, { query: () => text }) }),
Cleared: () => ({
model: modifyFields(model, { query: () => '' }),
commands: Command.mapMessages([SearchField.Focus({ id: 'find' })], () =>
Message.Focused(),
),
}),
Focused: () => ({ model }),
}),
},
view: ({ style, model, onMessage, h }) =>
SearchField.view(
{
id: 'find',
label: 'Search products',
placeholder: 'Search products',
value: model.query,
onInput: text => onMessage(Message.Typed({ text })),
clear: { onClear: onMessage(Message.Cleared()) },
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.
Keys of your own #
- Apple pie
- Apple crumble
- Baked apples
- Pear tart
- Plum cake
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Behaviors } from 'foldkit-mixins'
import { Recipes, SearchField, SearchFieldSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const dishes = ['Apple pie', 'Apple crumble', 'Baked apples', 'Pear tart', 'Plum cake']
const Model = Schema.Struct({ query: Schema.String, active: Schema.Number })
const Message = defineMessageUnion({ Typed: { text: Schema.String }, Next: {} })
const matches = (query: string) => dishes.filter(dish => dish.toLowerCase().includes(query.trim()))
// A key of the application's own shares the field's one keydown owner with
// Escape: ArrowDown moves to the next result, but only once there is text.
export default example({
title: 'Keys of your own',
slots: SearchFieldSlots,
recipe: Recipes.SearchField,
state: {
Model,
Message,
init: () => ({ model: { query: '', active: 0 } }),
update: (model, message) =>
Message.match(message, {
Typed: ({ text }) => ({ model: { query: text, active: 0 } }),
Next: () => ({
model: modifyFields(model, {
active: at => (at + 1) % Math.max(1, matches(model.query).length),
}),
}),
}),
},
view: ({ style, model, onMessage, h }) =>
h.div(
[],
[
SearchField.view(
{
id: 'dishes',
label: 'Find a dish',
placeholder: 'Type apple, then press the down arrow',
value: model.query,
onInput: text => onMessage(Message.Typed({ text })),
routes: [
Behaviors.Keys.route('Next', {
on: [Behaviors.Keys.chord('ArrowDown')],
handle: ({ input }) =>
input === '' ? Option.none() : Option.some(onMessage(Message.Next())),
}),
],
style,
},
h,
),
h.ul(
[],
matches(model.query).map((dish, at) =>
h.li([h.Key(dish), ...(at === model.active ? [h.AriaCurrent('true')] : [])], [dish]),
),
),
],
),
})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.
Busy, with a clear of your own #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, SearchField, SearchFieldSlots, Spinner } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Wheel = Recipes.Spinner.style({ size: 'sm', tone: 'current' })
const Model = Schema.Struct({ query: Schema.String })
const Message = defineMessageUnion({ Typed: { text: Schema.String }, Cleared: {} })
// `busy` is `aria-busy` and `data-busy`; it draws no spinner, so no page pays
// for one: the application composes its own into `end`. `clear.content`
// replaces the cross with words.
export default example({
title: 'Busy, with a clear of your own',
slots: SearchFieldSlots,
recipe: Recipes.SearchField,
state: {
Model,
Message,
init: () => ({ model: { query: 'lamp' } }),
update: (_, message) => ({
model: { query: Message.match(message, { Typed: ({ text }) => text, Cleared: () => '' }) },
}),
},
view: ({ style, model, onMessage, h }) =>
SearchField.view(
{
id: 'busy',
label: 'Search products',
value: model.query,
busy: model.query !== '',
end: model.query === '' ? undefined : Spinner.view({ style: Wheel }, h),
onInput: text => onMessage(Message.Typed({ text })),
clear: { onClear: onMessage(Message.Cleared()), content: h.span([], ['Reset']) },
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.
Invalid, disabled and with no clear #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Recipes, SearchField, SearchFieldSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'
const Field = Schema.Literals(['order', 'docs'])
const Model = Schema.Struct({ order: Schema.String, docs: Schema.String })
const Message = defineMessageUnion({ Typed: { field: Field, text: Schema.String } })
// Invalid names a field that cannot be searched yet. A disabled field takes
// no text or keys and draws no clear button. With no `clear` there is no
// button and Escape is left to the page.
export default example({
title: 'Invalid, disabled and with no clear',
slots: SearchFieldSlots,
recipe: Recipes.SearchField,
state: {
Model,
Message,
init: () => ({ model: { order: 'ord-', docs: 'focus' } }),
update: (model, { field, text }) => ({
model:
field === 'order'
? modifyFields(model, { order: () => text })
: modifyFields(model, { docs: () => text }),
}),
},
view: ({ style, model, onMessage, h }) =>
stack(
h,
[
SearchField.view(
{
id: 'order',
label: 'Search by order number',
value: model.order,
invalid: true,
onInput: text => onMessage(Message.Typed({ field: 'order', text })),
style,
},
h,
),
SearchField.view(
{
id: 'archive',
label: 'Search the archive',
value: 'lamps',
disabled: true,
onInput: text => onMessage(Message.Typed({ field: 'docs', text })),
style,
},
h,
),
SearchField.view(
{
id: 'docs',
label: 'Search the docs',
value: model.docs,
onInput: text => onMessage(Message.Typed({ field: 'docs', text })),
style,
},
h,
),
],
[h.Style({ alignItems: 'stretch', inlineSize: 'min(20rem, 100%)' })],
),
})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.
Related
- 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. - Keeping focus when something is removed A control that leaves the page takes its focus with it: say where focus goes next, to a neighbour (
FocusRecovery), to the opener (focusReturn) or to a control (keepFocus).