反应 - Expand/Collapse 全部
React - Expand/Collapse All
我想Expand/Collapse全部使用一个按钮点击。
使用 reactstrap 创建了一个新组件 CollapsiblePanel,并在容器中使用了创建的组件(index.js)。
现在我可以 expand/collapse 单击按钮时的所有容器。
https://codesandbox.io/s/fervent-banzai-s9pst
CollapsiblePanel.js
import React, { useState, useEffect } from "react";
import { Collapse } from "reactstrap";
function CollapsiblePanel({ children, ...props }) {
const { title, collapse } = props;
const [isCollapse, setIsCollapse] = useState(collapse);
const [icon, setIcon] = useState("fa fa-chevron-down");
const toggle = () => {
setIsCollapse(!isCollapse);
setIcon(state => {
return state === "fa fa-chevron-down"
? "fa fa-chevron-right"
: "fa fa-chevron-down";
});
};
useEffect(() => {
toggle();
}, [collapse]);
return (
<div className="coll-panel">
<button
type="button"
className="coll-panel-btn btn-primary btn-block text-left"
onClick={() => toggle()}
>
<i className={icon} /> {title}
</button>
<Collapse className="border text-left p-2" isOpen={isCollapse}>
{children}
</Collapse>
</div>
);
}
CollapsiblePanel.defaultProps = {
children: "Add node as a child",
title: "Collapsible Panel",
collapse: true
};
export default CollapsiblePanel;
index.js
import React, { useState } from "react";
import ReactDOM from "react-dom";
import CollapsiblePanel from "./component/CollapsiblePanel";
import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
import "./styles.css";
function App() {
const text =
"Lorem ipsum dolor sit amet consectetur adipisicing elit.Nihil earum illo ipsa velit facilis provident qui eligendi, quia ut magnam tenetur. Accusantium nisi quos delectus in necessitatibus ad. Ducimus, id!";
const [collapse, setCollapse] = useState(true);
const [title, setTitle] = useState("Expand All");
const [icon, setIcon] = useState("fa fa-chevron-right");
const collapseAll = () => {
setCollapse(!collapse);
setIcon(state => {
return state === "fa fa-chevron-right"
? "fa fa-chevron-down"
: "fa fa-chevron-right";
});
setTitle(state => {
return state === "Expand All" ? "Collapse All" : "Expand All";
});
};
return (
<div className="App container my-2">
{/* Expand/Collapse All */}
<button
type="button"
className="btn-warning mt-4 mb-3 float-left"
onClick={collapseAll}
>
<i className={icon} /> {title}
</button>
<CollapsiblePanel title="Title 1" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
<CollapsiblePanel title="Title 2" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
<CollapsiblePanel title="Title 3" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
<CollapsiblePanel title="Title 4" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
<CollapsiblePanel title="Title 5" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
</div>
);
}
现在:
在单击 Expand/Collapse 全部按钮之前,如果单击任何可折叠容器(在 index.js 中),然后在单击 Expand/Collapse 按钮时并非所有容器都展开。
打开即关闭,反之亦然
预期:
尽管容器明确地 opened/closed。单击 Expand/Collapse 我想 expand/collapse 全部。提前致谢。
在可折叠面板中,您正在切换之前的状态。相反,您必须明确设置 expand/collapse state..
const animate = collapse => {
setIsCollapse(collapse);
setIcon(state => {
return state === "fa fa-chevron-down"
? "fa fa-chevron-right"
: "fa fa-chevron-down";
});
};
useEffect(() => {
animate(!collapse);
}, [collapse]);
试试下面的沙箱。
我想Expand/Collapse全部使用一个按钮点击。
使用 reactstrap 创建了一个新组件 CollapsiblePanel,并在容器中使用了创建的组件(index.js)。
现在我可以 expand/collapse 单击按钮时的所有容器。
https://codesandbox.io/s/fervent-banzai-s9pst
CollapsiblePanel.js
import React, { useState, useEffect } from "react";
import { Collapse } from "reactstrap";
function CollapsiblePanel({ children, ...props }) {
const { title, collapse } = props;
const [isCollapse, setIsCollapse] = useState(collapse);
const [icon, setIcon] = useState("fa fa-chevron-down");
const toggle = () => {
setIsCollapse(!isCollapse);
setIcon(state => {
return state === "fa fa-chevron-down"
? "fa fa-chevron-right"
: "fa fa-chevron-down";
});
};
useEffect(() => {
toggle();
}, [collapse]);
return (
<div className="coll-panel">
<button
type="button"
className="coll-panel-btn btn-primary btn-block text-left"
onClick={() => toggle()}
>
<i className={icon} /> {title}
</button>
<Collapse className="border text-left p-2" isOpen={isCollapse}>
{children}
</Collapse>
</div>
);
}
CollapsiblePanel.defaultProps = {
children: "Add node as a child",
title: "Collapsible Panel",
collapse: true
};
export default CollapsiblePanel;
index.js
import React, { useState } from "react";
import ReactDOM from "react-dom";
import CollapsiblePanel from "./component/CollapsiblePanel";
import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
import "./styles.css";
function App() {
const text =
"Lorem ipsum dolor sit amet consectetur adipisicing elit.Nihil earum illo ipsa velit facilis provident qui eligendi, quia ut magnam tenetur. Accusantium nisi quos delectus in necessitatibus ad. Ducimus, id!";
const [collapse, setCollapse] = useState(true);
const [title, setTitle] = useState("Expand All");
const [icon, setIcon] = useState("fa fa-chevron-right");
const collapseAll = () => {
setCollapse(!collapse);
setIcon(state => {
return state === "fa fa-chevron-right"
? "fa fa-chevron-down"
: "fa fa-chevron-right";
});
setTitle(state => {
return state === "Expand All" ? "Collapse All" : "Expand All";
});
};
return (
<div className="App container my-2">
{/* Expand/Collapse All */}
<button
type="button"
className="btn-warning mt-4 mb-3 float-left"
onClick={collapseAll}
>
<i className={icon} /> {title}
</button>
<CollapsiblePanel title="Title 1" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
<CollapsiblePanel title="Title 2" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
<CollapsiblePanel title="Title 3" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
<CollapsiblePanel title="Title 4" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
<CollapsiblePanel title="Title 5" collapse={collapse}>
<span>{text}</span>
</CollapsiblePanel>
</div>
);
}
现在: 在单击 Expand/Collapse 全部按钮之前,如果单击任何可折叠容器(在 index.js 中),然后在单击 Expand/Collapse 按钮时并非所有容器都展开。
打开即关闭,反之亦然
预期:
尽管容器明确地 opened/closed。单击 Expand/Collapse 我想 expand/collapse 全部。提前致谢。
在可折叠面板中,您正在切换之前的状态。相反,您必须明确设置 expand/collapse state..
const animate = collapse => {
setIsCollapse(collapse);
setIcon(state => {
return state === "fa fa-chevron-down"
? "fa fa-chevron-right"
: "fa fa-chevron-down";
});
};
useEffect(() => {
animate(!collapse);
}, [collapse]);
试试下面的沙箱。