From e6e605f630a920cfb2341c758496d4265d447ed6 Mon Sep 17 00:00:00 2001 From: eikek Date: Sat, 4 Mar 2023 23:37:24 +0100 Subject: [PATCH] Map comma to dot for numeric custom fields Closes: #1975 --- .../src/main/elm/Comp/CustomFieldInput.elm | 22 +++++++++++++++---- .../src/main/elm/Comp/ItemSearchInput.elm | 1 + .../src/main/elm/Comp/SimpleTextInput.elm | 3 +++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm b/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm index b7d26faf..bd33d5e7 100644 --- a/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm +++ b/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm @@ -133,6 +133,15 @@ errorMsg texts model = Nothing +textInputNumberConfig : Comp.SimpleTextInput.Config +textInputNumberConfig = + let + cfg = + Comp.SimpleTextInput.defaultConfig + in + { cfg | valueTransform = normalizeStringToNumber } + + init : CustomField -> ( Model, Cmd Msg ) init = init1 Comp.SimpleTextInput.defaultConfig @@ -151,7 +160,7 @@ init1 cfg field = TextField (Comp.SimpleTextInput.init cfg Nothing) Data.CustomFieldType.Numeric -> - NumberField (FloatModel (Comp.SimpleTextInput.init cfg Nothing) (Err NoValue)) + NumberField (FloatModel (Comp.SimpleTextInput.init textInputNumberConfig Nothing) (Err NoValue)) Data.CustomFieldType.Money -> MoneyField (MoneyModel "" (Err NoValue)) @@ -193,7 +202,7 @@ initWith1 cfg value = Data.CustomFieldType.Numeric -> let fm = - Comp.SimpleTextInput.init cfg <| Just value.value + Comp.SimpleTextInput.init textInputNumberConfig <| Just value.value res = string2Float value.value @@ -414,7 +423,7 @@ updateFloatModel forSearch model lm fm parse = ( { input = result.model , result = Ok n } - , Value value + , Value (normalizeStringToNumber value) ) Err err -> @@ -569,9 +578,14 @@ mkLabel model = Maybe.withDefault model.field.name model.field.label +normalizeStringToNumber : String -> String +normalizeStringToNumber = + String.replace "," "." + + string2Float : String -> Result FieldError Float string2Float str = - case String.toFloat str of + case (normalizeStringToNumber >> String.toFloat) str of Just n -> Ok n diff --git a/modules/webapp/src/main/elm/Comp/ItemSearchInput.elm b/modules/webapp/src/main/elm/Comp/ItemSearchInput.elm index 6fe83107..75c60ac4 100644 --- a/modules/webapp/src/main/elm/Comp/ItemSearchInput.elm +++ b/modules/webapp/src/main/elm/Comp/ItemSearchInput.elm @@ -83,6 +83,7 @@ init cfg = , setOnTyping = True , setOnEnter = True , setOnBlur = False + , valueTransform = identity } in { searchModel = Comp.SimpleTextInput.init textCfg Nothing diff --git a/modules/webapp/src/main/elm/Comp/SimpleTextInput.elm b/modules/webapp/src/main/elm/Comp/SimpleTextInput.elm index a830198c..ed701046 100644 --- a/modules/webapp/src/main/elm/Comp/SimpleTextInput.elm +++ b/modules/webapp/src/main/elm/Comp/SimpleTextInput.elm @@ -40,6 +40,7 @@ type alias Config = , setOnTyping : Bool , setOnEnter : Bool , setOnBlur : Bool + , valueTransform : String -> String } @@ -49,6 +50,7 @@ defaultConfig = , setOnTyping = True , setOnEnter = True , setOnBlur = True + , valueTransform = identity } @@ -130,6 +132,7 @@ update msg (Model model) = let maybeStr = Util.Maybe.fromString str + |> Maybe.map model.cfg.valueTransform cmd_ = Task.succeed () |> Task.perform (\_ -> DelayedSet)