Radio group
One choice of a few, each with a line that explains it.
Previews are drawn in the theme you made.
Edit itDescriptions #
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, RadioGroup, RadioGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
// The focus cursor is the Bundle's; which option is chosen is the
// application's, set from the `Selected` the Bundle reports and given back
// as `value`.
const App = Bundle.compose({ plan: Schema.OptionFromNullOr(Schema.String) }).pipe(
Bundle.withChild('plans', Bundles.RadioGroup, {
args: { id: 'billing' },
onOut: out => model => ({ model: { ...model, plan: Option.some(out.value) } }),
}),
)
export default example({
title: 'Descriptions',
slots: RadioGroupSlots,
recipe: Recipes.RadioGroup,
state: App.placements.runtime({ Model: App.Model, initial: { plan: Option.some('annual') } }),
view: ({ style, model, onMessage, h }) =>
RadioGroup.view(
{
model: model.plans,
onMessage: message => onMessage(App.Message.GotPlansMessage({ message })),
legend: 'Billing',
options: [
{
value: 'monthly',
label: 'Monthly',
description: '12 dollars a month, cancel any time.',
},
{ value: 'annual', label: 'Annual', description: '100 dollars a year, two months free.' },
{ value: 'lifetime', label: 'Lifetime', description: 'Sold out.', disabled: true },
],
value: model.plan,
name: 'billing',
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.
Cards #
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Badge, Bundles, RadioGroup, RadioGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Tag = Recipes.Badge.style({})
const App = Bundle.compose({ plan: Schema.OptionFromNullOr(Schema.String) }).pipe(
Bundle.withChild('plans', Bundles.RadioGroup, {
args: { id: 'pricing' },
onOut: out => model => ({ model: { ...model, plan: Option.some(out.value) } }),
}),
)
// `variant: 'card'` boxes each row and fills the chosen one. A row's mark
// (`icon`) and its `trailing` badge are drawn in slots of their own, so a
// pricing card is data and two Html.
export default example({
title: 'Cards',
slots: RadioGroupSlots,
recipe: Recipes.RadioGroup,
pins: { variant: 'card' },
state: App.placements.runtime({ Model: App.Model, initial: { plan: Option.some('team') } }),
view: ({ style, model, onMessage, h }) =>
RadioGroup.view(
{
model: model.plans,
onMessage: message => onMessage(App.Message.GotPlansMessage({ message })),
legend: 'Plan',
options: [
{ value: 'solo', label: 'Solo', description: 'One editor.', icon: h.span([], ['1']) },
{
value: 'team',
label: 'Team',
description: 'Ten editors.',
icon: h.span([], ['10']),
trailing: Badge.view({ text: 'Popular', tone: 'accent', style: Tag }, h),
},
],
value: model.plan,
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.
Grid #
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, RadioGroup, RadioGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const App = Bundle.compose({ size: Schema.OptionFromNullOr(Schema.String) }).pipe(
Bundle.withChild('sizes', Bundles.RadioGroup, {
args: { id: 'shirt' },
onOut: out => model => ({ model: { ...model, size: Option.some(out.value) } }),
}),
)
// A grid is a look: the rows set in columns that wrap as the room allows.
export default example({
title: 'Grid',
slots: RadioGroupSlots,
recipe: Recipes.RadioGroup,
pins: { variant: 'card', layout: 'grid' },
state: App.placements.runtime({ Model: App.Model, initial: { size: Option.some('m') } }),
view: ({ style, model, onMessage, h }) =>
RadioGroup.view(
{
model: model.sizes,
onMessage: message => onMessage(App.Message.GotSizesMessage({ message })),
legend: 'Size',
options: ['xs', 's', 'm', 'l', 'xl', 'xxl'].map(value => ({
value,
label: value.toUpperCase(),
})),
value: model.size,
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 #
Choose an answer.
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, RadioGroup, RadioGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const App = Bundle.compose({ answer: Schema.OptionFromNullOr(Schema.String) }).pipe(
Bundle.withChild('choice', Bundles.RadioGroup, {
args: { id: 'answer' },
onOut: out => model => ({ model: { ...model, answer: Option.some(out.value) } }),
}),
)
// An `error` makes the group `aria-invalid`, says why under it and describes
// the group by it. It goes with the answer: the field's own rule is the
// application's.
export default example({
title: 'Invalid',
slots: RadioGroupSlots,
recipe: Recipes.RadioGroup,
state: App.placements.runtime({ Model: App.Model, initial: { answer: Option.none() } }),
view: ({ style, model, onMessage, h }) =>
RadioGroup.view(
{
model: model.choice,
onMessage: message => onMessage(App.Message.GotChoiceMessage({ message })),
legend: 'Will you attend?',
options: [
{ value: 'yes', label: 'Yes' },
{ value: 'no', label: 'No' },
],
value: model.answer,
...(Option.isNone(model.answer) ? { error: 'Choose an answer.' } : {}),
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.
Configure once #
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, RadioGroup, RadioGroupSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Cards = Recipes.RadioGroup.style({ variant: 'card' })
// Every group of the application is a row of cards with the arrows as the
// APG asks them; a preset stacks them. The Message lift and the Model stay
// at the use.
const AppRadioGroup = RadioGroup.configure({
base: { style: Cards, orientation: 'horizontal', mixins: [RadioGroup.readingKeys] },
presets: { stacked: { orientation: 'vertical' } },
})
const App = Bundle.compose({ tier: Schema.OptionFromNullOr(Schema.String) }).pipe(
Bundle.withChild('tiers', Bundles.RadioGroup, {
args: { id: 'tier' },
onOut: out => model => ({ model: { ...model, tier: Option.some(out.value) } }),
}),
)
export default example({
title: 'Configure once',
slots: RadioGroupSlots,
recipe: Recipes.RadioGroup,
state: App.placements.runtime({ Model: App.Model, initial: { tier: Option.some('b') } }),
view: ({ model, onMessage, h }) =>
AppRadioGroup.view(
{
model: model.tiers,
onMessage: message => onMessage(App.Message.GotTiersMessage({ message })),
legend: 'Tier',
options: [
{ value: 'a', label: 'Basic' },
{ value: 'b', label: 'Pro' },
],
value: model.tier,
},
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.