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

151 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;
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
}
}
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)
.GetIf(() => $"{prefixCls}-{this.Span.Value}", () => this.Span.Value != null)
.GetIf(() => $"{prefixCls}-order-{this.Order.Value}", () => this.Order.Value != null)
.GetIf(() => $"{prefixCls}-offset-{this.Offset.Value}", () => this.Offset.Value != null)
.GetIf(() => $"{prefixCls}-pull-{this.Pull.Value}", () => this.Pull.Value != null)
.GetIf(() => $"{prefixCls}-push-{this.Push.Value}", () => this.Push.Value != null)
.If($"{prefixCls}-rtl", () => RTL)
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
.GetIf(() => $"{prefixCls}-{sizeName}-{embedded.Span.Value}", () => embedded.Span.Value != null)
.GetIf(() => $"{prefixCls}-{sizeName}-order-{embedded.Order.Value}", () => embedded.Order.Value != null)
.GetIf(() => $"{prefixCls}-{sizeName}-offset-{embedded.Offset.Value}", () => embedded.Offset.Value != null)
.GetIf(() => $"{prefixCls}-{sizeName}-push-{embedded.Push.Value}", () => embedded.Push.Value != null)
.GetIf(() => $"{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 $"flex: 0 0 {str}";
2020-03-15 23:54:48 +08:00
}
return $"flex: {str}";
2020-03-15 23:54:48 +08:00
},
num => $"flex: {num} {num} auto");
2020-03-15 23:54:48 +08:00
}
protected override void OnInitialized()
{
this.Row?.AddCol(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?.RemoveCol(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
}
}
}