Skip to content

Commit 6e5ebca

Browse files
kb(Tooltip): Enhance KB, based on docs feedback (#3263)
Co-authored-by: Dimo Dimov <[email protected]>
1 parent 032bb44 commit 6e5ebca

File tree

1 file changed

+93
-20
lines changed

1 file changed

+93
-20
lines changed

knowledge-base/tooltip-no-update-from-main-model.md

Lines changed: 93 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,62 +60,135 @@ This means that its content is no longer a sibling or child of the current compo
6060

6161
## Solution
6262

63-
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.
63+
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
6464

65-
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
65+
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.
6666

6767
<div class="skip-repl"></div>
68-
````RAZOR MainComponent
69-
@* The API is the same, the contents are in their own component, see the adjacent tab *@
7068

71-
<TelerikButton Class="search-tooltip" ThemeColor="primary">Click for tooltip</TelerikButton>
69+
````RAZOR Home.razor
70+
<TelerikButton Class="search-tooltip-target" ThemeColor="@ThemeConstants.Button.ThemeColor.Primary">Click to Show Tooltip</TelerikButton>
7271
73-
<TelerikTooltip TargetSelector=".search-tooltip" Position="TooltipPosition.Bottom" ShowOn="TooltipShowEvent.Click">
72+
@SearchClickLog
73+
74+
<TelerikTooltip TargetSelector=".search-tooltip-target"
75+
Position="TooltipPosition.Bottom"
76+
ShowOn="TooltipShowEvent.Click">
7477
<Template>
75-
<SearchTooltipContent @bind-SearchText="@SearchText" OnSearch="@Search" />
78+
@* The Update AnotherParameter button will not work
79+
unless the whole Tooltip Template content is in a child component. *@
80+
<TelerikButton OnClick="@UpdateAnotherParameter"
81+
ThemeColor="@ThemeConstants.Button.ThemeColor.Error">
82+
Update AnotherParameter
83+
</TelerikButton>
84+
<TelerikButton OnClick="@UpdateAnotherValue"
85+
ThemeColor="@ThemeConstants.Button.ThemeColor.Success">
86+
Update AnotherValue
87+
</TelerikButton>
88+
<br />
89+
<br />
90+
<SearchTooltipContent @ref="@CounterRef"
91+
@bind-SearchText="@SearchText"
92+
AnotherParameter="@AnotherValue"
93+
OnSearch="@Search" />
7694
</Template>
7795
</TelerikTooltip>
7896
7997
@code{
80-
string SearchText { get; set; }
81-
void Search()
98+
private string SearchText { get; set; } = string.Empty;
99+
100+
private string SearchClickLog { get; set; } = string.Empty;
101+
102+
private int AnotherValue { get; set; }
103+
104+
private Counter? CounterRef { get; set; }
105+
106+
private void Search()
82107
{
83-
Console.WriteLine("Search Click");
108+
SearchClickLog = $"Search Button clicked at {DateTime.Now.ToString("HH:mm:ss.fff")} for {SearchText}.";
109+
}
110+
111+
private void UpdateAnotherParameter()
112+
{
113+
// Will not work inside a Tooltip,
114+
// unless this method is insde a child component of the Tooltip Template
115+
// and AnotherValue is consumed by a grand child.
116+
// Use the next method as an alternative.
117+
AnotherValue = DateTime.Now.Millisecond;
118+
}
119+
120+
private void UpdateAnotherValue()
121+
{
122+
AnotherValue = DateTime.Now.Millisecond;
123+
CounterRef?.GetValuesFromParent(AnotherValue);
84124
}
85125
}
86126
````
87127
````RAZOR SearchTooltipContent
88-
@* Sample content for the tooltip that fires up the necessary events and can update as necessary *@
89-
90-
<TelerikTextBox Value="@SearchText" ValueChanged="@( (string v) => ValueChangedHandler(v) )"></TelerikTextBox>
91-
<TelerikButton OnClick="@Search" Enabled="@(!string.IsNullOrEmpty(SearchText))">Search</TelerikButton>
128+
<TelerikTextBox Value="@SearchText"
129+
ValueChanged="@TextBoxValueChanged"
130+
ShowClearButton="true"
131+
Width="160px" />
132+
133+
<TelerikButton OnClick="@OnSearchButtonClick"
134+
ThemeColor="@ThemeConstants.Button.ThemeColor.Primary"
135+
Enabled="@(!string.IsNullOrEmpty(SearchText))">
136+
Search
137+
</TelerikButton>
138+
139+
<p>
140+
Another Parameter: @AnotherParameter,
141+
Another Value: @AnotherValue
142+
</p>
92143
93144
@code {
94145
[Parameter]
95-
public string SearchText { get; set; }
146+
public string SearchText { get; set; } = string.Empty;
96147
97148
[Parameter]
98149
public EventCallback<string> SearchTextChanged { get; set; }
99150
100151
[Parameter]
101152
public EventCallback OnSearch { get; set; }
102153
103-
void ValueChangedHandler(string v)
154+
[Parameter]
155+
public int AnotherParameter { get; set; }
156+
157+
private int AnotherValue { get; set; }
158+
159+
private async Task TextBoxValueChanged(string newValue)
104160
{
105-
SearchText = v;
161+
SearchText = newValue;
162+
106163
if (SearchTextChanged.HasDelegate)
107164
{
108-
SearchTextChanged.InvokeAsync(SearchText);
165+
await SearchTextChanged.InvokeAsync(SearchText);
109166
}
110167
}
111168
112-
async Task Search()
169+
private async Task OnSearchButtonClick()
113170
{
114171
if (OnSearch.HasDelegate)
115172
{
116173
await OnSearch.InvokeAsync(null);
117174
}
118175
}
176+
177+
public void GetValuesFromParent(int anotherParameterValue)
178+
{
179+
AnotherValue = anotherParameterValue;
180+
StateHasChanged();
181+
}
182+
183+
protected override void OnParametersSet()
184+
{
185+
// This method will not be called, except:
186+
// - On each new Tooltip display
187+
// - If this component is outside a Tooltip
188+
// - If this component is a grand child of a Tooltip Template
189+
// Call component instance methods as a workaround. See GetValuesFromParent()
190+
191+
base.OnParametersSet();
192+
}
119193
}
120194
````
121-

0 commit comments

Comments
 (0)