Skip to content

Commit 95711bb

Browse files
authored
refactor(Typed): override Equals method (#5293)
* refactor: 重构代码 * test: 更新单元测试 * refactor: 更新代码
1 parent a2756ed commit 95711bb

File tree

3 files changed

+89
-21
lines changed

3 files changed

+89
-21
lines changed

src/BootstrapBlazor/Components/Typed/Typed.razor.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public partial class Typed
2828
[Parameter]
2929
public Func<Task>? OnCompleteAsync { get; set; }
3030

31-
private string? _lastOptions;
32-
3331
private string? _text;
3432

33+
private TypedOptions? _options;
34+
3535
/// <summary>
3636
/// <inheritdoc/>
3737
/// </summary>
@@ -43,8 +43,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
4343

4444
if (firstRender)
4545
{
46-
_lastOptions = Options?.ToString();
4746
_text = Text;
47+
_options = Options;
4848
}
4949
else if (UpdateParameters())
5050
{
@@ -82,24 +82,12 @@ private bool UpdateParameters()
8282
return true;
8383
}
8484

85-
var optionString = GetOptionsString();
86-
if (string.Equals(optionString, _lastOptions, StringComparison.Ordinal))
85+
if (Options?.Equals(_options) ?? false)
8786
{
88-
return false;
89-
}
90-
91-
_lastOptions = optionString;
92-
return true;
93-
}
94-
95-
private string? GetOptionsString()
96-
{
97-
if (Options == null)
98-
{
99-
return null;
87+
return true;
10088
}
10189

102-
var textString = Options.Text == null ? "" : string.Join(",", Options.Text);
103-
return $"{Options} {textString}";
90+
_options = Options;
91+
return false;
10492
}
10593
}

src/BootstrapBlazor/Components/Typed/TypedOptions.cs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ namespace BootstrapBlazor.Components;
1010
/// <summary>
1111
/// TypedJs 组件配置类
1212
/// </summary>
13-
public record TypedOptions
13+
public class TypedOptions : IEquatable<TypedOptions>
1414
{
1515
/// <summary>
1616
/// 获得/设置 要打字的字符串数组
1717
/// </summary>
1818
[JsonPropertyName("strings")]
1919
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
20-
public List<string?>? Text { get; set; }
20+
public List<string>? Text { get; set; }
2121

2222
/// <summary>
2323
/// 获得/设置 打字速度 默认 null 未设置 单位毫秒
@@ -88,4 +88,67 @@ public record TypedOptions
8888
[JsonPropertyName("contentType")]
8989
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
9090
public string? ContentType { get; set; }
91+
92+
/// <summary>
93+
/// <inheritdoc/>
94+
/// </summary>
95+
/// <param name="option"></param>
96+
/// <returns></returns>
97+
public bool Equals(TypedOptions? option)
98+
{
99+
if (option == null)
100+
{
101+
return false;
102+
}
103+
104+
return EqualText(option.Text) &&
105+
TypeSpeed == option.TypeSpeed &&
106+
BackSpeed == option.BackSpeed &&
107+
SmartBackspace == option.SmartBackspace &&
108+
Shuffle == option.Shuffle &&
109+
BackDelay == option.BackDelay &&
110+
Loop == option.Loop &&
111+
LoopCount == option.LoopCount &&
112+
ShowCursor == option.ShowCursor &&
113+
CursorChar == option.CursorChar &&
114+
ContentType == option.ContentType;
115+
}
116+
117+
private bool EqualText(List<string>? text)
118+
{
119+
if (Text == null && text == null)
120+
{
121+
return true;
122+
}
123+
if (Text == null || text == null)
124+
{
125+
return false;
126+
}
127+
if (Text.Count != text.Count)
128+
{
129+
return false;
130+
}
131+
return Text.SequenceEqual(text);
132+
}
133+
134+
/// <summary>
135+
/// <inheritdoc/>
136+
/// </summary>
137+
/// <param name="obj"></param>
138+
/// <returns></returns>
139+
public override bool Equals(object? obj)
140+
{
141+
if (obj is TypedOptions option)
142+
{
143+
return Equals(option);
144+
}
145+
146+
return false;
147+
}
148+
149+
/// <summary>
150+
/// <inheritdoc/>
151+
/// </summary>
152+
/// <returns></returns>
153+
public override int GetHashCode() => base.GetHashCode();
91154
}

test/UnitTest/Components/TypedTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,21 @@ public void TypedOptions_Ok()
106106
Assert.Equal("|", options.CursorChar);
107107
Assert.Equal("html", options.ContentType);
108108
}
109+
110+
[Fact]
111+
public void Equal_Ok()
112+
{
113+
var options = new TypedOptions() { Text = ["test1", "test2"], TypeSpeed = 70 };
114+
Assert.False(options.Equals(null));
115+
116+
var options2 = new TypedOptions() { Text = ["test1", "test2", "test3"], TypeSpeed = 70 };
117+
Assert.False(options.Equals(options2));
118+
119+
var options3 = new TypedOptions() { Text = ["test1", "test2"], TypeSpeed = 70 };
120+
Assert.True(options.Equals(options3));
121+
122+
Assert.True(options.Equals((object)options3));
123+
Assert.False(options.Equals(new object()));
124+
Assert.True(options.GetHashCode() > 0);
125+
}
109126
}

0 commit comments

Comments
 (0)