我正在制作一个手风琴,当我们点击每个单独的项目时,它就会很好地打开或关闭。
现在我已经实现了全部展开或全部折叠选项,以使所有手风琴展开/折叠。
手风琴.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的链接,其中props实际上被传递了下来。
编辑:
如果我使用,{this.props.children}那么每个手风琴都会打开.. 没问题。
但是,如果我在单击特定项目时手动打开任何手风琴,那么如果我单击全部展开,则其已展开(预期)但如果我单击后退折叠所有选项,则并非所有手风琴都已关闭。我们之前打开的手风琴仍在打开状态.. 但这里的预期行为是一切都应该关闭。