CurrySoftware/elm-datepicker/2.1.1

NOTE: the latest version is 4.0.0

DatePicker

A customizable date picker component.

Tea ☕

type Msg

An opaque type representing messages that are passed inside the DatePicker.

= None
| FailedInput InputError
| Picked Date

The event resulting from a DatePicker.update. Three things can happen:

  • Nothing
  • The user might pick a date through clicking or typing
  • Or the user typed a date that is either invalid or disabled

If you do not care about case error handling for invalid inputs, just matching on Picked should suffice.

Have a look at the nightwash-simple example for basic error handling with InputError.

= Invalid String
| Disabled Date

When typing a date it can go wrong in two ways:

  • Invalid String: the typed string cannot be parsed into a date. The error string is given be the parser defined in the settings.
  • Disabled Date: a valid date was typed, but it is disabled as defined in the settings.

The DatePicker model. Opaque, hence no field docs.

init : ( DatePicker, Cmd Msg )

The default initial state of the Datepicker. You must execute the returned command (which, for the curious, sets the current date) for the date picker to behave correctly.

init =
    let
        ( datePicker, datePickerFx ) =
            DatePicker.init
    in
    ( { picker = datePicker }, Cmd.map ToDatePicker datePickerfx )

Initialize a DatePicker with a given Date

init date =
    ( { picker = DatePicker.initFromDate date }, Cmd.none )
initFromDates : Date -> Maybe Date -> DatePicker

Initialize a DatePicker with a date for today and Maybe a date picked

init today date =
    ( { picker = DatePicker.initFromDates today date }, Cmd.none )
update : Settings -> Msg -> DatePicker -> ( DatePicker, Cmd Msg, DateEvent )

The date picker update function. The third tuple member represents a user action to change the date.

view : Maybe Date -> Settings -> DatePicker -> Html Msg

The date picker view. The Date passed is whatever date it should treat as selected.

isOpen : DatePicker -> Bool

Expose if the datepicker is open

focusedDate : DatePicker -> Maybe Date

Expose the currently focused date

Settings

type alias Settings =
{ placeholder : String
, classNamespace : String
, containerClassList : List ( String, Bool )
, inputClassList : List ( String, Bool )
, inputName : Maybe String
, inputId : Maybe String
, inputAttributes : List (Attribute Msg)
, isDisabled : Date -> Bool
, parser : String -> Result String Date
, dateFormatter : Date -> String
, dayFormatter : Weekday -> String
, monthFormatter : Month -> String
, yearFormatter : Int -> String
, cellFormatter : String -> Html Msg
, firstDayOfWeek : Weekday
, changeYear : YearRange
}

The type of date picker settings.

defaultSettings : Settings

A record of default settings for the date picker. Extend this if you want to customize your date picker.

import DatePicker exposing (defaultSettings)


DatePicker.init { defaultSettings | placeholder = "Pick a date" }

To disable certain dates:

import Date exposing (Day(..), dayOfWeek)
import DatePicker exposing (defaultSettings)


DatePicker.init { defaultSettings | isDisabled = \d -> dayOfWeek d `List.member` [ Sat, Sun ] }
pick : Date -> Msg

Generate a message that will act as if the user has chosen a certain date, so you can call update on the model yourself. Note that this is different from just changing the "current chosen" date, since the picker doesn't actually have internal state for that. Rather, it will:

  • change the calendar focus

  • replace the input text with the new value

  • close the picker

    update datepickerSettings (pick someDate) datepicker

between : Int -> Int -> YearRange

Select a range of date to display

DatePicker.init { defaultSettings | changeYear = between 1555 2018 }
moreOrLess : Int -> YearRange

Select a symmetric range of date to display

DatePicker.init { defaultSettings | changeYear = moreOrLess 10 }
from : Int -> YearRange

Select a range from a given year to this year

DatePicker.init { defaultSettings | changeYear = from 1995 }
to : Int -> YearRange

Select a range from this year to a given year

DatePicker.init { defaultSettings | changeYear = to 2020 }
off : YearRange

Turn off the date range

DatePicker.init { defaultSettings | changeYear = off }

Generate a message that will act as if the user has focused on the input element. This will open the datePicker

update datepickerSettings open datepicker

Example usage is demonstrated in the simple-nightwash-example.

Generate a message that will act as if the user has removed focus from the input element. This will close the datePicker

update datepickerSettings close datepicker

Example usage is demonstrated in simple-nightwash-example.