MUI Components
NPM

Customization

By now, you should have a solid understanding of the customization options available for all components. This section provides even more flexibility, enabling you to style components globally, eliminating the need to duplicate code across your application.

All components share at least two common props: FormLabel and FormHelperText. Components like checkbox or switch also include a FormControlLabel. All of these can be preconfigured to apply uniform styles across all components.

Additionally, there's an option to customize the default dateAdapter for date and time pickers, with AdapterDayjs being recommended by Material UI.

There is also a boolean flag, allLabelsAboveFields, to simplify label placement across form components. When set to true, this flag ensures that labels are rendered above all form fields globally, eliminating the need to manually specify showLabelAboveFormField prop for each component.

The showLabelAboveFormField prop always takes precedence over the flag, so you can set it on a single field to override the global setting:

  • Fields with an inline label, such as MUITextField, keep the label inside the field by default. showLabelAboveFormField={false} keeps it inside even when allLabelsAboveFields is true.
  • Fields with no inline label, such as MUISlider or MUIRating, show the label above the field by default. showLabelAboveFormField={false} hides the visible label, and the accessible name is still applied.
  • MUIOTPInput hides its label by default, unless allLabelsAboveFields is true. Set showLabelAboveFormField to true to show it, or false to hide it even when the flag is on.

ConfigProvider

To streamline customization, the library provides a ConfigProvider component and the default values in DefaultMUIComponentsConfig object, allowing you to override or extend the default values for components globally.

You can either apply these customizations across all forms by initializing ConfigProvider at the root level of your application or use it within a specific form as a parent component.

The type definition for ConfigProvider is listed below -

type MUIComponentsConfigInput = {
  defaultFormLabelSx?: SxProps;
  defaultFormControlLabelSx?: SxProps;
  defaultFormHelperTextSx?: SxProps;
  dateAdapter?: AdapterDayjs | AdapterDateFns | AdapterLuxon | AdapterMoment;
  allLabelsAboveFields?: boolean;
}

The table below lists the default sx styles applied to the FormLabel, FormControlLabel, and FormHelperText components.

Prop NameDefault Value
defaultFormLabelSx{ marginBottom: "0.75rem" }
defaultFormControlLabelSx{}
defaultFormHelperTextSx{ marginTop: "0.25rem", marginLeft: 0 }

Usage

import {
  ConfigProvider,
  DefaultMUIComponentsConfig
} from '@nish1896/mui-components/config';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

Override the default FormLabel and FormHelperText values by providing the desired values in defaultFormLabelSx and defaultFormHelperTextSx props which accepts all valid values in the sx prop.

Additionally you also need to specify a dateAdapter for date and time pickers using ConfigProvider component. For a full list of available date adapters, see the MUI date library options. Please do install the npm package of the corresponding date library.

<ConfigProvider
  defaultFormLabelSx={{ color: '#007bff' }}
  defaultFormControlLabelSx={{ color: '#ea3677', mr: '1rem' }}
  defaultFormHelperTextSx={{ mt: '0.75rem' }}
  dateAdapter={AdapterDayjs}
  allLabelsAboveFields
>
  <form>
    {/* Form content */}
  </form>
</ConfigProvider>

ConfigProvider works the same way whether applied once at your app root or scoped to a single form, as shown above.

Example

Head over to the customization example to see ConfigProvider applied to a set of reusable form components.