Skip to content

[core] Generify props with component property #16487

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

Merged
merged 3 commits into from
Sep 3, 2019

Conversation

ypresto
Copy link
Contributor

@ypresto ypresto commented Jul 4, 2019

This PR apply #15193 to all1 components not only Button. (See #15827 (comment))
It would fix component={...} prop type mismatch issue (#15827) when you use Props type and component props at same time (e.g. (props: IconButtonProps) => <IconButtonProps {...props} component={...} /> ).

New generic parameters have default value to keep original (non-parametric) type.

(ListItem.d.ts is more difficult as it switches div and li by button flag, so kept unchanged.)

Edit @eps1lon:
Closes #16846

1

  • Avatar
  • ToggleButton
  • BottomNavigationAction
  • Breadcrumbs
  • Button
  • ButtonBase
  • ButtonGroup
  • CardActionArea
  • Chip
  • Didiver
  • ExpansionPanelSummary
  • Fab
  • FormControl
  • FormLabel
  • Grid
  • IconButton
  • Link
  • List
  • MenuItem
  • Skeleton
  • StepButton
  • Tab
  • TablePagination
  • TableSortLabel
  • TabScrollButton
  • Tabs

@mui-pr-bot
Copy link

mui-pr-bot commented Jul 4, 2019

No bundle size changes comparing fba5135...5bb044c

Generated by 🚫 dangerJS against 5bb044c

[Avatar] Generify AvatarProps
[BottomNavigationAction] Generify BottomNavigationActionProps
[Button] Allow omitting type parameter of ButtonTypeMap
[ButtonBase] Generify ButtonBaseProps
[Breadcrumbs] Generify BreadcrumbsProps
[ButtonGroup] Generify ButtonGroupProps
[CardActionArea] Generify CardActionAreaProps
[Chip] Generify ChipProps
[Divider] Generify DividerProps
[ExpansionPanelSummary] Generify ExpansionPanelSummaryProps
[Fab] Generify FabProps
[FormControl] Generify FormControlProps
[FormLabel] Generify FormLabelProps
[IconButton] Generify IconButtonProps
[Link] Generify LinkProps
[List] Generify ListProps
[StepButton] Generify StepButtonProps
[Tab] Generify TabProps
[TablePagination] Generify TablePaginationProps
[TableSortLabel] Generify TableSortLabelProps
[TabScrollButton] Generify TabScrollButtonProps
[Tabs] Generify TabsProps
[ToggleButton] Generify ToggleButtonProps
@ypresto
Copy link
Contributor Author

ypresto commented Jul 5, 2019

I'm in doubt how to omit or override some propreties of D. For example, I have custom IconButton with integrated react-router Link component, and to property is optional. Only when to is specified it'll pass `component={Link}.

@ypresto
Copy link
Contributor Author

ypresto commented Jul 5, 2019

import { IconButtonTypeMap as MuiIconButtonTypeMap } from '@material-ui/core/IconButton'
import { ForwardRefLink } from '../compat/ForwardRefLink'
import { ExtendButtonBaseTypeMap } from '@material-ui/core/ButtonBase'
import { OverrideProps } from '@material-ui/core/OverridableComponent'

export type IconButtonTypeMap<
  P = {},
  D extends React.ElementType = MuiIconButtonTypeMap['defaultComponent']
> = ExtendButtonBaseTypeMap<{
  props: P &
    MuiIconButtonTypeMap['props'] & {
      (custom props...)
    }
  defaultComponent: D
  classKey: MuiIconButtonTypeMap['classKey']
}>

export type IconButtonPropsWithTo<P = {}> = OverrideProps<
  IconButtonTypeMap<P & { to: LinkProps['to'] }, typeof ForwardRefLink>,
  typeof ForwardRefLink
>

export type IconButtonPropsWithoutTo<P = {}> = OverrideProps<
  IconButtonTypeMap<P>,
  IconButtonTypeMap['defaultComponent']
>

export type IconButtonProps<P = {}> = IconButtonPropsWithoutTo<P & { to?: undefined }> | IconButtonPropsWithTo<P>

function IconButton(props: IconButtonPropsWithTo): JSX.Element
function IconButton(props: IconButtonPropsWithoutTo): JSX.Element
function IconButton(props: IconButtonProps) {
  ...
}

hmm... looks ugly 😂

@akomm
Copy link
Contributor

akomm commented Jul 5, 2019

I'm in doubt how to omit or override some propreties of D. For example, I have custom IconButton with integrated react-router Link component, and to property is optional. Only when to is specified it'll pass `component={Link}.

@ypresto tried conditional types? Example:

type IconButtonProps<P> = P extends {to: string} ? A : B;
// more variants
type X<P> = 
  P extends X1 ? A :
  P extends X2 ? B :
  P extends X3 ? C :
  D

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

Really appreciate that you pushed this effort. This looks good to me. Just would like to clarify the default types usage.

@eps1lon eps1lon merged commit 225badb into mui:master Sep 3, 2019
@eps1lon
Copy link
Member

eps1lon commented Sep 3, 2019

@ypresto Much appreciated, thanks.

@eps1lon eps1lon changed the title [typescript] Generify props with component property [core] Generify props with component property Sep 3, 2019
@jraoult
Copy link

jraoult commented Sep 9, 2019

It seems that after this PR, TextField's onFocus prop type is not resolved properly (and probably any component overriding FormControl props). For example, this snippet was passing type checking because the target was inferred to be an HTMLInputElement:

 <TextField onFocus={({ target }) => target.select()} />

This is failing now because the event is just a React.EventHandler<any> (the default for onFocus in the newly introduced FormControlTypeMap). Is it the expected behaviour?

FYI: I'm using TS 3.6.2

@eps1lon
Copy link
Member

eps1lon commented Sep 9, 2019

@jraoult Please open a new issue and fill out the template. This would help a lot.

@oliviertassinari
Copy link
Member

#17369

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

Successfully merging this pull request may close these issues.

[Link] typescript error Property 'component' does not exist on type IntrinsicAttributes ...
6 participants