Skip to content

Commit d396358

Browse files
committed
pull up ShowClearButton into base class
1 parent f6832c2 commit d396358

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

src/MudBlazor/Base/MudBaseInput.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,38 @@ public string? Format
432432

433433
protected bool GetReadOnlyState() => ReadOnly || ParentReadOnly;
434434

435+
protected virtual bool GetClearable() => false;
436+
437+
/// <summary>
438+
/// Determine whether to show the clear button when Clearable==true.
439+
/// Of course the clear button won't show up if the text field is empty
440+
/// </summary>
441+
protected virtual bool ShowClearButton()
442+
{
443+
if (GetDisabledState())
444+
{
445+
return false;
446+
}
447+
448+
if (!GetClearable())
449+
{
450+
return false;
451+
}
452+
453+
// If this is a standalone input it will not be clearable when read-only
454+
if (SubscribeToParentForm && GetReadOnlyState())
455+
{
456+
return false;
457+
}
458+
459+
if (Value is string stringValue)
460+
{
461+
return !string.IsNullOrWhiteSpace(stringValue);
462+
}
463+
464+
return Value is not string and not null;
465+
}
466+
435467
protected virtual async Task SetTextAsync(string? text, bool updateValue = true)
436468
{
437469
if (Text != text)

src/MudBlazor/Components/Input/MudInput.razor.cs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -223,35 +223,7 @@ public override ValueTask SelectRangeAsync(int pos1, int pos2)
223223

224224
private Size GetButtonSize() => Margin == Margin.Dense ? Size.Small : Size.Medium;
225225

226-
/// <summary>
227-
/// Determine whether to show the clear button when Clearable==true.
228-
/// Of course the clear button won't show up if the text field is empty
229-
/// </summary>
230-
private bool ShowClearButton()
231-
{
232-
if (GetDisabledState())
233-
{
234-
return false;
235-
}
236-
237-
if (!Clearable)
238-
{
239-
return false;
240-
}
241-
242-
// If this is a standalone input it will not be clearable when read-only
243-
if (SubscribeToParentForm && GetReadOnlyState())
244-
{
245-
return false;
246-
}
247-
248-
if (Value is string stringValue)
249-
{
250-
return !string.IsNullOrWhiteSpace(stringValue);
251-
}
252-
253-
return Value is not string and not null;
254-
}
226+
protected override bool GetClearable() => Clearable;
255227

256228
protected virtual async Task HandleClearButtonAsync(MouseEventArgs e)
257229
{

src/MudBlazor/Components/Mask/MudMask.razor.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,31 @@ private string GetCounterText() => Counter == null
310310
? (string.IsNullOrEmpty(Text) ? "0" : $"{Text.Length}")
311311
: ((string.IsNullOrEmpty(Text) ? "0" : $"{Text.Length}") + $" / {Counter}"));
312312

313-
private bool ShowClearButton()
313+
protected override bool GetClearable() => _showClearable;
314+
315+
/// <summary>
316+
/// Determine whether to show the clear button when Clearable==true.
317+
/// Of course the clear button won't show up if the text field is empty
318+
/// </summary>
319+
protected override bool ShowClearButton()
314320
{
315-
if (SubscribeToParentForm)
316-
return _showClearable && !GetReadOnlyState() && !GetDisabledState();
317-
return _showClearable && !GetDisabledState();
321+
if (GetDisabledState())
322+
{
323+
return false;
324+
}
325+
326+
if (!GetClearable())
327+
{
328+
return false;
329+
}
330+
331+
// If this is a standalone input it will not be clearable when read-only
332+
if (SubscribeToParentForm && GetReadOnlyState())
333+
{
334+
return false;
335+
}
336+
337+
return true;
318338
}
319339

320340
/// <summary>

src/MudBlazor/Components/TextField/MudTextField.razor.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,7 @@ protected override Task SetTextAsync(string? text, bool updateValue = true)
217217

218218
internal override InputType GetInputType() => InputType;
219219

220-
private bool ShowClearButton()
221-
{
222-
if (SubscribeToParentForm)
223-
return Clearable && !GetReadOnlyState() && !GetDisabledState();
224-
return Clearable && !GetDisabledState();
225-
}
220+
protected override bool GetClearable() => Clearable;
226221

227222
private Task OnMaskedValueChanged(string s) => SetTextAsync(s);
228223

0 commit comments

Comments
 (0)