-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Simmerl
committed
Jan 16, 2017
1 parent
48364d2
commit b1fd021
Showing
9 changed files
with
225 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
module Formo exposing (..) | ||
|
||
import Dict exposing (Dict) | ||
|
||
|
||
-- MODEL | ||
|
||
|
||
type alias Element = | ||
{ errors : List ValidationError | ||
, focused : Bool | ||
, validators : List ElementValidator | ||
, value : String | ||
} | ||
|
||
|
||
type alias Elements = | ||
Dict String Element | ||
|
||
|
||
type alias ElementValidator = | ||
String -> ValidationError | ||
|
||
|
||
type alias Form = | ||
{ elements : Elements | ||
} | ||
|
||
|
||
type alias ValidationError = | ||
Maybe String | ||
|
||
|
||
initForm : List ( String, List ElementValidator ) -> Form | ||
initForm fields = | ||
let | ||
elements = | ||
List.foldl | ||
(\e -> Dict.insert (Tuple.first e) (initElement e)) | ||
Dict.empty | ||
fields | ||
in | ||
Form elements | ||
|
||
|
||
initElement : ( String, List ElementValidator ) -> Element | ||
initElement ( _, validators ) = | ||
Element [] False validators "" | ||
|
||
|
||
blurElement : Form -> String -> Form | ||
blurElement form name = | ||
case Dict.get name form.elements of | ||
Nothing -> | ||
form | ||
|
||
Just element -> | ||
let | ||
elements = | ||
Dict.insert | ||
name | ||
{ element | focused = False } | ||
form.elements | ||
in | ||
{ form | elements = elements } | ||
|
||
elementError : Form -> String -> String | ||
elementError form name = | ||
"" | ||
|
||
elementValue : Form -> String -> String | ||
elementValue form name = | ||
case Dict.get name form.elements of | ||
Nothing -> | ||
"" | ||
|
||
Just element -> | ||
element.value | ||
|
||
focusElement : Form -> String -> Form | ||
focusElement form name = | ||
case Dict.get name form.elements of | ||
Nothing -> | ||
form | ||
|
||
Just element -> | ||
let | ||
elements = | ||
Dict.insert | ||
name | ||
{ element | focused = True } | ||
form.elements | ||
in | ||
{ form | elements = elements } | ||
|
||
updateElementValue : Form -> String -> String -> Form | ||
updateElementValue form name value = | ||
case Dict.get name form.elements of | ||
Nothing -> | ||
form | ||
|
||
Just element -> | ||
let | ||
elements = | ||
Dict.insert | ||
name | ||
{ element | value = value } | ||
form.elements | ||
in | ||
{ form | elements = elements } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.