Skip to content

Commit 7d9b8a5

Browse files
authored
MudSplitter: Add 2-way bindable Dimension property (#90)
* Added 2-way bindable Dimension property * Changed bool? to bool
1 parent efaf653 commit 7d9b8a5

File tree

4 files changed

+83
-88
lines changed

4 files changed

+83
-88
lines changed

CodeBeam.MudExtensions/Components/Splitter/MudSplitter.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
@inherits MudComponentBase
33

44
<div class="@Classname" style="@Style">
5-
<MudSlider @ref="_slider" ondblclick="@OnDoubleClick" Class="@SliderClassname" Style="overflow: hidden; z-index: 6" T="double" Min="0" Max="100" Step="@Sensitivity" ValueChanged="UpdateDimensions" Disabled="DisableSlide"/>
5+
<MudSlider @ref="_slider" Value="@Dimension" ValueChanged="@UpdateDimension" ondblclick="@OnDoubleClick"
6+
T="double" Min="0" Max="100" Step="@Sensitivity" Disabled="@(!EnableSlide)"
7+
Class="@SliderClassname" Style="overflow: hidden; z-index: 6" />
68

79
<div class="@ContentClassname" style="@StyleContent">
810
@StartContent
@@ -13,7 +15,7 @@
1315
</div>
1416

1517
<style>
16-
@($".mud-splitter-generate-{_styleGuid} {{ grid-template-columns: {_firstContentDimension.ToInvariantString()}% {_secondContentDimension.ToInvariantString()}%; }}")
18+
@($".mud-splitter-generate-{_styleGuid} {{ grid-template-columns: {Dimension.ToInvariantString()}% {(100 - Dimension).ToInvariantString()}%; }}")
1719
1820
.mud-splitter-thumb-@(_styleGuid) ::-webkit-slider-thumb {
1921
@(Color == Color.Default ? "background-color: var(--mud-palette-action-default) !important;" : $"background-color: var(--mud-palette-{Color.ToDescriptionString()}) !important;") @(StyleBar)
Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
using Microsoft.AspNetCore.Components;
2-
using Microsoft.AspNetCore.Components.Web;
32
using MudBlazor;
4-
using MudBlazor.Components.Highlighter;
53
using MudBlazor.Extensions;
64
using MudBlazor.Utilities;
7-
using System;
8-
using System.Collections.Generic;
9-
using System.Linq;
10-
using System.Text;
11-
using System.Threading.Tasks;
12-
using MudExtensions.Extensions;
13-
using static MudBlazor.CategoryTypes;
145

156
namespace MudExtensions
167
{
@@ -21,13 +12,13 @@ public partial class MudSplitter : MudComponentBase
2112
MudSlider<double> _slider;
2213

2314
protected string Classname => new CssBuilder("mud-splitter")
24-
.AddClass($"border-solid border-8 mud-border-{Color.ToDescriptionString()}", Bordered == true)
15+
.AddClass($"border-solid border-2 mud-border-{Color.ToDescriptionString()}", Bordered == true)
2516
.AddClass($"mud-splitter-generate mud-splitter-generate-{_styleGuid}")
2617
.AddClass(Class)
2718
.Build();
2819

2920
protected string ContentClassname => new CssBuilder($"mud-splitter-content mud-splitter-content-{_styleGuid} d-flex")
30-
.AddClass("ma-2", !DisableMargin)
21+
.AddClass("ma-2", EnableMargin)
3122
.AddClass(ClassContent)
3223
.Build();
3324

@@ -40,24 +31,12 @@ public partial class MudSplitter : MudComponentBase
4031
[Parameter]
4132
public string ClassContent { get; set; }
4233

43-
string _height;
34+
//string _height;
4435
/// <summary>
4536
/// The height of splitter.
4637
/// </summary>
4738
[Parameter]
48-
public string Height
49-
{
50-
get => _height;
51-
set
52-
{
53-
if (value == _height)
54-
{
55-
return;
56-
}
57-
_height = value;
58-
UpdateDimensions().AndForgetExt();
59-
}
60-
}
39+
public string Height { get; set; }
6140

6241
/// <summary>
6342
/// The height of splitter.
@@ -81,25 +60,42 @@ public string Height
8160
/// The splitter bar's styles, seperated by space. All styles have to include !important and end with ';'
8261
/// </summary>
8362
[Parameter]
84-
public string StyleBar { get; set; }
63+
public string StyleBar { get; set; } = "width:2px !important;";
8564

8665
/// <summary>
8766
/// The slide sensitivity that should between 0.01 and 10. Smaller values increase the smooth but reduce performance. Default is 0.1
8867
/// </summary>
8968
[Parameter]
9069
public double Sensitivity { get; set; } = 0.1d;
9170

71+
[Obsolete("DisableSlide is deprecated, please use property EnableSlide to set Slide.")]
72+
[Parameter]
73+
public bool DisableSlide
74+
{
75+
get { return !EnableSlide; }
76+
set { EnableSlide = !value; }
77+
}
9278
/// <summary>
93-
/// If true, user cannot interact with splitter bar.
79+
/// If true, user can interact with splitter bar.
80+
/// Default is true.
9481
/// </summary>
9582
[Parameter]
96-
public bool DisableSlide { get; set; }
83+
public bool EnableSlide { get; set; } = true;
9784

85+
[Obsolete("DisableMargin is deprecated, please use property EnableMargin to set Margin.")]
86+
[Parameter]
87+
public bool DisableMargin
88+
{
89+
get { return !EnableMargin; }
90+
set { EnableMargin = !value; }
91+
}
9892
/// <summary>
99-
/// Disables the default margin.
93+
/// Enables the default margin.
94+
/// Default is true.
10095
/// </summary>
10196
[Parameter]
102-
public bool DisableMargin { get; set; }
97+
public bool EnableMargin { get; set; } = true;
98+
10399

104100
///// <summary>
105101
///// If true, splitter bar goes vertical.
@@ -113,47 +109,44 @@ public string Height
113109
[Parameter]
114110
public RenderFragment EndContent { get; set; }
115111

112+
113+
/// <summary>
114+
/// The start content's percentage.
115+
/// Default is 50.
116+
/// </summary>
117+
[Parameter]
118+
public double Dimension { get; set; } = 50;
119+
116120
[Parameter]
117-
public EventCallback DimensionChanged { get; set; }
121+
public EventCallback<double> DimensionChanged { get; set; }
118122

119123
[Parameter]
120124
public EventCallback OnDoubleClicked { get; set; }
121125

122-
protected override async Task OnInitializedAsync()
126+
protected override async Task OnInitializedAsync()
123127
{
124128
await base.OnInitializedAsync();
125-
await UpdateDimensions();
129+
await UpdateDimension(Dimension);
126130
}
127131

128-
double _firstContentDimension = 50;
129-
double _secondContentDimension = 50;
130-
protected async Task UpdateDimensions()
132+
protected async Task UpdateDimension(double percentage)
131133
{
132-
if (_slider == null)
133-
{
134-
return;
135-
}
136-
_firstContentDimension = _slider.Value;
137-
_secondContentDimension = 100d - _firstContentDimension;
138-
await DimensionChanged.InvokeAsync();
134+
Dimension = percentage;
135+
if (DimensionChanged.HasDelegate)
136+
await DimensionChanged.InvokeAsync(percentage);
139137
}
140138

141-
public double GetStartContentPercentage() => _firstContentDimension;
142-
143139
/// <summary>
144140
/// Updates the dimension with given the start content's percentage
145141
/// </summary>
146142
/// <param name="percentage"></param>
147-
public async Task SetDimensions(double percentage)
148-
{
149-
_slider.Value = percentage;
150-
await UpdateDimensions();
151-
}
143+
[Obsolete("SetDimensions is deprecated, please use property Dimension to set start content's percentage.")]
144+
public Task SetDimensions(double percentage) => UpdateDimension(percentage);
152145

153146
async Task OnDoubleClick()
154147
{
155-
if (OnDoubleClicked.HasDelegate)
156-
await OnDoubleClicked.InvokeAsync();
148+
if (OnDoubleClicked.HasDelegate)
149+
await OnDoubleClicked.InvokeAsync();
157150
}
158151
}
159152
}

ComponentViewer.Docs/Pages/Examples/SplitterExample1.razor

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
<MudGrid>
55
<MudItem xs="12" sm="8">
6-
<MudSplitter @ref="_splitter" Height="@_height" Color="_color" Bordered="_bordered" OnDoubleClicked="@OnDoubleClicked" DimensionChanged="DimensionChanged" DisableSlide="_disableSlide" DisableMargin="_disableMargin" Sensitivity="_sensitivity" Overlap="true">
6+
<MudSplitter @ref="_splitter"
7+
@bind-Dimension="@_percentage"
8+
OnDoubleClicked="@OnDoubleClicked"
9+
EnableSlide="@_sliderEnabled" EnableMargin="@_marginEnabled" Sensitivity="_sensitivity"
10+
Height="@_height" Color="_color" Bordered="_borderedEnabled">
711
<StartContent>
812
<MudText>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</MudText>
913
</StartContent>
@@ -16,42 +20,38 @@
1620

1721
<MudItem xs="12" sm="4">
1822
<MudStack>
19-
<MudTextField @bind-Value="_height" Variant="Variant.Outlined" Label="Height" Clearable="true" />
20-
<MudSelect @bind-Value="_color" Variant="Variant.Outlined">
21-
@foreach (Color col in Enum.GetValues<Color>())
22-
{
23-
<MudSelectItem Value="col">@col.ToDescriptionString()</MudSelectItem>
24-
}
23+
<MudTextField @bind-Value="_height" Variant="Variant.Outlined" Label="Height px" Clearable />
24+
<MudSelect @bind-Value="_color" Variant="Variant.Outlined" Clearable>
25+
@foreach (Color col in Enum.GetValues<Color>())
26+
{
27+
<MudSelectItem Value="col">@col.ToDescriptionString()</MudSelectItem>
28+
}
2529
</MudSelect>
26-
<MudSwitchM3 @bind-Checked="_bordered" Color="Color.Primary" Label="Border" />
27-
<MudSwitchM3 @bind-Checked="_disableSlide" Color="Color.Primary" Label="Disable" />
28-
<MudSwitchM3 @bind-Checked="_disableMargin" Color="Color.Primary" Label="Disable Margin" />
30+
<MudSwitchM3 @bind-Checked="_borderedEnabled" Color="Color.Primary" Label="Enable splitter-border" />
31+
<MudSwitchM3 @bind-Checked="_sliderEnabled" Color="Color.Primary" Label="Enable splitter-bar" />
32+
<MudSwitchM3 @bind-Checked="_marginEnabled" Color="Color.Primary" Label="Enable margin" />
33+
<MudSwitchM3 @bind-Checked="@_doubleClickEnabled" Color="Color.Primary" Label="Enable double-click" />
2934
<MudDivider />
3035
<MudNumericField @bind-Value="_sensitivity" Variant="Variant.Outlined" Min="0.01" Max="10" Step="0.1" Label="Sensitivity" />
31-
<MudNumericField @bind-Value="_percentage" Variant="Variant.Outlined" Label="Start Content Percentage" />
32-
<MudButton OnClick="@(() => _splitter.SetDimensions(_percentage))">Set Dimension (@_percentage)</MudButton>
36+
<MudNumericField @bind-Value="@_percentage" Variant="Variant.Outlined" Label="Dimension percentage" />
3337
</MudStack>
3438
</MudItem>
3539
</MudGrid>
3640

37-
@code{
38-
MudSplitter _splitter;
39-
string _height;
40-
Color _color;
41-
double _percentage = 50;
42-
bool _bordered = false;
43-
bool _disableSlide = false;
44-
bool _disableMargin = false;
45-
double _sensitivity = 0.1d;
41+
@code {
42+
MudSplitter _splitter;
43+
Color _color;
44+
string _height = "400px";
45+
double _sensitivity = 0.1d;
46+
double _percentage = 50;
47+
bool _sliderEnabled = true;
48+
bool _marginEnabled = true;
49+
bool _borderedEnabled = true;
50+
bool _doubleClickEnabled = true;
4651

47-
private void DimensionChanged()
48-
{
49-
_percentage = _splitter.GetStartContentPercentage();
50-
}
51-
52-
private async Task OnDoubleClicked()
53-
{
54-
_percentage = 50;
55-
await _splitter.SetDimensions(_percentage);
56-
}
52+
void OnDoubleClicked()
53+
{
54+
if (_doubleClickEnabled)
55+
_percentage = 50;
56+
}
5757
}

ComponentViewer.Docs/Pages/Examples/SplitterExample2.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<MudSplitter Height="@_height" Color="Color.Secondary">
44
<StartContent>
55
<div>
6-
<MudImage Src="https://cdn.pixabay.com/photo/2022/08/15/03/07/path-7387018_960_720.jpg" Fluid="true" />
6+
<MudImage Src="https://cdn.pixabay.com/photo/2022/08/15/03/07/path-7387018_960_720.jpg" Fluid />
77
</div>
88
</StartContent>
99

@@ -14,10 +14,10 @@
1414
</MudItem>
1515

1616
<MudItem xs="12" sm="4">
17-
<MudTextField @bind-Value="_height" Variant="Variant.Outlined" Label="Height" Clearable="true" />
17+
<MudTextField @bind-Value="@_height" Variant="Variant.Outlined" Label="Height px" Clearable />
1818
</MudItem>
1919
</MudGrid>
2020

2121
@code {
22-
string _height;
22+
string _height = "400px";
2323
}

0 commit comments

Comments
 (0)