-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BarcodeGenerator): redesign BarcodeGenerator component (#56)
* chore: 更新脚本 * refactor: 更新组件名称 * refactor: 规范组件名称 * chore: 更新依赖包到最新 * refactor: 重构 BarcodeGeneratorOption 配置类 * refactor: 增加扩展方法判断配置是否改变 * refactor: 精简代码 * refactor: 重构脚本 * chore: bump version 8.1.0 * chore: bump version 8.1.0
- Loading branch information
Showing
18 changed files
with
603 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
src/components/BootstrapBlazor.BarcodeGenerator/BarCodeGenerator.razor
This file was deleted.
Oops, something went wrong.
142 changes: 0 additions & 142 deletions
142
src/components/BootstrapBlazor.BarcodeGenerator/BarCodeGenerator.razor.cs
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/components/BootstrapBlazor.BarcodeGenerator/BarCodeGenerator.razor.js
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
src/components/BootstrapBlazor.BarcodeGenerator/BarcodeGenerator.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
84 changes: 84 additions & 0 deletions
84
src/components/BootstrapBlazor.BarcodeGenerator/BarcodeGenerator.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/components/BootstrapBlazor.BarcodeGenerator/BarcodeGenerator.razor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.