Textarea
A field for more than one line: it grows with its text up to a number of lines, counts toward a limit, and holds a row of controls.
Previews are drawn in the theme you made.
Edit itBasic #
import { FieldValidation } from 'foldkit'
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Textarea, TextareaSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ text: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// A field from a `FieldValidation.Field`: the label, the text, and what it says
// under itself. The text is the application's own.
export default example({
title: 'Basic',
slots: TextareaSlots,
recipe: Recipes.Textarea,
state: {
Model,
Message,
init: () => ({ model: { text: '' } }),
update: (_, { value }) => ({ model: { text: value } }),
},
view: ({ style, model, onMessage, h }) =>
h.div(
[h.Style({ inlineSize: 'min(100%, 24rem)' })],
[
Textarea.field(
{
field: FieldValidation.NotValidated({ value: model.text }),
changed: value => onMessage(Message.Typed({ value })),
id: 'message',
label: 'Message',
rows: 3,
placeholder: 'What should we know?',
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.
Grows with its text #
import { FieldValidation } from 'foldkit'
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Textarea, TextareaSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ text: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// `resize: 'auto'` (the default): two lines empty, a line more with each
// line written, and from six lines on it scrolls.
export default example({
title: 'Grows with its text',
slots: TextareaSlots,
recipe: Recipes.Textarea,
state: {
Model,
Message,
init: () => ({ model: { text: '' } }),
update: (_, { value }) => ({ model: { text: value } }),
},
view: ({ style, model, onMessage, h }) =>
h.div(
[h.Style({ inlineSize: 'min(100%, 24rem)' })],
[
Textarea.field(
{
field: FieldValidation.NotValidated({ value: model.text }),
changed: value => onMessage(Message.Typed({ value })),
id: 'notes',
label: 'Notes',
rows: 2,
maxRows: 6,
placeholder: 'Write a line, then another…',
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 fixed height #
import { FieldValidation } from 'foldkit'
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Textarea, TextareaSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const terms = [
'Terms',
'',
'1. You keep what you write.',
'2. We keep it safe.',
'3. Either of us may end this.',
].join('\n')
const Model = Schema.Struct({ text: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// Pinned: `Recipes.Textarea({ resize: 'vertical' })`. Four lines whatever
// the text, and the reader drags the corner for more; `resize: 'none'` holds it.
export default example({
title: 'A fixed height',
slots: TextareaSlots,
recipe: Recipes.Textarea,
pins: { resize: 'vertical' },
state: {
Model,
Message,
init: () => ({ model: { text: terms } }),
update: (_, { value }) => ({ model: { text: value } }),
},
view: ({ style, model, onMessage, h }) =>
h.div(
[h.Style({ inlineSize: 'min(100%, 24rem)' })],
[
Textarea.field(
{
field: FieldValidation.NotValidated({ value: model.text }),
changed: value => onMessage(Message.Typed({ value })),
id: 'terms',
label: 'Terms',
rows: 4,
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 character count #
import { FieldValidation } from 'foldkit'
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Textarea, TextareaSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ text: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// With a `maxLength` the field counts toward it, in the warning's ink from
// nine tenths and the error's at the limit, and the count describes the field.
export default example({
title: 'A character count',
slots: TextareaSlots,
recipe: Recipes.Textarea,
state: {
Model,
Message,
init: () => ({ model: { text: 'Arriving Thursday, two nights.' } }),
update: (_, { value }) => ({ model: { text: value } }),
},
view: ({ style, model, onMessage, h }) =>
h.div(
[h.Style({ inlineSize: 'min(100%, 24rem)' })],
[
Textarea.field(
{
field: FieldValidation.NotValidated({ value: model.text }),
changed: value => onMessage(Message.Typed({ value })),
id: 'request',
label: 'Requests for your host',
maxLength: 60,
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.
With a footer #
import { FieldValidation } from 'foldkit'
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Textarea, TextareaSlots, Button } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Ghost = Recipes.Button.style({ tone: 'neutral', variant: 'ghost', size: 'sm' })
const Send = Recipes.Button.style({ size: 'sm' })
const Model = Schema.Struct({ text: Schema.String })
const Message = defineMessageUnion({ Typed: { value: Schema.String } })
// A `footer` draws the field as one box, its controls under the text and
// the count at their end; the ring shows while the text has focus.
export default example({
title: 'With a footer',
slots: TextareaSlots,
recipe: Recipes.Textarea,
state: {
Model,
Message,
init: () => ({ model: { text: '' } }),
update: (_, { value }) => ({ model: { text: value } }),
},
view: ({ style, model, onMessage, h }) =>
h.div(
[h.Style({ inlineSize: 'min(100%, 24rem)' })],
[
Textarea.field(
{
field: FieldValidation.NotValidated({ value: model.text }),
changed: value => onMessage(Message.Typed({ value })),
id: 'reply',
label: 'Reply',
rows: 2,
maxRows: 8,
maxLength: 500,
placeholder: 'Write a reply…',
footer: [
Button.view({ label: 'Attach', style: Ghost }, h),
Button.view({ label: 'Send', disabled: model.text.trim() === '', style: Send }, 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.
Related
- What a form submits Give the widget a name and a form reads it as it reads a native field.