Skip to content

Commit

Permalink
Merge pull request #39 from open-formulieren/issue/105-number-currenc…
Browse files Browse the repository at this point in the history
…y-null-defaultValue

🐛 Allow null for currency/number components
  • Loading branch information
sergei-maertens authored Dec 28, 2023
2 parents 246b805 + 371cf07 commit 5d76946
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 38 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/formio/components/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {InputComponentSchema} from '..';
type Validator = 'required' | 'min' | 'max';
type TranslatableKeys = 'label' | 'description' | 'tooltip';

export type CurrencyInputSchema = InputComponentSchema<number, Validator, TranslatableKeys>;
export type CurrencyInputSchema = InputComponentSchema<number | null, Validator, TranslatableKeys>;

/**
* @group Form.io components
Expand All @@ -15,5 +15,5 @@ export interface CurrencyComponentSchema extends Omit<CurrencyInputSchema, 'hide
currency: 'EUR';
decimalLimit?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
allowNegative?: boolean;
defaultValue?: number;
defaultValue?: number | null;
}
4 changes: 2 additions & 2 deletions src/formio/components/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {InputComponentSchema} from '..';
type Validator = 'required' | 'min' | 'max';
type TranslatableKeys = 'label' | 'description' | 'tooltip' | 'suffix';

export type NumberInputSchema = InputComponentSchema<number, Validator, TranslatableKeys>;
export type NumberInputSchema = InputComponentSchema<number | null, Validator, TranslatableKeys>;

/**
* @group Form.io components
Expand All @@ -12,7 +12,7 @@ export type NumberInputSchema = InputComponentSchema<number, Validator, Translat
export interface BaseNumberComponentSchema
extends Omit<NumberInputSchema, 'hideLabel' | 'placeholder'> {
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.
Expand Down
46 changes: 28 additions & 18 deletions test-d/formio/components/currency.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ expectAssignable<CurrencyComponentSchema>({
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
} as const);
});

// no defaultValue, but using formio.js default of `null`
expectAssignable<CurrencyComponentSchema>({
id: '123',
type: 'currency',
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
defaultValue: null,
});

// with additional, number-component specific properties
expectAssignable<CurrencyComponentSchema>({
Expand All @@ -20,7 +30,7 @@ expectAssignable<CurrencyComponentSchema>({
currency: 'EUR',
decimalLimit: 3,
allowNegative: true,
} as const);
});

// multiple false (implicit) and appropriate default value type
expectAssignable<CurrencyComponentSchema>({
Expand All @@ -30,43 +40,43 @@ expectAssignable<CurrencyComponentSchema>({
label: 'A currency',
currency: 'EUR',
defaultValue: 3,
} as const);
});

// different component type
expectNotAssignable<CurrencyComponentSchema>({
type: 'textfield',
} as const);
type: 'textfield' as const,
});

// wrong currency
expectNotAssignable<CurrencyComponentSchema>({
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<CurrencyComponentSchema>({
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<CurrencyComponentSchema>({
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<CurrencyComponentSchema>({
Expand Down Expand Up @@ -127,20 +137,20 @@ expectAssignable<CurrencyComponentSchema>({
// invalid, multiple true
expectNotAssignable<CurrencyComponentSchema>({
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<CurrencyComponentSchema>({
id: '123',
type: 'currency',
type: 'currency' as const,
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
currency: 'EUR' as const,
multiple: false,
defaultValue: [1],
});
35 changes: 22 additions & 13 deletions test-d/formio/components/number.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ expectAssignable<NumberComponentSchema>({
type: 'number',
key: 'aNumber',
label: 'A number',
} as const);
});

// with additional, number-component specific properties
expectAssignable<NumberComponentSchema>({
Expand All @@ -18,7 +18,16 @@ expectAssignable<NumberComponentSchema>({
label: 'A number',
decimalLimit: 3,
allowNegative: true,
} as const);
});

// no defaultValue, but using formio.js default of `null`
expectAssignable<NumberComponentSchema>({
id: '123',
type: 'number',
key: 'aNumber',
label: 'A number',
defaultValue: null,
});

// multiple false (implicit) and appropriate default value type
expectAssignable<NumberComponentSchema>({
Expand All @@ -27,50 +36,50 @@ expectAssignable<NumberComponentSchema>({
key: 'aNumber',
label: 'A number',
defaultValue: 3,
} as const);
});

// different component type
expectNotAssignable<NumberComponentSchema>({
type: 'textfield',
} as const);
type: 'textfield' as const,
});

// using unsupported properties
expectNotAssignable<NumberComponentSchema>({
id: '123',
type: 'number',
type: 'number' as const,
key: 'aNumber',
label: 'A number',
hideLabel: true,
} as const);
});

// no multiple support -> no array defaultValue
expectNotAssignable<NumberComponentSchema>({
id: '123',
type: 'number',
type: 'number' as const,
key: 'aNumber',
label: 'A number',
defaultValue: [],
} as const);
});

expectNotAssignable<NumberComponentSchema>({
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<NumberComponentSchema>({
id: '123',
type: 'number',
type: 'number' as const,
key: 'aNumber',
label: 'A number',
validate: {
maxLength: 100,
},
} as const);
});

// full, correct schema
expectAssignable<NumberComponentSchema>({
Expand Down

0 comments on commit 5d76946

Please sign in to comment.