简体   繁体   English

如何构建标签组件 Reactjs

[英]How to build Tabs component Reactjs

I'm trying to build a Tabs component with ReactJS and I'm following a CSS stylesheet.So, I would love to have this as render :我正在尝试使用 ReactJS 构建一个 Tabs 组件,并且我正在关注 CSS 样式表。所以,我希望将其作为 render :

<div data-container="" class="tabs tabs-horizontal">
    <div class="tabs-header">
        <div class="tabs-header-tab ph active" data-tab="0" style="transform: translateX(0px) translateZ(0px);">Personal
        </div>
        <div class="tabs-header-tab ph undefined" data-tab="0" style="transform: translateX(0px) translateZ(0px);">Job
        </div>
        <div class="tabs-header-tab ph undefined" data-tab="0" style="transform: translateX(0px) translateZ(0px);">
            Salary</div>
    </div>
    <div data-container="" class="tabs-content">
        <div data-container="" class="tabs-content-tab open" data-tab="0">
            <div>I am a HR Specialist.</div>
        </div>
        <div data-container="" class="tabs-content-tab undefined" data-tab="0">
            <div>No records have been added yet</div>
        </div>
        <div data-container="" class="tabs-content-tab undefined" data-tab="0">
            <div>This type of information needs to be added by your manager</div>
        </div>
    </div>
</div>

And this is what the components looks like now :这就是组件现在的样子:

<Tabs>
  <TabHeaders>
    <TabHeadersItem text="Personal" state="active" />
    <TabHeadersItem text="Job" />
    <TabHeadersItem text="Salary" />
  </TabHeaders>
  <TabContents>
    <TabContentsItem
      content="I am a HR Specialist."
      open="open"
    />
    <TabContentsItem content="No records have been added yet" />
    <TabContentsItem content="This type of information needs to be added by your manager" />
  </TabContents>
</Tabs>

So, I'm trying to find a solution, when user clicks on a header it becomes "active" and its content "open" while the others should be closed and not active.所以,我试图找到一个解决方案,当用户点击一个标题时,它会变成“活动”并且它的内容“打开”,而其他人应该关闭而不是活动。

Ok so since you've edited your question, I'll do with mine.好的,既然你已经编辑了你的问题,我会处理我的。 The idea is the same: To keep the state of which tab is open, and since you don't want to keep your tabs in a list, then you need to link a TabHeader to a TabContent:想法是一样的:要保持哪个选项卡处于打开状态,并且由于您不想将选项卡保留在列表中,那么您需要将 TabHeader 链接到 TabContent:

const TabHeaderItem = props => {
  return (
    <div
      onClick={() => props.changeTab(props.text.toLowerCase())}
      className={props.state ? "active" : ""}
    >
      {props.text}
    </div>
  );
};

const TabContentsItem = props => {
  return <div className={props.open ? "open" : "closed"}>{props.content}</div>;
};

const TabComponent = () {
  const [active, setActive] = useState("personal");
  const changeTab = tab => {
    console.log("tab", tab);
    setActive(tab);
  };

  return (
    <div className="App">
      <div className="tabs">
        <TabHeaderItem
          changeTab={changeTab}
          text="Personal"
          state={active === "personal"}
        />
        <TabHeaderItem
          changeTab={changeTab}
          text="Job"
          state={active === "job"}
        />
        <TabHeaderItem
          changeTab={changeTab}
          text="Salary"
          state={active === "salary"}
        />
      </div>
      <div className="tabs-content">
        <TabContentsItem
          content="I am a HR Specialist."
          open={active === "personal"}
        />
        <TabContentsItem
          content="No records have been added yet"
          open={active === "job"}
        />
        <TabContentsItem
          content="This type of information needs to be added by your manager"
          open={active === "salary"}
        />
      </div>
    </div>
  );
}

You can check out the working example here: https://codesandbox.io/s/exciting-gauss-lbghq您可以在此处查看工作示例: https : //codesandbox.io/s/exciting-gauss-lbghq

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

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