Skip to content

Instantly share code, notes, and snippets.

@full-stack-concepts
Last active February 23, 2022 20:19
Show Gist options
  • Save full-stack-concepts/bcfb19d5d30c085b11dcb2a7a3dca659 to your computer and use it in GitHub Desktop.
Save full-stack-concepts/bcfb19d5d30c085b11dcb2a7a3dca659 to your computer and use it in GitHub Desktop.
Stepper-Step1
import React from 'react';
import {
Controller
} from 'react-hook-form';
import Box from '@mui/material/Box';
import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import Select from '@mui/material/Select';
import Typography from '@mui/material/Typography';
function SelectBox({
fieldName,
label,
startItemText,
value,
options,
control,
register,
onChange,
...props
}) {
return(<>
<Box sx={{ minWidth: 120 }}>
<FormControl fullWidth>
<InputLabel id={`${fieldName}-select`}>{label}</InputLabel>
<Controller
name={fieldName}
control={control}
defaultValue={value}
render={({ field: { value, name, ref }, fieldState: { error } }) => ( <>
<Select
labelId={`${fieldName}-demo`}
id={`${fieldName}`}
{...register( fieldName, {
onChange: (e) => onChange(e),
onBlur: (e) => onChange(e)
})}
value={value}
label={label}
>
<MenuItem value="0"><em>{startItemText}</em></MenuItem>
{options.map( (option,i) =>
<MenuItem value={option.value} key={i}>{`${option.label}`}</MenuItem>
)}
</Select>
{error &&
<Typography component="h6" variant="h6" color="red">
{error.message}
</Typography>
}
</>)}
/>
</FormControl>
</Box>
</>);
}
export default SelectBox;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment