Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.3.1-beta10</Version>
<Version>9.3.1-beta11</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
23 changes: 21 additions & 2 deletions src/BootstrapBlazor/Components/IFrame/IFrame.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public partial class IFrame
[Parameter]
public Func<object?, Task>? OnPostDataAsync { get; set; }

/// <summary>
/// 获得/设置 页面加载完毕后回调方法
/// </summary>
[Parameter]
public Func<Task>? OnReadyAsync { get; set; }

private string? ClassString => CssBuilder.Default("bb-frame")
.AddClassFromAttributes(AdditionalAttributes)
.Build();
Expand All @@ -53,7 +59,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(CallbackAsync));
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(TriggerPostData));

/// <summary>
/// 推送数据方法
Expand All @@ -68,11 +74,24 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
/// <param name="data"></param>
/// <returns></returns>
[JSInvokable]
public async Task CallbackAsync(object? data)
public async Task TriggerPostData(object? data)
{
if (OnPostDataAsync != null)
{
await OnPostDataAsync(data);
}
}

/// <summary>
/// 由 JavaScript 调用
/// </summary>
/// <returns></returns>
[JSInvokable]
public async Task TriggerLoaded()
{
if (OnReadyAsync != null)
{
await OnReadyAsync();
}
}
}
23 changes: 9 additions & 14 deletions src/BootstrapBlazor/Components/IFrame/IFrame.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@ export function init(id, invoke, callback) {
}
Data.set(id, handler)

window.addEventListener('message', handler)
}
window.addEventListener('message', handler);
const frame = document.getElementById(id);

export function execute(id, data) {
const frame = document.getElementById(id)
if (frame) {
if (frame.loaded) {
frame.contentWindow.postMessage(data)
}
else {
frame.onload = () => {
frame.loaded = true
frame.contentWindow.postMessage(data)
}
}
frame.onload = () => {
invoke.invokeMethodAsync("TriggerLoaded");
}
}

export async function execute(id, data) {
const frame = document.getElementById(id);
frame.contentWindow.postMessage(data);
}

export function dispose(id) {
const handler = Data.get(id)
Data.remove(id)
Expand Down
22 changes: 19 additions & 3 deletions test/UnitTest/Components/IFrameTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace UnitTest.Components;
public class IFrameTest : BootstrapBlazorTestBase
{
[Fact]
public void Frame_Ok()
public async Task Frame_Ok()
{
var postData = false;
var cut = Context.RenderComponent<IFrame>(pb =>
Expand All @@ -27,10 +27,26 @@ public void Frame_Ok()
pb.Add(a => a.Data, new { Rows = new List<string>() { "1", "2" } });
});

cut.InvokeAsync(async () =>
await cut.InvokeAsync(async () =>
{
await cut.Instance.CallbackAsync(new List<string> { "2", "3" });
await cut.Instance.TriggerPostData(new List<string> { "2", "3" });
Assert.True(postData);
});

var loaded = false;
cut.SetParametersAndRender(pb =>
{
pb.Add(a => a.OnReadyAsync, () =>
{
loaded = true;
return Task.CompletedTask;
});
});

await cut.InvokeAsync(async () =>
{
await cut.Instance.TriggerLoaded();
});
Assert.True(loaded);
}
}