【发布时间】:2021-05-09 04:42:25
【问题描述】:
我正在用情感构建一个 React 选项卡导航组件。我无法找到可以让我:
- 最初隐藏除按钮之外的所有内容,并且不对按钮设置样式。
- 当您单击按钮时,激活样式并显示与该按钮关联的内容。
- 最后,当您在外部单击或输入为空时重置为初始状态。
代码如下: Code
import React, { useState } from "react";
import ReactDOM from "react-dom";
import styled from "@emotion/styled";
import "./styles.css";
const StyledShowButton = styled("button", {
shouldForwardProp: (prop) => ["active"].indexOf(prop) === -1
})`
color: ${({ active }) => (active ? "red" : "black")};
`;
function App() {
const [active, setActive] = useState(0);
const [showInput, setShowInput] = useState(false);
const handleInputChange = (e) => {
if (e.target.value < 1) {
console.log("Reset Everyting");
}
};
const handleTabClick = (e) => {
const index = parseInt(e.target.id, 0);
if (index !== active) {
setActive(index);
}
if (!showInput) {
setShowInput(!showInput);
}
};
return (
<div className="App">
<StyledShowButton
type="button"
id={0}
active={active === 0}
onClick={handleTabClick}
>
First
</StyledShowButton>
<StyledShowButton
type="button"
id={1}
active={active === 1}
onClick={handleTabClick}
>
Second
</StyledShowButton>
{/* CONTENT */}
{active === 0 ? (
<input placeholder="First input" onChange={handleInputChange} />
) : (
<input placeholder="Second input" onChange={handleInputChange} />
)}
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
就问我是不是说的不够清楚,
先谢谢了!
埃里克
【问题讨论】:
标签: javascript css reactjs emotion