fix(module: table): support more generic units for scroll x/y (#1137)

This commit is contained in:
James Yeung 2021-02-14 06:15:53 -06:00 committed by GitHub
parent d98c851c70
commit 597133363a
4 changed files with 72 additions and 43 deletions

View File

@ -22,27 +22,39 @@ namespace AntDesign
Vmin,
Vmax,
Fr,
Calc,
}
public readonly struct CssSizeLength : IEquatable<CssSizeLength>
{
public int Value => _value;
public int Value => _value ?? 0;
public string StringValue => _stringValue;
internal CssSizeLengthUnit Unit => _unit;
private readonly int _value;
private readonly int? _value;
private readonly string _stringValue;
private readonly CssSizeLengthUnit _unit;
public override string ToString() => _value.ToString(CultureInfo.InvariantCulture) + _unit switch
public override string ToString()
{
CssSizeLengthUnit.Percent => "%",
_ => Enum.GetName(typeof(CssSizeLengthUnit), _unit).ToLowerInvariant()
};
var intValue = _value?.ToString(CultureInfo.InvariantCulture);
var unit = _unit switch
{
CssSizeLengthUnit.Percent => "%",
CssSizeLengthUnit.Calc => "",
_ => Enum.GetName(typeof(CssSizeLengthUnit), _unit).ToLowerInvariant()
};
return $"{intValue ?? StringValue}{unit}";
}
private CssSizeLength(int value, CssSizeLengthUnit unit)
{
_value = value;
_stringValue = null;
_unit = unit;
}
@ -53,6 +65,15 @@ namespace AntDesign
public CssSizeLength(string value)
{
value = value?.ToLowerInvariant() ?? throw new ArgumentNullException(nameof(value));
_stringValue = value;
if (value.StartsWith("calc", StringComparison.OrdinalIgnoreCase))
{
_stringValue = value;
_value = null;
_unit = CssSizeLengthUnit.Calc;
return;
}
var index = value
.Select((c, i) => ((char c, int i)?)(c, i))
@ -70,6 +91,7 @@ namespace AntDesign
if (index == value.Length)
{
_unit = CssSizeLengthUnit.Px;
_stringValue = null;
return;
}

View File

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components;
@ -67,40 +68,9 @@ namespace AntDesign
protected bool AppendExpandColumn => Table.HasExpandTemplate && ColIndex == (Table.TreeMode ? Table.TreeExpandIconColumnIndex : Table.ExpandIconColumnIndex);
protected string FixedStyle
{
get
{
if (Fixed == null || Context == null)
{
return "";
}
private string _fixedStyle;
var fixedWidth = 0;
if (Fixed == "left" && Context?.Columns.Count >= ColIndex)
{
for (int i = 0; i < ColIndex; i++)
{
fixedWidth += ((CssSizeLength)Context?.Columns[i].Width).Value;
}
}
else if (Fixed == "right")
{
for (int i = (Context?.Columns.Count ?? 1) - 1; i > ColIndex; i--)
{
fixedWidth += ((CssSizeLength)Context?.Columns[i].Width).Value;
}
}
if (IsHeader && Table.ScrollY != null && Table.ScrollX != null && Fixed == "right")
{
fixedWidth += Table.ScrollBarWidth;
}
return $"position: sticky; {Fixed}: {(CssSizeLength)fixedWidth};";
}
}
protected string FixedStyle => _fixedStyle;
private void SetClass()
{
@ -150,6 +120,11 @@ namespace AntDesign
Context?.AddRowColumn(this);
}
if (IsHeader || IsBody)
{
_fixedStyle = CalcFixedStyle();
}
SetClass();
}
@ -161,5 +136,37 @@ namespace AntDesign
}
base.Dispose(disposing);
}
private string CalcFixedStyle()
{
if (Fixed == null || Context == null)
{
return "";
}
var fixedWidths = Array.Empty<string>();
if (Fixed == "left" && Context?.Columns.Count >= ColIndex)
{
for (int i = 0; i < ColIndex; i++)
{
fixedWidths = fixedWidths.Append($"{(CssSizeLength)Context?.Columns[i].Width}");
}
}
else if (Fixed == "right")
{
for (int i = (Context?.Columns.Count ?? 1) - 1; i > ColIndex; i--)
{
fixedWidths = fixedWidths.Append($"{(CssSizeLength)Context?.Columns[i].Width}");
}
}
if (IsHeader && Table.ScrollY != null && Table.ScrollX != null && Fixed == "right")
{
fixedWidths = fixedWidths.Append($"{(CssSizeLength)Table.ScrollBarWidth}");
}
return $"position: sticky; {Fixed}: {(fixedWidths.Any() ? $"calc({string.Join(" + ", fixedWidths) })" : "0px")};";
}
}
}

View File

@ -64,10 +64,10 @@ namespace AntDesign
if (_table.ScrollX != null && Columns.Any(x => x.Width == null))
{
var zeroWidthCols = Columns.Where(x => x.Width == null).ToArray();
var totalWidth = Columns.Where(x => x.Width != null).Sum(x => ((CssSizeLength)x.Width).Value);
var totalWidth = string.Join(" + ", Columns.Where(x => x.Width != null).Select(x => (CssSizeLength)x.Width));
foreach (var col in Columns.Where(x => x.Width == null))
{
col.Width = $"{(((CssSizeLength)_table.ScrollX).Value - totalWidth + 3) / zeroWidthCols.Length}";
col.Width = $"calc(({(CssSizeLength)_table.ScrollX} - ({totalWidth}) + 3px) / {zeroWidthCols.Length})";
}
}

View File

@ -16,7 +16,7 @@ namespace AntDesign.TableModels
public string FieldName { get; }
public string Sort => _sortDirection.Name;
public string Sort => _sortDirection?.Name;
SortDirection ITableSortModel.SortDirection => _sortDirection;