MudBaseInput: ParameterState for Value#12268
Conversation
- Move Text-from-Value update logic from SetParametersAsync to OnValueParameterChangedAsync - Add _textParameterChanged flag to prevent Text update when both Text and Value change - Manually invoke ValueChanged callback when ForceUpdate is called with unchanged value - Fixes TextField_TextUpdateSuppression and Select_ValueChangeEventCountTest Co-authored-by: ScarletKuro <[email protected]>
…d values - Add call to _textState.SetValueAsync when color parsing fails - This attempts to reset the TextField to show the valid color - 2 of 3 tests now passing (TextField_TextUpdateSuppression, Select_ValueChangeEventCountTest) - SetColorInput_InvalidNoChange still fails due to ParameterState synchronization issue Co-authored-by: ScarletKuro <[email protected]>
- Add _inputResetKey counter to force TextField recreation on invalid input - Increment key when color parsing fails in SetInputStringAsync - Add @key directive to HEX MudTextField to bind to _inputResetKey - This forces Blazor to recreate TextField, resetting its display to valid color - All 3 tests now passing: TextField_TextUpdateSuppression, Select_ValueChangeEventCountTest, SetColorInput_InvalidNoChange Co-authored-by: ScarletKuro <[email protected]>
de4522b to
67b975a
Compare
|
@copilot I added ParameterView to ParameterChangedEventArgs, not sure if that somehow helps to get rid of |
- Remove the second reset at end of SetParametersAsync - Flag should only be reset before processing parameters, not after - The second reset was undoing the flag set by OnTextParameterChangedAsync - Add StateHasChanged after UpdateTextPropertyAsync in OnValueParameterChangedAsync to ensure child components receive updated Text parameter Co-authored-by: ScarletKuro <[email protected]>
…pdate logic - Restore text-from-value update in SetParametersAsync for unchanged Value parameters - ParameterState only fires OnValueParameterChangedAsync when value CHANGES - But we need to update Text even when Value is passed unchanged (for formatting) - Keep the hasValue && !hasText check in SetParametersAsync to handle this case - Fixes NumericFieldTestCultureFormat test failure Co-authored-by: ScarletKuro <[email protected]>
- Check ParameterView.TryGetValue in OnTextParameterChangedAsync to see if Value is also present - Check ParameterView.TryGetValue in OnValueParameterChangedAsync to see if Text is also present - Eliminates need for _textParameterChanged flag and its reset logic - Cleaner approach using ParameterView snapshot provided by ParameterState - Addresses concurrency concerns mentioned in code review Co-authored-by: ScarletKuro <[email protected]>
Implemented the ParameterView approach in commit 5572c3b: Changes:
Benefits:
The NumericFieldTestCultureFormat issue was also fixed in commit eada7fd by restoring the SetParametersAsync logic for unchanged Value parameters (needed for formatting). |
Migrates
MudBaseInput.Valueto use ParameterState pattern, consistent with previous Text migration (#12259).Changes
MudBaseInput
_valueStatewith change handlerOnValueParameterChangedAsyncSetParametersAsynctoOnValueParameterChangedAsyncto properly handle value-to-text synchronization with focus/suppression checksParameterView.TryGetValue()in change handlers to detect when both Text and Value are present in the same parameter update (maintainshasValue && !hasTextsemantics)hasValue && !hasTextlogic inSetParametersAsyncto handle cases where Value parameter is passed unchanged (needed for text formatting)ValueChangedcallback when value hasn't changed butforce: true(ParameterState doesn't fire callbacks for unchanged values)_valueState.Valueand_valueState.SetValueAsync()instead of backing fieldMudColorPicker Text/Value Synchronization Refactoring
@GetColorTextValue(_valueState.Value)to_textState.Valueto enable Blazor parameter change detectionOnColorPickerModeChangedAsyncto update Text when display mode changes (RGB → HSL → HEX)OnValueChangeHandlerAsync_textState.SetValueAsync()is called to reset Text, which triggers TextField to receive updated parameter and reset its display_textState, we force Blazor to pass the Text parameter to TextField, enabling proper reset on invalid inputTest Results
TextField_TextUpdateSuppression- FixedSelect_ValueChangeEventCountTest- FixedSetColorInput_InvalidNoChange- FixedNumericFieldTestCultureFormat- Fixed (regression introduced and resolved)Architecture Improvements
ParameterView-Based Conflict Detection
Eliminated the
_textParameterChangedflag by usingParameterView.TryGetValue()within change handlers. EachParameterChangedEventArgsnow includes a snapshot of theParameterView, allowing handlers to check if other related parameters are also present in the same update. This approach is thread-safe by design and eliminates the need for manual flag management or atomic operations.Two-Way Binding Edge Case
The refactoring addresses two critical edge cases in two-way binding scenarios:
_textState.Valuebinding and keeping Text synchronized with Value through ParameterState change handlers, TextField now properly receives parameter updates and resets its display.SetParametersAsynclogic ensures Text is updated from Value even when Value parameter is present but unchanged, enabling proper formatting.Checklist:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.