CurrySoftware/elm-datepicker/1.0.0

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.

= NoChange
| Changed (Maybe Date)

A sugaring of Maybe to explicitly tell you how to interpret Changed Nothing, because Just Nothing seems somehow wrong. Used to represent a request, by the datepicker, to change the selected date.

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 } ! []
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 } ! []
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 : Maybe 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 (Just 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 }