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

Maximum Call Stack Size Exceeded Error with slotProps in Storybook for TextField Component #45287

Open
MihirKotecha opened this issue Feb 11, 2025 · 2 comments
Assignees
Labels
component: text field This is the name of the generic UI component, not the React module! status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@MihirKotecha
Copy link

MihirKotecha commented Feb 11, 2025

Steps to reproduce

  1. Create a InputField component as shown below:

    import { TextField as MuiTextField, TextFieldProps as MuiTextFieldProps } from "@mui/material";
    import { ReactNode } from "react";
    
    export type InputFieldProps = MuiTextFieldProps & {
        id: string,
        children?: ReactNode;
        variant?: 'filled' | 'outlined' | 'standard';
    }
    
    export const InputField = ({ children, variant, ...props}: InputFieldProps) => {
        return (
            <MuiTextField {...props} variant={variant}>
                {children}
            </MuiTextField>
        )
    }
  2. Create a Storybook story for this component using slotProps for a password field with a toggle feature, similar to the code below:

    import { StoryFn } from "@storybook/react";
    import { InputField, InputFieldProps } from "../elements/Form";
    import { IconButton, InputAdornment } from "@mui/material";
    import { useState } from "react";
    import { Visibility, VisibilityOff } from "@mui/icons-material";
     
    export default {
      title: 'Input/Password With Toggle Input',
      component: InputField,
    };
     
    const customInputSlot = ({
      showPassword = true,
      handleClickShowPassword,
    }: {
      showPassword: boolean;
      handleClickShowPassword: () => void;
    }) => {
      return {
        input: {
          type: showPassword ? 'text' : 'password',
          endAdornment: (
            <InputAdornment position="end">
              <IconButton
                aria-label={showPassword ? 'hide the password' : 'display the password'}
                onClick={handleClickShowPassword}
                edge="end"
              >
                {showPassword ? <VisibilityOff /> : <Visibility />}
              </IconButton>
            </InputAdornment>
          ),
        },
      };
    };
     
    const Template: StoryFn<InputFieldProps> = (args) => {
      const [showPassword, setShowPassword] = useState(false);
     
      const handleClickShowPassword = () => {
        setShowPassword(!showPassword);
      };
     
      return (
        <InputField
          slotProps={customInputSlot({ showPassword, handleClickShowPassword })}
          {...args}
        />
      );
    };
     
    export const PasswordWithToggle = Template.bind({});
    PasswordWithToggle.args = {
      label: 'Password',
      id: 'password-input',
    };
  3. Run Storybook and load the story (PasswordWithToggle) to trigger the error.

  4. Observe the error: Upon loading the Storybook page for the PasswordWithToggle, you will see the error: Maximum call stack size exceeded.

Current behavior

When loading the Storybook page for the PasswordWithToggle story, the following error is triggered:

RangeError: Maximum call stack size exceeded

This error occurs when trying to render the InputField component with the slotProps for the password toggle functionality. The component does not render properly, and the page fails to load as expected.

The issue seems related to the recursive nature of the component rendering process, as indicated by the "Maximum call stack size exceeded" error message. This prevents the component from being displayed correctly in Storybook.

I have tried adjusting the slotProps structure and ensuring proper usage of state in the story, but the error persists.

Expected behavior

The InputField component with the password toggle functionality should render properly in Storybook without throwing any errors.

  • The password field should switch between "text" and "password" input types when the eye icon is clicked.
  • The endAdornment should display a clickable IconButton with either the Visibility or VisibilityOff icon, depending on whether the password is being shown or hidden.
  • The Storybook preview should render the InputField component correctly, with the password toggle functionality working as expected.
  • There should be no errors related to the component rendering process, such as "Maximum call stack size exceeded".

Context

No response

Your environment

npx @mui/envinfo
    System:
    OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
  Binaries:
    Node: 22.13.0 - ~/.nvm/versions/node/v22.13.0/bin/node
    npm: 11.0.0 - ~/.nvm/versions/node/v22.13.0/bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: 132.0.6834.110
  npmPackages:
    @emotion/react: ^11.14.0 => 11.14.0 
    @emotion/styled: ^11.14.0 => 11.14.0 
    @mui/core-downloads-tracker:  6.4.1 
    @mui/icons-material: ^6.4.1 => 6.4.1 
    @mui/material: ^6.4.1 => 6.4.1 
    @mui/private-theming:  6.4.1 
    @mui/styled-engine:  6.4.0 
    @mui/system:  6.4.1 
    @mui/types:  7.2.21 
    @mui/utils:  6.4.1 
    @types/react: ^18.3.18 => 18.3.18 
    react: ^18.3.1 => 18.3.1 
    react-dom: ^18.3.1 => 18.3.1 
    typescript: ~5.6.2 => 5.6.3 

Search keywords: TextField, React Storybook

@MihirKotecha MihirKotecha added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 11, 2025
@zannager zannager added the component: text field This is the name of the generic UI component, not the React module! label Feb 11, 2025
@aarongarciah aarongarciah added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 12, 2025
@aarongarciah
Copy link
Member

Hi @MihirKotecha, thanks for the report. Looks like the issue has nothing to do with Storybook. I suggest to update your code and avoid passing a new object to slotProps on each render. One way of achieving it is using React.useMemo, see this example: https://stackblitz.com/edit/vitejs-vite-hre9einv?file=src%2FApp.tsx

Let me know if this works in your case. If not, you'd need to provide a reproduction so we can take a look.

@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 Feb 12, 2025
@MihirKotecha
Copy link
Author

MihirKotecha commented Feb 12, 2025

https://stackblitz.com/edit/vitejs-vite-88t3tdp4?file=src%2Fstories%2FPasswordWithToggle.stories.tsx

Hi @aarongarciah, thnx for the response. I tried using useMemo hook but the issue still seems to persist. I have reproduced the issue in the above link. You can run npm run storybook to see the error. Also, inside the src/stories/PasswordWithToggle.stories.tsx file inside comments you can find the code where I have used the useMemo hook.

Please lmk if there is anything else needed from my side. Thnx again

@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 Feb 12, 2025
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! status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
None yet
Development

No branches or pull requests

4 participants