Convert date and datetime to handle maybe posix instead of string

This commit is contained in:
Lindsay Wardell 2023-02-03 09:23:07 -08:00
parent f2123dc943
commit 5c7b9eefcf
4 changed files with 55 additions and 43 deletions

View File

@ -96,15 +96,18 @@
"elm/core": "1.0.1 <= v < 2.0.0", "elm/core": "1.0.1 <= v < 2.0.0",
"elm/http": "2.0.0 <= v < 3.0.0", "elm/http": "2.0.0 <= v < 3.0.0",
"elm/json": "1.1.3 <= v < 2.0.0", "elm/json": "1.1.3 <= v < 2.0.0",
"elm/parser": "1.1.0 <= v < 2.0.0",
"elm/random": "1.0.0 <= v < 2.0.0", "elm/random": "1.0.0 <= v < 2.0.0",
"elm/regex": "1.0.0 <= v < 2.0.0", "elm/regex": "1.0.0 <= v < 2.0.0",
"elm/svg": "1.0.1 <= v < 2.0.0", "elm/svg": "1.0.1 <= v < 2.0.0",
"elm/time": "1.0.0 <= v < 2.0.0",
"elm-community/dict-extra": "2.4.0 <= v < 3.0.0", "elm-community/dict-extra": "2.4.0 <= v < 3.0.0",
"elm-community/list-extra": "8.6.0 <= v < 9.0.0", "elm-community/list-extra": "8.6.0 <= v < 9.0.0",
"elm-community/random-extra": "3.2.0 <= v < 4.0.0", "elm-community/random-extra": "3.2.0 <= v < 4.0.0",
"elm-community/string-extra": "4.0.1 <= v < 5.0.0", "elm-community/string-extra": "4.0.1 <= v < 5.0.0",
"pablohirafuji/elm-markdown": "2.0.5 <= v < 3.0.0", "pablohirafuji/elm-markdown": "2.0.5 <= v < 3.0.0",
"rtfeldman/elm-css": "17.0.1 <= v < 19.0.0", "rtfeldman/elm-css": "17.0.1 <= v < 19.0.0",
"rtfeldman/elm-iso8601-date-strings": "1.1.4 <= v < 2.0.0",
"rtfeldman/elm-sorter-experiment": "2.1.1 <= v < 3.0.0", "rtfeldman/elm-sorter-experiment": "2.1.1 <= v < 3.0.0",
"tesk9/accessible-html-with-css": "4.1.0 <= v < 6.0.0", "tesk9/accessible-html-with-css": "4.1.0 <= v < 6.0.0",
"tesk9/palette": "3.0.1 <= v < 4.0.0" "tesk9/palette": "3.0.1 <= v < 4.0.0"

View File

@ -1,6 +1,6 @@
module Nri.Ui.TextInput.V7 exposing module Nri.Ui.TextInput.V7 exposing
( view, generateId ( view, generateId
, number, float, text, newPassword, currentPassword, email, search, addressLevel2, addressLine1, countryName, familyName, givenName, username, organization, organizationTitle, postalCode, sex, tel, date, time, datetime , number, float, text, newPassword, currentPassword, email, search, addressLevel2, addressLine1, countryName, familyName, givenName, username, organization, organizationTitle, postalCode, sex, tel, date, datetime
, readOnlyText , readOnlyText
, value, map , value, map
, onFocus, onBlur, onEnter , onFocus, onBlur, onEnter
@ -32,7 +32,7 @@ module Nri.Ui.TextInput.V7 exposing
### Input types ### Input types
@docs number, float, text, newPassword, currentPassword, email, search, addressLevel2, addressLine1, countryName, familyName, givenName, username, organization, organizationTitle, postalCode, sex, tel, date, time, datetime @docs number, float, text, newPassword, currentPassword, email, search, addressLevel2, addressLine1, countryName, familyName, givenName, username, organization, organizationTitle, postalCode, sex, tel, date, datetime
@docs readOnlyText @docs readOnlyText
@ -63,6 +63,7 @@ import Html.Styled.Attributes as Attributes exposing (..)
import Html.Styled.Events as Events import Html.Styled.Events as Events
import InputErrorAndGuidanceInternal exposing (ErrorState, Guidance) import InputErrorAndGuidanceInternal exposing (ErrorState, Guidance)
import InputLabelInternal import InputLabelInternal
import Iso8601
import Keyboard.Event import Keyboard.Event
import Nri.Ui.ClickableSvg.V2 as ClickableSvg import Nri.Ui.ClickableSvg.V2 as ClickableSvg
import Nri.Ui.ClickableText.V3 as ClickableText import Nri.Ui.ClickableText.V3 as ClickableText
@ -72,6 +73,8 @@ import Nri.Ui.InputStyles.V4 as InputStyles exposing (defaultMarginTop)
import Nri.Ui.Svg.V1 as Svg import Nri.Ui.Svg.V1 as Svg
import Nri.Ui.UiIcon.V1 as UiIcon import Nri.Ui.UiIcon.V1 as UiIcon
import Nri.Ui.Util exposing (dashify) import Nri.Ui.Util exposing (dashify)
import Parser
import Time
{-| An input that allows text entry {-| An input that allows text entry
@ -485,13 +488,13 @@ sex onInput_ =
{-| An input that allows date entry {-| An input that allows date entry
-} -}
date : (String -> msg) -> Attribute String msg date : (Maybe Time.Posix -> msg) -> Attribute (Maybe Time.Posix) msg
date onInput_ = date onInput_ =
Attribute Attribute
{ emptyEventsAndValues { emptyEventsAndValues
| toString = Just identity | toString = Just (Maybe.map (Iso8601.fromTime >> String.slice 0 10) >> Maybe.withDefault "")
, fromString = Just identity , fromString = Just (Iso8601.toTime >> Result.toMaybe)
, onInput = Just (identity >> onInput_) , onInput = Just (Iso8601.toTime >> Result.toMaybe >> onInput_)
} }
(\config -> (\config ->
{ config { config
@ -502,34 +505,15 @@ date onInput_ =
) )
{-| An input that allows time entry
-}
time : (String -> msg) -> Attribute String msg
time onInput_ =
Attribute
{ emptyEventsAndValues
| toString = Just identity
, fromString = Just identity
, onInput = Just (identity >> onInput_)
}
(\config ->
{ config
| fieldType = Just "time"
, inputMode = Nothing
, autocomplete = Nothing
}
)
{-| An input that allows datetime entry {-| An input that allows datetime entry
-} -}
datetime : (String -> msg) -> Attribute String msg datetime : (Maybe Time.Posix -> msg) -> Attribute (Maybe Time.Posix) msg
datetime onInput_ = datetime onInput_ =
Attribute Attribute
{ emptyEventsAndValues { emptyEventsAndValues
| toString = Just identity | toString = Just (Maybe.map (Iso8601.fromTime >> String.dropRight 1) >> Maybe.withDefault "")
, fromString = Just identity , fromString = Just (Iso8601.toTime >> Result.toMaybe)
, onInput = Just (identity >> onInput_) , onInput = Just (Iso8601.toTime >> Result.toMaybe >> onInput_)
} }
(\config -> (\config ->
{ config { config

View File

@ -21,6 +21,9 @@ import Nri.Ui.Colors.V1 as Colors
import Nri.Ui.Heading.V3 as Heading import Nri.Ui.Heading.V3 as Heading
import Nri.Ui.TextInput.V7 as TextInput import Nri.Ui.TextInput.V7 as TextInput
import ViewHelpers exposing (viewExamples) import ViewHelpers exposing (viewExamples)
import Time
import Iso8601
import Parser
moduleName : String moduleName : String
@ -68,6 +71,7 @@ example =
] ]
) )
:: customizableExamples state :: customizableExamples state
in in
[ ControlView.view [ ControlView.view
{ ellieLinkConfig = ellieLinkConfig { ellieLinkConfig = ellieLinkConfig
@ -335,7 +339,14 @@ customizableExamples state =
} }
, toExample , toExample
{ name = "date" { name = "date"
, toString = identity , toString = (\result ->
case result of
Just date ->
date |> Iso8601.fromTime >> String.slice 0 10
Nothing ->
""
)
, inputType = TextInput.date , inputType = TextInput.date
, inputTypeCode = "TextInput.date" , inputTypeCode = "TextInput.date"
, inputTypeValueCode = \value -> Code.string (Maybe.withDefault "" value) , inputTypeValueCode = \value -> Code.string (Maybe.withDefault "" value)
@ -343,19 +354,16 @@ customizableExamples state =
, onBlur = "onBlur" , onBlur = "onBlur"
, onEnter = "onEnter" , onEnter = "onEnter"
} }
, toExample
{ name = "time"
, toString = identity
, inputType = TextInput.time
, inputTypeCode = "TextInput.time"
, inputTypeValueCode = \value -> Code.string (Maybe.withDefault "" value)
, onFocus = "onFocus"
, onBlur = "onBlur"
, onEnter = "onEnter"
}
, toExample , toExample
{ name = "datetime" { name = "datetime"
, toString = identity , toString = (\result ->
case result of
Just date ->
date |> Iso8601.fromTime >> String.dropRight 1
Nothing ->
""
)
, inputType = TextInput.datetime , inputType = TextInput.datetime
, inputTypeCode = "TextInput.datetime" , inputTypeCode = "TextInput.datetime"
, inputTypeValueCode = \value -> Code.string (Maybe.withDefault "" value) , inputTypeValueCode = \value -> Code.string (Maybe.withDefault "" value)
@ -369,6 +377,7 @@ customizableExamples state =
{-| -} {-| -}
type alias State = type alias State =
{ inputValues : Dict Int String { inputValues : Dict Int String
, date : Maybe Time.Posix
, showPassword : Bool , showPassword : Bool
, control : Control ExampleConfig , control : Control ExampleConfig
} }
@ -378,6 +387,7 @@ type alias State =
init : State init : State
init = init =
{ inputValues = Dict.empty { inputValues = Dict.empty
, date = Nothing
, showPassword = False , showPassword = False
, control = initControl , control = initControl
} }
@ -439,6 +449,7 @@ controlAttributes =
{-| -} {-| -}
type Msg type Msg
= SetInput Int String = SetInput Int String
| SetDate (Result (List Parser.DeadEnd) Time.Posix)
| SetShowPassword Bool | SetShowPassword Bool
| UpdateControl (Control ExampleConfig) | UpdateControl (Control ExampleConfig)
@ -461,3 +472,16 @@ update msg state =
( { state | control = newControl } ( { state | control = newControl }
, Cmd.none , Cmd.none
) )
SetDate result ->
let
_ = Debug.log "SetDate" result
in
case result of
Ok date ->
( { state | date = Just date }
, Cmd.none
)
Err _ ->
( state, Cmd.none )

View File

@ -19,6 +19,7 @@
"elm/random": "1.0.0", "elm/random": "1.0.0",
"elm/regex": "1.0.0", "elm/regex": "1.0.0",
"elm/svg": "1.0.1", "elm/svg": "1.0.1",
"elm/time": "1.0.0",
"elm/url": "1.0.0", "elm/url": "1.0.0",
"elm-community/dict-extra": "2.4.0", "elm-community/dict-extra": "2.4.0",
"elm-community/list-extra": "8.7.0", "elm-community/list-extra": "8.7.0",
@ -27,6 +28,7 @@
"myrho/elm-round": "1.0.5", "myrho/elm-round": "1.0.5",
"pablohirafuji/elm-markdown": "2.0.5", "pablohirafuji/elm-markdown": "2.0.5",
"rtfeldman/elm-css": "17.1.1", "rtfeldman/elm-css": "17.1.1",
"rtfeldman/elm-iso8601-date-strings": "1.1.4",
"rtfeldman/elm-sorter-experiment": "2.1.1", "rtfeldman/elm-sorter-experiment": "2.1.1",
"tesk9/accessible-html-with-css": "4.1.0", "tesk9/accessible-html-with-css": "4.1.0",
"tesk9/palette": "3.0.1", "tesk9/palette": "3.0.1",
@ -36,7 +38,6 @@
"SwiftsNamesake/proper-keyboard": "4.0.0", "SwiftsNamesake/proper-keyboard": "4.0.0",
"elm/bytes": "1.0.8", "elm/bytes": "1.0.8",
"elm/file": "1.0.5", "elm/file": "1.0.5",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.3", "elm/virtual-dom": "1.0.3",
"justinmimbs/date": "4.0.1", "justinmimbs/date": "4.0.1",
"justinmimbs/time-extra": "1.1.1", "justinmimbs/time-extra": "1.1.1",
@ -56,4 +57,4 @@
"mgold/elm-nonempty-list": "4.2.0" "mgold/elm-nonempty-list": "4.2.0"
} }
} }
} }