Skip to content

Commit

Permalink
✨ [#2] Implement type definitions for password component schema
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Dec 5, 2023
1 parent d27ff07 commit 4ac36d7
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/formio/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './file';
export * from './radio';
export * from './addressNL';
export * from './map';
export * from './password';

// Layout components
export * from './content';
Expand Down
32 changes: 32 additions & 0 deletions src/formio/components/password.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {InputComponentSchema, MultipleCapable} from '..';

type Validator = 'required';
type TranslatableKeys = 'label' | 'description' | 'tooltip';

export type PasswordInputSchema = InputComponentSchema<string, Validator, TranslatableKeys>;

/**
* @group Form.io components
* @category Base types
*
* @deprecated
*
* The password component should not be used anymore - it's intended usage has never
* been clear.
*/
export interface BasePasswordComponentSchema
extends Omit<PasswordInputSchema, 'hideLabel' | 'disabled' | 'placeholder'> {
type: 'password';
autocomplete?: string;
}

/**
* @group Form.io components
* @category Concrete types
*
* @deprecated
*
* The password component should not be used anymore - it's intended usage has never
* been clear.
*/
export type PasswordComponentSchema = MultipleCapable<BasePasswordComponentSchema>;
5 changes: 4 additions & 1 deletion src/formio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
MapComponentSchema,
NpFamilyMembersComponentSchema,
NumberComponentSchema,
PasswordComponentSchema,
PhoneNumberComponentSchema,
PostcodeComponentSchema,
RadioComponentSchema,
Expand Down Expand Up @@ -75,7 +76,9 @@ export type AnyComponentSchema =
// layout
| ContentComponentSchema
| ColumnsComponentSchema
| FieldsetComponentSchema;
| FieldsetComponentSchema
// deprecated
| PasswordComponentSchema;

/**
* Convenience type alias for all supported concrete component schema types.
Expand Down
113 changes: 113 additions & 0 deletions test-d/formio/components/password.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import {expectAssignable, expectNotAssignable} from 'tsd';

import {PasswordComponentSchema} from '../../../lib';

// minimal password component schema
expectAssignable<PasswordComponentSchema>({
id: 'yejak',
type: 'password',
key: 'somePassword',
label: 'Some password',
});

// with additional, password-component specific properties
expectAssignable<PasswordComponentSchema>({
id: 'yejak',
type: 'password',
key: 'somePassword',
label: 'Some password',
autocomplete: 'password',
});

// multiple false and appropriate default value type
expectAssignable<PasswordComponentSchema>({
id: 'yejak',
type: 'password',
key: 'somePassword',
label: 'Some password',
multiple: false,
defaultValue: '',
});

// multiple true and appropriate default value type
expectAssignable<PasswordComponentSchema>({
id: 'yejak',
type: 'password',
key: 'somePassword',
label: 'Some password',
multiple: true,
defaultValue: [''],
});

// full, correct schema
expectAssignable<PasswordComponentSchema>({
id: 'yejak',
type: 'password',
defaultValue: '',
// basic tab
label: 'Some password',
key: 'somePassword',
description: '',
tooltip: 'A tooltip',
showInSummary: true,
showInEmail: false,
showInPDF: true,
multiple: false,
hidden: false,
clearOnHide: true,
isSensitiveData: true,
autocomplete: 'password',
// Advanced tab
conditional: {
show: undefined,
when: '',
eq: '',
},
// Validation tab
validate: {
required: false,
plugins: [],
},
translatedErrors: {nl: {required: 'Geef wachtwoord.'}},
errors: {required: 'Geef wachtwoord.'},
// registration tab
registration: {
attribute: '',
},
// translations tab in builder form
openForms: {
translations: {
nl: {label: 'foo', description: 'bar', tooltip: 'baz'},
},
},
});

// invalid, multiple true and non-array default value
expectNotAssignable<PasswordComponentSchema>({
id: 'yejak',
type: 'password' as const,
key: 'somePassword',
label: 'Some password',
multiple: true,
defaultValue: '',
});

// invalid, multiple false and array default value
expectNotAssignable<PasswordComponentSchema>({
id: 'yejak',
type: 'password' as const,
key: 'somePassword',
label: 'Some password',
multiple: false,
defaultValue: [''],
});

// invalid, multiple true and wrong default value in array element
expectNotAssignable<PasswordComponentSchema>({
id: 'yejak',
type: 'password' as const,
key: 'somePassword',
label: 'Some password',
multiple: true,
defaultValue: [0],
});

0 comments on commit 4ac36d7

Please sign in to comment.