Skip to content

Commit

Permalink
Refactor ComponentRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
holubv committed Apr 14, 2021
1 parent 5138a70 commit 40f4721
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/components/FormManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FormManager extends React.Component {

_mapQuestion(question, index) {

let component = ComponentRegistry.mapQuestion(question, index);
let component = ComponentRegistry.mapComponent(question, index);
return React.createElement(component, {
key: question['@id'],
question: question,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export default class Question extends React.Component {
for (let i = 0; i < subQuestions.length; i++) {

let question = subQuestions[i];
let component = ComponentRegistry.mapQuestion(question, i);
let component = ComponentRegistry.mapComponent(question, i);

let element = React.createElement(component, {
key: 'sub-question-' + i,
Expand Down
2 changes: 1 addition & 1 deletion src/components/wizard/Wizard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const Wizard = () => {

const step = stepData[currentStep];

let stepComponent = ComponentRegistry.mapWizardStep(step);
let stepComponent = ComponentRegistry.mapComponent(step, currentStep, WizardStep);
return React.createElement(stepComponent, {
options: options,
key: 'step' + currentStep,
Expand Down
4 changes: 2 additions & 2 deletions src/components/wizard/WizardStep.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class WizardStep extends React.Component {

const categoryClass = Question._getQuestionCategoryClass(this.props.step);

let questionComponent = ComponentRegistry.mapQuestion(this.props.step, 0);
let questionComponent = ComponentRegistry.mapComponent(this.props.step, 0);
let questionElement = React.createElement(questionComponent, {
question: this.props.step,
onChange: this.onChange,
Expand Down Expand Up @@ -99,4 +99,4 @@ WizardStep.propTypes = {
stepIndex: PropTypes.number.isRequired,
isFirstStep: PropTypes.bool,
isLastStep: PropTypes.bool
};
};
24 changes: 5 additions & 19 deletions src/util/ComponentRegistry.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
import Question from '../components/Question';
import WizardStep from '../components/wizard/WizardStep';

let components = [];
let wizardSteps = [];

export default class ComponentRegistry {

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

static registerWizardStep(component, mapRule) {
wizardSteps.push({ component, mapRule });
}

static mapQuestion(question, index) {

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

return Question;
}

static mapWizardStep(step) {

for (let { component, mapRule } of wizardSteps) {
if (mapRule(step)) {
return component;
}
if (def !== undefined) {
return def;
}

return WizardStep;
return Question;
}

}
}

0 comments on commit 40f4721

Please sign in to comment.