简体   繁体   English

如何将glyphicon放置在react.js项目中的引导导航栏中

[英]How to place glyphicon in bootstrap navbar within a react.js project

How can I display a glyphicon in a bootstrap navbar? 如何在引导导航栏中显示glyphicon? I have a React.js project that I have been working on, and I would like to relocate the GitHub glyphicon to the navbar. 我有一个正在从事的React.js项目,我想将GitHub glyphicon移到导航栏中。 So far I have something looking like the below. 到目前为止,我有类似下面的内容。 github glyphicon

The code pertaining to the navbar in the App.js App.js中的导航栏相关的代码

import React, {PropTypes} from 'react';
import NavBar from './common/NavBar-test';
import FontAwesome from 'react-fontawesome';

var navbar = {};
navbar.brand =  {linkTo: "#", text: "app"};
navbar.links = [

  {linkTo: "#Demonstration", text: "Demonstration"},
  {linkTo: "#Demonstration2", text: "Demonstration #2"},
  {linkTo: "https://github.com/user/app", text:"GitHub Source Code"},
  {linkTo: "https://itunes.apple.com/us/app/", text: "App Store"},
  {linkTo: "https://github.com/user/appr", text: "The Future of app"}
];

And Navbar-test.js Navbar-test.js

import React, { PropTypes } from 'react';

// create classes
var NavBar = React.createClass({
  render: function(){
    return(
      <nav className="navbar navbar-default navbar-static-top">
        <div className="container-fluid">
          <div className="navbar-header">
            <button type="button" className="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
              <span className="sr-only">Toggle navigation</span>
              <span className="icon-bar"></span>
              <span className="icon-bar"></span>
              <span className="icon-bar"></span>
            </button>
            <NavBrand linkTo={this.props.brand.linkTo} text={this.props.brand.text} />
          </div>
          <div className="collapse navbar-collapse" id="navbar-collapse">
            <NavMenu links={this.props.links} />
          </div>
        </div>
      </nav>
    );
  }
});

var NavBrand = React.createClass({
  render: function(){
    return (
      <a className="navbar-brand" href={this.props.linkTo}>{this.props.text}</a>
    );
  }
});

var NavMenu = React.createClass({
  render: function(){
    var links = this.props.links.map(function(link){
      if(link.dropdown) {
        return (
          <NavLinkDropdown key={link.text} links={link.links} text={link.text} active={link.active} icon={link.icon} />
        );
      }
      else {
        return (
          <NavLink key={link.text} linkTo={link.linkTo} text={link.text} active={link.active} />
        );
      }
    });
    return (
      <ul className="nav navbar-nav">
        {links}
      </ul>
    );
  }
});

var NavLinkDropdown = React.createClass({
  render: function(){
    var active = false;
    var links = this.props.links.map(function(link){
      if(link.active){
        active = true;
      }
      return (
        <NavLink key={link.text} linkTo={link.linkTo} text={link.text} active={link.active} />
      );
    });
    return (
      <li className={"dropdown " + (active ? "active" : "")}>
        <a href="#" className="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
          {this.props.text}
          <span className="caret"></span>
        </a>
        <ul className="dropdown-menu">
          {links}
        </ul>
      </li>
    );
  }
});

var NavLink = React.createClass({
  render: function(){
    return(
      <li className={(this.props.active ? "active" : "")}>
        <a href={this.props.linkTo}>{this.props.text}{this.props.icon}</a>  
      </li>
    );
  }
});

export default NavBar;

Ideally, I'd like to place the GitHub glyphicon in front of the text that states GitHub Source Code . 理想情况下,我想将GitHub glyphicon放在声明GitHub Source Code的文本之前。

It looks like you are mapping over the links (which is cool), but you can add your desired GitHub data before the mapped links. 看起来您正在映射链接(很酷),但是您可以在映射的链接之前添加所需的GitHub数据。

You might end up with something like this: 您可能最终会得到如下结果:

<span>
    <NavLink>GitHub Source Code</NavLink>
    <GlyphIcon />
</span>
// Then map over links here

In short, I finally got the glyphicons to display how I would like them to. 简而言之,我终于有了字形图标来显示我希望它们如何运行。 布丁中的证明

I needed to modify the source of the Navbar-test to support icons. 我需要修改Navbar-test的源代码以支持图标。

Something like, 就像是,

class NavMenu extends React.Component {
  render() {
    var links = this.props.links.map(function(link){
      if(link.dropdown) {
        return (
          <NavLinkDropdown key={link.text} links={link.links} text={link.text} active={link.active} icon={link.icon} />
        );
      }
      else {
        return (
          <NavLink key={link.text} linkTo={link.linkTo} icon={link.icon} text={link.text} active={link.active}  />
        );
      }
    });
    return (
      <ul className="nav navbar-nav">
        {links}
      </ul>
    ); // close return
  } // close render
} // close NavMenu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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