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';
3
import { Button, IconButton, Box, Typography } from '@mui/material';
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
 
20
export function FormImagePicker({ name, label, ...rest }) {
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 (
48
    <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
49
      <Typography component='label' htmlFor={name} sx={{ display: 'block', mb: 1 }}>
50
        {label}
51
      </Typography>
52
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 2 }}>
53
        <Button component='label' variant='outlined' startIcon={<PhotoCamera />}>
54
          Subir Imagen
55
          <VisuallyHiddenInput
56
            id={name}
57
            name={name}
58
            type='file'
59
            onChange={handleChange}
60
            {...register(name)}
61
            {...rest}
62
          />
63
        </Button>
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
        )}
87
        {watchedImage instanceof File && !previewImage && (
88
          <Typography variant='caption' color='text.secondary'>
89
            Archivo seleccionado: {watchedImage.name}
90
          </Typography>
91
        )}
92
      </Box>
93
      {errors[name] && (
94
        <Typography variant='caption' color='error'>
95
          {errors[name]?.message}
96
        </Typography>
97
      )}
98
    </Box>
99
  );
100
}