Allow to search with wildcard in a number custom field

This commit is contained in:
Eike Kettner 2020-11-23 13:44:49 +01:00
parent 470471dff6
commit 29a5419072
3 changed files with 91 additions and 38 deletions

View File

@ -6,6 +6,7 @@ module Comp.CustomFieldInput exposing
, init , init
, initWith , initWith
, update , update
, updateSearch
, view , view
) )
@ -137,7 +138,7 @@ initWith value =
Data.CustomFieldType.Numeric -> Data.CustomFieldType.Numeric ->
let let
( fm, _ ) = ( fm, _ ) =
updateFloatModel value.value string2Float identity updateFloatModel False value.value string2Float identity
in in
NumberField fm NumberField fm
@ -145,6 +146,7 @@ initWith value =
let let
( fm, _ ) = ( fm, _ ) =
updateFloatModel updateFloatModel
False
value.value value.value
Data.Money.fromString Data.Money.fromString
Data.Money.normalizeInput Data.Money.normalizeInput
@ -170,6 +172,10 @@ initWith value =
) )
--- Update
type FieldResult type FieldResult
= NoResult = NoResult
| RemoveField | RemoveField
@ -183,40 +189,18 @@ type alias UpdateResult =
} }
updateFloatModel :
String
-> (String -> Result String Float)
-> (String -> String)
-> ( FloatModel, FieldResult )
updateFloatModel msg parse normalize =
case parse msg of
Ok n ->
( { input = normalize msg
, result = Ok n
}
, Value (normalize msg)
)
Err err ->
( { input = msg
, result = Err err
}
, NoResult
)
string2Float : String -> Result String Float
string2Float str =
case String.toFloat str of
Just n ->
Ok n
Nothing ->
Err ("Not a number: " ++ str)
update : Msg -> Model -> UpdateResult update : Msg -> Model -> UpdateResult
update msg model = update =
update1 False
updateSearch : Msg -> Model -> UpdateResult
updateSearch =
update1 True
update1 : Bool -> Msg -> Model -> UpdateResult
update1 forSearch msg model =
case ( msg, model.fieldModel ) of case ( msg, model.fieldModel ) of
( SetText str, TextField _ ) -> ( SetText str, TextField _ ) ->
let let
@ -231,7 +215,7 @@ update msg model =
( NumberMsg str, NumberField _ ) -> ( NumberMsg str, NumberField _ ) ->
let let
( fm, res ) = ( fm, res ) =
updateFloatModel str string2Float identity updateFloatModel forSearch str string2Float identity
model_ = model_ =
{ model | fieldModel = NumberField fm } { model | fieldModel = NumberField fm }
@ -242,6 +226,7 @@ update msg model =
let let
( fm, res ) = ( fm, res ) =
updateFloatModel updateFloatModel
forSearch
str str
Data.Money.fromString Data.Money.fromString
Data.Money.normalizeInput Data.Money.normalizeInput
@ -297,6 +282,45 @@ update msg model =
UpdateResult model Cmd.none NoResult UpdateResult model Cmd.none NoResult
updateFloatModel :
Bool
-> String
-> (String -> Result String Float)
-> (String -> String)
-> ( FloatModel, FieldResult )
updateFloatModel forSearch msg parse normalize =
let
hasWildCards =
String.startsWith "*" msg || String.endsWith "*" msg
in
if forSearch && hasWildCards then
( { input = normalize msg
, result = Ok 0
}
, Value (normalize msg)
)
else
case parse msg of
Ok n ->
( { input = normalize msg
, result = Ok n
}
, Value (normalize msg)
)
Err err ->
( { input = msg
, result = Err err
}
, NoResult
)
--- View
mkLabel : Model -> String mkLabel : Model -> String
mkLabel model = mkLabel model =
Maybe.withDefault model.field.name model.field.label Maybe.withDefault model.field.name model.field.label
@ -408,3 +432,17 @@ makeInput icon model =
, removeButton "" , removeButton ""
, i [ class (iconOr <| Icons.customFieldType Data.CustomFieldType.Date) ] [] , i [ class (iconOr <| Icons.customFieldType Data.CustomFieldType.Date) ] []
] ]
--- Helper
string2Float : String -> Result String Float
string2Float str =
case String.toFloat str of
Just n ->
Ok n
Nothing ->
Err ("Not a number: " ++ str)

View File

@ -11,6 +11,7 @@ module Comp.CustomFieldMultiInput exposing
, reset , reset
, setValues , setValues
, update , update
, updateSearch
, view , view
) )
@ -148,7 +149,17 @@ mkItem f =
update : Msg -> Model -> UpdateResult update : Msg -> Model -> UpdateResult
update msg model = update =
update1 False
updateSearch : Msg -> Model -> UpdateResult
updateSearch =
update1 True
update1 : Bool -> Msg -> Model -> UpdateResult
update1 forSearch msg model =
case msg of case msg of
CreateNewField -> CreateNewField ->
UpdateResult model Cmd.none FieldCreateNew UpdateResult model Cmd.none FieldCreateNew
@ -241,7 +252,11 @@ update msg model =
Just { field, inputModel } -> Just { field, inputModel } ->
let let
res = res =
Comp.CustomFieldInput.update lm inputModel if forSearch then
Comp.CustomFieldInput.updateSearch lm inputModel
else
Comp.CustomFieldInput.update lm inputModel
model_ = model_ =
{ model { model

View File

@ -710,7 +710,7 @@ updateDrop ddm flags settings msg model =
CustomFieldMsg lm -> CustomFieldMsg lm ->
let let
res = res =
Comp.CustomFieldMultiInput.update lm model.customFieldModel Comp.CustomFieldMultiInput.updateSearch lm model.customFieldModel
in in
{ model = { model =
{ model { model