Date picker
A date behind a button, picked from a calendar that opens under it.
Previews are drawn in the theme you made.
Edit itPlaced as a Bundle #
import { Option } from 'effect'
import { Calendar as Dates } from 'foldkit'
import { Bundles, Calendar, DatePicker, DatePickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { today } from '../../snippets/today.js'
// A picker is two contracts: its own Slots, picked above, and its calendar's.
const CalendarStyle = Recipes.Calendar.style({})
// The Bundle holds the popover, the calendar and the selection, and closes
// the popover on the pick that makes the selection whole.
export default example({
title: 'Placed as a Bundle',
slots: DatePickerSlots,
recipe: Recipes.DatePicker,
state: Bundles.DatePicker.with({ id: 'due', today, animated: true }),
view: ({ style, model, onMessage, h }) =>
DatePicker.view(
{
model,
onMessage,
label: 'Due date',
trigger: selection =>
Option.match(Calendar.selectedDate(selection), {
onNone: () => 'Pick a due date',
onSome: date => Dates.formatLong(date, Dates.defaultEnglishLocale),
}),
name: 'due',
today: { label: 'Today', selects: true },
style,
calendarStyle: CalendarStyle,
},
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 range #
import { Option } from 'effect'
import { Calendar as Dates } from 'foldkit'
import { Bundles, Calendar, DatePicker, DatePickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { today } from '../../snippets/today.js'
const CalendarStyle = Recipes.Calendar.style({})
const short = Dates.formatShort(Dates.defaultEnglishLocale)
// The mode is the `selection` arg's tag: a range closes the popover on its
// second pick, and the trigger shows both ends.
export default example({
title: 'A range',
slots: DatePickerSlots,
recipe: Recipes.DatePicker,
state: Bundles.DatePicker.with({ id: 'stay', today, selection: Calendar.empty('Range') }),
view: ({ style, model, onMessage, h }) =>
DatePicker.view(
{
model,
onMessage,
label: 'Stay',
trigger: selection =>
Option.match(Calendar.rangeOf(selection), {
onNone: () => 'Pick check-in and check-out',
onSome: ({ start, end }) => `${short(start)} to ${short(end)}`,
}),
name: 'stay',
style,
calendarStyle: CalendarStyle,
},
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 range with presets #
import { Option } from 'effect'
import { Calendar as Dates } from 'foldkit'
import { Bundles, Calendar, DatePicker, DatePickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { today } from '../../snippets/today.js'
const CalendarStyle = Recipes.Calendar.style({})
const short = Dates.formatShort(Dates.defaultEnglishLocale)
// A range with presets: a preset that makes the range whole closes the
// popover, as the second pick does. `closes: 'manual'` would leave it open.
export default example({
title: 'A range with presets',
slots: DatePickerSlots,
recipe: Recipes.DatePicker,
state: Bundles.DatePicker.with({ id: 'report', today, selection: Calendar.empty('Range') }),
view: ({ style, model, onMessage, h }) =>
DatePicker.view(
{
model,
onMessage,
label: 'Report period',
trigger: selection =>
Option.match(Calendar.rangeOf(selection), {
onNone: () => 'Pick a period',
onSome: ({ start, end }) => `${short(start)} to ${short(end)}`,
}),
presets: [
{ label: 'Last 7 days', selection: Calendar.lastDays(today, 7) },
{ label: 'Last 30 days', selection: Calendar.lastDays(today, 30) },
{ label: 'This month', selection: Calendar.monthOf(today) },
],
style,
calendarStyle: CalendarStyle,
},
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.