From e6c51fe87018626b1c619bc7267c18d162e4ae44 Mon Sep 17 00:00:00 2001
From: Simon Ziegler <simon@dioptra.tech>
Date: Sun, 25 Oct 2020 12:54:02 +0000
Subject: [PATCH] Update cascading values with IsFixed="true" (#310)

* Cascading values optimized per Steve Sanderson's suggestion

See https://github.com/dotnet/aspnetcore/issues/26230

* Further change
---
 Material.Blazor/Components/Card/MBCard.razor  |  2 +-
 .../Components/Dialog/MBDialog.razor          |  2 +-
 .../Components/Drawer/MBDrawer.razor          |  2 +-
 .../Components/TabBar/MBTabBar.razor          |  2 +-
 .../Foundation/InputComponentFoundation.cs    | 20 ++++++++++---------
 Material.Blazor/Material.Blazor.csproj        |  5 +++++
 6 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/Material.Blazor/Components/Card/MBCard.razor b/Material.Blazor/Components/Card/MBCard.razor
index 0de458006..a9ee611b4 100644
--- a/Material.Blazor/Components/Card/MBCard.razor
+++ b/Material.Blazor/Components/Card/MBCard.razor
@@ -2,7 +2,7 @@
 @inherits ComponentFoundation
 
 
-<CascadingValue Value="@this">
+<CascadingValue Value="@this" IsFixed="true">
     <div @attributes="@AttributesToSplat(SplatType.ExcludeEvents)">
 
         @if (Primary != null)
diff --git a/Material.Blazor/Components/Dialog/MBDialog.razor b/Material.Blazor/Components/Dialog/MBDialog.razor
index b9d40596a..ad13b699d 100644
--- a/Material.Blazor/Components/Dialog/MBDialog.razor
+++ b/Material.Blazor/Components/Dialog/MBDialog.razor
@@ -9,7 +9,7 @@
          @attributes="@MyAttributes"
          aria-modal="true">
 
-        <CascadingValue Value="@this">
+        <CascadingValue Value="@this" IsFixed="true">
             <div class="mdc-dialog__container">
                 <div class="mdc-dialog__surface @OverflowClass">
                     @if (HasTitle)
diff --git a/Material.Blazor/Components/Drawer/MBDrawer.razor b/Material.Blazor/Components/Drawer/MBDrawer.razor
index be0238a8d..126dcc005 100644
--- a/Material.Blazor/Components/Drawer/MBDrawer.razor
+++ b/Material.Blazor/Components/Drawer/MBDrawer.razor
@@ -6,7 +6,7 @@
        @attributes="@AttributesToSplat()">
 
     <div class="mdc-drawer__content">
-        <CascadingValue Value="@this">
+        <CascadingValue Value="@this" IsFixed="true">
             @if (ChildContent != null)
             {
                 @ChildContent
diff --git a/Material.Blazor/Components/TabBar/MBTabBar.razor b/Material.Blazor/Components/TabBar/MBTabBar.razor
index a97d17689..c23325769 100644
--- a/Material.Blazor/Components/TabBar/MBTabBar.razor
+++ b/Material.Blazor/Components/TabBar/MBTabBar.razor
@@ -28,7 +28,7 @@
                         <span class="mdc-tab__content">
                             @if (Icon != null)
                             {
-                                <CascadingValue Value="@tabBarIdentifierObject" Name="@TabBarIdentifier">
+                                <CascadingValue Value="@tabBarIdentifierObject" Name="@TabBarIdentifier" IsFixed="true">
                                     @Icon(item)
                                 </CascadingValue>
                             }
diff --git a/Material.Blazor/Foundation/InputComponentFoundation.cs b/Material.Blazor/Foundation/InputComponentFoundation.cs
index bc6ff63b2..fbbf52147 100644
--- a/Material.Blazor/Foundation/InputComponentFoundation.cs
+++ b/Material.Blazor/Foundation/InputComponentFoundation.cs
@@ -101,13 +101,13 @@ private protected T ComponentValue
             get => _componentValue;
             set
             {
-#if Logging
-                Logger.LogDebug("ComponentValue setter entered");
+#if LoggingVerbose
+                Logger.LogDebug($"ComponentValue setter entered: _componentValue is '{_cachedValue?.ToString() ?? "null"}' and new value is'{value?.ToString() ?? "null"}'");
 #endif
                 if (!EqualityComparer<T>.Default.Equals(value, _componentValue))
                 {
 #if Logging
-                    Logger.LogDebug("ComponentValue setter changing value to '" + value?.ToString() ?? "null" + "'");
+                    Logger.LogDebug($"ComponentValue setter changed _componentValue");
 #endif
                     _componentValue = value;
                     _ = ValueChanged.InvokeAsync(value);
@@ -280,7 +280,7 @@ public override async Task SetParametersAsync(ParameterView parameters)
                 _nullableUnderlyingType = Nullable.GetUnderlyingType(typeof(T));
                 _hasSetInitialParameters = true;
 #if Logging
-                Logger.LogDebug("SetParametersAsync setting ComponentValue value to '" + Value?.ToString() ?? "null" + "'");
+                Logger.LogDebug($"SetParametersAsync setting ComponentValue value to '{Value?.ToString() ?? "null"}'");
 #endif
                 _cachedValue = Value;
                 _componentValue = Value;
@@ -292,8 +292,7 @@ public override async Task SetParametersAsync(ParameterView parameters)
                 // We don't support changing EditContext because it's messy to be clearing up state and event
                 // handlers for the previous one, and there's no strong use case. If a strong use case
                 // emerges, we can consider changing this.
-                throw new InvalidOperationException($"{GetType()} does not support changing the " +
-                    $"{nameof(EditContext)} dynamically.");
+                throw new InvalidOperationException($"{GetType()} does not support changing the {nameof(EditContext)} dynamically.");
             }
 
             // For derived components, retain the usual lifecycle with OnInit/OnParametersSet/etc.
@@ -318,16 +317,19 @@ protected override async Task OnParametersSetAsync()
 
         private void CommonParametersSet()
         {
+#if LoggingVerbose
+            Logger.LogDebug($"OnParametersSet setter entered: _cachedValue is '{_cachedValue?.ToString() ?? "null"}' and Value is'{Value?.ToString() ?? "null"}'");
+#endif
             if (!EqualityComparer<T>.Default.Equals(_cachedValue, Value))
             {
                 _cachedValue = Value;
 #if Logging
-                Logger.LogDebug("OnParametersSet setting CachedValue value to '" + Value?.ToString() ?? "null" + "'");
+                Logger.LogDebug($"OnParametersSet changed _cachedValue value");
 #endif
-                if (!EqualityComparer<T>.Default.Equals(ComponentValue, Value))
+                if (!EqualityComparer<T>.Default.Equals(_componentValue, Value))
                 {
 #if Logging
-                    Logger.LogDebug("OnParametersSet setting ComponentValue value to '" + Value?.ToString() ?? "null" + "'");
+                    Logger.LogDebug("OnParametersSet update _componentValue value from '" + _componentValue?.ToString() ?? "null" + "'");
 #endif
                     _componentValue = Value;
                     if (_hasInstantiated)
diff --git a/Material.Blazor/Material.Blazor.csproj b/Material.Blazor/Material.Blazor.csproj
index 407058d4c..87ec6fa69 100644
--- a/Material.Blazor/Material.Blazor.csproj
+++ b/Material.Blazor/Material.Blazor.csproj
@@ -24,6 +24,7 @@
 
   <PropertyGroup Condition="'$(Configuration)'=='Debug'">
     <DefineConstants>Logging</DefineConstants>
+    <!--<DefineConstants>Logging;LoggingVerbose</DefineConstants>-->
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)'=='Release'">
@@ -33,6 +34,10 @@
   <PropertyGroup>
     <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
   </PropertyGroup>
+
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
+    <WarningLevel>5</WarningLevel>
+  </PropertyGroup>
   
   <ItemGroup>
     <Content Remove="package-lock.json" />