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

Autocomplete with TextField sloteProps - maxLength - issue #43869

Closed
yanai101 opened this issue Sep 24, 2024 · 2 comments
Closed

Autocomplete with TextField sloteProps - maxLength - issue #43869

yanai101 opened this issue Sep 24, 2024 · 2 comments
Assignees
Labels
component: autocomplete This is the name of the generic UI component, not the React module! support: Stack Overflow Please ask the community on Stack Overflow

Comments

@yanai101
Copy link

yanai101 commented Sep 24, 2024

Steps to reproduce

Link to live example: (required)

use Autocomplete with solo + try to limit the max length in the input

  <Autocomplete
        multiple
        id="tags-filled"
        options={top100Films.map((option) => option.title)}
        defaultValue={[top100Films[13].title]}
        freeSolo
        renderTags={(value, getTagProps) =>
          value.map((option, index) => {
            const { key, ...tagProps } = getTagProps({ index });
            return (
              <Chip variant="outlined" label={option} key={key} {...tagProps} />
            );
          })
        }
        renderInput={(params) => (
          <TextField
            {...params}
            slotProps={{
              htmlInput: { maxLength: 4 },
            }}
            variant="filled"
            label="freeSolo"
            placeholder="Favorites"
          />
        )}
      />

Steps:

  1. type any value in the input text
  2. press enter

BTW -

inputProps={{
                  ...params.inputProps,
                  maxLength: 5 // Limit input to 5 characters
                }}

work as expected

Current behavior

the value stays on the screen - not disappear and not added to the chips does

Expected behavior

value need to be added to the chips list

Context

i try to limit the length of the char in the chips

Your environment

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

Search keywords: maxLength, Autocomplete

@yanai101 yanai101 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 24, 2024
@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Sep 24, 2024
@mj12albert
Copy link
Member

@yanai101 Sorry your sandbox crashes immediately when I focus the <input> element 😓

i try to limit the length of the char in the chips
You should do it here:

renderTags={(value, getTagProps) =>
          value.map((option, index) => {
            const { key, ...tagProps } = getTagProps({ index });
            return (
              <Chip variant="outlined" label={option} key={key} {...tagProps} />
            );
          })
        }

extract the children or displayed text from the "Tag" out of tagProps before passing it into the Chip

The maxLength attribute passed to the input element could not affect anything outside of itself here

@mj12albert mj12albert added support: Stack Overflow Please ask the community on Stack Overflow and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Oct 24, 2024
Copy link

👋 Thanks for using this project!

We use GitHub issues exclusively as a bug and feature requests tracker, however, this issue appears to be a support request.

For support with Material UI please check out https://mui.com/material-ui/getting-started/support/. Thanks!

If you have a question on Stack Overflow, you are welcome to link to it here, it might help others.
If your issue is subsequently confirmed as a bug, and the report follows the issue template, it can be reopened.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module! support: Stack Overflow Please ask the community on Stack Overflow
Projects
None yet
Development

No branches or pull requests

3 participants