Skip to content

Commit 11d9d1a

Browse files
momijijinArgoZhang
andauthored
feat(ColorPicker): add IsSupportOpacity parameter (#4069)
* feat: Add new version of color picker associated file (file contents copied from old version) * feat: Implement simple hue slider and saturation/brightness Palette * feat: Add multiple color formats to display * feat: Added an extension to set the Alpha channel * feat: Adds a callback to send the last result after closing the Settings window * refactor: Move the mouse event to the js side for computation instead of sending it to blazor for computation and returning it to element * feat: Adds coordinates calculated based on the entered color values and modifies component positions * feat: Adjust the code format, fix some details, and add the corresponding usage document content * feat: Add tests for 'NeedAlpha' and 'FormatType' * refactor: 增加颜色拾取器脚本与样式 * chore: 增加脚本自动化 * feat: 更新组件 ColorPicker 组件支持透明度 * refactor: 更新样式 * feat: 增加默认值初始化逻辑 * doc: 更新示例 * refactor: 更新实现逻辑 * refactor: 实现透明度数值显示 * refactor: 更新 dom 结构 * revert: 撤销组件 * revert: 撤销示例 * revert: 撤销文档本地化更新 * test: 删除单元测试 * test: 增加单元测试 --------- Co-authored-by: Argo Zhang <[email protected]>
1 parent a7528e3 commit 11d9d1a

File tree

10 files changed

+145
-5
lines changed

10 files changed

+145
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,4 @@ src/**/wwwroot/**/uploader
381381
**/BootstrapBlazor/wwwroot/css/bootstrap.min.css
382382
**/BootstrapBlazor/wwwroot/css/sweetalert2.css
383383
**/BootstrapBlazor/wwwroot/css/motronic.min.css
384+
**/BootstrapBlazor/wwwroot/css/nano.min.css

src/BootstrapBlazor.Server/Components/Samples/ColorPickers.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
<h4>@Localizer["Description"]</h4>
77

8+
<DemoBlock Title="@Localizer["NormalTitle"]" Introduction="@Localizer["NormalIntro"]" Name="Normal">
9+
<ColorPicker Value="@Value" OnValueChanged="@OnColorChanged" IsSupportOpacity="true" />
10+
</DemoBlock>
11+
812
<DemoBlock Title="@Localizer["NormalTitle"]" Introduction="@Localizer["NormalIntro"]" Name="Normal">
913
<ColorPicker Value="@Value" OnValueChanged="@OnColorChanged" />
1014
<ConsoleLogger @ref="NormalLogger" />

src/BootstrapBlazor/Components/ColorPicker/ColorPicker.razor

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
@namespace BootstrapBlazor.Components
22
@inherits ValidateBase<string>
3+
@attribute [BootstrapModuleAutoLoader(JSObjectReference = true)]
34

45
@if (IsShowLabel)
56
{
67
<BootstrapLabel required="@Required" for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
78
}
8-
<div class="input-group color-picker">
9-
<input @attributes="@AdditionalAttributes" type="color" class="form-control form-control-color" disabled="@Disabled" value="@CurrentValueAsString" @onchange="EventCallback.Factory.CreateBinder<string>(this, async v => await Setter(v), CurrentValueAsString)" />
9+
<div class="input-group bb-color-picker">
10+
@if (IsSupportOpacity)
11+
{
12+
<div id="@Id" class="form-control form-control-color">
13+
<div class="bb-color-picker-body" style="--bb-color-pick-val: @CurrentValueAsString"></div>
14+
</div>
15+
}
16+
else
17+
{
18+
<input @attributes="@AdditionalAttributes" type="color" class="form-control form-control-color" disabled="@Disabled" value="@CurrentValueAsString" @onchange="EventCallback.Factory.CreateBinder<string>(this, async v => await Setter(v), CurrentValueAsString)" />
19+
}
1020

1121
@if (Template != null)
1222
{
1323
@Template(CurrentValueAsString)
1424
}
15-
else if(Formatter != null)
25+
else if (Formatter != null)
1626
{
1727
<input type="text" readonly class="@ClassName" value="@_formattedValueString" />
1828
}

src/BootstrapBlazor/Components/ColorPicker/ColorPicker.razor.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public partial class ColorPicker
2828
[Parameter]
2929
public Func<string, Task<string>>? Formatter { get; set; }
3030

31+
/// <summary>
32+
/// 获得/设置 是否支持透明度 默认 false 不支持
33+
/// </summary>
34+
[Parameter]
35+
public bool IsSupportOpacity { get; set; }
36+
3137
private string? _formattedValueString;
3238

3339
/// <summary>
@@ -40,6 +46,12 @@ protected override async Task OnParametersSetAsync()
4046
await FormatValue();
4147
}
4248

49+
/// <summary>
50+
/// <inheritdoc/>
51+
/// </summary>
52+
/// <returns></returns>
53+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new { IsSupportOpacity, Value });
54+
4355
private async Task Setter(string v)
4456
{
4557
CurrentValueAsString = v;
@@ -55,4 +67,17 @@ private async Task FormatValue()
5567
_formattedValueString = await Formatter(CurrentValueAsString);
5668
}
5769
}
70+
71+
/// <summary>
72+
/// 选中颜色值变化时回调此方法 由 JavaScript 调用
73+
/// </summary>
74+
/// <param name="value"></param>
75+
/// <returns></returns>
76+
[JSInvokable]
77+
public Task OnColorChanged(string value)
78+
{
79+
CurrentValueAsString = value;
80+
StateHasChanged();
81+
return Task.CompletedTask;
82+
}
5883
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import '../../lib/pickr/pickr.es5.min.js'
2+
import { addLink } from "../../modules/utility.js"
3+
import Data from "../../modules/data.js"
4+
5+
export async function init(id, invoke, options) {
6+
if (options.isSupportOpacity === true) {
7+
await addLink("./_content/BootstrapBlazor/css/nano.min.css");
8+
9+
const el = document.getElementById(id);
10+
const pickr = Pickr.create({
11+
el,
12+
theme: 'nano',
13+
default: options.value,
14+
useAsButton: true,
15+
swatches: [
16+
'rgba(244, 67, 54, 1)',
17+
'rgba(233, 30, 99, 0.95)',
18+
'rgba(156, 39, 176, 0.9)',
19+
'rgba(103, 58, 183, 0.85)',
20+
'rgba(63, 81, 181, 0.8)',
21+
'rgba(33, 150, 243, 0.75)',
22+
'rgba(3, 169, 244, 0.7)'
23+
],
24+
defaultRepresentation: 'HEXA',
25+
components: {
26+
preview: true,
27+
opacity: true,
28+
hue: true,
29+
30+
interaction: {
31+
hex: false,
32+
rgba: false,
33+
hsva: false,
34+
input: true,
35+
clear: true,
36+
save: true
37+
}
38+
}
39+
});
40+
41+
Data.set(id, { pickr });
42+
43+
pickr.on('save', (color, instance) => {
44+
instance.hide();
45+
invoke.invokeMethodAsync('OnColorChanged', formatColorString(color));
46+
}).on('swatchselect', color => {
47+
invoke.invokeMethodAsync('OnColorChanged', formatColorString(color));
48+
});
49+
}
50+
}
51+
52+
const formatColorString = color => {
53+
if (color === null) {
54+
return "#FFFFFF";
55+
}
56+
else {
57+
const hex = color.toRGBA();
58+
let val = `#${formatHexString(hex[0])}${formatHexString(hex[1])}${formatHexString(hex[2])}`;
59+
if (hex[3] !== 1) {
60+
val = `${val}${formatHexString(hex[3] * 255)}`;
61+
}
62+
return val.toUpperCase();
63+
}
64+
};
65+
66+
const formatHexString = hex => Math.round(hex).toString(16).padStart(2, '0');
67+
68+
export function dispose(id) {
69+
const data = Data.get(id);
70+
data.remove(id);
71+
if (data) {
72+
data.pickr.destroyAndRemove();
73+
}
74+
}

src/BootstrapBlazor/Components/ColorPicker/ColorPicker.razor.scss

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
.color-picker {
1+
.bb-color-picker {
22
.form-control-color {
33
max-width: 3rem;
44
}
5+
6+
.bb-color-picker-body {
7+
height: 100%;
8+
background-color: var(--bb-color-pick-val);
9+
border-radius: var(--bs-border-radius);
10+
}
511
}
612

7-
.input-group > .color-picker {
13+
.input-group > .bb-color-picker {
814
flex: 1;
915

1016
> .form-control-color {

src/BootstrapBlazor/Directory.Build.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<Message Text="Copy bootstrapblazor assets ..." Importance="high"></Message>
99
<Copy SourceFiles="$(MSBuildThisFileDirectory)wwwroot/lib/animate/animate.min.css" DestinationFiles="$(MSBuildThisFileDirectory)wwwroot/css/animate.min.css" SkipUnchangedFiles="true" ></Copy>
1010
<Copy SourceFiles="$(MSBuildThisFileDirectory)wwwroot/lib/swal/sweetalert2.css" DestinationFiles="$(MSBuildThisFileDirectory)wwwroot/css/sweetalert2.css" SkipUnchangedFiles="true" ></Copy>
11+
<Copy SourceFiles="$(MSBuildThisFileDirectory)wwwroot/lib/pickr/nano.min.css" DestinationFiles="$(MSBuildThisFileDirectory)wwwroot/css/nano.min.css" SkipUnchangedFiles="true" ></Copy>
1112

1213
<Message Text="Copy motronic theme ..." Importance="high"></Message>
1314
<Copy SourceFiles="$(MSBuildThisFileDirectory)wwwroot/src/css/motronic.css" DestinationFiles="$(MSBuildThisFileDirectory)wwwroot/css/motronic.min.css" SkipUnchangedFiles="true" ></Copy>

src/BootstrapBlazor/wwwroot/lib/pickr/nano.min.css

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BootstrapBlazor/wwwroot/lib/pickr/pickr.es5.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/UnitTest/Components/ColorPickerTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ public async Task Formatter_OK()
4949
await cut.InvokeAsync(() => input.Change("#000000"));
5050
cut.Contains("test-color-value#000000");
5151
}
52+
53+
[Fact]
54+
public async Task IsSupportOpacity_Ok()
55+
{
56+
var cut = Context.RenderComponent<ColorPicker>(builder =>
57+
{
58+
builder.Add(a => a.IsSupportOpacity, true);
59+
builder.Add(a => a.Value, "#AABBCCDD");
60+
});
61+
cut.Contains("<div class=\"bb-color-picker-body\" style=\"--bb-color-pick-val: #AABBCCDD\"></div>");
62+
63+
await cut.InvokeAsync(() => cut.Instance.OnColorChanged("#123456"));
64+
Assert.Equal("#123456", cut.Instance.Value);
65+
}
5266
}

0 commit comments

Comments
 (0)