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 multiple tags are not rendering when value exists and options is empty Array #276

Open
2 tasks done
tt0mmy opened this issue Jun 12, 2024 · 3 comments
Open
2 tasks done

Comments

@tt0mmy
Copy link

tt0mmy commented Jun 12, 2024

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

The AutocompleteElement tags are not rendering properly compared to the regular controlled Autocomplete from @mui/material

Expected behavior 🤔

The tags should be rendered properly like it did in version 6.

I suspect it's because of this https://github.com/dohomi/react-hook-form-mui/blob/master/packages/rhf-mui/src/AutocompleteElement.tsx#L210

Can we return just newValue instead of mapping it to matchOptionByValue?

              return (
                multiple
                  ? (Array.isArray(newValue) ? newValue : []) : newValue

One workaround is to always include the transform prop.

              transform={{
                input(value) {
                  return value ?? [];
                },
                output(event, value, reason, details) {
                  return value;
                },
              }}

Steps to reproduce 🕹

The AutcompleteElement should render the tags like the Autocomplete component. When the value is present and the options are empty Array, the tags are still rendered properly in the regular Autocomplete component. This behavior should be the same for the AutocompleteElement component.
https://codesandbox.io/p/sandbox/great-minsky-f8jvcn?file=%2Fsrc%2FDemo.tsx%3A23%2C41

@tt0mmy tt0mmy changed the title Autocomplete multiple tags are not rendering properly Autocomplete multiple tags are not rendering when value exists and options is empty Array Jun 12, 2024
@sadik-malik
Copy link
Contributor

@tt0mmy, the link https://codesandbox.io/p/sandbox/great-minsky-f8jvcn?file=%2Fsrc%2FDemo.tsx%3A23%2C41 is not accessible. Could you set up a new CodeSandbox?

@tt0mmy
Copy link
Author

tt0mmy commented Sep 9, 2024

@tt0mmy, the link https://codesandbox.io/p/sandbox/great-minsky-f8jvcn?file=%2Fsrc%2FDemo.tsx%3A23%2C41 is not accessible. Could you set up a new CodeSandbox?

can u try this again https://codesandbox.io/p/sandbox/great-minsky-f8jvcn

@sadik-malik
Copy link
Contributor

@tt0mmy add multiple prop to the AutocompleteElement and remember to keep the previously selected options in the options prop.

Let me know if this helps.

<AutocompleteElement
          name="testing"
          label="TESTING"
          options={opts ?? []}
          required
          textFieldProps={{
            error: true,
            helperText: "Helper text goes here",
          }}
          multiple
          autocompleteProps={{
            size: "small",
            clearOnBlur: true,
            freeSolo: true,
            onInputChange(event, value, reason) {
              setReason(value);
              setTimeout(() => {
                setOpts((previous) => [
                  ...previous,
                  {
                    label: `${Math.random().toString()}`,
                    id: `${Math.random().toString()}`,
                  },
                ]);
              }, 300);
            },
          }}
        />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants