Skip to content

Commit

Permalink
Merge pull request #25 from open-formulieren/feature/2-textarea
Browse files Browse the repository at this point in the history
✨ [#2] Implement types for `textarea`
  • Loading branch information
sergei-maertens authored Nov 15, 2023
2 parents 25941ad + 8f80dcd commit 788b3e1
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 0 deletions.
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;
rows: number;
autoExpand: boolean;
}

/**
* @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 @@ -17,6 +17,7 @@ import {
SelectComponentSchema,
SelectboxesComponentSchema,
TextFieldComponentSchema,
TextareaComponentSchema,
TimeComponentSchema,
} from './components';

Expand Down Expand Up @@ -53,6 +54,7 @@ export type AnyComponentSchema =
| PhoneNumberComponentSchema
| PostcodeComponentSchema
| FileComponentSchema
| TextareaComponentSchema
| NumberComponentSchema
| SelectComponentSchema
| CheckboxComponentSchema
Expand Down
179 changes: 179 additions & 0 deletions test-d/formio/components/textarea.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
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,
autoExpand: true,
});

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

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

// full, correct schema
expectAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
// basic tab in builder form
label: 'Some input',
rows: 3,
autoExpand: true,
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,
autoExpand: true,
hideLabel: true,
});

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

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

// invalid, multiple false and array default value
expectNotAssignable<TextareaComponentSchema>({
id: 'yejak',
type: 'textarea',
key: 'textarea',
label: 'Some textarea',
rows: 3,
autoExpand: true,
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,
autoExpand: true,
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,
autoExpand: true,
});

0 comments on commit 788b3e1

Please sign in to comment.