Advertisement
Guest User

Togglable.js

a guest
Nov 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useImperativeHandle } from "react"
  2. import Button from "@material-ui/core/Button"
  3. import { makeStyles } from "@material-ui/core/styles"
  4.  
  5. const useStyles = makeStyles(theme => ({
  6.   button: {
  7.     margin: theme.spacing(1),
  8.   },
  9.   input: {
  10.     display: "none",
  11.   },
  12. }))
  13.  
  14. const Togglable = React.forwardRef((props, ref) => {
  15.   const classes = useStyles()
  16.   const [visible, setVisible] = useState(false)
  17.  
  18.   const hideWhenVisible = { display: visible ? "none" : "" }
  19.   const showWhenVisible = { display: visible ? "" : "none" }
  20.  
  21.   const toggleVisibility = () => {
  22.     setVisible(!visible)
  23.   }
  24.  
  25.   useImperativeHandle(ref, () => {
  26.     return {
  27.       toggleVisibility,
  28.     }
  29.   })
  30.  
  31.   return (
  32.     <div>
  33.       <div style={hideWhenVisible}>
  34.         <Button
  35.           variant="contained"
  36.           color="primary"
  37.           size="small"
  38.           className={classes.button}
  39.           onClick={toggleVisibility}
  40.         >
  41.           {props.buttonLabel}
  42.         </Button>
  43.       </div>
  44.       <div style={showWhenVisible}>
  45.         {props.children}
  46.         <Button
  47.           variant="contained"
  48.           color="secondary"
  49.           size="small"
  50.           className={classes.button}
  51.           onClick={toggleVisibility}
  52.         >
  53.           Cancel
  54.         </Button>
  55.       </div>
  56.     </div>
  57.   )
  58. })
  59.  
  60. export default Togglable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement