From 1a0e705d45e7e2fbc66acfc39a7e9368119b5023 Mon Sep 17 00:00:00 2001 From: Jonty Schmidt Date: Fri, 29 May 2026 17:04:53 +1000 Subject: [PATCH 1/2] Add optional defaultValue to the Umbraco.Decimal PropertyEditor. --- .../DecimalConfigurationEditor.cs | 5 +++++ .../number/Umbraco.Decimal.ts | 7 +++++++ .../number/decimal-property-value-preset.ts | 19 ++++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Core/PropertyEditors/DecimalConfigurationEditor.cs b/src/Umbraco.Core/PropertyEditors/DecimalConfigurationEditor.cs index 4be45297b0f0..ed9afd6558f5 100644 --- a/src/Umbraco.Core/PropertyEditors/DecimalConfigurationEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/DecimalConfigurationEditor.cs @@ -17,6 +17,11 @@ public DecimalConfigurationEditor() Key = "min" }); + Fields.Add(new ConfigurationField(new DecimalValidator()) + { + Key = "defaultValue", + }); + Fields.Add(new ConfigurationField(new DecimalValidator()) { Key = "step", diff --git a/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/Umbraco.Decimal.ts b/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/Umbraco.Decimal.ts index 3794414c4cc7..bac217658665 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/Umbraco.Decimal.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/Umbraco.Decimal.ts @@ -14,6 +14,13 @@ export const manifests: Array = [ propertyEditorUiAlias: 'Umb.PropertyEditorUi.Decimal', config: [{ alias: 'step', value: '0.000001' }], }, + { + alias: 'defaultValue', + label: 'Default value', + description: 'Enter the default value to use when a new property is created', + propertyEditorUiAlias: 'Umb.PropertyEditorUi.Decimal', + config: [{ alias: 'step', value: '0.000001' }], + }, { alias: 'max', label: 'Maximum', diff --git a/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/decimal-property-value-preset.ts b/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/decimal-property-value-preset.ts index f7f1d30d6b6f..ce8b2c18d7de 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/decimal-property-value-preset.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/decimal-property-value-preset.ts @@ -6,13 +6,26 @@ export class UmbDecimalPropertyValuePreset implements UmbPropertyValuePreset { async processValue(value: undefined | UmbDecimalPropertyEditorUiValue, config: UmbPropertyEditorConfig) { - const min = Number(config.find((x) => x.alias === 'min')?.value ?? 0); - const minVerified = isNaN(min) ? 0 : min; + const defaultValue = this.#parseConfiguredNumber(config, 'defaultValue'); + if (defaultValue !== undefined) { + return value !== undefined ? value : defaultValue; + } - return value !== undefined ? value : minVerified; + const min = this.#parseConfiguredNumber(config, 'min') ?? 0; + return value !== undefined ? value : min; } destroy(): void {} + + #parseConfiguredNumber(config: UmbPropertyEditorConfig, alias: string): number | undefined { + const rawValue = config.find((x) => x.alias === alias)?.value; + if (rawValue === undefined || rawValue === null || rawValue === '') { + return undefined; + } + + const parsedValue = Number(rawValue); + return Number.isFinite(parsedValue) ? parsedValue : undefined; + } } export { UmbDecimalPropertyValuePreset as api }; From bf63d528a0366e41228a3146b29419aa0482179d Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Mon, 1 Jun 2026 07:54:40 +0200 Subject: [PATCH 2/2] Added a unit test for the decimal preset. --- .../decimal-property-value-preset.test.ts | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/Umbraco.Web.UI.Client/src/packages/property-editors/number/decimal-property-value-preset.test.ts diff --git a/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/decimal-property-value-preset.test.ts b/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/decimal-property-value-preset.test.ts new file mode 100644 index 000000000000..1098ee52462e --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/property-editors/number/decimal-property-value-preset.test.ts @@ -0,0 +1,67 @@ +import { UmbDecimalPropertyValuePreset } from './decimal-property-value-preset.js'; +import { expect } from '@open-wc/testing'; +import type { UmbPropertyEditorConfig } from '@umbraco-cms/backoffice/property-editor'; + +describe('UmbDecimalPropertyValuePreset', () => { + let preset: UmbDecimalPropertyValuePreset; + + beforeEach(() => { + preset = new UmbDecimalPropertyValuePreset(); + }); + + describe('with a configured default value', () => { + const config: UmbPropertyEditorConfig = [{ alias: 'defaultValue', value: '5.5' }]; + + it('uses the default value for a new property', async () => { + expect(await preset.processValue(undefined, config)).to.equal(5.5); + }); + + it('preserves an existing value over the default value', async () => { + expect(await preset.processValue(2.5, config)).to.equal(2.5); + }); + + it('preserves an existing value of 0 over the default value', async () => { + expect(await preset.processValue(0, config)).to.equal(0); + }); + + it('applies a configured default value of 0', async () => { + const zeroDefaultConfig: UmbPropertyEditorConfig = [{ alias: 'defaultValue', value: '0' }]; + const minConfig: UmbPropertyEditorConfig = [ + { alias: 'defaultValue', value: '0' }, + { alias: 'min', value: '10' }, + ]; + + expect(await preset.processValue(undefined, zeroDefaultConfig)).to.equal(0); + + // A configured default of 0 must win over min, not be treated as "no default". + expect(await preset.processValue(undefined, minConfig)).to.equal(0); + }); + }); + + describe('falling back to the minimum', () => { + it('uses min when no default value is configured', async () => { + const config: UmbPropertyEditorConfig = [{ alias: 'min', value: '3' }]; + expect(await preset.processValue(undefined, config)).to.equal(3); + }); + + it('falls back to min when the default value is an empty string', async () => { + const config: UmbPropertyEditorConfig = [ + { alias: 'defaultValue', value: '' }, + { alias: 'min', value: '3' }, + ]; + expect(await preset.processValue(undefined, config)).to.equal(3); + }); + + it('falls back to min when the default value is not a finite number', async () => { + const config: UmbPropertyEditorConfig = [ + { alias: 'defaultValue', value: 'not-a-number' }, + { alias: 'min', value: '3' }, + ]; + expect(await preset.processValue(undefined, config)).to.equal(3); + }); + + it('defaults to 0 when neither default value nor min is configured', async () => { + expect(await preset.processValue(undefined, [])).to.equal(0); + }); + }); +});