Skip to content

Commit

Permalink
[Upd #65] Refactor default icons behavior in seperated file
Browse files Browse the repository at this point in the history
  • Loading branch information
LaChope committed Nov 26, 2021
1 parent c351064 commit 0ea71b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
36 changes: 17 additions & 19 deletions src/components/Question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,41 +379,35 @@ export default class Question extends React.Component {
static renderQuestionHelp(question, options, onCommentChange, showIcon) {
const icons = options.icons;
let questionHelpIcon;
if (!icons) questionHelpIcon = {id: Constants.ICONS.QUESTION_HELP, behavior: Constants.ICON_BEHAVIOR.ENABLE};
if (!icons) {
questionHelpIcon = Constants.DEFAULT_OPTIONS.icons.find(icon => {
return icon.id === Constants.ICONS.QUESTION_HELP
});
}
else questionHelpIcon = this.getIconFromIconList(icons, Constants.ICONS.QUESTION_HELP);
return this.getIconComponent(questionHelpIcon, question, options, onCommentChange, showIcon);
}

static renderQuestionComments = (question, options, onCommentChange, showIcon) => {
const icons = options.icons;
let questionCommentsIcon;
if (!icons) questionCommentsIcon = {id: Constants.ICONS.QUESTION_COMMENTS, behavior: Constants.ICON_BEHAVIOR.ON_HOVER};
if (!icons) {
questionCommentsIcon = Constants.DEFAULT_OPTIONS.icons.find(icon => {
return icon.id === Constants.ICONS.QUESTION_COMMENTS
});
}
else questionCommentsIcon = this.getIconFromIconList(icons, Constants.ICONS.QUESTION_COMMENTS);
return this.getIconComponent(questionCommentsIcon, question, options, onCommentChange, showIcon);
}

static renderIcons(question, options, onCommentChange, showIcon) {
const icons = options.icons;
let icons;
if (options.icons) icons = options.icons;
else icons = Constants.DEFAULT_OPTIONS.icons;
let iconsArray = [];
const renderQuestionHelp = Question.renderQuestionHelp(question, options, onCommentChange, showIcon);
const renderQuestionComments = Question.renderQuestionComments(question, options, onCommentChange, showIcon);

if (icons) this.sortIcons(icons, iconsArray, renderQuestionComments, renderQuestionHelp);
else {
iconsArray.push(
<React.Fragment key={Math.random()}>
<li className="icon-list-item">{renderQuestionHelp}</li>
<li className="icon-list-item">{renderQuestionComments}</li>
</React.Fragment>
);
}

return (<ol className="icon-list-items">
{iconsArray}
</ol>);
}

static sortIcons(icons, iconsArray, renderQuestionComments, renderQuestionHelp) {
for (let i = 0; i < icons.length; i++) {
if (icons[i].id === Constants.ICONS.QUESTION_COMMENTS) {
iconsArray.push(
Expand All @@ -426,6 +420,10 @@ export default class Question extends React.Component {
);
}
}

return (<ol className="icon-list-items">
{iconsArray}
</ol>);
}

_renderPrefixes() {
Expand Down
12 changes: 10 additions & 2 deletions src/constants/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,19 @@ export default class Constants {
static ICONS = {
QUESTION_COMMENTS: "questionComments",
QUESTION_HELP: "questionHelp"
}
};

static ICON_BEHAVIOR = {
ENABLE: "enable",
DISABLE: "disable",
ON_HOVER: "onHover"
}
};

// Default form options
static DEFAULT_OPTIONS = {
icons: [
{id: Constants.ICONS.QUESTION_HELP, behavior: Constants.ICON_BEHAVIOR.ENABLE},
{id: Constants.ICONS.QUESTION_COMMENTS, behavior: Constants.ICON_BEHAVIOR.ON_HOVER}
]
};
}

0 comments on commit 0ea71b8

Please sign in to comment.