controlled input for onBlur #129
Replies: 6 comments
-
Hi, the browser itself does not update the Do you simply want to read the input element's input(
onBlur.mapToValue --> ...
) And you can still add |
Beta Was this translation helpful? Give feedback.
-
Thanks for your prompt response. Let me demonstrate my use case with a sample code. val valueVar = Var("")
input(
tpe := "text",
value <-- valueVar.signal,
onBlur.mapToValue --> Observer[String] { value =>
// To make it simple, I just replace all blank spaces with empty string
valueVar.set(value.replaceAll(" ", ""))
}
) Here is what happen:
I guess it is the correct behavior because the Is there any way to handle that case properly? |
Beta Was this translation helpful? Give feedback.
-
Oh, I think I still need to have input(
tpe := "text",
value <-- valueVar.signal,
onInput.mapToValue --> Observer[String] { v =>
valueVar.set(v)
},
onBlur.mapToValue --> Observer[String] { value =>
valueVar.set(value.replaceAll(" ", ""))
}
) This approach fixes the issue. |
Beta Was this translation helpful? Give feedback.
-
Ah sorry I've been working on the next version for too long, where vars and signals don't deduplicate updates, and this isn't a problem. Indeed, your last approach will work fine. A couple unrelated syntax tips:
|
Beta Was this translation helpful? Give feedback.
-
One other way you can update an input's value is to use input(
tpe := "text",
onBlur.mapToValue.map(_.replaceAll(" ", ""))).setAsValue --> Observer.empty // noop observer
) Or if you need the Var for other purposes: input(
tpe := "text",
// value <-- valueVar.signal, // optional – if you never write into the Var except below, it's not needed.
onBlur.mapToValue.map(_.replaceAll(" ", ""))).setAsValue --> valueVar
)
|
Beta Was this translation helpful? Give feedback.
-
Thank you a lot for the useful comments! The more time we have been working with Laminar, the more love we feel. |
Beta Was this translation helpful? Give feedback.
-
The
controlled
modifier currently only works withonInput
handler. However, in our application, the input handler is an expensive task. We would like to useonBlur
such asIs there any workaround to archive it?
Beta Was this translation helpful? Give feedback.
All reactions