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][Alert] TS error when using slotProps with customised component #42601

Closed
alexey-kozlenkov opened this issue Jun 10, 2024 · 8 comments · Fixed by #42787
Closed
Assignees
Labels
component: alert This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature good first issue Great for first contributions. Enable to learn the contribution process. package: material-ui Specific to @mui/material ready to take Help wanted. Guidance available. There is a high chance the change will be accepted typescript

Comments

@alexey-kozlenkov
Copy link
Contributor

alexey-kozlenkov commented Jun 10, 2024

Steps to reproduce

Link to live example: https://codesandbox.io/p/sandbox/damp-monad-sptgfr?file=%2Fsrc%2FDemo.tsx%3A12%2C4

Steps:

  1. Check out TS error in slotProps

Current behavior

On example of Alert component (it's likely a common issue):

slots allow to customise closeIcon component to be something else the default IconButton.
However, if I set it be some custom component, slotProps won't pickup prop types from defined component and keep pointing to IconButtonProps from mui package

Expected behavior

Complaint types

Context

No response

Your environment

No response

Search keywords: slot slotProps

@alexey-kozlenkov alexey-kozlenkov added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jun 10, 2024
@alexey-kozlenkov alexey-kozlenkov changed the title [Alert] TS disallow using slotProps with customised component [Alert] TS error when using slotProps with customised component Jun 10, 2024
@zannager zannager added the component: alert This is the name of the generic UI component, not the React module! label Jun 10, 2024
@appleSimple
Copy link
Contributor

not found example link T^T

@alexey-kozlenkov
Copy link
Contributor Author

@appleSimple my bad 😞
This one should work: https://codesandbox.io/p/sandbox/damp-monad-sptgfr?file=%2Fsrc%2FDemo.tsx%3A12%2C4

@appleSimple
Copy link
Contributor

slotProps's type could be any because of K extends Record<keyof Slots, any>, but not applied any type.

This may be related part of cause.

Alert.d.ts

export type AlertSlotsAndSlotProps = CreateSlotsAndSlotProps<
  AlertSlots,
  {
    closeButton: SlotProps<React.ElementType<IconButtonProps>, {}, AlertOwnerState>;
    closeIcon: SlotProps<React.ElementType<SvgIconProps>, {}, AlertOwnerState>;
  }
>;

types.ts

export type CreateSlotsAndSlotProps<Slots, K extends Record<keyof Slots, any>> = {
  /**
   * The components used for each slot inside.
   * @default {}
   */
  slots?: Slots;
  /**
   * The props used for each slot inside.
   * @default {}
   */
  slotProps?: {
    [P in keyof K]?: K[P];
  };
};

@alexey-kozlenkov
Copy link
Contributor Author

@appleSimple any could be passed as a slotProps's value to CreateSlotsAndSlotProps, can't be passed to Alert's close button slotProps cause it's already specified as SlotProps<React.ElementType<IconButtonProps>, {}, AlertOwnerState>

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Jun 24, 2024

You can type cast slotProps.closeButton to resolve the issue. Here's how:

<Alert
  icon={<CheckIcon fontSize="inherit" />}
  severity="success"
  onClose={() => void 0}
  slots={{
    closeIcon: CloseIcon,
    closeButton: MyIconButton,
  }}
  slotProps={{
    closeButton: {
      iconSize: 'medium',
    } as MyIconButtonProps,
  }}
>
  Here is a gentle confirmation that your action was successful.
</Alert>

This approach works, but it's not clear if it should be documented as a solution or is considered a workaround in which case this is a bug.

@ZeeshanTamboli ZeeshanTamboli added typescript package: material-ui Specific to @mui/material labels Jun 24, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [Alert] TS error when using slotProps with customised component [material-ui][Alert] TS error when using slotProps with customised component Jun 24, 2024
@alexey-kozlenkov
Copy link
Contributor Author

@ZeeshanTamboli I don't it's possible since slotProps values there are not just types of desired props but a SlotProps<...> wrapper around original MUI's IconButtonProps, as I specified at the beginning of that issue.

image

@DiegoAndai
Copy link
Member

Hey @alexey-kozlenkov, thanks for the report 😊

This use case is covered in other components by adding ...SlotPropsOverrides types, for example the BadgeRootSlotPropsOverrides type in the Badge component. These types can then be augmented to accomplish the expected behavior, for example: https://codesandbox.io/p/sandbox/slot-props-overrides-example-7nm889?file=%2Fsrc%2FApp.tsx%3A10%2C2.

This hasn't been implemented in all components, the Alert being one of them. I'm accepting this as an enhancement for the missing functionality and adding the ready-to-take label if anyone wishes to work on it. It should be enough to add new types AlertCloseButtonSlotPropsOverrides and AlertCloseIconSlotPropsOverrides following the pattern used in Badge.

@DiegoAndai DiegoAndai added good first issue Great for first contributions. Enable to learn the contribution process. ready to take Help wanted. Guidance available. There is a high chance the change will be accepted enhancement This is not a bug, nor a new feature and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jun 28, 2024
@alexey-kozlenkov
Copy link
Contributor Author

@DiegoAndai have a look please when convenient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: alert This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature good first issue Great for first contributions. Enable to learn the contribution process. package: material-ui Specific to @mui/material ready to take Help wanted. Guidance available. There is a high chance the change will be accepted typescript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants