Skip to content

Commit

Permalink
feat: add handling for excluded sectional elements
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpascual authored and hein-j committed Oct 28, 2022
1 parent d67202a commit 1f693d6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
limitations under the License.
*/
import { mapElements } from '../../../generate-form-definition/helpers';
import { FormDefinition, SectionalElement, ModelFieldsConfigs, StudioForm } from '../../../types';
import { FormDefinition, GenericSectionalElementConfig, ModelFieldsConfigs, StudioForm } from '../../../types';
import { getBasicFormDefinition } from '../../__utils__/basic-form-definition';

describe('mapElements', () => {
it('should map sectional elements & input elements with and without overrides', () => {
const sectionalConfig: SectionalElement = {
const sectionalConfig: GenericSectionalElementConfig = {
type: 'Text',
text: 'MyText',
position: { fixed: 'first' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { mapSectionalElement, getFormDefinitionSectionalElement } from '../../../generate-form-definition/helpers';
import { FormDefinition, SectionalElement } from '../../../types';
import { FormDefinition, GenericSectionalElementConfig, SectionalElementConfig } from '../../../types';
import { getBasicFormDefinition } from '../../__utils__/basic-form-definition';

describe('mapSectionalElement', () => {
Expand All @@ -25,7 +25,7 @@ describe('mapSectionalElement', () => {
elements: { Heading123: { componentType: 'Heading', props: {} } },
};

const element: { name: string; config: SectionalElement } = {
const element: { name: string; config: GenericSectionalElementConfig } = {
name: 'Heading123',
config: { type: 'Heading', level: 1, text: 'My Heading', position: { fixed: 'first' } },
};
Expand All @@ -36,7 +36,7 @@ describe('mapSectionalElement', () => {
it('should map configurations', () => {
const formDefinition: FormDefinition = getBasicFormDefinition();

const element: { name: string; config: SectionalElement } = {
const element: { name: string; config: GenericSectionalElementConfig } = {
name: 'Heading123',
config: { type: 'Heading', level: 1, text: 'My Heading', position: { fixed: 'first' } },
};
Expand All @@ -48,11 +48,22 @@ describe('mapSectionalElement', () => {
props: { level: 1, children: 'My Heading' },
});
});

it('should throw if attempting to map excluded sectional element', () => {
const formDefinition: FormDefinition = getBasicFormDefinition();

const element: { name: string; config: SectionalElementConfig } = {
name: 'Heading123',
config: { excluded: true },
};

expect(() => mapSectionalElement(element, formDefinition)).toThrow();
});
});

describe('getFormDefinitionSectionalElement', () => {
it('should map Text', () => {
const config: SectionalElement = {
const config: GenericSectionalElementConfig = {
type: 'Text',
text: 'MyText',
position: { fixed: 'first' },
Expand All @@ -65,7 +76,7 @@ describe('getFormDefinitionSectionalElement', () => {
});

it('should map Divider', () => {
const config: SectionalElement = {
const config: GenericSectionalElementConfig = {
type: 'Divider',
position: { fixed: 'first' },
orientation: 'horizontal',
Expand All @@ -78,7 +89,7 @@ describe('getFormDefinitionSectionalElement', () => {
});

it('should map Heading', () => {
const config: SectionalElement = {
const config: GenericSectionalElementConfig = {
type: 'Heading',
position: { fixed: 'first' },
};
Expand All @@ -90,7 +101,7 @@ describe('getFormDefinitionSectionalElement', () => {
});

it('should throw if componentType is unmappable', () => {
const config: SectionalElement = {
const config: GenericSectionalElementConfig = {
type: 'Invalid',
position: { fixed: 'first' },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export function generateFormDefinition({
.concat(
Object.entries(form.sectionalElements).map(([elementName, elementConfig]) => ({
name: elementName,
position: elementConfig.position,
excluded: false,
position: 'position' in elementConfig ? elementConfig.position : undefined,
excluded: 'excluded' in elementConfig ? elementConfig.excluded : false,
})),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import { FormDefinition, SectionalElement, FormDefinitionSectionalElement } from '../../types';
import { InvalidInputError } from '../../errors';
import {
FormDefinition,
GenericSectionalElementConfig,
SectionalElementConfig,
FormDefinitionSectionalElement,
} from '../../types';
import { InternalError, InvalidInputError } from '../../errors';
import { deleteUndefined } from './mapper-utils';
import { FORM_DEFINITION_DEFAULTS } from './defaults';

export function getFormDefinitionSectionalElement(config: SectionalElement): FormDefinitionSectionalElement {
export function getFormDefinitionSectionalElement(
config: GenericSectionalElementConfig,
): FormDefinitionSectionalElement {
const componentType = config.type;

let formDefinitionElement: FormDefinitionSectionalElement;
Expand Down Expand Up @@ -66,12 +73,15 @@ export function getFormDefinitionSectionalElement(config: SectionalElement): For
*/
/* eslint-disable no-param-reassign */
export function mapSectionalElement(
element: { name: string; config: SectionalElement },
element: { name: string; config: SectionalElementConfig },
formDefinition: FormDefinition,
) {
if (formDefinition.elements[element.name]) {
throw new InvalidInputError(`There is are form elements with the same name: ${element.name}`);
}
if ('excluded' in element.config) {
throw new InternalError(`Attempted to map excluded sectional element ${element.name}`);
}
formDefinition.elements[element.name] = getFormDefinitionSectionalElement(element.config);
}
/* eslint-enable no-param-reassign */
8 changes: 5 additions & 3 deletions packages/codegen-ui/lib/types/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { StudioFormStyle } from './style';
import { StudioFormFields, StudioFormFieldConfig, StudioGenericFieldConfig } from './fields';
import { SectionalElement } from './sectional-element';
import { GenericSectionalElementConfig, SectionalElementConfig, SectionalElementFields } from './sectional-element';
import { FormDefinition, ModelFieldsConfigs, FieldTypeMapKeys, ButtonConfig } from './form-definition';
import { StudioFieldInputConfig, StudioFormValueMappings } from './input-config';
import { StudioFieldPosition } from './position';
Expand Down Expand Up @@ -48,7 +48,7 @@ export type StudioForm = {

fields: StudioFormFields;

sectionalElements: { [elementName: string]: SectionalElement };
sectionalElements: SectionalElementFields;

style: StudioFormStyle;

Expand Down Expand Up @@ -83,7 +83,9 @@ export * from './form-validation';
export * from './form-cta';

export type {
SectionalElement,
GenericSectionalElementConfig,
SectionalElementConfig,
SectionalElementFields,
StudioFormFieldConfig,
StudioFormActionType,
FormDefinition,
Expand Down
15 changes: 14 additions & 1 deletion packages/codegen-ui/lib/types/form/sectional-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { StudioFieldPosition } from './position';
* Shape below represent API shapes after typecasting
*/

export type SectionalElement = {
export type GenericSectionalElementConfig = {
position?: StudioFieldPosition;
type: string;

Expand All @@ -29,3 +29,16 @@ export type SectionalElement = {

orientation?: string;
};

/**
* If a sectional element is excluded, it should have no other properties.
*/
type ExcludedSectionalElementConfig = {
excluded: true;
};

export type SectionalElementConfig = GenericSectionalElementConfig | ExcludedSectionalElementConfig;

export type SectionalElementFields = {
[elementname: string]: SectionalElementConfig;
};

0 comments on commit 1f693d6

Please sign in to comment.