The initial model of the date picker. Easy to us in your own init function:
(You probably want to get todays date to give it to the date picker using DatePicker.setToday)
init =
( { date = Nothing
, dateText = ""
, pickerModel = DatePicker.init
}
, Task.perform SetToday Date.today
)Use in your update function:
update msg model =
case msg of
ChangePicker changeEvent ->
case changeEvent of
DateChanged date ->
-- update both date and text
( { model
| date = Just date
, dateText = Date.toIsoString date
}
, Cmd.none
)
TextChanged text ->
( { model
| date =
-- parse the text in any way you like
Date.fromIsoString text
|> Result.toMaybe
|> Maybe.Extra.orElse model.date
, dateText = text
}
, Cmd.none
)
DateCleared ->
( { model
| date =
Nothing
, dateText = ""
}
, Cmd.none
)
PickerChanged subMsg ->
-- internal stuff changed
-- call DatePicker.update
( { model
| pickerModel =
model.pickerModel
|> DatePicker.update subMsg
}
, Cmd.none
)Reasonable default settings.
The initial model of the date picker and sets the given date as today.
For when you want to be more in control
Closes the date picker.
Example: close date picker on date input:
DateChanged date ->
( { model
| date = Just date
, dateText = Date.toIsoString date
, pickerModel =
model.pickerModel
|> DatePicker.close
}
, Cmd.none
)Note: the date picker will reopen on focus and click. To prevent this, close the date picker on every update:
PickerChanged subMsg ->
( { model
| pickerModel =
model.pickerModel
|> DatePicker.update subMsg
--picker will never open
|> DatePicker.close
}
, Cmd.none
)Opens the date picker
Example: start with open picker:
init : ( Model, Cmd Msg )
init =
( { date = Nothing
, dateText = ""
, pickerModel =
DatePicker.init
|> DatePicker.open
}
, Task.perform SetToday Date.today
)Sets the current visible month of the date picker.
Use it like you would
Input.text, the attributes,text,placeholderandlabelwill behave exactly like forInput.text. It has however a more complexonChange, aselecteddate, the internalmodeland somesettings.Note:
Events.onClick,Events.onFocusandEvents.onLoseFocusare used internally by the date picker. This means, that your ownEvents.onClick,Events.onFocusandEvents.onLoseFocusattributes have no effect and will not fire.