【发布时间】:2021-01-15 03:40:08
【问题描述】:
我正在制作手风琴,当我们单击每个单独的项目时,它会打开或关闭。
现在我已经实现了全部展开或全部折叠选项以使所有手风琴展开/折叠。
Accordion.js
const accordionArray = [
{ heading: "Heading 1", text: "Text for Heading 1" },
{ heading: "Heading 2", text: "Text for Heading 2" },
{ heading: "Heading 3", text: "Text for Heading 3" }
];
.
.
.
{accordionArray.map((item, index) => (
<div key={index}>
<Accordion>
<Heading>
<div className="heading-box">
<h1 className="heading">{item.heading}</h1>
</div>
</Heading>
<Text expandAll={expandAll}>
<p className="text">{item.text}</p>
</Text>
</Accordion>
</div>
))}
text.js 是一个文件,我在其中执行打开手风琴的任何特定内容的操作,代码如下,
import React from "react";
class Text extends React.Component {
render() {
return (
<div style={{ ...this.props.style }}>
{this.props.expandAll ? (
<div className={`content open`}>
{this.props.render && this.props.render(this.props.text)}
</div>
) : (
<div className={`content ${this.props.text ? "open" : ""}`}>
{this.props.text ? this.props.children : ""}
{this.props.text
? this.props.render && this.props.render(this.props.text)
: ""}
</div>
)}
</div>
);
}
}
export default Text;
这里通过this.props.expandAll 我得到了expandAll 是真还是假的值。如果是真的,那么所有的手风琴都会得到className={`content open`} 的类,所以所有的都会被打开。
问题:
open 类已应用,但内部文本内容未呈现。
所以这条线行不通,
{this.props.render && this.props.render(this.props.text)}
要求:
如果单击全部展开/全部折叠按钮,则应分别打开/关闭所有手风琴。
这应该不考虑之前打开/关闭的手风琴。所以如果全部展开,那么它应该打开所有的手风琴,否则需要关闭所有的手风琴,即使它之前打开/关闭了。
链接:
这是文件https://codesandbox.io/s/react-accordion-forked-sm5fw?file=/src/GetAccordion.js 的链接,其中实际上传递了道具。
编辑:
如果我使用{this.props.children},那么每个手风琴都会打开。没有问题。
但是,如果我在单击特定项目时手动打开任何手风琴,那么如果我单击全部展开然后它的展开(预期)但是如果我单击返回折叠所有选项然后 不是全部手风琴关闭。 . 我们之前打开的那些仍然处于打开状态.. 但这里的预期行为是一切都应该关闭。
【问题讨论】:
标签: javascript css reactjs accordion collapse