Material-ui: ListItem doesn't accept prop htmlFor (TypeScript)

Created on 12 Aug 2018  Â·  11Comments  Â·  Source: mui-org/material-ui

This is a Typescript error

The ListItem component accepts label for the prop component but doesn't accept the htmlFor prop as it should

  • [x] This is a v1.x issue.
  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

If I provide a value of label to the prop component of the ListItem component I should also be able to provide the prop htmlFor

So

<ListItem component="label" htmlFor="foo" />

should crate this HTML

<label for="foo />

Current Behavior

Instead I get the type error

Property 'htmlFor' does not exist on type 'IntrinsicAttributes & ListItemProps & { children?: ReactNode; }'.

Steps to Reproduce

Link: https://codesandbox.io/s/r0pr5qjnzq
screen shot 2018-08-12 at 17 15 41

Context

I try to use the label to trigger a hidden <input type="file" />

Your Environment

Link to my current package.json and the repo itself

typescript

Most helpful comment

The solution to this problem is well known but not documented. @pelotom maybe we should put it into the TypeScript guide? I don't know I have seen this question multiples times here. Or maybe we should just direct people to StackOverflow.

@pixelkritzel You need to explicitly use an intermediate component. Here is how I would do it in pure JavaScript, you can add the typing after:

const Label = props => <label htmlFor="foo" {...props} />

<ListItem component={Label} />

All 11 comments

My current solution is kind of weird

The solution to this problem is well known but not documented. @pelotom maybe we should put it into the TypeScript guide? I don't know I have seen this question multiples times here. Or maybe we should just direct people to StackOverflow.

@pixelkritzel You need to explicitly use an intermediate component. Here is how I would do it in pure JavaScript, you can add the typing after:

const Label = props => <label htmlFor="foo" {...props} />

<ListItem component={Label} />

Thx! OK, this is much better than my current solution.

@oliviertassinari as it happens I've been working on a better solution to this problem, basically a continuation of #11731... stay tuned!

Going through the comments, the docs label was removed? Are we no longer adding this

const Label = props => <label htmlFor="foo" {...props} />
<ListItem component={Label} />

somewhere in the docs? or should we add a typing for ListItem component for prop htmlFor? @oliviertassinari

@oliviertassinari This no longer produces a type error :+1:

Does it mean we can close it? Do we have a test for it?

Yes, haven't checked for the existence of a test outside of ListItem.spec.tsx. Tested it manually and got no errors, can open a PR with a test if you want.
It was fixed in https://github.com/mui-org/material-ui/pull/13868/files#diff-a242e1d972e33ce7b57d1b3296de00aa

Great! In this case, option 1. We add a simple test, in case we have to refactor the logic in the future, option 2. It's a solved generic issue, we close it.

Should be able to go with option 2, other components test the exact same logic

Should be able to go with option 2, other components test the exact same logic

Yep. Would need to test for every component + html attribute combination. Shouldâ„¢ be fine without a test.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FranBran picture FranBran  Â·  3Comments

mb-copart picture mb-copart  Â·  3Comments

activatedgeek picture activatedgeek  Â·  3Comments

iamzhouyi picture iamzhouyi  Â·  3Comments

rbozan picture rbozan  Â·  3Comments