-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Umbraco.Decimal Property Editor: Add Default value (closes #21787) #23015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,13 @@ export const manifests: Array<UmbExtensionManifest> = [ | |
| propertyEditorUiAlias: 'Umb.PropertyEditorUi.Decimal', | ||
| config: [{ alias: 'step', value: '0.000001' }], | ||
| }, | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
|
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', | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
| return undefined; | ||
| } | ||
|
|
||
| const parsedValue = Number(rawValue); | ||
| return Number.isFinite(parsedValue) ? parsedValue : undefined; | ||
| } | ||
| } | ||
|
|
||
| export { UmbDecimalPropertyValuePreset as api }; | ||
Uh oh!
There was an error while loading. Please reload this page.