Skip to content

Commit

Permalink
chapter 2
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed Feb 14, 2018
1 parent 7528945 commit 5b68953
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 29 deletions.
67 changes: 51 additions & 16 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
.App {
.page {
margin: 20px;
}

.interactions {
text-align: center;
}

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
.table {
margin: 20px 0;
}

.table-header {
display: flex;
line-height: 24px;
font-size: 16px;
padding: 0 10px;
justify-content: space-between;
}

.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
.table-empty {
margin: 200px;
text-align: center;
font-size: 16px;
}

.table-row {
display: flex;
line-height: 24px;
white-space: nowrap;
margin: 10px 0;
padding: 10px;
background: #ffffff;
border: 1px solid #e3e3e3;
}

.App-title {
font-size: 1.5em;
.table-header > span {
overflow: hidden;
text-overflow: ellipsis;
padding: 0 5px;
}

.App-intro {
font-size: large;
.table-row > span {
overflow: hidden;
text-overflow: ellipsis;
padding: 0 5px;
}

@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
.button-inline {
border-width: 0;
background: transparent;
color: inherit;
text-align: inherit;
-webkit-font-smoothing: inherit;
padding: 0;
font-size: inherit;
cursor: pointer;
}

.button-active {
border-radius: 0;
border-bottom: 1px solid #38BB6C;
}
101 changes: 90 additions & 11 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,102 @@ const list = [
},
];

const isSearched = searchTerm => item =>
item.title.toLowerCase().includes(searchTerm.toLowerCase());

class App extends Component {
constructor(props) {
super(props);

this.state = {
list,
searchTerm: '',
};

this.onSearchChange = this.onSearchChange.bind(this);
this.onDismiss = this.onDismiss.bind(this);
}

onSearchChange(event) {
this.setState({ searchTerm: event.target.value });
}

onDismiss(id) {
const isNotId = item => item.objectID !== id;
const updatedList = this.state.list.filter(isNotId);
this.setState({ list: updatedList });
}

render() {
const { searchTerm, list } = this.state;
return (
<div className="App">
{list.map(item =>
<div key={item.objectID}>
<span>
<a href={item.url}>{item.title}</a>
</span>
<span>{item.author}</span>
<span>{item.num_comments}</span>
<span>{item.points}</span>
</div>
)}
<div className="page">
<div className="interactions">
<Search
value={searchTerm}
onChange={this.onSearchChange}
>
Search
</Search>
</div>
<Table
list={list}
pattern={searchTerm}
onDismiss={this.onDismiss}
/>
</div>
);
}
}

const Search = ({ value, onChange, children }) =>
<form>
{children} <input
type="text"
value={value}
onChange={onChange}
/>
</form>

const Table = ({ list, pattern, onDismiss }) =>
<div className="table">
{list.filter(isSearched(pattern)).map(item =>
<div key={item.objectID} className="table-row">
<span style={{ width: '40%' }}>
<a href={item.url}>{item.title}</a>
</span>
<span style={{ width: '30%' }}>
{item.author}
</span>
<span style={{ width: '10%' }}>
{item.num_comments}
</span>
<span style={{ width: '10%' }}>
{item.points}
</span>
<span style={{ width: '10%' }}>
<Button
onClick={() => onDismiss(item.objectID)}
className="button-inline"
>
Dismiss
</Button>
</span>
</div>
)}
</div>

const Button = ({
onClick,
className = '',
children,
}) =>
<button
onClick={onClick}
className={className}
type="button"
>
{children}
</button>

export default App;
43 changes: 41 additions & 2 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
body {
margin: 0;
color: #222;
background: #f4f4f4;
font: 400 14px CoreSans, Arial,sans-serif;
}

a {
color: #222;
}

a:hover {
text-decoration: underline;
}

ul, li {
list-style: none;
padding: 0;
font-family: sans-serif;
margin: 0;
}

input {
padding: 10px;
border-radius: 5px;
outline: none;
margin-right: 10px;
border: 1px solid #dddddd;
}

button {
padding: 10px;
border-radius: 5px;
border: 1px solid #dddddd;
background: transparent;
color: #808080;
cursor: pointer;
}

button:hover {
color: #222;
}

*:focus {
outline: none;
}

0 comments on commit 5b68953

Please sign in to comment.