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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Tabs] Error preventing the tab to get selected #40237

Closed
2 tasks done
agustinzamar opened this issue Dec 18, 2023 · 2 comments
Closed
2 tasks done

[material-ui][Tabs] Error preventing the tab to get selected #40237

agustinzamar opened this issue Dec 18, 2023 · 2 comments
Assignees
Labels
component: tabs This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information

Comments

@agustinzamar
Copy link

agustinzamar commented Dec 18, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 馃暪

Link to live example:

Steps:

  1. I have a PrivateTab component which is a wrapper for the tab and it shows or hides the tab based on the user
export const PrivateTab = ({ validRoles, ...props }) => {
  const { auth: user } = useAuth();
  if (!hasAnyRole(user, validRoles)) return null;

  return <Tab {...props} />;
};
  1. Then I use it like this (The use tabs hook just adds it to the url so I can preserve the page state if the user shares the url)
export default function Index() {
  const { auth: user } = useAuth();
  const { tab, onChangeTab } = useTabs(user.role_id === RoleEnum.MANAGER ? 2 : 1);

  return (
    <>
        <Tabs value={tab} onChange={handleChangeTab} sx={{ my: 2 }} variant="scrollable" scrollButtons="auto">
          <PrivateTab validRoles={[RoleEnum.WAREHOUSE_MANAGER, RoleEnum.CARRIER]} label="Envios" value={1} />
          <Tab label="Entregas" value={2} />
        </Tabs>

      <TabPanel index={1} value={tab}>
        // Here the content...
      </TabPanel>

      <TabPanel index={2} value={tab}>
          // Here the content...
      </TabPanel>
    </>
  );
}
  1. This is the useTabs hook
export default function useTabs(defaultTab = 1) {
  const [searchParams, setSearchParams] = useSearchParams();

  const tab = Number(searchParams.get('tab')) || defaultTab;

  const handleChange = (event, newValue) => {
    setSearchParams({ tab: newValue });
  };

  return {
    tab,
    onChangeTab: handleChange,
  };
}

Current behavior 馃槸

I get the following error on the browser console and also the tab never gets underlined like when it is selected

MUI: The value provided to the Tabs component is invalid.
None of the Tabs' children match with "2".
You can provide one of the following values: 1, 2.

Expected behavior 馃

The tab has to be marked as selected and the error not shown in console since the value provided is valid

Context 馃敠

The idea of my code was to have some tabs displayed or not based on the role of the user

Your environment 馃寧

System:
OS: macOS 14.1.1
Binaries:
Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v18.17.1/bin/yarn
npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm
Browsers:
Chrome: 120.0.6099.109
Edge: 116.0.1938.81
Safari: 17.1
npmPackages:
@emotion/react: ^11.10.5 => 11.11.1
@emotion/styled: ^11.10.5 => 11.11.0
@mui/base: 5.0.0-beta.24
@mui/core-downloads-tracker: 5.14.18
@mui/icons-material: ^5.14.3 => 5.14.18
@mui/material: ^5.14.4 => 5.14.18
@mui/private-theming: 5.14.18
@mui/styled-engine: 5.14.18
@mui/system: 5.14.18
@mui/types: 7.2.9
@mui/utils: 5.14.18
@types/react: 18.2.39
react: ^18.2.0 => 18.2.0
react-dom: ^18.2.0 => 18.2.0
typescript: 4.9.5

@agustinzamar agustinzamar added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 18, 2023
@zannager zannager added the component: tabs This is the name of the generic UI component, not the React module! label Dec 19, 2023
@sai6855
Copy link
Contributor

sai6855 commented Dec 25, 2023

@agustinzamar can you please share a CodeSandbox that reproduces the problem you're facing so the maintainers can inspect it more easily?

@sai6855 sai6855 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 Dec 25, 2023
@danilo-leal danilo-leal changed the title The value provided to the Tabs component is invalid. None of the Tabs' children match with "2". You can provide one of the following values: 1, 2. [material-ui][Tabs] Error preventing the tab to get selected Dec 25, 2023
@danilo-leal danilo-leal added the package: material-ui Specific to @mui/material label Dec 25, 2023
Copy link

github-actions bot commented Jan 1, 2024

Since the issue is missing key information and has been inactive for 7 days, it has been automatically closed. If you wish to see the issue reopened, please provide the missing information.

@github-actions github-actions bot closed this as completed Jan 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: tabs This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information
Projects
None yet
Development

No branches or pull requests

5 participants