Skip to content

Commit

Permalink
Refactor QuestionWithAdvanced to render switch only
Browse files Browse the repository at this point in the history
  • Loading branch information
holubv committed May 6, 2021
1 parent 3f7ecc0 commit b36fc44
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 99 deletions.
1 change: 0 additions & 1 deletion src/SmartComponents.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import CompositeQuestion from "./components/CompositeQuestion";
import JsonLdUtils from "jsonld-utils";
import Constants from "./Constants";
import QuestionWithAdvanced from "./components/QuestionWithAdvanced";
import {Constants as SConstants, FormUtils} from "s-forms";
import WizardStepComponent from "./components/WizardStepComponent";
import QuestionWithUnit from "./components/QuestionWithUnit";
Expand Down
10 changes: 4 additions & 6 deletions src/components/SectionComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Question, FormUtils, Constants as SConstants, Answer, ConfigurationConte
import Constants from "../Constants";
import classNames from 'classnames';
import SmartComponents from "../SmartComponents";
import QuestionWithAdvanced from "./QuestionWithAdvanced";
import ShowAdvancedSwitch from "./ShowAdvancedSwitch";
import TypeQuestionAnswer from "./TypeQuestionAnswer";

export default class SectionComponent extends Question {
Expand All @@ -28,15 +28,12 @@ export default class SectionComponent extends Question {
_renderShowAdvanced() {
const question = this.props.question;

if (!QuestionWithAdvanced.mappingRule(question)) {
if (!ShowAdvancedSwitch.mappingRule(question)) {
return null;
}

return (
<QuestionWithAdvanced
{...this.props}
switchOnly={true}
/>
<ShowAdvancedSwitch {...this.props} />
);
}

Expand Down Expand Up @@ -117,6 +114,7 @@ export default class SectionComponent extends Question {

_getSubQuestions() {
const sub = super._getSubQuestions();

return sub;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import JsonldUtils from 'jsonld-utils';
import PropTypes from "prop-types";
import Utils from "../Utils";

export default class QuestionWithAdvanced extends Question {
export default class ShowAdvancedSwitch extends Question {

static mappingRule = q => Utils.hasSubQuestionWithValue(q, Constants.SHOW_ADVANCED_QUESTION, true);

Expand Down Expand Up @@ -49,12 +49,12 @@ export default class QuestionWithAdvanced extends Question {
};

_getShowAdvancedQuestion() {
return QuestionWithAdvanced.findShowAdvancedQuestion(this.props.question);
return ShowAdvancedSwitch.findShowAdvancedQuestion(this.props.question);
}

_getShowAdvancedState() {
let {question} = this._getShowAdvancedQuestion();
return QuestionWithAdvanced.isShowAdvanced(question);
return ShowAdvancedSwitch.isShowAdvanced(question);
}

_toggleAdvanced = (e) => {
Expand Down Expand Up @@ -111,89 +111,8 @@ export default class QuestionWithAdvanced extends Question {
}

render() {

if (this.props.switchOnly === true) {
return this._renderSwitch();
}

const question = this.props.question;

if (FormUtils.isHidden(question)) {
return null;
}

if (!FormUtils.isRelevant(question)) {
return null;
}

const {collapsible, withoutCard} = this.props;
const categoryClass = Question._getQuestionCategoryClass(question);

if (withoutCard) {
return <div>{this._renderQuestionContent()}</div>;
}
const label = JsonLdUtils.getLocalized(question[JsonLdUtils.RDFS_LABEL], this.context.options.intl);

const headerClassName = classNames(
FormUtils.isEmphasised(question) ? Question.getEmphasizedClass(question) : 'bg-info',
collapsible ? 'cursor-pointer' : '',
this.state.expanded ? 'section-expanded' : 'section-collapsed'
);

if (FormUtils.isAnswerable(question)) {
return this.renderAnswerableSection();
}

const cardBody = (
<Card.Body className={classNames('p-3', categoryClass)}>{this._renderQuestionContent()}</Card.Body>
);

return (
<Accordion defaultActiveKey={!this.state.expanded ? label : undefined}>
<Card className="mb-3">
<Accordion.Toggle as={Card.Header} onClick={this._toggleCollapse} className={headerClassName}>
<h6 className="d-inline" id={question['@id']}>
{collapsible && this._renderCollapseToggle()}
{label}
</h6>

{this._renderQuestionHelp()}

{this._renderSwitch()}

</Accordion.Toggle>
{collapsible ? <Accordion.Collapse>{cardBody}</Accordion.Collapse> : {cardBody}}

</Card>
</Accordion>
);
return this._renderSwitch();
}

// renderAnswers() {
// const question = this.props.question;
//
// if (!FormUtils.isAnswerable(question)) {
// return super.renderAnswers();
// }
//
// const answer = this._getAnswers()[0];
//
// let cls = classNames(
// 'answer',
// Question._getQuestionCategoryClass(question),
// Question.getEmphasizedOnRelevantClass(question)
// );
//
// return [(
// <div key={'row-item-0'} className={cls} id={question['@id']}>
// <Answer index={0} answer={answer} question={question} onChange={this.onAnswerChange} />
// {this._renderSwitch()}
// </div>
// )];
// }

}

QuestionWithAdvanced.contextType = ConfigurationContext;

QuestionWithAdvanced.propTypes.switchOnly = PropTypes.bool;
ShowAdvancedSwitch.contextType = ConfigurationContext;
7 changes: 3 additions & 4 deletions src/components/WizardStepComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {WizardStep, Question, Constants as SConstants, HelpIcon, FormUtils, FormQuestionsContext} from 's-forms';
import {Card, Form} from 'react-bootstrap';
import JsonLdUtils from 'jsonld-utils';
import QuestionWithAdvanced from "./QuestionWithAdvanced";
import ShowAdvancedSwitch from "./ShowAdvancedSwitch";
import Utils from "../Utils";
import Constants from "../Constants";

Expand All @@ -14,16 +14,15 @@ export default class WizardStepComponent extends WizardStep {
_renderShowAdvanced() {
const question = this.props.step;

if (!QuestionWithAdvanced.mappingRule(question)) {
if (!ShowAdvancedSwitch.mappingRule(question)) {
return null;
}

return (
<QuestionWithAdvanced
<ShowAdvancedSwitch
question={this.props.step}
onChange={this.onChange}
index={this.props.stepIndex}
switchOnly={true}
/>
);
}
Expand Down
2 changes: 0 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import SmartComponents from "./SmartComponents";
import Constants from "./Constants";

import CompositeQuestion from "./components/CompositeQuestion";
import QuestionWithAdvanced from "./components/QuestionWithAdvanced";
import WizardStepComponent from "./components/WizardStepComponent";


Expand All @@ -12,6 +11,5 @@ export {
Constants,

CompositeQuestion,
QuestionWithAdvanced,
WizardStepComponent
};

0 comments on commit b36fc44

Please sign in to comment.