Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3452 stevensc 1
import React, { useState } from 'react';
2
import { useFormContext } from 'react-hook-form';
3472 stevensc 3
import { Button, IconButton, Box, Typography, FormControl, InputLabel } from '@mui/material';
3452 stevensc 4
import { styled } from '@mui/material/styles';
5
import PhotoCamera from '@mui/icons-material/PhotoCamera';
6
import DeleteIcon from '@mui/icons-material/Delete';
7
 
8
const VisuallyHiddenInput = styled('input')({
9
  clip: 'rect(0 0 0 0)',
10
  clipPath: 'inset(50%)',
11
  height: 1,
12
  overflow: 'hidden',
13
  position: 'absolute',
14
  bottom: 0,
15
  left: 0,
16
  whiteSpace: 'nowrap',
17
  width: 1
18
});
19
 
3472 stevensc 20
export function FormImagePicker({ name, label, styles, ...rest }) {
3452 stevensc 21
  const {
22
    register,
23
    setValue,
24
    watch,
25
    formState: { errors }
26
  } = useFormContext();
27
  const [previewImage, setPreviewImage] = useState(null);
28
  const watchedImage = watch(name);
29
 
30
  const handleChange = (event) => {
31
    const file = event.target.files?.[0];
32
    if (file) {
33
      setValue(name, file);
34
      const reader = new FileReader();
35
      reader.onloadend = () => {
36
        setPreviewImage(reader.result);
37
      };
38
      reader.readAsDataURL(file);
39
    }
40
  };
41
 
42
  const handleRemove = () => {
43
    setValue(name, null);
44
    setPreviewImage(null);
45
  };
46
 
47
  return (
3472 stevensc 48
    <FormControl variant='standard' fullWidth sx={styles}>
49
      {label && <InputLabel shrink>{label}</InputLabel>}
50
 
3452 stevensc 51
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 2 }}>
52
        <Button component='label' variant='outlined' startIcon={<PhotoCamera />}>
53
          Subir Imagen
54
          <VisuallyHiddenInput
55
            id={name}
56
            name={name}
57
            type='file'
58
            onChange={handleChange}
59
            {...register(name)}
60
            {...rest}
61
          />
62
        </Button>
3472 stevensc 63
 
3452 stevensc 64
        {previewImage && (
65
          <Box sx={{ position: 'relative' }}>
66
            <img
67
              src={previewImage}
68
              alt='Vista previa'
69
              style={{ height: 100, width: 100, objectFit: 'cover' }}
70
            />
71
            <IconButton
72
              aria-label='eliminar'
73
              size='small'
74
              onClick={handleRemove}
75
              sx={{
76
                position: 'absolute',
77
                top: -10,
78
                right: -10,
79
                backgroundColor: 'white',
80
                '&:hover': { backgroundColor: 'lightgray' }
81
              }}
82
            >
83
              <DeleteIcon fontSize='inherit' />
84
            </IconButton>
85
          </Box>
86
        )}
3472 stevensc 87
 
3452 stevensc 88
        {watchedImage instanceof File && !previewImage && (
89
          <Typography variant='caption' color='text.secondary'>
90
            Archivo seleccionado: {watchedImage.name}
91
          </Typography>
92
        )}
93
      </Box>
3472 stevensc 94
 
3452 stevensc 95
      {errors[name] && (
96
        <Typography variant='caption' color='error'>
97
          {errors[name]?.message}
98
        </Typography>
99
      )}
3472 stevensc 100
    </FormControl>
3452 stevensc 101
  );
102
}