From 28170fa62b489994a42cfe00f121b421946c3169 Mon Sep 17 00:00:00 2001 From: capdiem Date: Tue, 17 Dec 2024 09:45:05 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(TextField):=20ensure=20getti?= =?UTF-8?q?ng=20the=20latest=20value=20in=20OnEnter=20event=20(#2285)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/TextField/MTextField.razor.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Masa.Blazor/Components/TextField/MTextField.razor.cs b/src/Masa.Blazor/Components/TextField/MTextField.razor.cs index df62384e36..8b6fc0ecec 100644 --- a/src/Masa.Blazor/Components/TextField/MTextField.razor.cs +++ b/src/Masa.Blazor/Components/TextField/MTextField.razor.cs @@ -734,6 +734,8 @@ public virtual async Task HandleOnKeyDownAsync(KeyboardEventArgs args) { if (args.Key is KeyCodes.Enter or KeyCodes.NumpadEnter) { + await UpdateValueImmediatelyAsync(); + if (OnEnter.HasDelegate) { await OnEnter.InvokeAsync(); @@ -748,6 +750,19 @@ public virtual async Task HandleOnKeyDownAsync(KeyboardEventArgs args) } } + /// + /// Update the bound value immediately. + /// For some scenarios, like OnEnter event and inner icon click event, + /// we need to update the value immediately. + /// + [MasaApiPublicMethod] + public async Task UpdateValueImmediatelyAsync() + { + var originValue = await Js.InvokeAsync(JsInteropConstants.GetProp, InputElement, "value"); + + await HandleOnInputOrChangeEvent(new ChangeEventArgs { Value = originValue }, OnChange, nameof(OnChange)); + } + public virtual async Task HandleOnClearClickAsync(MouseEventArgs args) { UpdateInternalValue(default, InternalValueChangeType.InternalOperation);