Skip to content

Instantly share code, notes, and snippets.

@pca2
Last active July 16, 2018 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pca2/8eff87200086f0de451ce3b66c3777a4 to your computer and use it in GitHub Desktop.
Save pca2/8eff87200086f0de451ce3b66c3777a4 to your computer and use it in GitHub Desktop.
Chapter2
<div id="root"></div>
const list = [
{
title: "React",
url: "https://facebook.github.io/react/",
author: "Jordan Walke",
num_comments: 3,
points: 4,
objectID: 0
},
{
title: "Redux",
url: "https://github.com/reactjs/redux",
author: "Dan Abramov, Andrew Clark",
num_comments: 2,
points: 5,
objectID: 1
}
];
const isSearched = searchTerm => item => item.title.toLowerCase().includes(searchTerm.toLowerCase())
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
list,
searchTerm: '',
}
this.onDismiss = this.onDismiss.bind(this);
this.onSearchChange = this.onSearchChange.bind(this);
}
onSearchChange(event) {
this.setState({ searchTerm: event.target.value });
}
onDismiss(id){
const updatedList = this.state.list.filter(item => item.objectID !== id);
this.setState({list: updatedList});
}
render() {
const { searchTerm, list } = this.state;
return (
<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>
)
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
body {
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;
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;
}
.page {
margin: 20px;
}
.interactions {
text-align: center;
}
.table {
margin: 20px 0;
}
.table-header {
display: flex;
line-height: 24px;
font-size: 16px;
padding: 0 10px;
justify-content: space-between;
}
.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;
}
.table-header > span {
overflow: hidden;
text-overflow: ellipsis;
padding: 0 5px;
}
.table-row > span {
overflow: hidden;
text-overflow: ellipsis;
padding: 0 5px;
}
.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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment