Skip to content

Commit

Permalink
Introduce aliases to field rendering types (#37)
Browse files Browse the repository at this point in the history
* Introduce aliases to field rendering types

* Code review changes

---------

Co-authored-by: Makombe <makombe>
  • Loading branch information
makombe authored Apr 25, 2023
1 parent f41a3cd commit ab7221d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/registry/registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { OHRIMultiSelect } from '../components/inputs/multi-select/ohri-multi-select.component';
import OHRINumber from '../components/inputs/number/ohri-number.component';
import { getFieldComponent } from './registry';

describe('registry', () => {
it('should load the OHRINumber component with alias "numeric"', async () => {
const result = await getFieldComponent('numeric');
expect(result).toEqual({ default: OHRINumber });
});

it('should load the OHRIMultiSelect component with alias "multiCheckbox"', async () => {
const result = await getFieldComponent('multiCheckbox');
expect(result).toEqual({ default: OHRIMultiSelect });
});

it('should return undefined if no matching component is found', async () => {
const result = await getFieldComponent('unknown');
expect(result).toBeUndefined();
});
});
22 changes: 20 additions & 2 deletions src/registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface PostSubmissionActionRegistration extends ComponentRegistration
export interface CustomControlRegistration extends Omit<ComponentRegistration, 'load'> {
loadControl: () => Promise<any>;
type: string;
alias?: string;
}
interface ValidatorRegistryItem extends RegistryItem {
component: FieldValidator;
Expand All @@ -55,81 +56,97 @@ export const baseFieldComponents: Array<CustomControlRegistration> = [
id: 'OHRIText',
loadControl: () => Promise.resolve({ default: OHRIText }),
type: 'text',
alias: '',
},
{
id: 'OHRIRadio',
loadControl: () => Promise.resolve({ default: OHRIRadio }),
type: 'radio',
alias: '',
},
{
id: 'OHRIDate',
loadControl: () => Promise.resolve({ default: OHRIDate }),
type: 'date',
alias: '',
},
{
id: 'OHRINumber',
loadControl: () => Promise.resolve({ default: OHRINumber }),
type: 'number',
alias: 'numeric',
},
{
id: 'OHRIMultiSelect',
loadControl: () => Promise.resolve({ default: OHRIMultiSelect }),
type: 'checkbox',
alias: 'multiCheckbox',
},
{
id: 'OHRIContentSwitcher',
loadControl: () => Promise.resolve({ default: OHRIContentSwitcher }),
type: 'content-switcher',
alias: '',
},
{
id: 'OHRIEncounterLocationPicker',
loadControl: () => Promise.resolve({ default: OHRIEncounterLocationPicker }),
type: 'encounter-location',
alias: '',
},
{
id: 'OHRIDropdown',
loadControl: () => Promise.resolve({ default: OHRIDropdown }),
type: 'select',
alias: '',
},
{
id: 'OHRITextArea',
loadControl: () => Promise.resolve({ default: OHRITextArea }),
type: 'textarea',
alias: '',
},
{
id: 'OHRIToggle',
loadControl: () => Promise.resolve({ default: OHRIToggle }),
type: 'toggle',
alias: '',
},
{
id: 'OHRIObsGroup',
loadControl: () => Promise.resolve({ default: OHRIObsGroup }),
type: 'group',
alias: '',
},
{
id: 'OHRIRepeat',
loadControl: () => Promise.resolve({ default: OHRIRepeat }),
type: 'repeating',
alias: '',
},
{
id: 'OHRIFixedValue',
loadControl: () => Promise.resolve({ default: OHRIFixedValue }),
type: 'fixed-value',
alias: '',
},
{
id: 'OHRIMarkdown',
loadControl: () => Promise.resolve({ default: OHRIMarkdown }),
type: 'markdown',
alias: '',
},
{
id: 'OHRIExtensionParcel',
loadControl: () => Promise.resolve({ default: OHRIExtensionParcel }),
type: 'extension-widget',
alias: '',
},
{
id: 'OHRIDateTime',
loadControl: () => Promise.resolve({ default: OHRIDate }),
type: 'datetime',
alias: '',
},
];

Expand Down Expand Up @@ -172,9 +189,10 @@ const fieldValidators: Array<ValidatorRegistryItem> = [
];

export const getFieldComponent = renderType => {
let lazy = baseFieldComponents.find(item => item.type == renderType)?.loadControl;
let lazy = baseFieldComponents.find(item => item.type == renderType || item?.alias == renderType)?.loadControl;
if (!lazy) {
lazy = getOHRIFormsStore().customControls.find(item => item.type == renderType)?.loadControl;
lazy = getOHRIFormsStore().customControls.find(item => item.type == renderType || item?.alias == renderType)
?.loadControl;
}
return lazy?.();
};
Expand Down

0 comments on commit ab7221d

Please sign in to comment.