From 4ac36d7badb025fb305424cc3eb73d83c85ab870 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Tue, 5 Dec 2023 15:55:27 +0100 Subject: [PATCH] :sparkles: [#2] Implement type definitions for password component schema --- src/formio/components/index.ts | 1 + src/formio/components/password.ts | 32 ++++++ src/formio/index.ts | 5 +- test-d/formio/components/password.test-d.ts | 113 ++++++++++++++++++++ 4 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/formio/components/password.ts create mode 100644 test-d/formio/components/password.test-d.ts diff --git a/src/formio/components/index.ts b/src/formio/components/index.ts index a622c3f..6ec1cff 100644 --- a/src/formio/components/index.ts +++ b/src/formio/components/index.ts @@ -20,6 +20,7 @@ export * from './file'; export * from './radio'; export * from './addressNL'; export * from './map'; +export * from './password'; // Layout components export * from './content'; diff --git a/src/formio/components/password.ts b/src/formio/components/password.ts new file mode 100644 index 0000000..401e96f --- /dev/null +++ b/src/formio/components/password.ts @@ -0,0 +1,32 @@ +import {InputComponentSchema, MultipleCapable} from '..'; + +type Validator = 'required'; +type TranslatableKeys = 'label' | 'description' | 'tooltip'; + +export type PasswordInputSchema = InputComponentSchema; + +/** + * @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 { + 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; diff --git a/src/formio/index.ts b/src/formio/index.ts index 678e718..d07999e 100644 --- a/src/formio/index.ts +++ b/src/formio/index.ts @@ -15,6 +15,7 @@ import { MapComponentSchema, NpFamilyMembersComponentSchema, NumberComponentSchema, + PasswordComponentSchema, PhoneNumberComponentSchema, PostcodeComponentSchema, RadioComponentSchema, @@ -75,7 +76,9 @@ export type AnyComponentSchema = // layout | ContentComponentSchema | ColumnsComponentSchema - | FieldsetComponentSchema; + | FieldsetComponentSchema + // deprecated + | PasswordComponentSchema; /** * Convenience type alias for all supported concrete component schema types. diff --git a/test-d/formio/components/password.test-d.ts b/test-d/formio/components/password.test-d.ts new file mode 100644 index 0000000..4b3cb1c --- /dev/null +++ b/test-d/formio/components/password.test-d.ts @@ -0,0 +1,113 @@ +import {expectAssignable, expectNotAssignable} from 'tsd'; + +import {PasswordComponentSchema} from '../../../lib'; + +// minimal password component schema +expectAssignable({ + id: 'yejak', + type: 'password', + key: 'somePassword', + label: 'Some password', +}); + +// with additional, password-component specific properties +expectAssignable({ + id: 'yejak', + type: 'password', + key: 'somePassword', + label: 'Some password', + autocomplete: 'password', +}); + +// multiple false and appropriate default value type +expectAssignable({ + id: 'yejak', + type: 'password', + key: 'somePassword', + label: 'Some password', + multiple: false, + defaultValue: '', +}); + +// multiple true and appropriate default value type +expectAssignable({ + id: 'yejak', + type: 'password', + key: 'somePassword', + label: 'Some password', + multiple: true, + defaultValue: [''], +}); + +// full, correct schema +expectAssignable({ + 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({ + id: 'yejak', + type: 'password' as const, + key: 'somePassword', + label: 'Some password', + multiple: true, + defaultValue: '', +}); + +// invalid, multiple false and array default value +expectNotAssignable({ + 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({ + id: 'yejak', + type: 'password' as const, + key: 'somePassword', + label: 'Some password', + multiple: true, + defaultValue: [0], +});