Skip to content

Fix TextInput value preservation when parent re-renders without ValueChanged callback#59

Merged
LittleLittleCloud merged 5 commits intomainfrom
copilot/fix-textinput-two-way-binding
Nov 10, 2025
Merged

Fix TextInput value preservation when parent re-renders without ValueChanged callback#59
LittleLittleCloud merged 5 commits intomainfrom
copilot/fix-textinput-two-way-binding

Conversation

Copy link
Contributor

Copilot AI commented Oct 24, 2025

Fix TextInput two-way binding issue

  • Explore repository structure and understand the issue
  • Identify the root cause in OnParametersSet method
  • Implement fix by tracking parent value changes separately
  • Verify fix with existing tests (all 88 tests pass)
  • Run all tests to ensure no regressions
  • Format code according to project standards
  • Remove unused _lastSetValue field per code review feedback

Summary

Fixed the TextInput component to properly preserve user input when the parent component re-renders without updating the Value parameter. This resolves the issue where user input was being reset during UI refreshes.

Root Cause

The OnParametersSet() method was comparing the incoming Value parameter against _lastSetValue, which gets updated by both:

  1. User input (via HandleInputAsync)
  2. Parent parameter updates (via OnParametersSet)

This caused user input to be incorrectly reset when the component re-rendered with the same parent value.

Solution

Introduced _lastIncomingValue field to track values received from the parent via the Value parameter. Now OnParametersSet() only updates the internal value when the Value parameter actually changes from the parent, preserving user input across re-renders.

The original _lastSetValue field was removed as it became redundant after the fix.

Changes Made

  • File: src/RazorConsole.Core/Components/TextInput.razor
    • Added _lastIncomingValue field to track incoming parent values
    • Removed unused _lastSetValue field (4 assignments removed)
    • Updated OnParametersSet() to compare against _lastIncomingValue
    • Added comment explaining the fix

Testing

✅ All 88 existing tests pass on both .NET 8.0 and .NET 9.0
✅ Code formatted according to project standards
✅ Backward compatible with components using @bind-Value or explicit ValueChanged callbacks

How It Works Now

Scenario 1: Two-way binding with @bind-Value

  • Works as before - parent value stays in sync with user input

Scenario 2: One-way binding with just Value (the bug scenario)

  • User input is now preserved across re-renders
  • Parent can still update the value by changing the Value parameter

Scenario 3: Parent updates value

  • Component correctly updates to show the new parent value
Original prompt

This section details on the original issue you should resolve

<issue_title>Value property of TextInput is not two-way binding</issue_title>
<issue_description>When creating the TextInput component, setting the Value property does not create two-way binding as described in components documentation. The input is set to the default property value when the UI is refreshed. Workaround is to use @bind-Value to make the two-way binding. </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Oct 24, 2025
…tely

Co-authored-by: LittleLittleCloud <16876986+LittleLittleCloud@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix two-way binding for TextInput component Fix TextInput value preservation when parent re-renders without ValueChanged callback Oct 24, 2025
@TeseySTD
Copy link
Member

TeseySTD commented Nov 9, 2025

@copilot Field _lastSetValue is no longer used after introducing _lastIncomingValue . Consider either removing it along with assignments in HandleInputAsync() and HandleSubmitAsync() , or clarifying its purpose if it's intended for future use

Co-authored-by: TeseySTD <150385054+TeseySTD@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 9, 2025

@copilot Field _lastSetValue is no longer used after introducing _lastIncomingValue . Consider either removing it along with assignments in HandleInputAsync() and HandleSubmitAsync() , or clar...

Removed the _lastSetValue field and all its assignments in commit bad1467. The field was indeed redundant after introducing _lastIncomingValue.

Copilot AI requested a review from TeseySTD November 9, 2025 14:59
@TeseySTD TeseySTD marked this pull request as ready for review November 9, 2025 16:15
@LittleLittleCloud LittleLittleCloud enabled auto-merge (squash) November 10, 2025 07:48
@LittleLittleCloud LittleLittleCloud merged commit 6f995d0 into main Nov 10, 2025
5 checks passed
@LittleLittleCloud LittleLittleCloud deleted the copilot/fix-textinput-two-way-binding branch November 10, 2025 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Value property of TextInput is not two-way binding

3 participants