jon/frontend/NumberInput.elm

30 lines
657 B
Elm

module NumberInput exposing (..)
type alias Model a =
{ value : Maybe a
, original : String
, convert : String -> Maybe a
}
fromFloat : Float -> Model Float
fromFloat x = Model (Just x) (String.fromFloat x) String.toFloat
fromInt : Int -> Model Int
fromInt x = Model (Just x) (String.fromInt x) String.toInt
get : Model a -> Maybe a
get = .value
withDefault : a -> Model a -> a
withDefault d = Maybe.withDefault d << get
isValid : Model a -> Bool
isValid model = model.value /= Nothing
update : String -> Model a -> Model a
update str model =
{ model | value = model.convert str, original = str }
show : Model a -> String
show = .original