Skip to content

Commit

Permalink
Merge pull request #448 from ubc-biztech/other-checkbox
Browse files Browse the repository at this point in the history
[146] Other Checkbox
  • Loading branch information
ddennis924 authored Aug 28, 2023
2 parents e0e6b8c + 885d668 commit 5712af7
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 25 deletions.
25 changes: 21 additions & 4 deletions src/pages/admin/DynamicForm/FormRegister.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
BASIC_QUESTIONS,
QUESTION_DOMAINS
} from "constants/index";
import OtherCheckbox from "./components/OtherCheckbox";

const styles = {
// Container for custom form image
Expand Down Expand Up @@ -236,6 +237,10 @@ const FormRegister = (props) => {
Array.from(Array(formData.questions.length))
); // index of errors correspond to responses array (right above)

// data from "Other" checkboxes
const [otherData] = useState({
});

useEffect(() => {
const fetchEvent = async () => {
if (!registeredEvents && user) {
Expand Down Expand Up @@ -385,8 +390,10 @@ const FormRegister = (props) => {
// todo; check if response already exists (shouldn't happen, but to be safe)

if (checked) {
// add
responses[index].push(value);
if (value) {
// add
responses[index].push(value);
}
} else {
// remove
const newArr = responses[index].filter((choice) => choice !== value);
Expand All @@ -395,7 +402,9 @@ const FormRegister = (props) => {
} else {
// no items in yet
const initialArr = [];
initialArr.push(value);
if (value) {
initialArr.push(value);
}
responses[index] = initialArr;
}
setResponseData(responses);
Expand Down Expand Up @@ -480,6 +489,9 @@ const FormRegister = (props) => {
<FormControl error={!!responseError[i]}>
<FormGroup>
{choicesArr.map((item) => {
if (item === "...") {
return <OtherCheckbox key={item} onChange={(e) => updateCheckbox(i, e.target.checked, null)} otherData={otherData} index={i}/>;
}
return (
<FormControlLabel
key={item}
Expand Down Expand Up @@ -768,7 +780,7 @@ const FormRegister = (props) => {
if (question.questionType === "CHECKBOX") {
// check if empty
if (question.required) {
if (!responseData[i] || responseData[i].length <= 0) {
if (!otherData[i] && (!responseData[i] || responseData[i].length <= 0)) {
newErrors[i] = "A selection is required";
valid = false;
}
Expand Down Expand Up @@ -898,6 +910,11 @@ const FormRegister = (props) => {
};
for (let i = BASIC_QUESTIONS.length; i < formData.questions.length; i++) {
if (formData.questions[i].questionType === "CHECKBOX") {
if (otherData[i]) {
dynamicResponses[formData.questions[i].questionId] = responseData[
i
].push(otherData[i]);
}
dynamicResponses[formData.questions[i].questionId] = responseData[
i
]?.join(", ");
Expand Down
27 changes: 22 additions & 5 deletions src/pages/admin/DynamicForm/FormRegisterPartner.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
} from "../../../constants/_constants/theme";
import ImagePlaceholder from "../../../assets/placeholder.jpg";
import Loading from "pages/Loading";
import OtherCheckbox from "./components/OtherCheckbox";

const styles = {
// Container for custom form image
Expand Down Expand Up @@ -219,6 +220,8 @@ const FormRegisterPartner = (props) => {
const [responseError, setResponseError] = useState(
Array.from(Array(formData.questions.length))
); // index of errors correspond to responses array (right above)
const [otherData] = useState({
});

useEffect(() => {
const fetchEvent = async () => {
Expand Down Expand Up @@ -249,8 +252,10 @@ const FormRegisterPartner = (props) => {
// todo; check if response already exists (shouldn't happen, but to be safe)

if (checked) {
// add
responses[index].push(value);
if (value) {
// add
responses[index].push(value);
}
} else {
// remove
const newArr = responses[index].filter((choice) => choice !== value);
Expand All @@ -259,7 +264,9 @@ const FormRegisterPartner = (props) => {
} else {
// no items in yet
const initialArr = [];
initialArr.push(value);
if (value) {
initialArr.push(value);
}
responses[index] = initialArr;
}
setResponseData(responses);
Expand Down Expand Up @@ -331,6 +338,9 @@ const FormRegisterPartner = (props) => {
<FormControl error={!!responseError[i]}>
<FormGroup>
{choicesArr.map((item) => {
if (item === "...") {
return <OtherCheckbox key={item} onChange={(e) => updateCheckbox(i, e.target.checked, null)} otherData={otherData} index={i}/>;
}
return (
<FormControlLabel
key={item}
Expand Down Expand Up @@ -540,7 +550,7 @@ const FormRegisterPartner = (props) => {
if (question.questionType === "CHECKBOX") {
// check if empty
if (question.required) {
if (!responseData[i] || responseData[i].length <= 0) {
if (!otherData[i] && (!responseData[i] || responseData[i].length <= 0)) {
newErrors[i] = "A selection is required";
valid = false;
}
Expand Down Expand Up @@ -576,7 +586,14 @@ const FormRegisterPartner = (props) => {
};
for (let i = basicQuestions.length; i < formData.questions.length; i++) {
if (formData.questions[i].questionType === "CHECKBOX") {
dynamicResponses[formData.questions[i].questionId] = responseData[i]?.join(", ");
if (otherData[i]) {
dynamicResponses[formData.questions[i].questionId] = responseData[
i
].push(otherData[i]);
}
dynamicResponses[formData.questions[i].questionId] = responseData[
i
]?.join(", ");
} else {
dynamicResponses[formData.questions[i].questionId] = responseData[i];
}
Expand Down
38 changes: 22 additions & 16 deletions src/pages/admin/DynamicForm/components/CustomQuestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,29 @@ const CustomQuestion = (props) => {
)}

{(type === "SELECT" || type === "CHECKBOX") && (
<TextField
id={`${id}.choices`}
name={`${name}.choices`}
label="Options"
fullWidth
required
margin="normal"
variant="filled"
onChange={handleChange}
onBlur={handleBlur}
error={showError("choices")}
helperText={
showError("choices") &&
<>
<TextField
id={`${id}.choices`}
name={`${name}.choices`}
label="Options"
fullWidth
required
margin="normal"
variant="filled"
onChange={handleChange}
onBlur={handleBlur}
error={showError("choices")}
helperText={
showError("choices") &&
errors.registrationQuestions[index].choices
}
value={choices}
/>
}
value={choices}
/>
<p style={{
color: "#FFFFFF",
opacity: "0.7"
}}>{"Separate options by comma, no spaces (Option 1,Option 2)"} <br/> {"To use the 'Other' option, add an ellipsis (Option 1,Option 2,...)"}</p>
</>
)}

{type === "WORKSHOP SELECTION" && (
Expand Down
78 changes: 78 additions & 0 deletions src/pages/admin/DynamicForm/components/OtherCheckbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, {
useEffect,
useState
} from "react";
import Checkbox from "@material-ui/core/Checkbox";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import TextField from "@material-ui/core/TextField";
import {
makeStyles
} from "@material-ui/core/styles";

const OtherCheckbox = ({
onChange, otherData, index
}) => {
const useStyles = makeStyles((theme) => ({
otherContainer: {
display: "flex",
alignItems: "center",
},
otherCheckbox: {
marginRight: 0
}
}));
const classes = useStyles();
const [checked, setChecked] = useState(false);
const [otherValue, setOtherValue] = useState("");

useEffect(() => {
if (checked && otherValue) {
otherData[index] = otherValue;
} else if (!checked && otherValue) {
delete otherData[index];
setOtherValue("");
}
}, [otherValue, checked]);

const handleCheckboxChange = (event) => {
setChecked(event.target.checked);
onChange(event);
};

const handleInputChange = (event) => {
setOtherValue(event.target.value);
};

return (
<>
<div className={classes.otherContainer}>
<FormControlLabel
className={checked ? classes.otherCheckbox : ""}
control={
<Checkbox
checked={checked}
onChange={handleCheckboxChange}
color="primary"
/>
}
label={checked ? "" : "Other"}
/>
{checked && (
<TextField
placeholder='Specify'
value={otherValue}
onChange={handleInputChange}
/>
)}
</div>
<p style={{
color: "#FFFFFF",
opacity: "0.7",
margin: 0
}}>To add multiple options, separate by comma, no spaces (Opion 1,Option 2)</p>
</>
);
};

export default OtherCheckbox;

5 changes: 5 additions & 0 deletions src/pages/admin/DynamicForm/components/QuestionPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "@material-ui/core";
import CloudUpload from "@material-ui/icons/CloudUpload";
import ImagePlaceholder from "../../../../assets/placeholder.jpg";
import OtherCheckbox from "./OtherCheckbox";

const styles = {
imageContainer: {
Expand Down Expand Up @@ -93,6 +94,10 @@ const QuestionPreview = (props) => {
)}
<FormGroup>
{choicesArr.map((item) => {
if (item === "...") {
return <OtherCheckbox key={item} otherData={{
}} onChange={() => {}}/>;
}
return (
<FormControlLabel
key={item}
Expand Down

0 comments on commit 5712af7

Please sign in to comment.