反应路由器3面包屑下拉列表

时间:2017-11-10 18:51:14

标签: reactjs react-router dropdown breadcrumbs

我正在尝试根据面包屑中的下拉选项更改路线。任何建议都将受到赞赏。

这是获得第一个选项的组件,但我不确定如何在生成下拉列表后对其他选项进行格式化。

const MenuItemViews = ({ params: { category, subCategory, item }, children }) => {
  const menuItem = sideNavData.lookupItem(category, subCategory, item);
  console.log(menuItem);
  console.info(children);
  return (
    <div>
      {
        menuItem.name === 'Bill'
          ? <div>
            <h2>Labels</h2>
            {
              !children
                ? <Link to={`/${category}/${subCategory}/${item}/${menuItem.childItems[0].name}`} >
                  <Image src={menuItem.content} />
                </Link>
                : children
            }
          </div>
          : <ContentContainer>
            <h1>{menuItem.name}</h1>
            <Image src={menuItem.content} alt='item' />
          </ContentContainer>
      }
    </div>
  );
};

这是显示面包屑的组件。

const labelDropdownOptions = [
  { key: 'OptionOne', value: 'OptionOne', text: 'OptionOne' },
  { key: 'OptionTwo', value: 'OptionTwo', text: 'OptionTwo' },
  { key: 'OptionThree', value: 'OptionThree', text: 'OptionThree' },
];

class TopBar extends Component {
  resolver = (key) => {
    if (key === 'Home') {
      return key;
    }
    return this.props.params[key];
  }

  dropdownLink = (link, key, text, index, routes) => {
    console.log(routes);
    if (text === 'OptionOne') {
      return (
        <Dropdown defaultValue={'OptionOne'} key={key} options={labelDropdownOptions} />
      );
    }
    return <Link key={key} to={link}>{text}</Link>;
  }

  render() {
    const { routes, params } = this.props;
    return (
      <TopBarHeader>
        <IndexLink to='/'>
          <HomeIcon><Icon name='home' /></HomeIcon>
        </IndexLink>
        <BreadcrumbWrapper>
          <Breadcrumbs
            createLink={this.dropdownLink}
            params={params}
            resolver={this.resolver}
            routes={routes}
          />
        </BreadcrumbWrapper>
      </TopBarHeader>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

我可以通过将this.props.router.push传递到onClick prop并指定值来实现此目的。

&#13;
&#13;
class TopBar extends Component {
  resolver = (key) => {
    if (key === 'Home') {
      return key;
    }
    return this.props.params[key];
  }

  dropdownLink = (link, key, text, index, routes) => {
    const category = sideNavData.lookupCategory(this.props.category);
    if (link === '/TabTwo/Names/Bill/OptionOne' || link === '/TabTwo/Names/Bill/OptionTwo' || link === '/TabTwo/Names/Bill/OptionThree') {
      return (
        <span key={index}>
          {
            Object.keys(category).map((subCategory, i) => {
              return (
                <span key={i}>
                  {
                    Object.keys(category[subCategory]).map((item, itemIndex) => (
                      <span key={itemIndex}>
                        {
                          category[subCategory][item].name === 'Bill'
                            ? <Dropdown
                              defaultValue={'OptionOne'}
                              options={category[subCategory][item].childItems}
                              onChange={(event, data) => { this.props.router.push(`/${this.props.category}/${subCategory}/${category[subCategory][item].name}/${data.value}`); }}
                            />
                            : null
                        }
                      </span>
                    ))
                  }
                </span>
              );
            })
          }
        </span>
      );
    }
    return <Link key={key} to={link}>{text}</Link>;
  }

  render() {
    const { routes, params } = this.props;
    return (
      <TopBarHeader>
        <IndexLink to='/'>
          <HomeIcon><Icon name='home' /></HomeIcon>
        </IndexLink>
        <BreadcrumbWrapper>
          <Breadcrumbs
            createLink={this.dropdownLink}
            params={params}
            resolver={this.resolver}
            routes={routes}
          />
        </BreadcrumbWrapper>
      </TopBarHeader>
    );
  }
}
&#13;
&#13;
&#13;