Skip to content

Commit

Permalink
MudSlider: Add nullable value parameter (MudBlazor#8881)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletKuro authored May 6, 2024
1 parent d0b0545 commit fdb7151
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 69 deletions.
180 changes: 180 additions & 0 deletions src/MudBlazor.Docs.Compiler/T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,185 @@ static bool INumberBase<T>.TryParse(string s, NumberStyles style, IFormatProvide

public override int GetHashCode() => 0;
}

public struct TStruct : INumber<TStruct>
{
int IComparable.CompareTo(object obj) => 0;

int IComparable<TStruct>.CompareTo(TStruct other) => 0;

bool IEquatable<TStruct>.Equals(TStruct other) => false;

string IFormattable.ToString(string format, IFormatProvider formatProvider) => string.Empty;

bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
charsWritten = 0;
return false;
}

static TStruct IParsable<TStruct>.Parse(string s, IFormatProvider provider) => new();

static bool IParsable<TStruct>.TryParse(string s, IFormatProvider provider, out TStruct result)
{
result = new TStruct();
return false;
}

static TStruct ISpanParsable<TStruct>.Parse(ReadOnlySpan<char> s, IFormatProvider provider) => new();

static bool ISpanParsable<TStruct>.TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out TStruct result)
{
result = new TStruct();
return false;
}

static TStruct IAdditionOperators<TStruct, TStruct, TStruct>.operator +(TStruct left, TStruct right) => new();

static TStruct IAdditiveIdentity<TStruct, TStruct>.AdditiveIdentity => new();

static bool IEqualityOperators<TStruct, TStruct, bool>.operator ==(TStruct left, TStruct right) => false;

static bool IEqualityOperators<TStruct, TStruct, bool>.operator !=(TStruct left, TStruct right) => false;

static bool IComparisonOperators<TStruct, TStruct, bool>.operator >(TStruct left, TStruct right) => false;

static bool IComparisonOperators<TStruct, TStruct, bool>.operator >=(TStruct left, TStruct right) => false;

static bool IComparisonOperators<TStruct, TStruct, bool>.operator <(TStruct left, TStruct right) => false;

static bool IComparisonOperators<TStruct, TStruct, bool>.operator <=(TStruct left, TStruct right) => false;

static TStruct IDecrementOperators<TStruct>.operator --(TStruct value) => new();

static TStruct IDivisionOperators<TStruct, TStruct, TStruct>.operator /(TStruct left, TStruct right) => new();

static TStruct IIncrementOperators<TStruct>.operator ++(TStruct value) => new();

static TStruct IModulusOperators<TStruct, TStruct, TStruct>.operator %(TStruct left, TStruct right) => new();

static TStruct IMultiplicativeIdentity<TStruct, TStruct>.MultiplicativeIdentity => new();

static TStruct IMultiplyOperators<TStruct, TStruct, TStruct>.operator *(TStruct left, TStruct right) => new();

static TStruct ISubtractionOperators<TStruct, TStruct, TStruct>.operator -(TStruct left, TStruct right) => new();

static TStruct IUnaryNegationOperators<TStruct, TStruct>.operator -(TStruct value) => new();

static TStruct IUnaryPlusOperators<TStruct, TStruct>.operator +(TStruct value) => new();

static TStruct INumberBase<TStruct>.Abs(TStruct value) => new();

static bool INumberBase<TStruct>.IsCanonical(TStruct value) => false;

static bool INumberBase<TStruct>.IsComplexNumber(TStruct value) => false;

static bool INumberBase<TStruct>.IsEvenInteger(TStruct value) => false;

static bool INumberBase<TStruct>.IsFinite(TStruct value) => false;

static bool INumberBase<TStruct>.IsImaginaryNumber(TStruct value) => false;

static bool INumberBase<TStruct>.IsInfinity(TStruct value) => false;

static bool INumberBase<TStruct>.IsInteger(TStruct value) => false;

static bool INumberBase<TStruct>.IsNaN(TStruct value) => false;

static bool INumberBase<TStruct>.IsNegative(TStruct value) => false;

static bool INumberBase<TStruct>.IsNegativeInfinity(TStruct value) => false;

static bool INumberBase<TStruct>.IsNormal(TStruct value) => false;

static bool INumberBase<TStruct>.IsOddInteger(TStruct value) => false;

static bool INumberBase<TStruct>.IsPositive(TStruct value) => false;

static bool INumberBase<TStruct>.IsPositiveInfinity(TStruct value) => false;

static bool INumberBase<TStruct>.IsRealNumber(TStruct value) => false;

static bool INumberBase<TStruct>.IsSubnormal(TStruct value) => false;

static bool INumberBase<TStruct>.IsZero(TStruct value) => false;

static TStruct INumberBase<TStruct>.MaxMagnitude(TStruct x, TStruct y) => new();

static TStruct INumberBase<TStruct>.MaxMagnitudeNumber(TStruct x, TStruct y) => new();

static TStruct INumberBase<TStruct>.MinMagnitude(TStruct x, TStruct y) => new();

static TStruct INumberBase<TStruct>.MinMagnitudeNumber(TStruct x, TStruct y) => new();

static TStruct INumberBase<TStruct>.Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider) => new();

static TStruct INumberBase<TStruct>.Parse(string s, NumberStyles style, IFormatProvider provider) => new();

static bool INumberBase<TStruct>.TryConvertFromChecked<TOther>(TOther value, out TStruct result)
{
result = new TStruct();
return false;
}

static bool INumberBase<TStruct>.TryConvertFromSaturating<TOther>(TOther value, out TStruct result)
{
result = new TStruct();
return false;
}

static bool INumberBase<TStruct>.TryConvertFromTruncating<TOther>(TOther value, out TStruct result)
{
result = new TStruct();
return false;
}

static bool INumberBase<TStruct>.TryConvertToChecked<TOther>(TStruct value, out TOther result)
{
result = default;
return false;
}

static bool INumberBase<TStruct>.TryConvertToSaturating<TOther>(TStruct value, out TOther result)
{
result = default;
return false;
}

static bool INumberBase<TStruct>.TryConvertToTruncating<TOther>(TStruct value, out TOther result)
{
result = default;
return false;
}

static bool INumberBase<TStruct>.TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out TStruct result)
{
result = new TStruct();
return false;
}

static bool INumberBase<TStruct>.TryParse(string s, NumberStyles style, IFormatProvider provider, out TStruct result)
{
result = new TStruct();
return false;
}

static TStruct INumberBase<TStruct>.One => new();

static int INumberBase<TStruct>.Radix => 0;

static TStruct INumberBase<TStruct>.Zero => new();

static TStruct INumberBase<TStruct>.CreateChecked<TOther>(TOther value) => new();

static TStruct INumberBase<TStruct>.CreateTruncating<TOther>(TOther value) => new();

static TStruct INumberBase<TStruct>.CreateSaturating<TOther>(TOther value) => new();

public override bool Equals(object obj) => false;

public override int GetHashCode() => 0;
}

public class U { }
}
35 changes: 30 additions & 5 deletions src/MudBlazor.Docs.Compiler/TestsForApiPages.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Components;

namespace MudBlazor.Docs.Compiler
{
#nullable enable
public class TestsForApiPages
{
public bool Execute()
Expand Down Expand Up @@ -49,7 +51,7 @@ public bool Execute()
continue;
if (type.Name.Contains("Base"))
continue;
if (type.Namespace.Contains("InternalComponents"))
if (type.Namespace is not null && type.Namespace.Contains("InternalComponents"))
continue;
if (IsObsolete(type))
continue;
Expand All @@ -75,7 +77,7 @@ public bool Execute()
}
catch (Exception e)
{
Console.WriteLine($"Error generating {paths.ApiPageTestsFilePath} : {e.Message}");
Console.WriteLine($@"Error generating {paths.ApiPageTestsFilePath} : {e.Message}");
success = false;
}

Expand All @@ -84,19 +86,42 @@ public bool Execute()

public static bool IsObsolete(Type type)
{
var attributes = (ObsoleteAttribute[])
type.GetCustomAttributes(typeof(ObsoleteAttribute), false);
return attributes != null && attributes.Length > 0;
var attributes = (ObsoleteAttribute[])type.GetCustomAttributes(typeof(ObsoleteAttribute), false);

return attributes is { Length: > 0 };
}

private static string SafeTypeName(Type type, bool removeT = false)
{
if (!type.IsGenericType)
{
return type.Name;
}

var genericTypename = type.Name;
if (removeT)
{
return genericTypename.Replace("`1", string.Empty).Replace("`2", string.Empty);
}

if (RequiresStructConstraint(type))
{
return genericTypename.Replace("`1", "<TStruct>").Replace("`2", "<T, U>");
}
return genericTypename.Replace("`1", "<T>").Replace("`2", "<T, U>");
}

private static bool RequiresStructConstraint(Type componentType)
{
if (componentType.IsGenericType)
{
var genericArgs = componentType.GetGenericArguments();
var requiresStructConstraint = genericArgs.Any(arg => arg.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint));

return requiresStructConstraint;
}

return false;
}
}
}
2 changes: 1 addition & 1 deletion src/MudBlazor.Docs/Models/T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MudBlazor.Docs.Models
{
public class T : INumber<T>
public struct T : INumber<T>
{
int IComparable.CompareTo(object obj) => 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@namespace MudBlazor.Docs.Examples

<MudSlider T="int" @bind-Value="@_value" @bind-NullableValue="@_nullableValue" />
<div class="d-flex flex-column align-center">
<MudText Class="pb-4">@_value</MudText>
<MudText Class="pb-4">Nullable Value: @(_nullableValue is null ? "null" : _nullableValue)</MudText>
<MudButton @onclick="Reset" Variant="Variant.Filled" Color="Color.Primary">Reset to Null</MudButton>
</div>

@code {
private int _value;
private int? _nullableValue;

private void Reset() => _nullableValue = null;
}
17 changes: 14 additions & 3 deletions src/MudBlazor.Docs/Pages/Components/Slider/SliderPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@
<SliderMinMaxExample />
</SectionContent>
</DocsPageSection>

<DocsPageSection>
<SectionHeader Title="Nullable">
<Description>
If undefined state is required use <CodeInline>@nameof(MudSlider<double>.NullableValue)</CodeInline>.
</Description>
</SectionHeader>
<SectionContent Code="@nameof(SliderNullableExample)">
<SliderNullableExample />
</SectionContent>
</DocsPageSection>

<DocsPageSection>
<SectionHeader Title="Ticks" />
Expand Down Expand Up @@ -66,11 +77,11 @@
<DocsPageSection>
<SectionHeader Title="Value Label">
<Description>
Use the slider below to see the value label, this can be used by setting <CodeInline>@nameof(MudSlider<T>.ValueLabel)</CodeInline> property to true.
Use the slider below to see the value label, this can be used by setting <CodeInline>@nameof(MudSlider<double>.ValueLabel)</CodeInline> property to true.
<br />
Use <CodeInline>@nameof(MudSlider<T>.ValueLabelStringFormat)</CodeInline> and <CodeInline>@nameof(MudSlider<T>.ValueLabelCultureInfo)</CodeInline> to change the formatting.
Use <CodeInline>@nameof(MudSlider<double>.ValueLabelStringFormat)</CodeInline> and <CodeInline>@nameof(MudSlider<double>.ValueLabelCultureInfo)</CodeInline> to change the formatting.
<br />
For more customization use <CodeInline>@nameof(MudSlider<T>.ValueLabelContent)</CodeInline> RenderFragment.
For more customization use <CodeInline>@nameof(MudSlider<double>.ValueLabelContent)</CodeInline> RenderFragment.
</Description>
</SectionHeader>
<SectionContent Code="@nameof(SliderValueLabelExample)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

<MudSlider Max="100.0" Value="20.0" Min="0.0" ValueLabel="true">
<ValueLabelContent>
<MudAlert>@context</MudAlert>
<MudAlert>@context.Value</MudAlert>
</ValueLabelContent>
</MudSlider>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@namespace MudBlazor.UnitTests.TestComponents

<MudSlider T="double" @bind-NullableValue="NullableValue" Max="100.0" Min="0.0" Step="0.5" />

@code {
public double? NullableValue { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@namespace MudBlazor.UnitTests.TestComponents

<MudSlider T="double" @bind-Value="Value" @bind-NullableValue="NullableValue" Max="100.0" Min="0.0" Step="0.5" />

@code {
public double? NullableValue { get; set; }

public double Value { get; set; }
}
Loading

0 comments on commit fdb7151

Please sign in to comment.