Skip to content

Commit 901ad06

Browse files
authored
feat(SUrlTextField): improve URL handling in Value property (#768)
Updated the setter of the `Value` property to better handle input URLs. Now, if the input is null or empty, it sets `_protocol` to "https" and clears `TextValue`. If the input starts with "https://" or "http://", it updates `_protocol` and `TextValue` accordingly; otherwise, it retains the original protocol and only updates `TextValue`. Simplified the `ValueTextUpdateAsync` method to return an empty string for null or whitespace input, and constructs the full URL for valid input, invoking `ValueChanged` with the complete URL.
1 parent 164524e commit 901ad06

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/Masa.Stack.Components/Shared/IntegrationComponents/SUrlTextField.razor.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,27 @@ public string Value
2222
get => $"{Protocol}://{TextValue}";
2323
set
2424
{
25-
if (string.IsNullOrEmpty(Value) is false)
25+
if (string.IsNullOrEmpty(value))
2626
{
27-
if (value.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
28-
{
29-
_protocol = "https";
30-
TextValue = value.TrimStart("https://".ToCharArray());
31-
}
32-
else if (value.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
33-
{
34-
_protocol = "http";
35-
TextValue = value.TrimStart("http://".ToCharArray());
36-
}
37-
else
38-
{
39-
_protocol = "https";
40-
TextValue = "";
41-
}
27+
_protocol = "https";
28+
TextValue = "";
29+
return;
4230
}
43-
else
31+
32+
if (value.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
4433
{
4534
_protocol = "https";
46-
TextValue = "";
35+
TextValue = value.Substring("https://".Length);
36+
}
37+
else if (value.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
38+
{
39+
_protocol = "http";
40+
TextValue = value.Substring("http://".Length);
41+
}
42+
else
43+
{
44+
// 如果输入的值不以协议开头,保留原有的协议,只更新文本部分
45+
TextValue = value;
4746
}
4847
}
4948
}
@@ -66,17 +65,15 @@ public string Value
6665

6766
public async Task ValueTextUpdateAsync(string value)
6867
{
69-
if (!string.IsNullOrWhiteSpace(value))
70-
{
71-
value = $"{Protocol}://{value.TrimStart("https://".ToArray()).TrimStart("http://".ToArray())}";
72-
}
68+
string fullUrl = string.IsNullOrWhiteSpace(value) ? "" : $"{Protocol}://{value}";
69+
7370
if (ValueChanged.HasDelegate)
7471
{
75-
await ValueChanged.InvokeAsync(value);
72+
await ValueChanged.InvokeAsync(fullUrl);
7673
}
7774
else
7875
{
79-
Value = value;
76+
Value = fullUrl;
8077
}
8178
}
8279

0 commit comments

Comments
 (0)