|
1 | 1 | import React from 'react' |
2 | 2 | import PropTypes from 'prop-types' |
3 | 3 | import './Modal.css' |
4 | | -import Context from '../context' |
5 | | -import TextareaAutosize from 'react-textarea-autosize' |
6 | 4 |
|
7 | | -function calcMaxRows() { |
8 | | - const small = 576 |
9 | | - const middle = 768 |
10 | | - const large = 992 |
11 | | - const winWidth = window.innerWidth |
12 | | - |
13 | | - |
14 | | - if (winWidth < small) return '7' |
15 | | - else if (winWidth < middle) return '8' |
16 | | - else if (winWidth < large) return '10' |
17 | | - else return '17' |
| 5 | +const classes = { |
| 6 | + modalWrapper: ' modal-window-wrapper ', |
| 7 | + modalBody: ' modal-window-body ' |
18 | 8 | } |
19 | | - |
| 9 | +export const ModalContext = React.createContext() |
20 | 10 | function Modal(props) { |
| 11 | + |
21 | 12 | const [isOpen, setOpenState] = React.useState(false) |
22 | | - const { removeCard, changeCardState, unsetEditCard, editCardContent } = React.useContext(Context) |
23 | | - |
24 | | - |
25 | | - if (props.card !== null && !isOpen) open() |
| 13 | + const Component = props.component |
| 14 | + const ComponentProps = props.componentProps |
26 | 15 |
|
27 | 16 | function open() { |
28 | 17 | setOpenState(true) |
29 | 18 | } |
30 | 19 |
|
31 | 20 | function close() { |
32 | | - unsetEditCard() |
33 | 21 | setOpenState(false) |
34 | 22 | } |
35 | 23 |
|
36 | | - function save(text) { |
37 | | - editCardContent(props.index, text) |
38 | | - } |
39 | | - |
40 | | - function onInputCange(e) { |
41 | | - save(e.target.value) |
42 | | - } |
43 | | - |
44 | 24 | return ( |
45 | 25 | <React.Fragment> |
46 | 26 | <div className="container p-1 d-none"> |
47 | 27 | <button className="btn" onClick={open}>Open modal</button> |
48 | 28 | </div> |
49 | 29 |
|
50 | | - {isOpen && ( |
51 | | - <div className='edit-modal' onClick={(e) => { if (e.target.className === 'edit-modal') close() }}> |
52 | | - <div className='edit-modal-body bg-light'> |
53 | | - |
54 | | - <div className='edit-modal-content bg-light'> |
55 | | - {props.card ? ( |
56 | | - <React.Fragment> |
57 | | - <h1 className="mb-2">Id: {props.card.id}</h1> |
58 | | - <h5 className="mb-2">Completed: |
59 | | - <span className="px-2 py-1 m-1 d-inline-block text-center" style={{ borderRadius: "5px", width: "3em", color: "white", backgroundColor: props.card.completed ? "green" : "red" }}> |
60 | | - {String(props.card.completed)} |
61 | | - </span> |
62 | | - </h5> |
63 | | - <TextareaAutosize |
64 | | - className="form-control p-0 mb-2 bg-light" |
65 | | - style={{ border: "none", outline: "none", boxShadow: "none", resize: "none" }} |
66 | | - minRows={3} |
67 | | - maxRows={calcMaxRows()} |
68 | | - value={props.card.text} |
69 | | - onChange={onInputCange} |
70 | | - /> |
71 | | - </React.Fragment> |
72 | | - ) : ( |
73 | | - <h1>No card</h1> |
74 | | - )} |
75 | | - </div> |
76 | | - |
77 | | - <div className='edit-modal-footer bg-light'> |
78 | | - <div> |
79 | | - <button |
80 | | - className="btn btn-light mx-1" |
81 | | - disabled={!props.card} |
82 | | - onClick={() => changeCardState(props.index)} |
83 | | - >✓</button> |
84 | | - <button |
85 | | - className="btn btn-light" |
86 | | - disabled={!props.card} |
87 | | - onClick={() => { close(); removeCard(props.index); }} |
88 | | - >✗</button> |
89 | | - </div> |
90 | | - <div> |
91 | | - <button |
92 | | - className="btn btn-light" |
93 | | - onClick={close} |
94 | | - >Close</button> |
95 | | - </div> |
96 | | - </div> |
97 | | - |
98 | | - </div> |
| 30 | + <div |
| 31 | + style={{ display: !isOpen ? "none" : "flex" }} |
| 32 | + className={classes.modalWrapper} |
| 33 | + onClick={(e) => { if (e.target.className === classes.modalWrapper) close() }} |
| 34 | + > |
| 35 | + <div className={classes.modalBody + 'bg-light'}> |
| 36 | + <ModalContext.Provider value={{ open, close, isOpen, classes }}> |
| 37 | + <Component {...ComponentProps} /> |
| 38 | + </ModalContext.Provider> |
99 | 39 | </div> |
100 | | - )} |
| 40 | + </div> |
| 41 | + |
101 | 42 | </React.Fragment> |
102 | 43 | ) |
103 | 44 | } |
104 | 45 |
|
105 | 46 | Modal.propTypes = { |
106 | | - card: PropTypes.object.isRequired, |
| 47 | + componentProps: PropTypes.object, |
107 | 48 | index: PropTypes.number |
108 | 49 | } |
109 | 50 |
|
|
0 commit comments