Skip to content

Commit

Permalink
✨ [#2] Implement columns component schema type
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Nov 30, 2023
1 parent c90192c commit dad9370
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/formio/components/columns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {AnyComponentSchema, LayoutComponentSchema} from '..';

/**
* Configuration of a single column.
*
* Note that this deviates from Formio.js' own schema, as they are tightly coupled with
* Bootstrap properties/class names, which we deliberately avoid.
*/
export interface Column {
/**
* Size on non-mobile viewports.
*
* The size specifies the number of columns spanned in a 12-column grid. If no mobile
* size is specified, a 100% width is assumed.
*/
size: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
/**
* Size on mobile viewports.
*
* The mobile size specifies the numbers of columns spanned in a 4-column grid.
*
* @todo Add backend migration to ensure this attribute is set everywhere.
*/
sizeMobile: 1 | 2 | 3 | 4;
/**
* Nested components inside a single column.
*/
components: AnyComponentSchema[];
}

/**
* The columns component schema.
*
* Columns are used to manage layout on desktop and mobile viewports by grouping nested
* components inside into columns.
*
* @group Form.io components
* @category Concrete types
*/
export interface ColumnsComponentSchema
extends Omit<
LayoutComponentSchema<never>,
| 'label'
| 'placeholder'
| 'multiple'
| 'defaultValue'
| 'conditional'
| 'errors'
| 'description'
| 'tooltip'
| 'hideLabel'
| 'disabled'
| 'validateOn'
> {
id: string;
key: string;
type: 'columns';
columns: Column[];
}
1 change: 1 addition & 0 deletions src/formio/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './radio';

// Layout components
export * from './content';
export * from './columns';
4 changes: 3 additions & 1 deletion src/formio/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BsnComponentSchema,
CheckboxComponentSchema,
ColumnsComponentSchema,
ContentComponentSchema,
CurrencyComponentSchema,
DateComponentSchema,
Expand Down Expand Up @@ -67,7 +68,8 @@ export type AnyComponentSchema =
| BsnComponentSchema
| NpFamilyMembersComponentSchema
// layout
| ContentComponentSchema;
| ContentComponentSchema
| ColumnsComponentSchema;

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

import {Column, ColumnsComponentSchema} from '../../../lib';

// Minimal schema
expectAssignable<ColumnsComponentSchema>({
id: 'eqegfc',
type: 'columns',
key: 'columns',
columns: [],
});

// Full schema
expectAssignable<ColumnsComponentSchema>({
id: 'eqegfc',
type: 'columns',
key: 'columns',
columns: [
{
size: 6,
sizeMobile: 4,
components: [
{
id: 'wien53il',
type: 'textfield',
key: 'nestedTextField',
label: 'Nested text field',
},
],
},
{
size: 3,
sizeMobile: 4,
components: [
{
id: 'abcdefgh',
type: 'textfield',
key: 'nested2',
label: 'Second text field',
},
],
},
],
});

expectNotAssignable<Column>({
size: 0 as const,
sizeMobile: 4 as const,
components: [],
});

expectNotAssignable<Column>({
size: 13 as const,
sizeMobile: 4 as const,
components: [],
});

expectNotAssignable<Column>({
size: 3.14 as number,
sizeMobile: 4 as const,
components: [],
});

0 comments on commit dad9370

Please sign in to comment.