简体   繁体   English

如何使用 reactjs 和 css 创建垂直制表符

[英]How to create vertical tab using reactjs and css

Am trying to bulid vertical tab that will function exactly like in the demo link below sample links from w3schools我正在尝试构建垂直选项卡,该选项卡将 function 与 w3schools 示例链接下方的演示链接中的完全一样

here is the screenshot of what am trying to achieve as per the demo sample above这是根据上面的演示示例尝试实现的屏幕截图演示示例截图

To this effect tried solution found here at but it does not give me what I want as per demo sample above Stackoverflow link为此,尝试了在此处找到的解决方案,但它没有按照Stackoverflow 链接上方的演示示例提供我想要的内容

Now I have decided to go my own way in trying it out.现在我决定 go 以我自己的方式尝试一下。

I have succeeded in displaying the content from an array via reactjs. when user click on each country, the content of that country gets displayed.我已成功通过 reactjs 显示数组中的内容。当用户单击每个国家/地区时,将显示该国家/地区的内容。

My Problem:我的问题:

My issue is that I cannot get it to display the content in a vertical tab div as can be seen in the screenshot我的问题是我无法让它在垂直选项卡 div 中显示内容,如屏幕截图所示

截屏

Here is the coding so far这是到目前为止的编码

import React, { Component, Fragment } from "react";
import { render } from "react-dom";

class Country extends React.Component {
  state = { open: false };
  toggleOpen = id => {
    alert(id);

    this.setState(prevState => ({
      open: !prevState.open
    }));
  };

  render() {
    return (
      <React.Fragment>
        <div key={this.props.data.id}>
          <button onClick={() => this.toggleOpen(this.props.data.id)}>
            {this.props.data.name}
          </button>
        </div>

        <div>
          {this.state.open && (
            <div>
              <div>
                <b>id: </b>
                {this.props.data.id}
              </div>
              <div>
                <b>Info: </b>
                {this.props.data.info}
              </div>
              <div>
                <b>Country name:</b> {this.props.data.name}
              </div>
              content for <b> {this.props.data.name}</b> will appear here..
            </div>
          )}
        </div>
      </React.Fragment>
    );
  }
}

class VerticalTab extends React.Component {
  constructor() {
    super();

    this.state = {
      data: [
        { id: "1", name: "London", info: "London is the capital city of England." },
        { id: "2", name: "Paris", info: "Paris is the capital of France." },
        { id: "3", name: "Tokyo", info: "Tokyo is the capital of Japan." }
      ]
    };
  }

  render() {
    return (
      <div>
        <div>
          {this.state.data.map(country => (
            <Country key={country.id} data={country} />
          ))}
        </div>
      </div>
    );
  }
}

Is this what you are looking for? 这是你想要的?

 class App extends React.Component { constructor(props) { super(props); this.state = { currentTab: -1, data: [ { id: "1", name: "London" ,info: "London is the capital city of England."}, { id: "2", name: "Paris" ,info: "Paris is the capital of France." }, { id: "3", name: "Tokyo" ,info: "Tokyo is the capital of Japan."} ] }; this.handleClick = this.handleClick.bind(this); } handleClick(currentTab) { this.setState({ currentTab }); } render() { return ( <div> <h2>Vertical Tabs</h2> <p>Click on the buttons inside the tabbed menu:</p> <div className="tab"> {this.state.data.map((button, i) => ( <button key={button.name} className="tablinks" onClick={() => this.handleClick(i)}>{button.name}</button> ) ) } </div> <div className="tabcontent"> {this.state.currentTab !== -1 && <React.Fragment> <h3>{this.state.data[this.state.currentTab].name}</h3> <p>{this.state.data[this.state.currentTab].info}</p> </React.Fragment> } </div> </div> ) } } ReactDOM.render( < App / > , document.getElementById('root') ); 
 * {box-sizing: border-box} body {font-family: "Lato", sans-serif;} /* Style the tab */ .tab { float: left; border: 1px solid #ccc; background-color: #f1f1f1; width: 30%; height: 300px; } /* Style the buttons inside the tab */ .tab button { display: block; background-color: inherit; color: black; padding: 22px 16px; width: 100%; border: none; outline: none; text-align: left; cursor: pointer; transition: 0.3s; font-size: 17px; } /* Change background color of buttons on hover */ .tab button:hover { background-color: #ddd; } /* Create an active/current "tab button" class */ .tab button.active { background-color: #ccc; } /* Style the tab content */ .tabcontent { float: left; padding: 0px 12px; border: 1px solid #ccc; width: 70%; border-left: none; height: 300px; } 
 <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script> <div id="root" /> 

React Tabs with Hooks带有钩子的 React 选项卡

Here is a link to react tabs.这是反应标签的链接。 Maybe this will help you.也许这会对你有所帮助。

在此处输入图像描述

enter code here

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

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