Rating
A score out of five stars, halves or hearts, chosen with a press or the arrows, or shown read-only to any fraction.
Previews are drawn in the theme you made.
Edit itBasic #
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Rating, RatingSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ value: Schema.OptionFromNullOr(Schema.Number) })
const Message = defineMessageUnion({ Rated: { value: Schema.Number } })
// The stars given are the application's field. The group's name and the
// native radios under one name are the widget's: the arrows move the choice,
// and a form submits the number.
export default example({
title: 'Basic',
slots: RatingSlots,
recipe: Recipes.Rating,
state: {
Model,
Message,
init: () => ({ model: { value: Option.none() } }),
update: (_, { value }) => ({ model: { value: Option.some(value) } }),
},
view: ({ style, model, onMessage, h }) =>
Rating.view(
{
id: 'stay',
label: 'Your stay',
value: model.value,
onChange: value => onMessage(Message.Rated({ 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.
Half stars, cleared by a second press #
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Rating, RatingSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ value: Schema.OptionFromNullOr(Schema.Number) })
const Message = defineMessageUnion({ Rated: { value: Schema.Number }, Cleared: {} })
// Each star is two radios. A press on the star already chosen sends
// `onClear`; without it, the press keeps the rating.
export default example({
title: 'Half stars, cleared by a second press',
slots: RatingSlots,
recipe: Recipes.Rating,
state: {
Model,
Message,
init: () => ({ model: { value: Option.some(3.5) } }),
update: (model, message) =>
Message.match(message, {
Rated: ({ value }) => ({ model: { value: Option.some(value) } }),
Cleared: () => ({ model: Option.isNone(model.value) ? model : { value: Option.none() } }),
}),
},
view: ({ style, model, onMessage, h }) =>
Rating.view(
{
id: 'film',
label: 'The film',
value: model.value,
precision: 0.5,
onChange: value => onMessage(Message.Rated({ value })),
onClear: () => onMessage(Message.Cleared()),
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 read-only average #
import { Option } from 'effect'
import { Rating, RatingSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
// Read-only, the group is one image whose name says the rating, and a
// fraction fills the last star as far as it goes. It sends nothing, so it
// takes no Message.
export default example({
title: 'A read-only average',
slots: RatingSlots,
recipe: Recipes.Rating,
view: ({ style, h }) =>
Rating.view(
{
id: 'average',
label: 'Average of 1,204 reviews',
value: Option.some(4.3),
readOnly: true,
showValue: true,
style,
},
h,
),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
Hearts #
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Rating, RatingSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ value: Schema.OptionFromNullOr(Schema.Number) })
const Message = defineMessageUnion({ Rated: { value: Schema.Number } })
// Hearts are a look: `glyph`, `tone` and `size` of the recipe, pinned here.
// What the group reads aloud is the words.
export default example({
title: 'Hearts',
slots: RatingSlots,
recipe: Recipes.Rating,
pins: { glyph: 'heart', tone: 'danger', size: 'lg' },
state: {
Model,
Message,
init: () => ({ model: { value: Option.some(4) } }),
update: (_, { value }) => ({ model: { value: Option.some(value) } }),
},
view: ({ style, model, onMessage, h }) =>
Rating.view(
{
id: 'hearts',
label: 'How much you liked it',
value: model.value,
words: { one: '{value} heart', other: '{value} hearts' },
onChange: value => onMessage(Message.Rated({ 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 word per value #
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Rating, RatingSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ value: Schema.OptionFromNullOr(Schema.Number) })
const Message = defineMessageUnion({ Rated: { value: Schema.Number } })
// `labels` names each whole number of stars, from one up: read aloud for
// each radio, and shown beside the stars with `showValue`.
export default example({
title: 'A word per value',
slots: RatingSlots,
recipe: Recipes.Rating,
state: {
Model,
Message,
init: () => ({ model: { value: Option.none() } }),
update: (_, { value }) => ({ model: { value: Option.some(value) } }),
},
view: ({ style, model, onMessage, h }) =>
Rating.view(
{
id: 'support',
label: 'Your support call',
value: model.value,
labels: ['Useless', 'Poor', 'Ok', 'Good', 'Excellent'],
showValue: true,
onChange: value => onMessage(Message.Rated({ 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.
Configure once #
import { Option } from 'effect'
import { Rating, RatingSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'
// The words and the scale every rating of the application starts from, said
// once; the preset changes only the scale. A call gives what is its own.
const AppRating = Rating.configure({
base: { max: 10, words: { one: '{value} point', other: '{value} points' } },
presets: { five: { max: 5 } },
})
export default example({
title: 'Configure once',
slots: RatingSlots,
recipe: Recipes.Rating,
view: ({ style, h }) =>
row(h, [
AppRating.view(
{ id: 'quality', label: 'Quality', value: Option.some(7), readOnly: true, style },
h,
),
AppRating.view(
{
id: 'service',
label: 'Service',
preset: 'five',
value: Option.some(4),
readOnly: true,
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.