-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #448 from ubc-biztech/other-checkbox
[146] Other Checkbox
- Loading branch information
Showing
5 changed files
with
148 additions
and
25 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
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; | ||
|
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