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

[material-ui][TextField] slotProps.htmlInput.component does not replace input html element #44580

Closed
balazs-zsoldos opened this issue Nov 27, 2024 · 4 comments
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 typescript

Comments

@balazs-zsoldos
Copy link

balazs-zsoldos commented Nov 27, 2024

Steps to reproduce

The following works and replaces the wrapper div of the input element:

<TextField slotProps={{ input: { component: () => <div>Hello world</div> } }} />

The following does not work:

<TextField slotProps={{ htmlInput: { component: () => <div>Hello world</div> } }} />

Current behavior

Does not replace the element in the rendered component.

Expected behavior

Replaces the <input element in the rendered component.

Context

I would like to put a link instead of the html element in the component, so I can keep other e.g.: shrinked label, so the link will fit with the other fields in the form.

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: htmlinput

@balazs-zsoldos balazs-zsoldos added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 27, 2024
@sai6855
Copy link
Contributor

sai6855 commented Nov 28, 2024

@balazs-zsoldos use slots.htmlInput to replace wrapper around htmlInput

Paste below code here for working demo

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

export default function BasicTextFields() {
  return (
    <Box
      component="form"
      sx={{ '& > :not(style)': { m: 1, width: '25ch' } }}
      noValidate
      autoComplete="off"
    >
      <TextField slots={{htmlInput:()=><div>hello</div>}} id="outlined-basic" label="Outlined" variant="outlined" />
    
    </Box>
  );
}

component prop is not supported in htmlInput. I believe the reason TypeScript doesn't throw a type error is that, currently, InputBaseComponentProps accepts any string.

current:

export interface InputBaseComponentProps
extends React.HTMLAttributes<HTMLInputElement | HTMLTextAreaElement> {
// Accommodate arbitrary additional props coming from the `inputProps` prop
[arbitrary: string]: any;
}

Updating the InputBaseComponentProps type as shown below will cause TypeScript to throw an error when the component prop is passed to htmlInput. cc @DiegoAndai @ZeeshanTamboli wdyt, should i make this change?

Post change:

export interface InputBaseComponentProps
  extends React.HTMLAttributes<HTMLInputElement | HTMLTextAreaElement> {
  // Accommodate arbitrary additional props coming from the `inputProps` prop
  [arbitrary: string]: any;
  component?: never;
}

@sai6855 sai6855 added component: text field This is the name of the generic UI component, not the React module! typescript package: material-ui Specific to @mui/material labels Nov 28, 2024
@aarongarciah aarongarciah added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 28, 2024
@aarongarciah aarongarciah self-assigned this Nov 28, 2024
@DiegoAndai DiegoAndai added the status: expected behavior Does not imply the behavior is intended. Just that we know about it and can't work around it label Nov 29, 2024
@DiegoAndai
Copy link
Member

Hey, @balazs-zsoldos, thanks for the report. As @sai6855 explained, this is expected as the HTML input element does not accept a component prop/attribute. Did the provided example work for you?

@sai6855 I wouldn't change this type at this moment.

@balazs-zsoldos
Copy link
Author

balazs-zsoldos commented Dec 2, 2024

@DiegoAndai @sai6855 Thanks for the clarification. All clear now.

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Dec 2, 2024
@sai6855 sai6855 removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 2, 2024
@sai6855 sai6855 closed this as completed Dec 2, 2024
Copy link

github-actions bot commented Dec 2, 2024

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@balazs-zsoldos How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

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 typescript
Projects
None yet
Development

No branches or pull requests

4 participants