Skip to content

Commit

Permalink
Create component registry
Browse files Browse the repository at this point in the history
  • Loading branch information
holubv committed Mar 16, 2021
1 parent e1a906c commit 7b5c84d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/components/FormManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ import Card from 'react-bootstrap/Card';
import JsonLdUtils from 'jsonld-utils';
import Constants from '../constants/Constants';
import CompositeQuestion from './CompositeQuestion';
import ComponentRegistry from '../util/ComponentRegistry';

class FormManager extends React.Component {

constructor(props) {
super(props);

ComponentRegistry.registerComponent(
CompositeQuestion,
q => JsonLdUtils.getJsonAttValue(q, Constants.COMPOSITE_PATTERN)
);
}

getFormData = () => {
const data = this.context.getData();
const formQuestionsData = this.context.getFormQuestionsData();
Expand Down Expand Up @@ -38,21 +49,17 @@ class FormManager extends React.Component {

_mapQuestion(question, index) {

if (JsonLdUtils.getJsonAttValue(question, Constants.COMPOSITE_PATTERN)) {
return (<CompositeQuestion
key={question['@id']}
question={question}
onChange={(index, change) => this.onStepChange(question, index, change)}
index={index}
/>);
let component = ComponentRegistry.mapQuestion(question, index);
if (!component) {
component = Question;
}

return (<Question
key={question['@id']}
question={question}
onChange={(index, change) => this.onStepChange(question, index, change)}
index={index}
/>);
return React.createElement(component, {
key: question['@id'],
question: question,
onChange: (index, change) => this.onStepChange(question, index, change),
index: index
});
}

render() {
Expand Down
20 changes: 20 additions & 0 deletions src/util/ComponentRegistry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let components = [];

export default class ComponentRegistry {

static registerComponent(component, mapRule) {
components.push({ component, mapRule });
}

static mapQuestion(question, index) {

for (let { component, mapRule } of components) {
if (mapRule(question, index)) {
return component;
}
}

return null;
}

}

0 comments on commit 7b5c84d

Please sign in to comment.