stoeffel/editable/1.2.0

NOTE: this package is not compatible with Elm 0.19.1

Editable

Editable represents a value that can be read-only or editable. ReadOnly a holds the locked value and Editable a a holds both the old and the newly modified value.

type Editable a
= Editable a a
| ReadOnly a

An Editable value is either ReadOnly or Editable.

view : Editable String -> Html msg
view editable =
    case editable of
        Editable saved modified ->
            input [ defaultValue modified ] []


        ReadOnly saved ->
            text saved
cancel : Editable a -> Editable a

Cancels a modified value. This puts the old value into the context of ReadOnly.

Editable.Editable "old" "new"
    |> Editable.cancel       --> ReadOnly "old"
edit : Editable a -> Editable a

Makes a ReadOnly value Editable.

Editable.ReadOnly "old"
    |> Editable.edit
    |> Editable.update "new" --> Editable "old" "new"
map : (a -> a) -> Editable a -> Editable a

Apply a function to an Editable.

Editable.ReadOnly "old"
    |> Editable.map String.toUpper --> ReadOnly "old"


Editable.Editable "old" "old"
    |> Editable.map String.toUpper --> Editable "old" "OLD"
save : Editable a -> Editable a

Save a modified value. This puts the modified value into the context of ReadOnly.

Editable.Editable "old" "new"
    |> Editable.save          --> ReadOnly "new"


Editable.ReadOnly "old"
    |> Editable.edit
    |> Editable.update "new"
    |> Editable.save          --> ReadOnly "new"
update : a -> Editable a -> Editable a

Updates an Editable and doesn't change a ReadOnly.

Editable.ReadOnly "old"
    |> Editable.update "new"  --> ReadOnly "old"


Editable.Editable "old" "old"
    |> Editable.update "new"  --> Editable "old" "new"
value : Editable a -> a

Returns the current value of an Editable.

Editable.ReadOnly "old"
    |> Editable.value  --> "old"


Editable.Editable "old" "new"
    |> Editable.value  --> "new"
isDirty : Editable a -> Bool

Determines if a modified value has changed from the saved one, by checking equality of both values.

If the Editable is ReadOnly then we return False.

Editable.Editable "old" "old"
    |> Editable.isDirty  --> False


Editable.Editable "old" "new"
    |> Editable.isDirty  --> True


Editable.ReadOnly "old"
    |> Editable.isDirty  --> False
isDirtyWith : (a -> a -> Bool) -> Editable a -> Bool

Determines if a modified value has changed from the saved one, by a provided function.

If the Editable is ReadOnly then we return False.

Editable.Editable "old" "old"
    |> Editable.isDirtyWith (/=)  --> False


Editable.Editable "old" "new"
    |> Editable.isDirtyWith (/=)  --> True


Editable.ReadOnly "old"
    |> Editable.isDirtyWith (/=)  --> False