Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import React from 'react'
  2. import Button from '@material-ui/core/Button'
  3.  
  4. export interface BasicFileInputProps {
  5. label: string
  6. accept: string
  7. value: File | null
  8. onChange: (file: File | null) => void
  9. }
  10. const BasicFileInput: React.FC<BasicFileInputProps> = ({
  11. label,
  12. accept,
  13. value,
  14. onChange
  15. }) => {
  16. const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  17. if (!e.target.files) {
  18. onChange(null)
  19. return
  20. }
  21. const file = e.target.files[0]
  22.  
  23. if (!file) {
  24. onChange(null)
  25. return
  26. }
  27.  
  28. onChange(file)
  29. }
  30. return (
  31. <span>
  32. <Button variant="outlined" component="label">
  33. {label}
  34. <input
  35. onChange={handleChange}
  36. type="file"
  37. accept={accept}
  38. style={{ display: 'none' }}
  39. />
  40. </Button>
  41. {value && <span style={{ marginLeft: 8 }}>{value.name}</span>}
  42. </span>
  43. )
  44. }
  45.  
  46. export default BasicFileInput
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement