diff --git a/knowledge-base/tooltip-no-update-from-main-model.md b/knowledge-base/tooltip-no-update-from-main-model.md index b7de9dd37..f28cddc73 100644 --- a/knowledge-base/tooltip-no-update-from-main-model.md +++ b/knowledge-base/tooltip-no-update-from-main-model.md @@ -60,39 +60,90 @@ This means that its content is no longer a sibling or child of the current compo ## Solution -The solution is to encapsulate the desired content in its own component so that its own logic and view-model updates itself and its own rendering, and changes from the parent component also fire up its `OnParametersSet` method so it can also re-render as needed. +Encapsulate the desired Tooltip content in a separate child component that has its own logic and component life cycle. This will help with the rendering updates. You can expose the necessary parameters and events, so that there are no API changes in the view-model of the main component -You can expose the necessary parameters and events from it so that there are no API changes in the view-model of the main component +Note that the child component will call its `OnParametersSet` method only on Tooltip display. If you need `OnParametersSet` to execute on each parameter change from the parent component, then use two nested components inside the Tooltip template.
-````RAZOR MainComponent -@* The API is the same, the contents are in their own component, see the adjacent tab *@ -Click for tooltip +````RAZOR Home.razor +Click to Show Tooltip - +@SearchClickLog + + @code{ - string SearchText { get; set; } - void Search() + private string SearchText { get; set; } = string.Empty; + + private string SearchClickLog { get; set; } = string.Empty; + + private int AnotherValue { get; set; } + + private Counter? CounterRef { get; set; } + + private void Search() { - Console.WriteLine("Search Click"); + SearchClickLog = $"Search Button clicked at {DateTime.Now.ToString("HH:mm:ss.fff")} for {SearchText}."; + } + + private void UpdateAnotherParameter() + { + // Will not work inside a Tooltip, + // unless this method is insde a child component of the Tooltip Template + // and AnotherValue is consumed by a grand child. + // Use the next method as an alternative. + AnotherValue = DateTime.Now.Millisecond; + } + + private void UpdateAnotherValue() + { + AnotherValue = DateTime.Now.Millisecond; + CounterRef?.GetValuesFromParent(AnotherValue); } } ```` ````RAZOR SearchTooltipContent -@* Sample content for the tooltip that fires up the necessary events and can update as necessary *@ - - -Search + + + + Search + + +

+ Another Parameter: @AnotherParameter, + Another Value: @AnotherValue +

@code { [Parameter] - public string SearchText { get; set; } + public string SearchText { get; set; } = string.Empty; [Parameter] public EventCallback SearchTextChanged { get; set; } @@ -100,22 +151,44 @@ You can expose the necessary parameters and events from it so that there are no [Parameter] public EventCallback OnSearch { get; set; } - void ValueChangedHandler(string v) + [Parameter] + public int AnotherParameter { get; set; } + + private int AnotherValue { get; set; } + + private async Task TextBoxValueChanged(string newValue) { - SearchText = v; + SearchText = newValue; + if (SearchTextChanged.HasDelegate) { - SearchTextChanged.InvokeAsync(SearchText); + await SearchTextChanged.InvokeAsync(SearchText); } } - async Task Search() + private async Task OnSearchButtonClick() { if (OnSearch.HasDelegate) { await OnSearch.InvokeAsync(null); } } + + public void GetValuesFromParent(int anotherParameterValue) + { + AnotherValue = anotherParameterValue; + StateHasChanged(); + } + + protected override void OnParametersSet() + { + // This method will not be called, except: + // - On each new Tooltip display + // - If this component is outside a Tooltip + // - If this component is a grand child of a Tooltip Template + // Call component instance methods as a workaround. See GetValuesFromParent() + + base.OnParametersSet(); + } } ```` -