Input
A labelled text field with a description.
Previews are drawn in the theme you made.
Edit itBasic #
We send the receipt here.
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Input, InputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ email: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// With a `label` the field draws itself: label, control, description. The text
// is the application's, folded by hand: no Bundle is needed.
export default example({
title: 'Basic',
slots: InputSlots,
recipe: Recipes.Input,
state: {
Model,
Message,
init: () => ({ model: { email: '' } }),
update: (model, { value }) => ({ model: modifyFields(model, { email: () => value }) }),
},
view: ({ style, model, onMessage, h }) =>
Input.view(
{
id: 'email',
label: 'Email',
description: 'We send the receipt here.',
type: 'email',
autocomplete: 'email',
placeholder: 'ada@example.com',
value: model.email,
onInput: value => onMessage(Message.Typed({ value })),
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 prefix and a suffix #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Input, InputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ site: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// `start` and `end` sit inside the field's box, beside the control: the box
// carries the border and the focus ring, so the three read as one field.
export default example({
title: 'A prefix and a suffix',
slots: InputSlots,
recipe: Recipes.Input,
state: {
Model,
Message,
init: () => ({ model: { site: 'foldkit' } }),
update: (model, { value }) => ({ model: modifyFields(model, { site: () => value }) }),
},
view: ({ style, model, onMessage, h }) =>
Input.view(
{
id: 'site',
label: 'Website',
value: model.site,
onInput: value => onMessage(Message.Typed({ value })),
start: h.span([], ['https://']),
end: h.span([], ['.com']),
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 label #
Shown on your public profile.
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Input, InputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ handle: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// `labelAside` stands beside the label, at its end, and not inside it, so an
// interactive one is not part of what names the control.
export default example({
title: 'Beside the label',
slots: InputSlots,
recipe: Recipes.Input,
state: {
Model,
Message,
init: () => ({ model: { handle: '' } }),
update: (model, { value }) => ({ model: modifyFields(model, { handle: () => value }) }),
},
view: ({ style, model, onMessage, h }) =>
Input.view(
{
id: 'handle',
label: 'Handle',
labelAside: h.span([], [`${model.handle.length} / 15`]),
description: 'Shown on your public profile.',
maxLength: 15,
value: model.handle,
onInput: value => onMessage(Message.Typed({ value })),
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.
An amount #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { modifyFields } from 'foldkit/struct'
import { Input, InputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ amount: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// A unit before and after the number, and the keyboard a phone offers for it.
export default example({
title: 'An amount',
slots: InputSlots,
recipe: Recipes.Input,
state: {
Model,
Message,
init: () => ({ model: { amount: '49.00' } }),
update: (model, { value }) => ({ model: modifyFields(model, { amount: () => value }) }),
},
view: ({ style, model, onMessage, h }) =>
Input.view(
{
id: 'amount',
name: 'amount',
label: 'Price',
inputMode: 'decimal',
value: model.amount,
onInput: value => onMessage(Message.Typed({ value })),
start: h.span([], ['$']),
end: h.span([], ['USD']),
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 masked phone number #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import * as Mount from 'foldkit/mount'
import { modifyFields } from 'foldkit/struct'
import { InputMask } from 'foldkit-primitives/dom'
import { Input, InputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ phone: Schema.String })
const Message = defineMessageUnion({ Masked: { value: Schema.String } })
// A phone number is typed as digits and shown as `(555) 123-4567`. `InputMask`
// is a Mount on the control that rewrites it and keeps the caret; the Input
// only passes it through `attributes`. The masked text is what is kept here.
export default example({
title: 'A masked phone number',
slots: InputSlots,
recipe: Recipes.Input,
state: {
Model,
Message,
init: () => ({ model: { phone: '' } }),
update: (model, { value }) => ({ model: modifyFields(model, { phone: () => value }) }),
},
view: ({ style, model, onMessage, h }) =>
Input.view(
{
id: 'masked-phone',
name: 'phone',
label: 'Phone',
type: 'tel',
inputMode: 'tel',
placeholder: '(555) 123-4567',
value: model.phone,
start: h.span([], ['+1']),
attributes: {
input: [
h.OnMount(
Mount.mapMessage(InputMask({ pattern: '(###) ###-####' }), ({ value }) =>
onMessage(Message.Masked({ value })),
),
),
],
},
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 a form, and its states #
That code has expired.
import { Input, InputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'
// What a form reads: the `name` a value submits under, `required`, and a
// `readOnly` value, which can be selected and is submitted with the rest.
// `invalid` and `disabled` mark the field as a whole, adornments included.
export default example({
title: 'In a form, and its states',
slots: InputSlots,
recipe: Recipes.Input,
view: ({ style, h }) =>
stack(
h,
[
Input.view(
{
id: 'phone',
name: 'phone',
label: 'Phone',
type: 'tel',
inputMode: 'tel',
required: true,
placeholder: '+1 555 0100',
style,
},
h,
),
Input.view(
{ id: 'order', name: 'order', label: 'Order', value: 'ORD-2041', readOnly: true, style },
h,
),
Input.view(
{
id: 'code',
label: 'Promo code',
description: 'That code has expired.',
value: 'SPRING',
invalid: true,
style,
},
h,
),
Input.view(
{
id: 'region',
label: 'Region',
value: 'eu-west',
disabled: true,
start: h.span([], ['region:']),
style,
},
h,
),
],
[h.Style({ alignItems: 'stretch', inlineSize: 'min(20rem, 100%)' })],
),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
A look of your own #
import { Style } from 'foldkit-mixins'
import { Input, InputSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
// The shipped recipe has no `width` axis. `add` gives it one, and its
// variants and sizes still apply to the control and the group.
const Inputs = Recipes.Input.add({
width: { narrow: { root: Style.self({ inlineSize: '12rem' }) } },
})
export default example({
title: 'A look of your own',
slots: InputSlots,
recipe: Inputs,
pins: { width: 'narrow' },
view: ({ style, h }) =>
Input.view({ id: 'zip', label: 'Postcode', placeholder: 'SW1A 1AA', style }, h),
})In your application: recipe goes to forSlots, 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
- What a form submits Give the widget a name and a form reads it as it reads a native field.