pmiddend/elm-number-spinner/1.0.0

NumberSpinner.NumberSpinner

Provide a component that lets you manipulate decimal numbers digit-wise.

Elm Architecture stuff

type alias Model msg =
{ number : DigitalNumber
, cursorPosition : CursorPosition
, hasFocus : Bool
, toMsg : Msg -> msg
}

Elm Architecture model structure

type Msg

Elm Architecture Msg type

Int
-> DecimalNumber
-> DecimalNumber
-> DecimalNumber
-> (Msg -> msg)
-> Model msg

Initialize the number spinner with the number of decimal places, the minimum bound, maximum bound, current value and a converter function into your message type. Example

NumberSpinner.init
    -- two odecimal places
    2
    -- minimum value (inclusive)
    (Decimal.fromInt 0)
    -- maximum value (inclusive)
    (Decimal.fromInt 10)
    -- current value (inited from a string, which might fail because it's at runtime)
    (Maybe.withDefault (Decimal.fromInt 0) <| Decimal.fromString "1.23")
    -- convert to my custom SpinnerMessage
    SpinnerMessage
update : Msg -> Model msg -> ( Model msg, Cmd msg )

Elm Architecture update function

view : Model msg -> Html msg

Elm architecture view function

isMutatingMsg : Msg -> Bool

Check if the incoming Msg actually mutates the decimal number value, or if it is just moving the cursor around, for example

Modify

Modify cursor position to move to the left, if possible (skipping "boundaries" between sign, integral and decimal places)

This is exposed mainly for the tests.

Modify cursor position to move to the right, if possible (skipping "boundaries" between sign, integral and decimal places)

This is exposed mainly for the tests.

setValue : Model msg -> DecimalNumber -> Model msg

Set current "digital number" value of the spinner

Deconstruct

getDecimalValue : Model msg -> DecimalNumber

Get current decimal value of the spinner

valueAsFloat : Model msg -> Float

Convert the decimal number to the approximate floating point value

Internal types exposed for testing

= OnSign SpinnerBounds
| OnInteger SpinnerBounds Int
| OnDecimal SpinnerBounds Int

A type signifying where the cursor currently is, and where it can move.

type alias SpinnerBounds =
{ boundsDecimals : Int
, boundsIntegers : Int
, hasSign : Bool
}

We use this class for the "cursor left" and "cursor right" motions in conjunction with the CursorPosition type (see below). We want to define cursor movement without regards to the current number displayed. For movement, we need the left and right bounds, which we store here.