diff --git a/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/map-elements.test.ts b/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/map-elements.test.ts index ec110144d..3d15a32e4 100644 --- a/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/map-elements.test.ts +++ b/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/map-elements.test.ts @@ -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' }, diff --git a/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/sectional-element.test.ts b/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/sectional-element.test.ts index bf4b773f1..c72c1f874 100644 --- a/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/sectional-element.test.ts +++ b/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/sectional-element.test.ts @@ -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', () => { @@ -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' } }, }; @@ -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' } }, }; @@ -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' }, @@ -65,7 +76,7 @@ describe('getFormDefinitionSectionalElement', () => { }); it('should map Divider', () => { - const config: SectionalElement = { + const config: GenericSectionalElementConfig = { type: 'Divider', position: { fixed: 'first' }, orientation: 'horizontal', @@ -78,7 +89,7 @@ describe('getFormDefinitionSectionalElement', () => { }); it('should map Heading', () => { - const config: SectionalElement = { + const config: GenericSectionalElementConfig = { type: 'Heading', position: { fixed: 'first' }, }; @@ -90,7 +101,7 @@ describe('getFormDefinitionSectionalElement', () => { }); it('should throw if componentType is unmappable', () => { - const config: SectionalElement = { + const config: GenericSectionalElementConfig = { type: 'Invalid', position: { fixed: 'first' }, }; diff --git a/packages/codegen-ui/lib/generate-form-definition/generate-form-definition.ts b/packages/codegen-ui/lib/generate-form-definition/generate-form-definition.ts index 88d660634..ab4add658 100644 --- a/packages/codegen-ui/lib/generate-form-definition/generate-form-definition.ts +++ b/packages/codegen-ui/lib/generate-form-definition/generate-form-definition.ts @@ -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, })), ); diff --git a/packages/codegen-ui/lib/generate-form-definition/helpers/sectional-element.ts b/packages/codegen-ui/lib/generate-form-definition/helpers/sectional-element.ts index 1f6afc236..cadff2e0d 100644 --- a/packages/codegen-ui/lib/generate-form-definition/helpers/sectional-element.ts +++ b/packages/codegen-ui/lib/generate-form-definition/helpers/sectional-element.ts @@ -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; @@ -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 */ diff --git a/packages/codegen-ui/lib/types/form/index.ts b/packages/codegen-ui/lib/types/form/index.ts index 917922a8d..697d95a45 100644 --- a/packages/codegen-ui/lib/types/form/index.ts +++ b/packages/codegen-ui/lib/types/form/index.ts @@ -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'; @@ -48,7 +48,7 @@ export type StudioForm = { fields: StudioFormFields; - sectionalElements: { [elementName: string]: SectionalElement }; + sectionalElements: SectionalElementFields; style: StudioFormStyle; @@ -83,7 +83,9 @@ export * from './form-validation'; export * from './form-cta'; export type { - SectionalElement, + GenericSectionalElementConfig, + SectionalElementConfig, + SectionalElementFields, StudioFormFieldConfig, StudioFormActionType, FormDefinition, diff --git a/packages/codegen-ui/lib/types/form/sectional-element.ts b/packages/codegen-ui/lib/types/form/sectional-element.ts index 84f866653..e9608031b 100644 --- a/packages/codegen-ui/lib/types/form/sectional-element.ts +++ b/packages/codegen-ui/lib/types/form/sectional-element.ts @@ -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; @@ -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; +};