Skip to content

Commit

Permalink
Merge pull request #331 from bvaughn/prettier-format-examples
Browse files Browse the repository at this point in the history
Format examples with Prettier too
  • Loading branch information
bvaughn committed Nov 24, 2017
2 parents a4cfe30 + f0f3753 commit 1d14413
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 101 deletions.
8 changes: 8 additions & 0 deletions .prettierrc.examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"bracketSpacing": false,
"jsxBracketSameLine": true,
"parser": "flow",
"printWidth": 40,
"singleQuote": true,
"trailingComma": "es5"
}
2 changes: 1 addition & 1 deletion examples/components-and-props/composing-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ function App() {
ReactDOM.render(
<App />,
document.getElementById('root')
);
);
21 changes: 13 additions & 8 deletions examples/components-and-props/extracting-components-continued.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ function formatDate(date) {

function Avatar(props) {
return (
<img className="Avatar"
src={props.user.avatarUrl}
alt={props.user.name} />
<img
className="Avatar"
src={props.user.avatarUrl}
alt={props.user.name}
/>
);
}

Expand Down Expand Up @@ -37,16 +39,19 @@ function Comment(props) {

const comment = {
date: new Date(),
text: 'I hope you enjoy learning React!',
text:
'I hope you enjoy learning React!',
author: {
name: 'Hello Kitty',
avatarUrl: 'http://placekitten.com/g/64/64'
}
avatarUrl:
'http://placekitten.com/g/64/64',
},
};
ReactDOM.render(
<Comment
date={comment.date}
text={comment.text}
author={comment.author} />,
author={comment.author}
/>,
document.getElementById('root')
);
);
21 changes: 13 additions & 8 deletions examples/components-and-props/extracting-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<img className="Avatar"
src={props.author.avatarUrl}
alt={props.author.name} />
<img
className="Avatar"
src={props.author.avatarUrl}
alt={props.author.name}
/>
<div className="UserInfo-name">
{props.author.name}
</div>
Expand All @@ -25,16 +27,19 @@ function Comment(props) {

const comment = {
date: new Date(),
text: 'I hope you enjoy learning React!',
text:
'I hope you enjoy learning React!',
author: {
name: 'Hello Kitty',
avatarUrl: 'http://placekitten.com/g/64/64'
}
avatarUrl:
'http://placekitten.com/g/64/64',
},
};
ReactDOM.render(
<Comment
date={comment.date}
text={comment.text}
author={comment.author} />,
author={comment.author}
/>,
document.getElementById('root')
);
);
2 changes: 1 addition & 1 deletion examples/components-and-props/rendering-a-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ const element = <Welcome name="Sara" />;
ReactDOM.render(
element,
document.getElementById('root')
);
);
6 changes: 4 additions & 2 deletions examples/es5-syntax-example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const element = <h1>Hello, world!</h1>;
const container = document.getElementById('root');
ReactDOM.render(element, container);
const container = document.getElementById(
'root'
);
ReactDOM.render(element, container);
8 changes: 4 additions & 4 deletions examples/introducing-jsx.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function formatName(user) {
return user.firstName + ' ' + user.lastName;
return (
user.firstName + ' ' + user.lastName
);
}

const user = {
Expand All @@ -8,9 +10,7 @@ const user = {
};

const element = (
<h1>
Hello, {formatName(user)}!
</h1>
<h1>Hello, {formatName(user)}!</h1>
);

ReactDOM.render(
Expand Down
2 changes: 1 addition & 1 deletion examples/jsx-simple-example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function hello() {
return <div>Hello world!</div>;
}
}
108 changes: 72 additions & 36 deletions examples/reconciliation/index-used-as-key.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
const ToDo = (props) => (
const ToDo = props => (
<tr>
<td><label>{props.id}</label></td>
<td><input/></td>
<td><label>{props.createdAt.toTimeString()}</label></td>
<td>
<label>{props.id}</label>
</td>
<td>
<input />
</td>
<td>
<label>
{props.createdAt.toTimeString()}
</label>
</td>
</tr>
);

Expand All @@ -14,86 +22,114 @@ class ToDoList extends React.Component {
this.state = {
todoCounter: todoCounter,
list: [
{ id: todoCounter, createdAt: date },
]
}
{
id: todoCounter,
createdAt: date,
},
],
};
}

sortByEarliest() {
const sortedList = this.state.list.sort((a, b) => {
return a.createdAt - b.createdAt;
});
const sortedList = this.state.list.sort(
(a, b) => {
return (
a.createdAt - b.createdAt
);
}
);
this.setState({
list: [...sortedList]
})
list: [...sortedList],
});
}

sortByLatest() {
const sortedList = this.state.list.sort((a, b) => {
return b.createdAt - a.createdAt;
});
const sortedList = this.state.list.sort(
(a, b) => {
return (
b.createdAt - a.createdAt
);
}
);
this.setState({
list: [...sortedList]
})
list: [...sortedList],
});
}

addToEnd() {
const date = new Date();
const nextId = this.state.todoCounter + 1;
const nextId =
this.state.todoCounter + 1;
const newList = [
...this.state.list,
{ id: nextId, createdAt: date }
{id: nextId, createdAt: date},
];
this.setState({
list: newList,
todoCounter: nextId
todoCounter: nextId,
});
}

addToStart() {
const date = new Date();
const nextId = this.state.todoCounter + 1;
const nextId =
this.state.todoCounter + 1;
const newList = [
{ id: nextId, createdAt: date },
...this.state.list
{id: nextId, createdAt: date},
...this.state.list,
];
this.setState({
list: newList,
todoCounter: nextId
todoCounter: nextId,
});
}

render() {
return(
return (
<div>
<code>key=index</code><br/>
<button onClick={this.addToStart.bind(this)}>
<code>key=index</code>
<br />
<button
onClick={this.addToStart.bind(
this
)}>
Add New to Start
</button>
<button onClick={this.addToEnd.bind(this)}>
<button
onClick={this.addToEnd.bind(
this
)}>
Add New to End
</button>
<button onClick={this.sortByEarliest.bind(this)}>
<button
onClick={this.sortByEarliest.bind(
this
)}>
Sort by Earliest
</button>
<button onClick={this.sortByLatest.bind(this)}>
<button
onClick={this.sortByLatest.bind(
this
)}>
Sort by Latest
</button>
<table>
<tr>
<th>ID</th><th></th><th>created at</th>
<th>ID</th>
<th />
<th>created at</th>
</tr>
{
this.state.list.map((todo, index) => (
{this.state.list.map(
(todo, index) => (
<ToDo
key={index}
{...todo}
/>
))
}
)
)}
</table>
</div>
)
);
}
}

Expand Down

0 comments on commit 1d14413

Please sign in to comment.