Когда появляется модальный, я хочу удалить элемент списка. Из компонента App я хочу передать функцию удаления компоненту 'modal'. Когда появляется модальное -> я нажимаю удалить -> элемент в списке исчезает? как переместить индекс удалённого предмета в модал?
Щелкните значок крестиком внутри li -> Показать модальное окно ---> Нажмите Удалить -> Удалить элемент
import React, { Component } from "react";
import ReactDOM from "react-dom";
import Modal from "./components/Modal";
class App extends Component {
constructor(props) {
super(props);
this.state = {
isOpen: false,
arrays: [],
index: null
};
}
remove = index =>
this.setState({
arrays: [
...this.state.arrays.slice(0, index),
...this.state.arrays.slice(index + 1)
],
isOpen: false
});
toggleModal = () => {
this.setState({
isOpen: !this.state.isOpen,
index: index
});
};
render() {
return (
<div className="App">
<ul>
{this.state.arrays.map((array, index) => (
<li key={index}>
{array.name}
<i className="fa fa-cross" onClick={() =>
this.toggleModal(index)}/>
</li>
))}
</ul>
<Modal
show={this.state.isOpen}
index={this.state.index}
onRemove={this.remove}
onClose={this.toggleModal}
/>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
компоненты / modal.js
import React, { Component } from "react";
class Modal extends Component {
render() {
// Render nothing if the "show" prop is false
if (!this.props.show) {
return null;
}
// The gray background
const backdropStyle = {
position: "fixed",
top: 0,
bottom: 0,
left: 0,
right: 0,
backgroundColor: "rgba(0,0,0,0.3)",
padding: 50
};
// The modal "window"
const modalStyle = {
backgroundColor: "#fff",
borderRadius: 5,
maxWidth: 500,
minHeight: 300,
margin: "0 auto",
padding: 30
};
return (
<div className="backdrop" style={backdropStyle}>
<div className="modal" style={modalStyle}>
{this.props.children}
<div className="footer">
<button onClick=
{this.props.onRemove(this.props.index}>Delete</button>
<button onClick={this.props.onClose}>Cancel</button>
</div>
</div>
</div>
);
}
}
export default Modal;