Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3692 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React from 'react';
2
import { Controller } from 'react-hook-form';
3
import { FormControl, InputLabel, MenuItem, Select as MuiSelect } from '@mui/material';
4
 
5
import FormErrorFeedback from '../form/FormErrorFeedback';
6
 
7
const Select = ({
8
  name,
9
  control,
10
  label,
11
  rules,
12
  defaultValue,
13
  onChange,
14
  value,
15
  error,
16
  placeholder = 'Seleccione una opción',
17
  options = [],
18
  ...props
19
}) => {
20
  if (control) {
21
    return (
22
      <Controller
23
        name={name}
24
        control={control}
25
        rules={rules}
26
        defaultValue={defaultValue}
27
        render={({ field }) => (
28
          <FormControl fullWidth>
29
            {label && <InputLabel shrink>{label}</InputLabel>}
30
 
31
            <MuiSelect
32
              {...field}
33
              fullWidth
34
              displayEmpty
35
              sx={{
36
                borderRadius: '4px'
37
              }}
38
              {...props}
39
            >
40
              <MenuItem value='' disabled>
41
                {placeholder}
42
              </MenuItem>
43
 
44
              {options.map(({ label, value }) => (
45
                <MenuItem key={value} value={value}>
46
                  {label}
47
                </MenuItem>
48
              ))}
49
            </MuiSelect>
50
 
51
            {error && <FormErrorFeedback>{error}</FormErrorFeedback>}
52
          </FormControl>
53
        )}
54
      />
55
    );
56
  }
57
 
58
  return (
59
    <FormControl fullWidth>
60
      {label && <InputLabel shrink>{label}</InputLabel>}
61
 
62
      <MuiSelect
63
        name={name}
64
        defaultValue={defaultValue}
65
        value={value}
66
        onChange={onChange}
67
        fullWidth
68
        displayEmpty
69
        sx={{
70
          borderRadius: '4px'
71
        }}
72
        {...props}
73
      >
74
        <MenuItem value='' disabled>
75
          {placeholder}
76
        </MenuItem>
77
 
78
        {options.map(({ label, value }) => (
79
          <MenuItem key={value} value={value}>
80
            {label}
81
          </MenuItem>
82
        ))}
83
      </MuiSelect>
84
 
85
      {error && <FormErrorFeedback>{error}</FormErrorFeedback>}
86
    </FormControl>
87
  );
88
};
89
 
90
export default Select;