Coder Social home page Coder Social logo

isabella232 / elm-datepicker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cultureamp/elm-datepicker

0.0 0.0 0.0 132 KB

A reusable date picker component in Elm.

Home Page: http://package.elm-lang.org/packages/elm-community/elm-datepicker/latest

License: BSD 3-Clause "New" or "Revised" License

CSS 14.46% Elm 85.54%

elm-datepicker's Introduction

elm-datepicker

elm package install elm-community/elm-datepicker

A reusable date picker component in Elm.

Usage

The DatePicker.init function initialises the DatePicker. It returns the initialised DatePicker and associated Cmds so it must be done in your program's init or update functions:

Note Make sure you don't throw away the initial Cmd!

   
init : (Model, Cmd Msg)
...
    let
        ( datePicker, datePickerCmd ) =
            DatePicker.init 
    in
        (
            { model | datePicker = datePicker },
            Cmd.map SetDatePicker datePickerCmd
        )

The DatePicker can be displayed in a view using the DatePicker.view function. It returns its own message type so you should wrap it in one of your own messages using Html.map:

type Msg
    = ...
    | SetDatePicker DatePicker.Msg
    | ...


view : Model -> Html Msg
view model =
    ...
    div [] [
        DatePicker.view
            model.date 
            someSettings
            model.startDatePicker 
         |> Html.map SetDatePicker
        ]

To handle Msg in your update function, you should unwrap the DatePicker.Msg and pass it down to the DatePicker.update function. The DatePicker.update function returns:

  • the new model
  • any command
  • the new date as a DateEvent (Maybe Date), where DateEvent is really just Maybe with different semantics, to avoid a potentially confusing Maybe Maybe.

To create the settings to pass to update, DatePicker.defaultSettings` is provided to make it easier to use. You only have to override the settings that you are interested in.

Note The datepicker does not retain an internal idea of a picked date in its model. That is, it depends completely on you for an idea of what date is chosen, so that third tuple member is important! Evan Czaplicki has a compelling argument for why components should not necessarily have an their own state for the primary data they manage here.

someSettings : DatePicker.Settings
someSettings = 
    { defaultSettings
        | inputClassList = [ ( "form-control", True ) ]
        , inputId = Just "datepicker"
    }

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        ...

         SetDatePicker msg ->
            let
                ( newDatePicker, datePickerCmd, dateEvent ) =
                    DatePicker.update someSettings msg model.startDatePicker

                date =
                    case dateEvent of
                        NoChange ->
                            model.date

                        Changed newDate ->
                            newDate |> processDate
            in
                { model
                    | date = date
                    , datePicker = newDatePicker
                }
                    ! [ Cmd.map SetDatePicker datePickerCmd ]

Examples

See the examples folder or try it on ellie-app: simple example and bootstrap example.

CSS

The CSS for the date picker is distributed separately. You can grab the compiled CSS from here or you can grab the SCSS source from here.

elm-datepicker's People

Contributors

augustin82 avatar bardt avatar bbqbaron avatar bogdanp avatar chalmagean avatar julianjelfs avatar lazamar avatar marcosccm avatar michaeljones avatar pdamoc avatar roine avatar tjmw avatar zkessin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.