Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [#2] Implement types for textarea #25

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/formio/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Input components
export * from './textfield';
export * from './textarea';
export * from './currency';
export * from './email';
export * from './date';
Expand Down
25 changes: 25 additions & 0 deletions src/formio/components/textarea.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {InputComponentSchema, MultipleCapable} from '..';

type Validator = 'required' | 'maxLength' | 'pattern';
type TranslatableKeys = 'label' | 'description' | 'tooltip' | 'defaultValue' | 'placeholder';

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

/**
* @group Form.io components
* @category Base types
*/
export interface BaseTextareaComponentSchema extends Omit<TextareaInputSchema, 'hideLabel'> {
type: 'textarea';
// additional properties
showCharCount?: boolean;
autocomplete?: string;
// OF custom properties
rows: number;
Viicos marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @group Form.io components
* @category Concrete types
*/
export type TextareaComponentSchema = MultipleCapable<BaseTextareaComponentSchema>;
2 changes: 2 additions & 0 deletions src/formio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
RadioComponentSchema,
SelectboxesComponentSchema,
TextFieldComponentSchema,
TextareaComponentSchema,
TimeComponentSchema,
} from './components';

Expand Down Expand Up @@ -50,6 +51,7 @@ export type AnyComponentSchema =
| PhoneNumberComponentSchema
| PostcodeComponentSchema
| FileComponentSchema
| TextareaComponentSchema
| NumberComponentSchema
| CheckboxComponentSchema
| SelectboxesComponentSchema
Expand Down
169 changes: 169 additions & 0 deletions test-d/formio/components/textarea.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import {expectAssignable, expectNotAssignable} from 'tsd';

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

// minimal textarea component schema
expectAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'someInput',
label: 'Some input',
rows: 3,
});

// multiple false and appropriate default value type
expectAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'someEmail',
label: 'Some input',
rows: 3,
multiple: false,
defaultValue: '',
});

// multiple true and appropriate default value type
expectAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'someEmail',
label: 'Some input',
rows: 3,
multiple: true,
defaultValue: [''],
});

// full, correct schema
expectAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
// basic tab in builder form
label: 'Some input',
rows: 3,
key: 'someInput',
description: 'A description',
tooltip: 'A tooltip',
showInSummary: true,
showInEmail: false,
showInPDF: true,
multiple: false,
hidden: false,
clearOnHide: true,
isSensitiveData: false,
defaultValue: '',
autocomplete: 'name',
disabled: false,
placeholder: '',
showCharCount: true,
// advanced tab in builder form
conditional: {
show: undefined,
when: undefined,
eq: undefined,
},
// validation tab in builder form
validate: {
required: false,
plugins: undefined,
maxLength: 20,
pattern: '',
},
translatedErrors: {
nl: {
required: 'Je moet een waarde opgeven!!!',
maxLength: 'Een maximale lengte.',
},
},
errors: {
// translatedErrors is converted into errors by the backend
required: 'Je moet een waarde opgeven!!!',
maxLength: 'Een maximale lengte.',
},
// registration tab in builder form
registration: {
attribute: '',
},
// translations tab in builder form
openForms: {
translations: {
nl: {
label: 'foo',
description: 'bar',
},
},
},
});

// different component type
expectNotAssignable<TextareaComponentSchema>({
type: 'fieldset',
});

// using unsupported properties
expectNotAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'someInput',
label: 'Some input',
rows: 3,
hideLabel: true,
});

// incorrect, invalid validator key
expectNotAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'someInput',
label: 'Some input',
rows: 3,
validate: {
min: 3,
},
});

// invalid, multiple true and non-array default value
expectNotAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'textarea',
label: 'Some textarea',
rows: 3,
multiple: true,
defaultValue: '',
});

// invalid, multiple false and array default value
expectNotAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'textarea',
label: 'Some textarea',
rows: 3,
multiple: false,
defaultValue: [''],
});

// invalid, multiple true and wrong default value in array element
expectNotAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'textarea',
label: 'Some textarea',
rows: 3,
multiple: true,
defaultValue: [0],
});

// invalid, with prefill
expectNotAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'someInput',
label: 'Some input',
prefill: {
plugin: '',
attribute: '',
identifierRole: 'main',
},
rows: 3,
});