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][Autocomplete] Fix PaperComponent prop type #41837

Closed

Conversation

hectortav
Copy link

@mui-bot
Copy link

mui-bot commented Apr 10, 2024

Netlify deploy preview

https://deploy-preview-41837--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against f969326

@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Apr 10, 2024
@danilo-leal danilo-leal added the package: material-ui Specific to @mui/material label Apr 10, 2024
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hectortav Thanks for the PR. Are you encountering any issues with it? If so, could you provide a link to the corresponding GitHub issue in the description? Or is it simply an enhancement?

@ZeeshanTamboli ZeeshanTamboli added enhancement This is not a bug, nor a new feature and removed enhancement This is not a bug, nor a new feature labels Apr 14, 2024
@hectortav
Copy link
Author

Hello!
The current implementation will result in a Typescript error when passing props to the PaperComponent that do not exist in React.HTMLAttributes<HTMLElement> (eg. variant, square) or with any type added through extending the theme and TS augmentation. Please also view this example.

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Apr 15, 2024

Hello! The current implementation will result in a Typescript error when passing props to the PaperComponent that do not exist in React.HTMLAttributes<HTMLElement> (eg. variant, square) or with any type added through extending the theme and TS augmentation. Please also view this example.

Thanks for the example. This should be mentioned in the PR description when opening a PR. Could you add a TypeScript test related to this in Autocomplete.spec.tsx?

@ZeeshanTamboli ZeeshanTamboli added the bug 🐛 Something doesn't work label Apr 15, 2024
Comment on lines 176 to 193
type PaperComponentProps = { title?: string };
declare module '@mui/material/Paper' {
export interface PaperBaseProps extends PaperComponentProps {}
}

function PaperComponent({ title, children, ...props }: PaperProps) {
return (
<Paper {...props}>
{title}
{children}
</Paper>
);
}

function AutocompleteCustomPaper() {
return (
<Autocomplete
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} />}
PaperComponent={PaperComponent}
componentsProps={{
paper: {
title: 'Title',
variant: 'outlined',
},
}}
/>
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't testing what's needed. Because if I revert the PaperComponent type back to React.JSXElementConstructor<React.HTMLAttributes<HTMLElement>>, the test still passes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello again! Sorry for the late response. It should be fixed now

… extended props

[material-ui][Autocomplete] Fix typescript tests
@hectortav hectortav force-pushed the autocomplete-paper-component-type branch from 3501b67 to f039ff2 Compare April 18, 2024 08:20
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately, once the slots prop is introduced and utilized for the Autocomplete component, this PR won't serve much purpose as the PaperComponent will become deprecated. What are your thoughts on this, @DiegoAndai? (I suggest reading all the PR comments)

Comment on lines 181 to 184
// eslint-disable-next-line react/no-unstable-nested-components
PaperComponent={({ elevation, variant, ...props }) => (
<Paper {...{ elevation, variant }} {...props} />
)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rendering the component inline isn't recommended due to unnecessary re-renders which is mentioned in the eslint rule react/no-unstable-nested-components. However, it seems we can't test it any other way. Let's keep the PaperComponent type modification but remove this test. Developers can and should render a custom Paper component like this:

function CustomPaper(props: PaperProps) {
  return <Paper square {...props} />;
}

function AutocompleteCustomPaper() {
  return (
    <Autocomplete
      options={['one', 'two', 'three']}
      renderInput={(params) => <TextField {...params} />}
      PaperComponent={CustomPaper}
      componentsProps={{
        paper: {
          elevation: 2,
          variant: 'outlined',
        },
      }}
    />
  );
}

It seems okay not to have a test for the modification of the PaperComponent prop type because, apart from inlining a component (not recommended), we can't test it, with it having React.JSXElementConstructor type. In any case, we plan to deprecate this approach and use the slots prop instead (slots.paper).

@hectortav hectortav force-pushed the autocomplete-paper-component-type branch from 6ae1290 to befda08 Compare April 18, 2024 10:23
@DiegoAndai
Copy link
Member

Thanks for the callout @ZeeshanTamboli. We shouldn't merge this change as PaperComponent is soon to be deprecated. Same thought process as: #41165 (comment).

We currently have a PR open to deprecate PaperComponent in favor of slots: #41875. @lhilgert9 may I ask you to incorporate this fix into the slots types (but keeping the PaperComponent prop type unchanged)

@ZeeshanTamboli
Copy link
Member

Alright, I'll close this PR. Thanks for trying to fix it, @hectortav.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants