[TextControl] First element in container margin interferes with label absolute positioning #34113
Open
2 tasks done
Labels
bug 馃悰
Something doesn't work
component: text field
This is the name of the generic UI component, not the React module!
Duplicates
Latest version
Current behavior 馃槸
When placing a TextField or FormControl component inside of a container element such as a stack or DialogContent element, the first TextField element doesn't have any margin-top applied to it. This causes the label, onFocus to render outside the bounding box of the input element, and since there is no margin-top outside the parent container as well, resulting in the label being cut off.
A working (non-working) example is included here: https://codesandbox.io/s/react-mui-forked-fpt0xl?file=/dialog.tsx where the Enter title form input becomes cutoff.
Expected behavior 馃
I would expect the first element in a Stack or DialogContent, if it were an input element with an animated lable, to have enough margin for the label to be rendered outside the bounding box of the component and not be cutoff from the parent component. The code at issue seems to be similar to the following from mui-material/src/Stack/Stack.js lines 95-104:
I'm not sure how to solve this. On one hand, overriding the margin: 0 seems to be the answer so that input elements with labels always have the room to animate into the margin area, but on the other hand, this seems to be heavy-handed and would disrupt the layouts of many other first child elements in a Stack that don't require the margin to work properly. Perhaps all Inputs should be wrapped in a parent div that has padding of the appropriate number of pixels, to contain the Input element and leave room for the animation to work properly.
Steps to reproduce 馃暪
Steps:
Follow the link to a code sandbox example of this issue:
https://codesandbox.io/s/react-mui-forked-fpt0xl?file=/dialog.tsx
OR
Run this file within a React application:
import * as React from 'react';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import Stack from '@mui/material/Stack';
import DialogContent from '@mui/material/DialogContent';
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
import { DesktopDatePicker } from '@mui/x-date-pickers';
import { LocalizationProvider } from '@mui/x-date-pickers';
import { FormControl, InputLabel, Input, FormHelperText } from '@mui/material';
import DialogTitle from '@mui/material/DialogTitle';
import Backdrop from '@mui/material/Backdrop';
import Paper, { PaperProps } from '@mui/material/Paper';
import Draggable from 'react-draggable';
import TextField from '@mui/material/TextField';
import { DateTime } from 'luxon';
function PaperComponent(props: PaperProps) {
return (
<Draggable
handle="#draggable-dialog-title"
cancel={'[class*="MuiDialogContent-root"]'}
>
<Paper {...props} />
);
}
export default function DraggableDialog() {
const [postDate, setPostDate] = React.useState<DateTime | null>(
DateTime.now()
);
const [summary, setSummary] = React.useState('');
return (
<Dialog
maxWidth="lg"
fullWidth
open={true}
onClose={() => {}}
PaperComponent={PaperComponent}
aria-labelledby="draggable-dialog-title"
>
<DialogTitle style={{ cursor: 'move' }} id="draggable-dialog-title">
Add Story
Title
Enter title
<DesktopDatePicker
label="Date"
renderInput={(props) => <TextField {...props} />}
value={postDate}
onChange={(newValue: DateTime | null) => {
setPostDate(newValue);
}}
/>
Summary
<Input
id="story-summary"
aria-describedby="story-summary-text"
value={summary}
multiline
minRows={5}
onChange={(e) => {
const { value } = e.target;
if (value.length <= 1000) setSummary(e.target.value);
}}
/>
{
Enter Summary: (${summary.length}/1000
}<Button autoFocus onClick={() => {}}>
Cancel
<Button onClick={() => {}}>Add Story
);
}
Context 馃敠
I've run into this issue trying to design a basic pop-up form for a personal project and cannot help but think I'm not the first person that has run into the issue of having an input element cutoff from its label. I believe that as a software package that focuses on layout and expressiveness and provides a lot of styling out of the box, that this is just one of those things that should work without having to provide sx styles on a per-component basis.
Your environment 馃寧
npx @mui/envinfo
The text was updated successfully, but these errors were encountered: