ant-design-blazor/components/grid/Col.razor.cs

154 lines
4.8 KiB
C#
Raw Normal View History

using System.Text.RegularExpressions;
2020-03-15 23:54:48 +08:00
using Microsoft.AspNetCore.Components;
using OneOf;
namespace AntDesign
{
2020-03-15 23:54:48 +08:00
using StringNumber = OneOf<string, int>;
public class EmbeddedProperty
{
public StringNumber Span { get; set; }
2020-03-15 23:54:48 +08:00
public StringNumber Pull { get; set; }
2020-03-15 23:54:48 +08:00
public StringNumber Push { get; set; }
2020-03-15 23:54:48 +08:00
public StringNumber Offset { get; set; }
2020-03-15 23:54:48 +08:00
public StringNumber Order { get; set; }
2020-03-15 23:54:48 +08:00
}
public partial class Col : AntDomComponentBase
{
[Parameter]
public RenderFragment ChildContent { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public StringNumber Flex { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public StringNumber Span { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public StringNumber Order { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public StringNumber Offset { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public StringNumber Push { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public StringNumber Pull { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public OneOf<int, EmbeddedProperty> Xs { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public OneOf<int, EmbeddedProperty> Sm { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public OneOf<int, EmbeddedProperty> Md { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public OneOf<int, EmbeddedProperty> Lg { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public OneOf<int, EmbeddedProperty> Xl { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public OneOf<int, EmbeddedProperty> Xxl { get; set; }
2020-03-15 23:54:48 +08:00
[CascadingParameter]
public Row Row { get; set; }
2020-03-15 23:54:48 +08:00
private string _hostFlexStyle = null;
2020-03-15 23:54:48 +08:00
private string GutterStyle { get; set; }
2020-03-15 23:54:48 +08:00
internal void RowGutterChanged((int horizontalGutter, int verticalGutter) gutter)
{
GutterStyle = "";
2020-03-15 23:54:48 +08:00
if (gutter.horizontalGutter > 0)
{
GutterStyle = $"padding-left: {gutter.horizontalGutter / 2}px;padding-right: {gutter.horizontalGutter / 2}px;";
2020-03-15 23:54:48 +08:00
}
if (gutter.verticalGutter > 0)
{
GutterStyle += $"padding-top: {gutter.verticalGutter / 2}px;padding-bottom: {gutter.verticalGutter / 2}px;";
2020-03-15 23:54:48 +08:00
}
}
private void SetHostClassMap()
{
2020-04-24 18:32:50 +08:00
var prefixCls = "ant-col";
2020-03-15 23:54:48 +08:00
this.ClassMapper.Clear()
.Add(prefixCls)
.If($"{prefixCls}-{this.Span.Value}", () => this.Span.Value != null)
.If($"{prefixCls}-order-{this.Order.Value}", () => this.Order.Value != null)
.If($"{prefixCls}-offset-{this.Offset.Value}", () => this.Offset.Value != null)
.If($"{prefixCls}-pull-{this.Pull.Value}", () => this.Pull.Value != null)
.If($"{prefixCls}-push-{this.Push.Value}", () => this.Push.Value != null)
2020-03-15 23:54:48 +08:00
;
SetSizeClassMapper(prefixCls, Xs, "xs");
SetSizeClassMapper(prefixCls, Sm, "sm");
SetSizeClassMapper(prefixCls, Md, "md");
SetSizeClassMapper(prefixCls, Lg, "lg");
SetSizeClassMapper(prefixCls, Xl, "xl");
SetSizeClassMapper(prefixCls, Xxl, "xxl");
}
2020-03-15 23:54:48 +08:00
private void SetSizeClassMapper(string prefixCls, OneOf<int, EmbeddedProperty> parameter, string sizeName)
{
parameter.Switch(strNum =>
{
ClassMapper.If($"{prefixCls}-{sizeName}-{strNum}", () => strNum > 0);
}, embedded =>
{
ClassMapper
.If($"{prefixCls}-{sizeName}-{embedded.Span.Value}", () => embedded.Span.Value != null)
.If($"{prefixCls}-{sizeName}-order-{embedded.Order.Value}", () => embedded.Order.Value != null)
.If($"{prefixCls}-{sizeName}-offset-{embedded.Offset.Value}", () => embedded.Offset.Value != null)
.If($"{prefixCls}-{sizeName}-push-{embedded.Push.Value}", () => embedded.Push.Value != null)
.If($"{prefixCls}-{sizeName}-pull-{embedded.Pull.Value}", () => embedded.Pull.Value != null);
});
2020-03-15 23:54:48 +08:00
}
private void SetHostFlexStyle()
{
if (this.Flex.Value == null)
2020-03-15 23:54:48 +08:00
return;
this._hostFlexStyle = this.Flex.Match(str =>
2020-03-15 23:54:48 +08:00
{
if (Regex.Match(str, "^\\d+(\\.\\d+)?(px|em|rem|%)$").Success)
{
return $"0 0 {Flex}";
2020-03-15 23:54:48 +08:00
}
return Flex.AsT0;
2020-03-15 23:54:48 +08:00
},
num => $"{Flex} {Flex} auto");
2020-03-15 23:54:48 +08:00
}
protected override void OnInitialized()
{
this.Row?.Cols.Add(this);
2020-03-15 23:54:48 +08:00
this.SetHostClassMap();
this.SetHostFlexStyle();
base.OnInitialized();
2020-03-15 23:54:48 +08:00
}
2020-04-24 18:32:50 +08:00
protected override void Dispose(bool disposing)
2020-03-15 23:54:48 +08:00
{
this.Row?.Cols.Remove(this);
2020-03-15 23:54:48 +08:00
2020-04-24 18:32:50 +08:00
base.Dispose(disposing);
2020-03-15 23:54:48 +08:00
}
}
}