Skip to content

Commit

Permalink
✅ [#2] -- make number input properly multiple capable
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Jun 6, 2023
1 parent 3f0c58f commit 6c0404f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/formio/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ type UnusedFormioProperties =

// Define our base schema which refines Form.io's (too loose) schema. This applies
// to *most* of the component types we support.
interface StrictComponentSchema<T> extends Omit<ComponentSchema<T>, UnusedFormioProperties> {
//
// We also exclude 'multiple' here to force people to explicitly opt-in through the
// `MultipleCapable` for components that support it.
interface StrictComponentSchema<T>
extends Omit<ComponentSchema<T>, UnusedFormioProperties | 'multiple'> {
id: string;
key: string;
type: string;
Expand Down
6 changes: 4 additions & 2 deletions src/formio/components/number.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {InputComponentSchema} from '..';
import {InputComponentSchema, MultipleCapable} from '..';

type Validator = 'required' | 'min' | 'max';

export interface NumberComponentSchema
interface BaseNumberComponentSchema
extends Omit<InputComponentSchema<number, Validator>, 'hideLabel'> {
type: 'number';
/*
Expand All @@ -12,3 +12,5 @@ export interface NumberComponentSchema
decimalLimit?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
allowNegative?: boolean;
}

export type NumberComponentSchema = MultipleCapable<BaseNumberComponentSchema>;
52 changes: 51 additions & 1 deletion test-d/formio/components/number.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ expectAssignable<NumberComponentSchema>({
allowNegative: true,
});

// multiple false and appropriate default value type
expectAssignable<NumberComponentSchema>({
id: '123',
type: 'number',
key: 'aNumber',
label: 'A number',
multiple: false,
defaultValue: 3,
});

// multiple true and appropriate default value type
expectAssignable<NumberComponentSchema>({
id: '123',
type: 'number',
key: 'aNumber',
label: 'A number',
multiple: true,
defaultValue: [2.1],
});

// different component type
expectNotAssignable<NumberComponentSchema>({
type: 'textfield',
Expand All @@ -45,7 +65,7 @@ expectAssignable<NumberComponentSchema>({
showInSummary: true,
showInEmail: false,
showInPDF: true,
multiple: false,
// multiple: false,
hidden: false,
clearOnHide: true,
isSensitiveData: false,
Expand Down Expand Up @@ -87,3 +107,33 @@ expectAssignable<NumberComponentSchema>({
},
},
});

// invalid, multiple true and non-array default value
expectNotAssignable<NumberComponentSchema>({
id: '8aosjaw',
type: 'number',
key: 'number',
label: 'Some number',
multiple: true,
defaultValue: 31415926535,
});

// invalid, multiple false and array default value
expectNotAssignable<NumberComponentSchema>({
id: '8aosjaw',
type: 'number',
key: 'number',
label: 'Some number',
multiple: false,
defaultValue: [42],
});

// invalid, multiple true and wrong default value in array element
expectNotAssignable<NumberComponentSchema>({
id: '8aosjaw',
type: 'number',
key: 'number',
label: 'Some number',
multiple: true,
defaultValue: [''],
});

0 comments on commit 6c0404f

Please sign in to comment.