Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public DecimalConfigurationEditor()
Key = "min"
});

Fields.Add(new ConfigurationField(new DecimalValidator())
Comment thread
nielslyngsoe marked this conversation as resolved.
{
Key = "defaultValue",
});

Fields.Add(new ConfigurationField(new DecimalValidator())
{
Key = "step",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export const manifests: Array<UmbExtensionManifest> = [
propertyEditorUiAlias: 'Umb.PropertyEditorUi.Decimal',
config: [{ alias: 'step', value: '0.000001' }],
},
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps move this so it comes after the step setting? Seems like min and max (and arguably step) should go together as they are part of the same process of considering what valid values are.

alias: 'defaultValue',
Comment thread
nielslyngsoe marked this conversation as resolved.
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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
implements UmbPropertyValuePreset<UmbDecimalPropertyEditorUiValue, UmbPropertyEditorConfig>
{
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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I note you can configure a default value that's outside of the min and max and therefore will be immediately invalid. We could look to clamp it, so if they default is lower than the min it'll use the min and if greater than the max it'll use the max. But I'm not sure it's necessary. When it comes to data type configuration validation we don't currently enforce that these settings are internally correct, and rely on the developer to get it right for their editors.

So no action here I feel, just noting.

}

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 === '') {

Check warning on line 22 in src/Umbraco.Web.UI.Client/src/packages/property-editors/number/decimal-property-value-preset.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ New issue: Complex Conditional

UmbDecimalPropertyValuePreset.parseConfiguredNumber has 1 complex conditionals with 2 branches, threshold = 2. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.
return undefined;
}

const parsedValue = Number(rawValue);
return Number.isFinite(parsedValue) ? parsedValue : undefined;
}
}

export { UmbDecimalPropertyValuePreset as api };
Loading