From 371cf072897c1328ed2e0c9209872cfa0da21d02 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Thu, 28 Dec 2023 15:27:04 +0100 Subject: [PATCH] :bug: Allow null for currency/number components Semantically this makes sense, and it's also what Formio uses as defaults when creating instances of those components. --- package-lock.json | 4 +- package.json | 2 +- src/formio/components/currency.ts | 4 +- src/formio/components/number.ts | 4 +- test-d/formio/components/currency.test-d.ts | 46 +++++++++++++-------- test-d/formio/components/number.test-d.ts | 35 ++++++++++------ 6 files changed, 57 insertions(+), 38 deletions(-) diff --git a/package-lock.json b/package-lock.json index 33b0f08..c66427b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@open-formulieren/types", - "version": "0.18.2", + "version": "0.18.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@open-formulieren/types", - "version": "0.18.2", + "version": "0.18.3", "license": "EUPL-1.2", "devDependencies": { "@trivago/prettier-plugin-sort-imports": "^4.1.1", diff --git a/package.json b/package.json index 28fdb39..474964c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@open-formulieren/types", - "version": "0.18.2", + "version": "0.18.3", "description": "Typescript type definitions for Open Forms' Form.io extensions", "main": "index.js", "module": "./lib/index.js", diff --git a/src/formio/components/currency.ts b/src/formio/components/currency.ts index baa607f..4c0e8a2 100644 --- a/src/formio/components/currency.ts +++ b/src/formio/components/currency.ts @@ -3,7 +3,7 @@ import {InputComponentSchema} from '..'; type Validator = 'required' | 'min' | 'max'; type TranslatableKeys = 'label' | 'description' | 'tooltip'; -export type CurrencyInputSchema = InputComponentSchema; +export type CurrencyInputSchema = InputComponentSchema; /** * @group Form.io components @@ -15,5 +15,5 @@ export interface CurrencyComponentSchema extends Omit; +export type NumberInputSchema = InputComponentSchema; /** * @group Form.io components @@ -12,7 +12,7 @@ export type NumberInputSchema = InputComponentSchema { type: 'number'; - defaultValue?: number; + defaultValue?: number | null; /* formio does math on `decimalLimit`` and feeds it to lodash.repeat -> must be int at runtime. There does not appear to be a more elegant way to only allow positive integers. diff --git a/test-d/formio/components/currency.test-d.ts b/test-d/formio/components/currency.test-d.ts index fb3310b..ff890a1 100644 --- a/test-d/formio/components/currency.test-d.ts +++ b/test-d/formio/components/currency.test-d.ts @@ -9,7 +9,17 @@ expectAssignable({ key: 'aCurrency', label: 'A currency', currency: 'EUR', -} as const); +}); + +// no defaultValue, but using formio.js default of `null` +expectAssignable({ + id: '123', + type: 'currency', + key: 'aCurrency', + label: 'A currency', + currency: 'EUR', + defaultValue: null, +}); // with additional, number-component specific properties expectAssignable({ @@ -20,7 +30,7 @@ expectAssignable({ currency: 'EUR', decimalLimit: 3, allowNegative: true, -} as const); +}); // multiple false (implicit) and appropriate default value type expectAssignable({ @@ -30,43 +40,43 @@ expectAssignable({ label: 'A currency', currency: 'EUR', defaultValue: 3, -} as const); +}); // different component type expectNotAssignable({ - type: 'textfield', -} as const); + type: 'textfield' as const, +}); // wrong currency expectNotAssignable({ id: '123', - type: 'currency', + type: 'currency' as const, key: 'aCurrency', label: 'A currency', - currency: 'dummy', -} as const); + currency: 'dummy' as const, +}); // using unsupported properties expectNotAssignable({ id: '123', - type: 'currency', + type: 'currency' as const, key: 'aCurrency', label: 'A currency', - currency: 'EUR', + currency: 'EUR' as const, hideLabel: true, -} as const); +}); // invalid, only the number validators may be assignable expectNotAssignable({ id: '123', - type: 'currency', + type: 'currency' as const, key: 'aCurrency', label: 'A currency', - currency: 'EUR', + currency: 'EUR' as const, validate: { maxLength: 100, }, -} as const); +}); // full, correct schema expectAssignable({ @@ -127,20 +137,20 @@ expectAssignable({ // invalid, multiple true expectNotAssignable({ id: '123', - type: 'currency', + type: 'currency' as const, key: 'aCurrency', label: 'A currency', - currency: 'EUR', + currency: 'EUR' as const, multiple: true, }); // invalid, multiple false and array default value expectNotAssignable({ id: '123', - type: 'currency', + type: 'currency' as const, key: 'aCurrency', label: 'A currency', - currency: 'EUR', + currency: 'EUR' as const, multiple: false, defaultValue: [1], }); diff --git a/test-d/formio/components/number.test-d.ts b/test-d/formio/components/number.test-d.ts index bad4615..ff88f9e 100644 --- a/test-d/formio/components/number.test-d.ts +++ b/test-d/formio/components/number.test-d.ts @@ -8,7 +8,7 @@ expectAssignable({ type: 'number', key: 'aNumber', label: 'A number', -} as const); +}); // with additional, number-component specific properties expectAssignable({ @@ -18,7 +18,16 @@ expectAssignable({ label: 'A number', decimalLimit: 3, allowNegative: true, -} as const); +}); + +// no defaultValue, but using formio.js default of `null` +expectAssignable({ + id: '123', + type: 'number', + key: 'aNumber', + label: 'A number', + defaultValue: null, +}); // multiple false (implicit) and appropriate default value type expectAssignable({ @@ -27,50 +36,50 @@ expectAssignable({ key: 'aNumber', label: 'A number', defaultValue: 3, -} as const); +}); // different component type expectNotAssignable({ - type: 'textfield', -} as const); + type: 'textfield' as const, +}); // using unsupported properties expectNotAssignable({ id: '123', - type: 'number', + type: 'number' as const, key: 'aNumber', label: 'A number', hideLabel: true, -} as const); +}); // no multiple support -> no array defaultValue expectNotAssignable({ id: '123', - type: 'number', + type: 'number' as const, key: 'aNumber', label: 'A number', defaultValue: [], -} as const); +}); expectNotAssignable({ id: '123', - type: 'number', + type: 'number' as const, key: 'aNumber', label: 'A number', multiple: true, defaultValue: [], -} as const); +}); // invalid, only the number validators may be assignable expectNotAssignable({ id: '123', - type: 'number', + type: 'number' as const, key: 'aNumber', label: 'A number', validate: { maxLength: 100, }, -} as const); +}); // full, correct schema expectAssignable({