Ryan's Answer is quite informative. However, there have been some changes in the latest version of v5 documentation. The use of the labelWidth
prop is no longer supported. Instead, a two-step process is now required for the notch feature.
The method involving a notched
prop may seem a bit forced, as it results in a visible notch or whitespace at the top of the input box under all circumstances. If this is your desired outcome, you must include both notched={true}
(or simply notched
) and
label="Your label text here"
. This will create a notch aligned with the width of the specified label text.
<OutlinedInput notched={true} id="test" label="HI" />
In case the notched prop is not explicitly defined, the behavior will be such that the notch only appears when the input is focused. It disappears once the focus is removed, irrespective of any text present in the input field.
A more standard approach involves supplying a label for the input component. This is typically done when creating custom components using Material-UI elements. In this scenario, the notched prop does not need to be specified, as the default behavior suffices. Nevertheless, an additional InputLabel
component must be declared, enclosed within a FormControl
, for the notch to accommodate the necessary label.
<FormControl>
<InputLabel>Label Text</InputLabel>
<OutlinedInput label="Label Text" />
</FormControl>
The provided code set should effectively implement the label functionality. Nonetheless, it is advisable to include both a for
and id
attribute for enhanced accessibility with screen readers.
<FormControl>
<InputLabel htmlFor="test" id="testlabel">
Label Text
</InputLabel>
<OutlinedInput id="test" label="Label Text" />
</FormControl>