From 41b77142291d97e50920e81ae47ffb70ad86f665 Mon Sep 17 00:00:00 2001 From: leekelleher Date: Mon, 15 Apr 2024 18:22:30 +0100 Subject: [PATCH] ColorPicker: Prefixes values with a hash in property-editor component, not the input component. The assumption is that the color hex values would already have a hash prefix. --- .../input-color/input-color.element.ts | 22 +++++------- ...property-editor-ui-color-picker.element.ts | 34 ++++++++++++++----- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/components/input-color/input-color.element.ts b/src/Umbraco.Web.UI.Client/src/packages/core/components/input-color/input-color.element.ts index b616c98dfd..9f3be5034e 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/components/input-color/input-color.element.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/components/input-color/input-color.element.ts @@ -1,7 +1,7 @@ import { html, customElement, property, map, nothing } from '@umbraco-cms/backoffice/external/lit'; -import { UUIFormControlMixin } from '@umbraco-cms/backoffice/external/uui'; import { UmbChangeEvent } from '@umbraco-cms/backoffice/event'; import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; +import { UUIFormControlMixin } from '@umbraco-cms/backoffice/external/uui'; import type { UmbSwatchDetails } from '@umbraco-cms/backoffice/models'; import type { UUIColorSwatchesEvent } from '@umbraco-cms/backoffice/external/uui'; @@ -11,25 +11,24 @@ import type { UUIColorSwatchesEvent } from '@umbraco-cms/backoffice/external/uui */ @customElement('umb-input-color') export class UmbInputColorElement extends UUIFormControlMixin(UmbLitElement, '') { - @property({ type: Boolean }) - showLabels = false; - - @property({ type: Array }) - swatches?: UmbSwatchDetails[]; - protected getFormElement() { return undefined; } + @property({ type: Boolean }) + showLabels = false; + + @property({ type: Array }) + swatches?: Array; + #onChange(event: UUIColorSwatchesEvent) { - event.stopPropagation(); this.value = event.target.value; this.dispatchEvent(new UmbChangeEvent()); } render() { return html` - + ${this.#renderColors()} `; @@ -40,10 +39,7 @@ export class UmbInputColorElement extends UUIFormControlMixin(UmbLitElement, '') return map( this.swatches, (swatch) => html` - + `, ); } diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/property-editor/uis/color-picker/property-editor-ui-color-picker.element.ts b/src/Umbraco.Web.UI.Client/src/packages/core/property-editor/uis/color-picker/property-editor-ui-color-picker.element.ts index 505a769cce..02b4ac2e34 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/property-editor/uis/color-picker/property-editor-ui-color-picker.element.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/property-editor/uis/color-picker/property-editor-ui-color-picker.element.ts @@ -14,20 +14,38 @@ export class UmbPropertyEditorUIColorPickerElement extends UmbLitElement impleme #defaultShowLabels = false; @property({ type: Object }) - value?: UmbSwatchDetails; + public set value(value: UmbSwatchDetails | undefined) { + if (!value) return; + this.#value = this.#ensureHashPrefix(value); + } + public get value(): UmbSwatchDetails | undefined { + return this.#value; + } + #value?: UmbSwatchDetails | undefined; @state() private _showLabels = this.#defaultShowLabels; @state() - private _swatches: UmbSwatchDetails[] = []; + private _swatches: Array = []; public set config(config: UmbPropertyEditorConfigCollection | undefined) { - this._showLabels = config?.getValueByAlias('useLabel') ?? this.#defaultShowLabels; - this._swatches = config?.getValueByAlias('items') ?? []; + if (!config) return; + + this._showLabels = config?.getValueByAlias('useLabel') ?? this.#defaultShowLabels; + + const swatches = config?.getValueByAlias>('items') ?? []; + this._swatches = swatches.map((swatch) => this.#ensureHashPrefix(swatch)); } - private _onChange(event: UUIColorSwatchesEvent) { + #ensureHashPrefix(swatch: UmbSwatchDetails): UmbSwatchDetails { + return { + label: swatch.label, + value: swatch.value.startsWith('#') ? swatch.value : `#${swatch.value}`, + }; + } + + #onChange(event: UUIColorSwatchesEvent) { const value = event.target.value; this.value = this._swatches.find((swatch) => swatch.value === value); this.dispatchEvent(new UmbPropertyValueChangeEvent()); @@ -35,10 +53,10 @@ export class UmbPropertyEditorUIColorPickerElement extends UmbLitElement impleme render() { return html``; + ?showLabels=${this._showLabels} + @change=${this.#onChange}>`; } }