|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +using Microsoft.AspNetCore.Components.Rendering; |
| 7 | +using Microsoft.AspNetCore.Components.Web; |
| 8 | + |
| 9 | +namespace BootstrapBlazor.Components; |
| 10 | + |
| 11 | +class TabHeader : IComponent |
| 12 | +{ |
| 13 | + [Parameter] |
| 14 | + public string? Url { get; set; } |
| 15 | + |
| 16 | + [Parameter] |
| 17 | + public string? Class { get; set; } |
| 18 | + |
| 19 | + [Parameter] |
| 20 | + public Func<Task>? OnClick { get; set; } |
| 21 | + |
| 22 | + [Parameter] |
| 23 | + public RenderFragment? ChildContent { get; set; } |
| 24 | + |
| 25 | + private RenderHandle _renderHandle; |
| 26 | + |
| 27 | + void IComponent.Attach(RenderHandle renderHandle) |
| 28 | + { |
| 29 | + _renderHandle = renderHandle; |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// <inheritdoc/> |
| 34 | + /// </summary> |
| 35 | + /// <param name="parameters"></param> |
| 36 | + /// <returns></returns> |
| 37 | + Task IComponent.SetParametersAsync(ParameterView parameters) |
| 38 | + { |
| 39 | + parameters.SetParameterProperties(this); |
| 40 | + |
| 41 | + RenderContent(); |
| 42 | + return Task.CompletedTask; |
| 43 | + } |
| 44 | + private void RenderContent() |
| 45 | + { |
| 46 | + _renderHandle.Render(BuildRenderTree); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// <inheritdoc/> |
| 51 | + /// </summary> |
| 52 | + /// <param name="builder"></param> |
| 53 | + private void BuildRenderTree(RenderTreeBuilder builder) |
| 54 | + { |
| 55 | + builder.OpenElement(0, "a"); |
| 56 | + builder.AddAttribute(10, "class", Class); |
| 57 | + builder.AddAttribute(20, "href", Url); |
| 58 | + builder.AddAttribute(30, "onclick", EventCallback.Factory.Create<MouseEventArgs>(this, e => OnClickCallback())); |
| 59 | + builder.AddContent(40, ChildContent); |
| 60 | + builder.CloseElement(); |
| 61 | + } |
| 62 | + |
| 63 | + private async Task OnClickCallback() |
| 64 | + { |
| 65 | + if (OnClick != null) |
| 66 | + { |
| 67 | + await OnClick(); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments