Skip to content

Commit 126d138

Browse files
committed
Added withModal HOC demo
1 parent eec171e commit 126d138

File tree

6 files changed

+73
-4
lines changed

6 files changed

+73
-4
lines changed

src/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import withBorder from './components/HOC/withBorder';
99

1010
class App extends Component {
1111
state = {
12-
show: false
12+
show: false,
1313
}
1414

1515
showModal = () => {
@@ -20,6 +20,7 @@ class App extends Component {
2020
}
2121
render() {
2222
let WithBorderInput = withBorder (InputTag);
23+
2324
return (
2425
<div className="App">
2526
{/* <WithBorderInput placeholder="press enter/space to + tag" /> */}
@@ -31,7 +32,6 @@ class App extends Component {
3132
<Modal onClose={this.showModal} show={this.state.show}>
3233
This message is from Modal!
3334
</Modal>
34-
3535
</div>
3636
);
3737
}

src/AppWithModal.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { Component } from 'react';
2+
import withModal from './components/HOC/withModal';
3+
import TodoItem from './features/todo/todo-item';
4+
import './appwithmodal.css';
5+
6+
export default class AppWithModal extends Component {
7+
state = {
8+
todos: [
9+
{id: 1, title: "Learn React"},
10+
{id: 2, title: "Learn Angular"},
11+
{id: 3, title: "Learn Vue"},
12+
{id: 4, title: "Learn Node"},
13+
{id: 5, title: "Learn MongoDB"},
14+
{id: 6, title: "Learn GraphQL"},
15+
{id: 7, title: "Learn Redux"},
16+
{id: 8, title: "Learn .NET Core"},
17+
]
18+
}
19+
20+
render() {
21+
let taskUI = this.state.todos.map((todo) => {
22+
let TodoItemWithModal = withModal(TodoItem);
23+
return (
24+
<TodoItemWithModal key={todo.id} todo={todo} />
25+
);
26+
});
27+
28+
return (
29+
<div>
30+
<h2>HOC - Modal Dialog Demo</h2>
31+
<div className="todo-list">
32+
{taskUI}
33+
</div>
34+
</div>
35+
);
36+
}
37+
}

src/appwithmodal.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
body {
2+
margin: 20px;
3+
}
4+
.todo-list {
5+
display: flex;
6+
flex: 1; /* All items same length, regardless of content */
7+
flex-direction: row;
8+
flex-wrap: wrap;
9+
}
10+
11+
.todo-item {
12+
padding: 20px;
13+
width: 200px;
14+
background-color: coral;
15+
color: white;
16+
margin: 10px;
17+
user-select: none;
18+
19+
}

src/components/Modal/Modal.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,6 @@ export default class Modal extends React.Component {
8383
}
8484

8585
Modal.propTypes = {
86-
onClose: PropTypes.func.isRequired
86+
onClose: PropTypes.func.isRequired,
87+
show: PropTypes.bool.isRequired
8788
}

src/features/todo/todo-item.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
const todoItem = ({todo}) => {
4+
return(
5+
<div className="todo-item">
6+
<h2>{todo.title}</h2>
7+
</div>
8+
);
9+
}
10+
11+
export default todoItem;

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import './index.css';
44
import App from './App';
55
import AppDragDropDemo from './AppDragDropDemo';
66
import AppSlideShow from './AppSlideShow';
7+
import AppWithModal from './AppWithModal';
78

89
import registerServiceWorker from './registerServiceWorker';
910

10-
ReactDOM.render(<App />, document.getElementById('root'));
11+
ReactDOM.render(<AppWithModal />, document.getElementById('root'));
1112
registerServiceWorker();

0 commit comments

Comments
 (0)