Skip to content

[dev-v5] Add a global observeAttributeChange JS method - #3230

Merged
Denis Voituron (dvoituron) merged 3 commits into
dev-v5from
users/dvoituron/dev-v5/attribute-changes
Jan 22, 2025
Merged

Denis Voituron (dvoituron) merged 3 commits into
dev-v5from
users/dvoituron/dev-v5/attribute-changes

Conversation

@dvoituron

Copy link
Copy Markdown
Collaborator

[dev-v5] Add a global observeAttributeChange JS method

Many HTML input components and FluentUI Web Component v3 do not reflect attribute changes (e.g. value) when these are changed by Blazor.

This observeAttributeChange JS function registers an observer on the specified element to detect the change in the corresponding JS object.

Parameters:

  • element The element to observe.
  • attributeName The name of the attribute to observe.
  • propertyType Optional. The type of the property to update (default is string).
  • propertyName Optional. The name of the property to update (default is the attributeName).
  • forceRefresh Optional. If true, all properties will be refreshed when the attribute changes (default is false).
    Returns true if the observer was added, false if the observer was already added.

Example:

const element = document.getElementById('myCheckbox');
observeAttributeChange(element, 'checked', 'boolean')                    // Observe the 'checked' HTML attribute to update the 'checked' JavaScript property.
observeAttributeChange(element, 'indeterminate', 'boolean', '', true)    // Observe the 'indeterminate' HTML attribute to update all registered JavaScript property (forceRefresh=true).

Usage

Using this global function, the FluentTextInput can be simplified to

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        await JSRuntime.InvokeVoidAsync("Microsoft.FluentUI.Blazor.Utilities.Attributes.observeAttributeChange", Element, "value");
    }
}

And the FluentCheckbox can register the observers:

<fluent-checkbox id="chk" checked="@(Checked ? "true" : "false")" indeterminate="@(Indeterminate ? "true" : "false")" @onchange="ChangeHandler" />
protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        await JSRuntime.InvokeVoidAsync("Microsoft.FluentUI.Blazor.Utilities.Attributes.observeAttributeChange", Element, "indeterminate", "boolean", "", true);   // Force to refresh the Checked when Indeterminate is updated.
        await JSRuntime.InvokeVoidAsync("Microsoft.FluentUI.Blazor.Utilities.Attributes.observeAttributeChange", Element, "checked", "boolean");
    }
}

@github-actions

github-actions Bot commented Jan 22, 2025 •

Copy link
Copy Markdown

✅ All tests passed successfully

Details on your Workflow / Core Tests page.

@github-actions

github-actions Bot commented Jan 22, 2025 •

Copy link
Copy Markdown

Summary - Unit Tests Code Coverage

Summary
Generated on: 01/22/2025 - 17:07:51
Coverage date: 01/22/2025 - 17:07:40
Parser: Cobertura
Assemblies: 1
Classes: 58
Files: 68
Line coverage: 98.1% (1142 of 1164)
Covered lines: 1142
Uncovered lines: 22
Coverable lines: 1164
Total lines: 5337
Branch coverage: 91.4% (727 of 795)
Covered branches: 727
Total branches: 795
Method coverage: Feature is only available for sponsors
Tag: 2456_12913344698

Coverage

Microsoft.FluentUI.AspNetCore.Components - 98.1%
Name Line Branch
Microsoft.FluentUI.AspNetCore.Components 98.1% 91.4%
Microsoft.FluentUI.AspNetCore.Components.Dialog.MessageBox.FluentMessageBox 100% 100%
Microsoft.FluentUI.AspNetCore.Components.DialogEventArgs 100% 92.8%
Microsoft.FluentUI.AspNetCore.Components.DialogInstance 100% 100%
Microsoft.FluentUI.AspNetCore.Components.DialogOptions 65.5%
Microsoft.FluentUI.AspNetCore.Components.DialogOptionsFooter 100% 100%
Microsoft.FluentUI.AspNetCore.Components.DialogOptionsFooterAction 100% 100%
Microsoft.FluentUI.AspNetCore.Components.DialogOptionsHeader 100%
Microsoft.FluentUI.AspNetCore.Components.DialogResult 100% 100%
Microsoft.FluentUI.AspNetCore.Components.DialogResult`1 100%
Microsoft.FluentUI.AspNetCore.Components.DialogService 98.9% 81.4%
Microsoft.FluentUI.AspNetCore.Components.DialogToggleEventArgs 100%
Microsoft.FluentUI.AspNetCore.Components.Extensions.EnumExtensions 100% 100%
Microsoft.FluentUI.AspNetCore.Components.Extensions.FluentInputExtensions 100% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentButton 100% 89.5%
Microsoft.FluentUI.AspNetCore.Components.FluentComponentBase 100% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentDialog 97.4% 89.7%
Microsoft.FluentUI.AspNetCore.Components.FluentDialogBody 100% 91.6%
Microsoft.FluentUI.AspNetCore.Components.FluentDialogInstance 100% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentDialogProvider 100% 79.1%
Microsoft.FluentUI.AspNetCore.Components.FluentGrid 100% 90%
Microsoft.FluentUI.AspNetCore.Components.FluentGridItem 100% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentIcon`1 100% 95.8%
Microsoft.FluentUI.AspNetCore.Components.FluentInputBase`1 97% 66.6%
Microsoft.FluentUI.AspNetCore.Components.FluentInputImmediateBase`1 100% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentJSModule 100% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentLabel 100%
Microsoft.FluentUI.AspNetCore.Components.FluentLayout 100% 71.4%
Microsoft.FluentUI.AspNetCore.Components.FluentLayoutHamburger 100% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentLayoutItem 100% 89.2%
Microsoft.FluentUI.AspNetCore.Components.FluentListBase`1 93.3% 94.7%
Microsoft.FluentUI.AspNetCore.Components.FluentLocalizerExtensions 100%
Microsoft.FluentUI.AspNetCore.Components.FluentLocalizerInternal 100%
Microsoft.FluentUI.AspNetCore.Components.FluentOption 91.6% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentSelect`1 100% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentServiceBase`1 100%
Microsoft.FluentUI.AspNetCore.Components.FluentServiceProviderException`1 100%
Microsoft.FluentUI.AspNetCore.Components.FluentText 100% 100%
Microsoft.FluentUI.AspNetCore.Components.FluentTextInput 100% 100%
Microsoft.FluentUI.AspNetCore.Components.Icon 100% 91.1%
Microsoft.FluentUI.AspNetCore.Components.IconFromImage 100%
Microsoft.FluentUI.AspNetCore.Components.IconInfo 100%
Microsoft.FluentUI.AspNetCore.Components.IFluentLocalizer 100% 100%
Microsoft.FluentUI.AspNetCore.Components.InternalListContext`1 60%
Microsoft.FluentUI.AspNetCore.Components.Localization.LanguageResource 100% 100%
Microsoft.FluentUI.AspNetCore.Components.MessageBoxOptions 100%
Microsoft.FluentUI.AspNetCore.Components.Migration.AppearanceExtensions 100% 100%
Microsoft.FluentUI.AspNetCore.Components.Migration.FluentInputAppearanceExt
ensions
83.3% 75%
Microsoft.FluentUI.AspNetCore.Components.ServiceProviderExtensions 100%
Microsoft.FluentUI.AspNetCore.Components.SpacingExtensions 100% 97%
Microsoft.FluentUI.AspNetCore.Components.Utilities.AddTag 100% 100%
Microsoft.FluentUI.AspNetCore.Components.Utilities.CssBuilder 100% 92.8%
Microsoft.FluentUI.AspNetCore.Components.Utilities.Debounce 100% 83.3%
Microsoft.FluentUI.AspNetCore.Components.Utilities.Identifier 100% 100%
Microsoft.FluentUI.AspNetCore.Components.Utilities.IdentifierContext 100% 75%
Microsoft.FluentUI.AspNetCore.Components.Utilities.InlineStyleBuilder 100% 92.8%
Microsoft.FluentUI.AspNetCore.Components.Utilities.InternalDebounce.Dispatc
herTimerExtensions
95.7% 75%
Microsoft.FluentUI.AspNetCore.Components.Utilities.StyleBuilder 100% 100%
Microsoft.FluentUI.AspNetCore.Components.ZIndex 100%

@dvoituron
Denis Voituron (dvoituron) merged commit 20f536c into dev-v5 Jan 22, 2025
@dvoituron
Denis Voituron (dvoituron) deleted the users/dvoituron/dev-v5/attribute-changes branch January 22, 2025 17:06
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.

2 participants