Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextField label is messed up when component is controlled and value is set by other event trigger #38805

Closed
2 tasks done
zivl opened this issue Sep 4, 2023 · 2 comments
Closed
2 tasks done
Assignees
Labels
component: text field This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: expected behavior Does not imply the behavior is intended. Just that we know about it and can't work around it

Comments

@zivl
Copy link

zivl commented Sep 4, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

Link to live example:
https://9hyhks.csb.app/
https://codesandbox.io/s/cocky-yalow-9hyhks?file=/Demo.tsx

Steps:
When you click the "Click Me" button - you'll see the value is set while the label is still inside the input box and not going up as usual:

import * as React from "react";
import Box from "@mui/material/Box";
import TextField from "@mui/material/TextField";
import Button from "@mui/material/Button";

export default function BasicTextFields() {
  const [value, setValue] = React.useState<string | null>(null);
  return (
    <Box>
      <Button onClick={() => setValue("sssss")}>Click Me</Button>
      <TextField
        label="test"
        value={value}
        onClick={(e) => setValue(e.target.value as string)}
      />
    </Box>
  );
}

Current behavior 😯

When you click the "Click Me" button - you'll see the value is set while the label is still inside the input box and not going up as usual

Expected behavior 🤔

When you click the "Click Me" button - the label should go up to the input box's border

Context 🔦

No response

Your environment 🌎

"@emotion/react": "latest",
"@emotion/styled": "latest",
"@mui/material": "latest",
"@types/react": "latest",
"@types/react-dom": "latest",
"react": "latest",
"react-dom": "latest",
"typescript": "latest"
@zivl zivl added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 4, 2023
@DiegoAndai DiegoAndai self-assigned this Sep 4, 2023
@sai6855
Copy link
Contributor

sai6855 commented Sep 5, 2023

@zivl component should not be changed from uncontrolled to controlled within same render cycle. To make issue you described to fix, change Textfield to controlled component by setting initial value of value to empty string.

csb: https://codesandbox.io/s/billowing-fog-pzlwln?file=/Demo.tsx

Full working code:

import * as React from "react";
import Box from "@mui/material/Box";
import TextField from "@mui/material/TextField";
import Button from "@mui/material/Button";

export default function BasicTextFields() {
  const [value, setValue] = React.useState("");
  return (
    <Box>
      <Button onClick={() => setValue("sssss")}>Click Me</Button>
      <TextField
        label="test"
        value={value}
        onClick={(e) => setValue(e.target.value as string)}
      />
    </Box>
  );
}

@zivl
Copy link
Author

zivl commented Sep 5, 2023

oh.... wow! thanks, i thought passing null value should act the same as empty string ''
thanks for the clarification

@zivl zivl closed this as completed Sep 5, 2023
@sai6855 sai6855 added component: text field This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: expected behavior Does not imply the behavior is intended. Just that we know about it and can't work around it and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: expected behavior Does not imply the behavior is intended. Just that we know about it and can't work around it
Projects
None yet
Development

No branches or pull requests

3 participants