Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BarcodeGenerator): redesign BarcodeGenerator component #56

Merged
merged 11 commits into from
Oct 14, 2024
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
7 changes: 4 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ jobs:
NUGET_API_KEY: ${{secrets.NUGET_API_KEY}}
Bundle: True
run: |
dotnet restore src/BootstrapBlazor --no-cache
dotnet build src/BootstrapBlazor
dotnet restore --no-cache
dotnet build

- name: Test
run: |
dotnet test test/UnitTest --collect:"XPlat Code Coverage"
dotnet test ./test/UnitTestEditor --collect:"XPlat Code Coverage"
dotnet test ./test/UnitTestHoliday --collect:"XPlat Code Coverage"

- name: Upload to Codecov
uses: codecov/codecov-action@v4
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@namespace BootstrapBlazor.Components
@inherits BootstrapModuleComponentBase
@attribute [JSModuleAutoLoader("./_content/BootstrapBlazor.BarcodeGenerator/BarcodeGenerator.razor.js", AutoInvokeInit = false, AutoInvokeDispose = false)]

<span @attributes="AdditionalAttributes" id="@Id" class="@ClassString">
<svg></svg>
</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using Microsoft.AspNetCore.Components;

namespace BootstrapBlazor.Components;

/// <summary>
/// 条码生成器组件
/// </summary>
public partial class BarcodeGenerator
{
private string? _value;
private BarcodeGeneratorOption _options = new();

/// <summary>
/// 获得/设置 条码值
/// </summary>
[Parameter]
public string? Value { get; set; }

/// <summary>
/// 获得/设置 <see cref="BarcodeGeneratorOption"/> 实例值
/// </summary>
[Parameter]
public BarcodeGeneratorOption? Options { get; set; }

/// <summary>
/// 获得/设置 条码生成回调方法
/// </summary>
[Parameter]
public Func<string?, Task>? OnCompletedAsync { get; set; }

private string? ClassString => CssBuilder.Default("bb-barcode")
.AddClassFromAttributes(AdditionalAttributes)
.Build();

/// <summary>
/// <inheritdoc/>
/// </summary>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

var render = false;
if (_value != Value)
{
_value = Value;
render = true;
}
if (_options.DifferAndAssign(Options))
{
render = true;
}

if (render)
{
var result = await GenerateBarCode(Value, Options);
if (OnCompletedAsync != null)
{
await OnCompletedAsync(result);
}
}
}

/// <summary>
/// 生成条码方法
/// </summary>
/// <param name="value"></param>
/// <param name="options"></param>
/// <returns></returns>
public async Task<string?> Generate(string? value = null, BarcodeGeneratorOption? options = null)
{
if (_value != value)
{
_value = Value = value;
}

return await GenerateBarCode(_value, options);
}

private Task<string?> GenerateBarCode(string? value, BarcodeGeneratorOption? options = null) => InvokeAsync<string?>("generate", Id, value, options);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import './JsBarcode.all.min.js';

export function generate(id, value, options) {
const el = document.getElementById(id);
if (el === null) {
return;
}

if (typeof (JsBarcode) !== 'function') {
console.error('import JsBarcode failed');
}

const svg = el.querySelector("svg");
JsBarcode(svg, value, options);
return svg.outerHTML;
}
Loading
Loading