Skip to content

Commit 6ea1cbd

Browse files
committed
refactor: 重构代码
1 parent a5cecd4 commit 6ea1cbd

File tree

2 files changed

+32
-45
lines changed

2 files changed

+32
-45
lines changed

src/BootstrapBlazor/Components/Dialog/Dialog.razor

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@namespace BootstrapBlazor.Components
22
@inherits BootstrapComponentBase
33

4-
<Modal @ref="ModalContainer" IsBackdrop="_isBackdrop" IsKeyboard="@_isKeyboard" IsFade="@_isFade"
5-
OnShownAsync="@OnShownAsync" OnCloseAsync="OnCloseAsync">
4+
<Modal @ref="_modal" IsBackdrop="_isBackdrop" IsKeyboard="@_isKeyboard" IsFade="@_isFade"
5+
OnShownAsync="@_onShownAsync" OnCloseAsync="@_onCloseAsync">
66
@for (var index = 0; index < DialogParameters.Keys.Count; index++)
77
{
88
@RenderDialog(index, DialogParameters.Keys.ElementAt(index))

src/BootstrapBlazor/Components/Dialog/Dialog.razor.cs

+30-43
Original file line numberDiff line numberDiff line change
@@ -6,94 +6,81 @@
66
namespace BootstrapBlazor.Components;
77

88
/// <summary>
9-
/// Dialog 对话框组件
9+
/// Dialog component
1010
/// </summary>
1111
public partial class Dialog : IDisposable
1212
{
13-
/// <summary>
14-
/// 获得/设置 Modal 容器组件实例
15-
/// </summary>
16-
[NotNull]
17-
private Modal? ModalContainer { get; set; }
18-
19-
/// <summary>
20-
/// 获得/设置 弹出对话框实例集合
21-
/// </summary>
22-
private Dictionary<Dictionary<string, object>, (bool IsKeyboard, bool IsBackdrop)> DialogParameters { get; } = [];
23-
24-
private bool _isKeyboard = false;
25-
26-
private bool _isBackdrop = false;
27-
28-
private bool _isFade = true;
29-
30-
/// <summary>
31-
/// DialogServices 服务实例
32-
/// </summary>
3313
[Inject]
3414
[NotNull]
3515
private DialogService? DialogService { get; set; }
3616

3717
[NotNull]
38-
private Func<Task>? OnShownAsync { get; set; }
18+
private Modal? _modal = null;
3919

4020
[NotNull]
41-
private Func<Task>? OnCloseAsync { get; set; }
21+
private Func<Task>? _onShownAsync = null;
4222

43-
private Dictionary<string, object>? CurrentParameter { get; set; }
23+
[NotNull]
24+
private Func<Task>? _onCloseAsync = null;
25+
26+
private readonly Dictionary<Dictionary<string, object>, (bool IsKeyboard, bool IsBackdrop)> DialogParameters = [];
27+
private Dictionary<string, object>? _currentParameter;
28+
private bool _isKeyboard = false;
29+
private bool _isBackdrop = false;
30+
private bool _isFade = true;
4431

4532
/// <summary>
46-
/// OnInitialized 方法
33+
/// <inheritdoc/>
4734
/// </summary>
4835
protected override void OnInitialized()
4936
{
5037
base.OnInitialized();
5138

52-
// 注册 Dialog 弹窗事件
39+
// Register Dialog popup event
5340
DialogService.Register(this, Show);
5441
}
5542

5643
/// <summary>
57-
/// OnAfterRenderAsync 方法
44+
/// <inheritdoc/>
5845
/// </summary>
5946
/// <param name="firstRender"></param>
6047
/// <returns></returns>
6148
protected override async Task OnAfterRenderAsync(bool firstRender)
6249
{
6350
await base.OnAfterRenderAsync(firstRender);
6451

65-
if (CurrentParameter != null)
52+
if (_currentParameter != null)
6653
{
67-
await ModalContainer.Show();
54+
await _modal.Show();
6855
}
6956
}
7057

7158
private async Task Show(DialogOption option)
7259
{
73-
OnShownAsync = async () =>
60+
_onShownAsync = async () =>
7461
{
7562
if (option.OnShownAsync != null)
7663
{
7764
await option.OnShownAsync();
7865
}
7966
};
8067

81-
OnCloseAsync = async () =>
68+
_onCloseAsync = async () =>
8269
{
83-
// 回调 OnCloseAsync
70+
// Callback OnCloseAsync
8471
if (option.OnCloseAsync != null)
8572
{
8673
await option.OnCloseAsync();
8774
}
8875

89-
// 移除当前 DialogParameter
90-
if (CurrentParameter != null)
76+
// Remove current DialogParameter
77+
if (_currentParameter != null)
9178
{
92-
DialogParameters.Remove(CurrentParameter);
79+
DialogParameters.Remove(_currentParameter);
9380

94-
// 多弹窗支持
81+
// Support for multiple dialogs
9582
var p = DialogParameters.LastOrDefault();
96-
CurrentParameter = p.Key;
83+
_currentParameter = p.Key;
9784
_isKeyboard = p.Value.IsKeyboard;
9885
_isBackdrop = p.Value.IsBackdrop;
9986

@@ -105,7 +92,7 @@ private async Task Show(DialogOption option)
10592
_isBackdrop = option.IsBackdrop;
10693
_isFade = option.IsFade;
10794

108-
option.Modal = ModalContainer;
95+
option.Modal = _modal;
10996

11097
var parameters = option.ToAttributes();
11198
var content = option.BodyTemplate ?? option.Component?.Render();
@@ -166,10 +153,10 @@ private async Task Show(DialogOption option)
166153
}
167154
}
168155

169-
// 保存当前 Dialog 参数
170-
CurrentParameter = parameters;
156+
// Save current Dialog parameters
157+
_currentParameter = parameters;
171158

172-
// 添加 ModalDialog 到容器中
159+
// Add ModalDialog to the container
173160
DialogParameters.Add(parameters, (_isKeyboard, _isBackdrop));
174161
await InvokeAsync(StateHasChanged);
175162
}
@@ -183,7 +170,7 @@ private static RenderFragment RenderDialog(int index, Dictionary<string, object>
183170
};
184171

185172
/// <summary>
186-
/// Dispose 方法
173+
/// Dispose method
187174
/// </summary>
188175
/// <param name="disposing"></param>
189176
protected virtual void Dispose(bool disposing)
@@ -195,7 +182,7 @@ protected virtual void Dispose(bool disposing)
195182
}
196183

197184
/// <summary>
198-
/// Dispose 方法
185+
/// <inheritdoc/>
199186
/// </summary>
200187
public void Dispose()
201188
{

0 commit comments

Comments
 (0)