Field
The shell around a control you drew: its label and mark, a description and errors, beside it or above it.
Previews are drawn in the theme you made.
Edit itA label, a description and a mark #
We send the receipt here.
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Field, FieldSlots, Input, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Control = Recipes.Input.style({})
const Model = Schema.Struct({ email: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// The shell draws the label, the description and the errors; the control is
// drawn by the application, with the id the label names and the `describedBy`
// the shell's description is reached by. It owns no state.
export default example({
title: 'A label, a description and a mark',
slots: FieldSlots,
recipe: Recipes.Field,
state: {
Model,
Message,
init: () => ({ model: { email: '' } }),
update: (_, { value }) => ({ model: { email: value } }),
},
view: ({ style, model, onMessage, h }) =>
Field.view(
{
id: 'email',
label: 'Email',
required: true,
description: 'We send the receipt here.',
control: Input.view(
{
id: 'email',
type: 'email',
required: true,
value: model.email,
onInput: value => onMessage(Message.Typed({ value })),
attributes: { input: Field.describedBy(h, 'email', { description: true }) },
style: Control,
draw: ({ input }, h) => h.input(input),
},
h,
),
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.
Errors #
- No spaces
- Letters and digits only
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Field, FieldSlots, Input, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Control = Recipes.Input.style({})
const Model = Schema.Struct({ handle: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// Errors are a list of lines under the control, in an alert; the shell and
// the control both say it is invalid, and the control points at the list.
export default example({
title: 'Errors',
slots: FieldSlots,
recipe: Recipes.Field,
state: {
Model,
Message,
init: () => ({ model: { handle: 'ada lovelace!' } }),
update: (_, { value }) => ({ model: { handle: value } }),
},
view: ({ style, model, onMessage, h }) => {
const errors = [
...(/\s/.test(model.handle) ? ['No spaces'] : []),
...(/[^\w\s]/.test(model.handle) ? ['Letters and digits only'] : []),
]
return Field.view(
{
id: 'handle',
label: 'Handle',
optional: true,
errors,
control: Input.view(
{
id: 'handle',
value: model.handle,
invalid: errors.length > 0,
onInput: value => onMessage(Message.Typed({ value })),
attributes: {
input: Field.describedBy(h, 'handle', { errors: errors.length > 0 }),
},
style: Control,
draw: ({ input }, h) => h.input(input),
},
h,
),
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.
Beside the control #
As on your passport.
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Field, FieldSlots, Input, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Control = Recipes.Input.style({})
const Model = Schema.Struct({ name: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// `horizontal` puts the label in a column of its own beside the control, the
// description under the control; `labelAside` stands beside the label.
export default example({
title: 'Beside the control',
slots: FieldSlots,
recipe: Recipes.Field,
state: {
Model,
Message,
init: () => ({ model: { name: 'Ada Lovelace' } }),
update: (_, { value }) => ({ model: { name: value } }),
},
view: ({ style, model, onMessage, h }) =>
Field.view(
{
id: 'name',
label: 'Full name',
orientation: 'horizontal',
description: 'As on your passport.',
control: Input.view(
{
id: 'name',
value: model.name,
onInput: value => onMessage(Message.Typed({ value })),
attributes: { input: Field.describedBy(h, 'name', { description: true }) },
style: Control,
draw: ({ input }, h) => h.input(input),
},
h,
),
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.