gpt4 book ai didi

reactjs - 如何在 react-bootstrap 中打开多个 Accordion 选项卡?

转载 作者:行者123 更新时间:2023-12-03 16:11:09 25 4
gpt4 key购买 nike

如果可能,我正在尝试打开多个 Accordion 。当前使用 react-bootstrap图书馆。
以下是我的实现:

<Accordion>
{data.rows.map((item, index) => {
return (
<Card
style={{
border: "none",
marginTop: "1em",
borderBottom: "1px solid #f1f1f1",
}}
>
<Card.Header
style={{ background: "transparent", padding: "0.75em 0" }}
>
<Row>
<Col lg="10" sm xs="9" style={{ alignSelf: "center" }}>
<p
className="cardtitle"
style={{ fontWeight: "600" }}
>
{item.title}
</p>
</Col>
<Col style={{ textAlign: "right" }} xs sm>
{" "}
<ContextAwareToggle eventKey={index}>
+
</ContextAwareToggle>
</Col>
</Row>
</Card.Header>
<Accordion.Collapse eventKey={index}>
<Card.Body>
<p className="cardcontent">{item.content}</p>
</Card.Body>
</Accordion.Collapse>
</Card>
);
})}{" "}
</Accordion>

...


function ContextAwareToggle({ children, eventKey, callback }) {
const currentEventKey = useContext(Fifth);

const decoratedOnClick = useAccordionToggle(
eventKey,
() => callback && callback(eventKey)
);

const isCurrentEventKey = currentEventKey === eventKey;
console.log(currentEventKey);
return (
<button
type="button"
className="accordianButton"
// style={{ backgroundColor: isCurrentEventKey ? "pink" : "lavender" }}
style={{
backgroundColor: "transparent",
fontSize: "1.8em",
fonWeight: "700",
border: "transparent",
color: "green",
}}
onClick={decoratedOnClick}
>
{isCurrentEventKey ? "-" : "+"}
</button>
);
}
以下是我获得代码引用的链接: https://react-bootstrap.github.io/components/accordion/
另外,如果不是 Accordion ,请建议任何其他组件。一直试图在我的项目中创建一个常见问题模板。
谢谢

最佳答案

Accordion 组件旨在一次扩展一张卡片。要实现多个tab打开的效果,需要像这样使用多个Accordion:

import React from "react";
import { Accordion, Card, Button } from "react-bootstrap";

const tabs = [
{ id: 1, label: "Tab 1", description: "Content of Tab 1" },
{ id: 2, label: "Tab 2", description: "Content of Tab 2" },
{ id: 3, label: "Tab 3", description: "Content of Tab 3" }
];

export default function App() {
return (
<div className="App">
{tabs.map(tab => (
<Accordion key={tab.id} defaultActiveKey={tab.id}>
<Card>
<Card.Header>
<Accordion.Toggle as={Button} variant="link" eventKey={tab.id}>
{tab.label}
</Accordion.Toggle>
</Card.Header>
<Accordion.Collapse eventKey={tab.id}>
<Card.Body>{tab.description}</Card.Body>
</Accordion.Collapse>
</Card>
</Accordion>
))}
</div>
);
}

这是一个代码沙箱 https://codesandbox.io/s/react-bootstrap-multiple-accordion-tabs-oboks

关于reactjs - 如何在 react-bootstrap 中打开多个 Accordion 选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62358575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com