material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

ListItem has link ?

Open TonyFrancis opened this issue 11 years ago • 28 comments

I am trying to use Listitem as link to other url but Link's property override ListItem 's property is there any solution to this

< ListItem primaryText = { <Link to="a" > as </Link> } />

TonyFrancis avatar Aug 13 '15 07:08 TonyFrancis

@TonyFrancis You should be able to add an href attribute to the ListItem.

hai-cea avatar Aug 13 '15 15:08 hai-cea

@TonyFrancis Got your comment on gitter and it sounds like you got this to work. I'll go ahead and close this for now - please reopen if there's still an issue.

hai-cea avatar Aug 14 '15 18:08 hai-cea

href needs to be added to doc

antoinerousseau avatar Feb 25 '16 03:02 antoinerousseau

Edit: Dang it! This issue is addressed at #2243. In general; it's not clear from the documentation what all available properties are. This should be documented and the following statement doesn't help. Other properties (no documented) are applied to the root element. I looked at the github List implementation and I'm not seeing a container component being used that has the containerElement property.

Based on @TonyFrancis response the href doesn't fully satisfy the need. If you're using react router which is likely where the Link component is coming from then you get some benefits like being able to specify a basename.

For example if you have <Router basename='/my-app'> wrapping your app then

< ListItem primaryText = { <Link to="a" > as </Link> } />

would link to my-app/a without the need to carry that basename around everywhere.

r4lphio avatar Dec 14 '16 19:12 r4lphio

The documentation is not clear enough for me. hmmm

raitucarp avatar Jan 17 '17 02:01 raitucarp

There is no href field in the document for ListItem

jiaxin-lin avatar Jun 23 '17 07:06 jiaxin-lin

Just ran into this. I'm using this and it works:

<ListItem button component={Link} to='/some-url'>...</ListItem>

mariotacke avatar Oct 12 '17 00:10 mariotacke

@mariotacke ^ this code generates ul > a markup, which is not semantically valid, see https://github.com/mui-org/material-ui/issues/7956#issuecomment-362433602

edit: it's fine if the parent is a <nav> or uses component="nav"

caub avatar Feb 02 '18 14:02 caub

Now, you can create this with following code:

      <ListItem
        primaryText="Text"
        containerElement={<Link to="/a" />}
      />

Glaadiss avatar Feb 17 '18 14:02 Glaadiss

Hey @Glaadiss Tnx, it works

sukiasyan avatar Mar 27 '18 13:03 sukiasyan

@Glaadiss that markup gives me an error:

Warning: React does not recognize the containerElementprop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercasecontainerelement instead. If you accidentally passed it from a parent component, remove it from the DOM element.

Is that because I have an outdated version of React or MUI?

renannobile avatar Apr 06 '18 13:04 renannobile

@renannobile Check out if you passing unnecessary {...props} in one of your components. It's a common mistake when we copy-paste from guides :P

Glaadiss avatar Apr 06 '18 14:04 Glaadiss

@Glaadiss I don't seem to be able to use this on 1.0.0-beta.44

Tried:

<List component="nav">
    <ListItem component={Link} to="/admin/user">
      <ListItemIcon>
        <PersonIcon />
      </ListItemIcon>
      <ListItemText primary="Inbox" />
    </ListItem>
    <ListItem button>
      <ListItemIcon>
        <DraftsIcon />
      </ListItemIcon>
      <ListItemText primary="Drafts" />
    </ListItem>
  </List>

The above case works but without the ripple effect and it results in an a with a div as child, which is illegal.

<List component="nav">
    <ListItem primaryText="Inbox" component={Link} to="/admin/user">
      <ListItemIcon>
        <PersonIcon />
      </ListItemIcon>
    </ListItem>
    <ListItem button>
      <ListItemIcon>
        <DraftsIcon />
      </ListItemIcon>
      <ListItemText primary="Drafts" />
    </ListItem>
  </List>

The above case works but without the ripple effect and it results in an item without visible text.

<List component="nav">
    <ListItem primaryText="Inbox" containerElement={<Link to="/admin/user" />}>
      <ListItemIcon>
        <PersonIcon />
      </ListItemIcon>
    </ListItem>
    <ListItem button>
      <ListItemIcon>
        <DraftsIcon />
      </ListItemIcon>
      <ListItemText primary="Drafts" />
    </ListItem>
  </List>

This above case shows an [object Object] attribute for containerElement in the HTML.

<List component="nav">
    <ListItem ContainerComponent={() => <Link to="/admin/user" />}>
      <ListItemIcon>
        <PersonIcon />
      </ListItemIcon>
      <ListItemText primary="Inbox" />
    </ListItem>
    <ListItem button>
      <ListItemIcon>
        <DraftsIcon />
      </ListItemIcon>
      <ListItemText primary="Drafts" />
    </ListItem>
  </List>
<List component="nav">
    <ListItem ContainerComponent={Link to="/admin/user" />}>
      <ListItemIcon>
        <PersonIcon />
      </ListItemIcon>
      <ListItemText primary="Inbox" />
    </ListItem>
    <ListItem button>
      <ListItemIcon>
        <DraftsIcon />
      </ListItemIcon>
      <ListItemText primary="Drafts" />
    </ListItem>
  </List>
<List component="nav">
    <ListItem ContainerComponent={Link} ContainerProps={{ to: '/admin/user' }}>
      <ListItemIcon>
        <PersonIcon />
      </ListItemIcon>
      <ListItemText primary="Inbox" />
    </ListItem>
    <ListItem button>
      <ListItemIcon>
        <DraftsIcon />
      </ListItemIcon>
      <ListItemText primary="Drafts" />
    </ListItem>
  </List>

mschipperheyn avatar May 03 '18 15:05 mschipperheyn

Ahh! The solution is this.

<List component="nav">
    <ListItem component={Link} to="/admin/user" button>
      <ListItemIcon>
        <PersonIcon />
      </ListItemIcon>
      <ListItemText primary="Inbox" />
    </ListItem>
    <ListItem button>
      <ListItemIcon>
        <DraftsIcon />
      </ListItemIcon>
      <ListItemText primary="Drafts" />
    </ListItem>
  </List>

mschipperheyn avatar May 03 '18 15:05 mschipperheyn

@mschipperheyn this works but it messes with the ripple effect

nephix avatar Jun 01 '18 15:06 nephix

Hello, any updates on how to properly do this?

kevinpaulretiban avatar Jun 06 '18 09:06 kevinpaulretiban

This works for me, including ripple:

<ListItem button component={
  ({ children, ...props }) =>
    <Link to="/" {...props as LinkProps}>
      {children}
    </Link>
}>

squidfunk avatar Jul 19 '18 14:07 squidfunk

The following approach worked for me, because it's able to retain the original style and flex behavior.

<ListItem component={props => (
  <ListItem button>
    <Link style={{
      position: "absolute",
      top: 0,
      left: 0,
      right: 0,
      bottom: 0
    }}
    to={"/"}>
    </Link>
    {props.children}
  </ListItem>
)}>
    <ListItemIcon>
      <ArrowForwardIcon/>
    </ListItemIcon>
    <ListItemText primary="List Item" />
</ListItem>

leeonel avatar Jul 31 '18 13:07 leeonel

For Next.js users:

<List component="nav">
  <Link href="/admin/resources" passHref>
    <ListItem component="a" button>
      <ListItemIcon>
        <InboxIcon />
      </ListItemIcon>
      <ListItemText primary="Resources" />
    </ListItem>
  </Link>
  ...

seungha-kim avatar Aug 09 '18 08:08 seungha-kim

This may seem obvious - but if you don't need to use <Link/> and can use <a> (i.e. - you don't get the advantages of the SPA you are writing) you can do this:

<a href="/yourlink">
  <ListItem
    button
  >
    <ListItemText primary="Your Link Text" />
  </ListItem>
</a>

josephecombs avatar Sep 12 '18 20:09 josephecombs