-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added multiple choice type - copied from CeMaFac (#138)
* Added multiple choice type - copied from CeMaFac * Added other type as an input * Added styling * Fix the import * Improving styling * Version bump
- Loading branch information
Showing
7 changed files
with
327 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { useState } from "react"; | ||
import PropTypes from "prop-types"; | ||
import { ListHeader } from "../list-header/list-header"; | ||
import { List } from "../list/list"; | ||
import { ListItem } from "../list-item/list-item"; | ||
import { Input } from "../input/input"; | ||
import "./multipleChoice.scss"; | ||
|
||
function MultipleChoice({ question, onAnswer, currentResponse = [] }) { | ||
const [answers, setAnswers] = useState([]); | ||
const isSelected = option => { | ||
return currentResponse.includes(option.value); | ||
}; | ||
|
||
const handleClick = option => { | ||
let newAnswers; | ||
if (answers.includes(option.value)) { | ||
newAnswers = answers.filter(item => item !== option.value); | ||
} else { | ||
newAnswers = [...answers, option.value]; | ||
} | ||
setAnswers(newAnswers); | ||
onAnswer({ | ||
questionId: question.questionId, | ||
value: newAnswers | ||
}); | ||
}; | ||
|
||
const onInputForOther = event => { | ||
let newValueForAnswers = [...answers]; | ||
if (event.target.value !== "") { | ||
newValueForAnswers = [...answers, event.target.value]; | ||
} | ||
|
||
onAnswer({ | ||
questionId: question.questionId, | ||
value: newValueForAnswers | ||
}); | ||
}; | ||
|
||
const choiceFor = option => { | ||
if (option.type === "OTHER") { | ||
const size = currentResponse.length; | ||
let existingValue = ""; | ||
if (size > 0 && isNaN(currentResponse[size - 1])) { | ||
existingValue = currentResponse[size - 1]; | ||
} | ||
return ( | ||
<div className={"__list-item other"}> | ||
<Input | ||
key={`answer_${question.questionId}_${option.value}`} | ||
usePlaceholder={false} | ||
onChange={onInputForOther} | ||
defaultValue={existingValue} | ||
label={option.label} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<ListItem | ||
key={`answer_${question.questionId}_${option.value}`} | ||
title={option.label} | ||
active={isSelected(option)} | ||
onClick={() => handleClick(option)} | ||
/> | ||
); | ||
}; | ||
return ( | ||
<div> | ||
<ListHeader title={question.questionText} /> | ||
<div> | ||
<List>{question.options.map(option => choiceFor(option))}</List> | ||
</div> | ||
</div> | ||
); | ||
} | ||
MultipleChoice.propTypes = { | ||
question: PropTypes.shape({ | ||
questionId: PropTypes.number.isRequired, | ||
questionText: PropTypes.string.isRequired, | ||
type: PropTypes.oneOf([ | ||
"FINAL", | ||
"SINGLE_CHOICE", | ||
"MULTIPLE_CHOICE", | ||
"FREE_TEXT" | ||
]), | ||
options: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
label: PropTypes.string.isRequired, | ||
value: PropTypes.number.isRequired | ||
}) | ||
) | ||
}), | ||
onAnswer: PropTypes.func, | ||
currentResponse: PropTypes.arrayOf(PropTypes.number) | ||
}; | ||
export default MultipleChoice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.other > .field { | ||
width: 100% | ||
} |
Oops, something went wrong.
d15acc5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: