reactjsmaterial-uimaterial-designformikformik-material-ui

How to reduce height and size of Material-UI<Select> input field in Material UI & formik?


I am making a web project using React, Material, Formik, formik-material-ui.

This is the input screen I am getting of my form. The select input height is bigger than the textfield inputs.

enter image description here

The InputField component is as follows:

import { Field } from "formik";
import { TextField } from "formik-material-ui";

const TextFieldStyle = {
    padding: 7,
    fontSize: "0.75rem",
};

export default (props: any) => {
    return (
        <Field
            component={TextField}
            inputProps={{
                style: TextFieldStyle,
            }}
            size="small"
            margin="none"
            variant="outlined"
            {...props} // add props at the key to override any user defined similar props
        >
            {props.children}
        </Field>
    );
};

The Select Field component is as follows:

import { Field } from "formik";
import { TextField } from "formik-material-ui";

const SelectFieldStyle = {
    padding: 7,
    fontSize: "0.75rem",
};

export default (props: any) => {
    return (
        <Field
            component={TextField}
            inputProps={{
                style: SelectFieldStyle,
            }}
            type="text"
            select={true}
            align="left"
            size="small"
            fullWidth
            margin="none"
            variant="outlined"
            {...props} // add props at the key to override any user defined similar props
        >
              <MenuItem value={1}>A</MenuItem>
              <MenuItem value={2}>B</MenuItem>
              <MenuItem value={3}>C</MenuItem>
        </Field>
    );
};

Changing the style in Select component is not bringing any visual change.

How can I bring the select component to the same height as input field?


Solution

  • Try using SelectProps on your Select field thusly:

    import { Field } from "formik";
    import { TextField } from "formik-material-ui";
    
    const SelectFieldStyle = {
        padding: 7,
        fontSize: "0.75rem",
    };
    
    export default (props: any) => {
        return (
            <Field
                component={TextField}
                SelectProps={{
                    style: SelectFieldStyle,
                }}
                type="text"
                select={true}
                align="left"
                size="small"
                fullWidth
                margin="none"
                variant="outlined"
                {...props} // add props at the key to override any user defined similar props
            >
                  <MenuItem value={1}>A</MenuItem>
                  <MenuItem value={2}>B</MenuItem>
                  <MenuItem value={3}>C</MenuItem>
            </Field>
        );
    };
    

    Here's the link to the API: https://material-ui.com/api/text-field/