Number field
A number typed or stepped, inside its bounds and on its step.
Previews are drawn in the theme you made.
Edit itBasic #
From 1 to 12; the arrow keys step it.
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { NumberField, NumberFieldSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ servings: Schema.Option(Schema.Number) })
const Message = defineMessageUnion({ Changed: { value: Schema.Option(Schema.Number) } })
// The number is the application's, and `Option.none()` is an empty field.
// The arrows step it, Page Up and Page Down step by ten, and Home and End
// take it to the bounds; a typed number commits on blur or Enter.
export default example({
title: 'Basic',
slots: NumberFieldSlots,
recipe: Recipes.NumberField,
state: {
Model,
Message,
init: () => ({ model: { servings: Option.some(4) } }),
update: (_, { value }) => ({ model: { servings: value } }),
},
view: ({ style, model, onMessage, h }) =>
NumberField.view(
{
id: 'servings',
label: 'Servings',
description: 'From 1 to 12; the arrow keys step it.',
value: model.servings,
onChange: value => onMessage(Message.Changed({ value })),
min: 1,
max: 12,
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 locale, with a unit #
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { NumberField, NumberFieldSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ price: Schema.Option(Schema.Number) })
const Message = defineMessageUnion({ Changed: { value: Schema.Option(Schema.Number) } })
// `locale` draws and reads that locale's marks (`1.234,5`), with the unit
// beside the number in `end`: the box is one field, the number the Model's.
export default example({
title: 'In a locale, with a unit',
slots: NumberFieldSlots,
recipe: Recipes.NumberField,
state: {
Model,
Message,
init: () => ({ model: { price: Option.some(1234.5) } }),
update: (_, { value }) => ({ model: { price: value } }),
},
view: ({ style, model, onMessage, h }) =>
NumberField.view(
{
id: 'price',
label: 'Preis',
value: model.price,
onChange: value => onMessage(Message.Changed({ value })),
locale: 'de-DE',
step: 0.5,
min: 0,
end: h.span([], ['EUR']),
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, read only and disabled #
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { NumberField, NumberFieldSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'
const Model = Schema.Struct({ seats: Schema.Option(Schema.Number) })
const Message = defineMessageUnion({ Changed: { value: Schema.Option(Schema.Number) } })
// `invalid` draws the error edge and marks the input. `readOnly` can be read
// and selected but not changed, and its steppers are disabled.
export default example({
title: 'Invalid, read only and disabled',
slots: NumberFieldSlots,
recipe: Recipes.NumberField,
state: {
Model,
Message,
init: () => ({ model: { seats: Option.some(40) } }),
update: (_, { value }) => ({ model: { seats: value } }),
},
view: ({ style, model, onMessage, h }) =>
stack(
h,
[
{ id: 'seats', label: 'Seats (12 at most)', max: 12, invalid: true },
{ id: 'floor', label: 'Floor', readOnly: true },
{ id: 'rooms', label: 'Rooms', disabled: true },
].map(options =>
NumberField.view(
{
...options,
value: model.seats,
onChange: value => onMessage(Message.Changed({ value })),
style,
},
h,
),
),
[h.Style({ alignItems: 'stretch' })],
),
})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 French #
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { NumberField, NumberFieldSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ chairs: Schema.Option(Schema.Number) })
const Message = defineMessageUnion({ Changed: { value: Schema.Option(Schema.Number) } })
// The steppers' names are words, merged over the defaults one by one: a word
// you do not give keeps its English default.
export default example({
title: 'In French',
slots: NumberFieldSlots,
recipe: Recipes.NumberField,
state: {
Model,
Message,
init: () => ({ model: { chairs: Option.some(6) } }),
update: (_, { value }) => ({ model: { chairs: value } }),
},
view: ({ style, model, onMessage, h }) =>
NumberField.view(
{
id: 'chairs',
label: 'Chaises',
value: model.chairs,
onChange: value => onMessage(Message.Changed({ value })),
words: { decrease: 'Moins', increase: 'Plus' },
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.
Related
- What a form submits Give the widget a name and a form reads it as it reads a native field.