Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3473 | Rev 3475 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React from 'react';
import { Controller, useFormContext } from 'react-hook-form';
import { FormControl, InputLabel, MenuItem, Select as MuiSelect, Typography } from '@mui/material';

export function FormSelect({
  name,
  label,
  rules,
  placeholder = 'Seleccione una opción',
  options = [],
  style = {},
  ...props
}) {
  const { control } = useFormContext();

  return (
    <Controller
      name={name}
      control={control}
      rules={rules}
      render={({ field, fieldState: { error } }) => (
        <FormControl variant='standard' fullWidth sx={style}>
          {label && <InputLabel shrink>{label}</InputLabel>}

          <MuiSelect
            {...field}
            fullWidth
            displayEmpty
            sx={{
              marginTop: '0',
              borderRadius: '4px'
            }}
            {...props}
          >
            <MenuItem value='' disabled>
              {placeholder}
            </MenuItem>
            {options.map(({ label, value }) => (
              <MenuItem key={value} value={value}>
                {label}
              </MenuItem>
            ))}
          </MuiSelect>

          {error && (
            <Typography variant='caption' color='red'>
              {error.message}
            </Typography>
          )}
        </FormControl>
      )}
    />
  );
}