简体   繁体   中英

How to make TreeNode in Ant tree design as link?

I have a tree design from Ant Design, got everything done, but at some specific places I want the tree node to act as link, tried giving link directly, that didn't work.

array.map((node) => {
   if(node.type !== 'last') {
      <TreeNode title={item.name} key={item.id} dataRef={item} />
   } else{
      <Link className="container" to={{ pathname: 'somepath'}}></Link>
  }

})

Not sure I fully understand what you need, but maybe pass Link as a title:

<TreeNode
    title={<Link className="container" to={{ pathname: 'somepath'}}>{item.name}</Link>}
    key={item.id}
    dataRef={item}
/>

is good enough?

Also there is onCheck and onSelect callbacks in TreeNode api , which can do redirect to specific url, ie:

class LinkAbleTreeNode extends PureComponent {
  onCheck = () => {
    props.history.push(props.url)
  }

  render() {
    return <TreeNode title={item.name} key={item.id} dataRef={item} onCheck={this.onCheck} />
  }
}

export default withRouter(LinkAbleTreeNode);

And pass url as a prop:

<LinkAbleTreeNode /*...tree node args... */ url='somepath' />

This solution require withRouter hoc to be added into application. But if you have available the router props in parent component, you can just pass them through.

UPDATE

For antd v4 only pass title prop with custom component is the acceptable solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM