ant-design-blazor/components/drawer/DrawerRef.cs
zxyao 31aac9f914 refactor: unified use of FeedbackComponent for modal comfirm and drawer (#1263)
* refactor: support to use the same template for confirm and modal

* refactor: support to use the same template for drawer

* refactor: separate interface IOkCancelRef

* chore: modify EventUtil class summary

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-04-04 15:40:54 +08:00

111 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Threading.Tasks;
namespace AntDesign
{
public class DrawerRef : FeedbackRefBase
{
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Please replace it with Config")]
public DrawerOptions Options => Config;
public DrawerOptions Config { get; private set; }
public Drawer Drawer { get; set; }
public Func<ModalClosingEventArgs, Task> OnClosing { get; set; }
protected readonly DrawerService _service;
internal DrawerRef(DrawerOptions options, DrawerService service)
{
Config = options;
_service = service;
}
/// <summary>
/// close Confirm dialog
/// </summary>
/// <returns></returns>
public override async Task CloseAsync()
{
var e = new ModalClosingEventArgs();
await (OnClosing?.Invoke(e) ?? Task.CompletedTask);
if (!e.Cancel)
{
await _service.CloseAsync(this);
if (OnClose != null)//before close
await OnClose.Invoke();
}
}
/// <summary>
/// Open Confirm dialog
/// </summary>
/// <returns></returns>
public override async Task OpenAsync()
{
await _service.OpenAsync(this);
if (OnOpen != null)
await OnOpen.Invoke();
}
/// <summary>
/// update Confirm dialog config which Visible=true
/// </summary>
/// <returns></returns>
public override async Task UpdateConfigAsync()
{
await (_service?.UpdateAsync(this) ?? Task.CompletedTask);
}
/// <summary>
/// update Confirm dialog config with a new ConfirmOptions
/// </summary>
/// <param name="config"></param>
/// <returns></returns>
public async Task UpdateConfigAsync(DrawerOptions config)
{
Config = config;
await UpdateConfigAsync();
}
}
public class DrawerRef<TResult> : DrawerRef
{
internal TaskCompletionSource<TResult> TaskCompletionSource { get; set; }
public Func<TResult, Task> OnClosed { get; set; }
internal DrawerRef(DrawerOptions options, DrawerService service) :base(options,service)
{
}
/// <summary>
/// 关闭抽屉
/// </summary>
/// <returns></returns>
public async Task CloseAsync(TResult result)
{
var closeEventArgs = new ModalClosingEventArgs();
if (OnClosing != null)//before close
await OnClosing.Invoke(closeEventArgs);
if (closeEventArgs.Cancel)
return;
await _service.CloseAsync(this);
if (OnClosed != null)//after close
await OnClosed.Invoke(result);
TaskCompletionSource?.SetResult(result);
}
}
}