Browse Source

Format examples with Prettier

main
Brian Vaughn 7 years ago
parent
commit
a03aa6c812
  1. 4
      examples/components-and-props/composing-components.js
  2. 20
      examples/components-and-props/extracting-components-continued.js
  3. 20
      examples/components-and-props/extracting-components.js
  4. 4
      examples/components-and-props/rendering-a-component.js
  5. 6
      examples/es5-syntax-example.js
  6. 2
      examples/hello-world.js
  7. 10
      examples/introducing-jsx.js
  8. 2
      examples/jsx-simple-example.js
  9. 111
      examples/reconciliation/index-used-as-key.js
  10. 106
      examples/reconciliation/no-index-used-as-key.js
  11. 2
      examples/tutorial-expanded-version.js
  12. 8
      package.json

4
examples/components-and-props/composing-components.js

@ -14,5 +14,5 @@ function App() {
ReactDOM.render( ReactDOM.render(
<App />, <App />,
document.getElementById('root') document.getElementById('root'),
); );

20
examples/components-and-props/extracting-components-continued.js

@ -4,9 +4,11 @@ function formatDate(date) {
function Avatar(props) { function Avatar(props) {
return ( return (
<img className="Avatar" <img
src={props.user.avatarUrl} className="Avatar"
alt={props.user.name} /> src={props.user.avatarUrl}
alt={props.user.name}
/>
); );
} }
@ -40,13 +42,15 @@ const comment = {
text: 'I hope you enjoy learning React!', text: 'I hope you enjoy learning React!',
author: { author: {
name: 'Hello Kitty', name: 'Hello Kitty',
avatarUrl: 'http://placekitten.com/g/64/64' avatarUrl:
} 'http://placekitten.com/g/64/64',
},
}; };
ReactDOM.render( ReactDOM.render(
<Comment <Comment
date={comment.date} date={comment.date}
text={comment.text} text={comment.text}
author={comment.author} />, author={comment.author}
document.getElementById('root') />,
); document.getElementById('root'),
);

20
examples/components-and-props/extracting-components.js

@ -6,9 +6,11 @@ function Comment(props) {
return ( return (
<div className="Comment"> <div className="Comment">
<div className="UserInfo"> <div className="UserInfo">
<img className="Avatar" <img
src={props.author.avatarUrl} className="Avatar"
alt={props.author.name} /> src={props.author.avatarUrl}
alt={props.author.name}
/>
<div className="UserInfo-name"> <div className="UserInfo-name">
{props.author.name} {props.author.name}
</div> </div>
@ -28,13 +30,15 @@ const comment = {
text: 'I hope you enjoy learning React!', text: 'I hope you enjoy learning React!',
author: { author: {
name: 'Hello Kitty', name: 'Hello Kitty',
avatarUrl: 'http://placekitten.com/g/64/64' avatarUrl:
} 'http://placekitten.com/g/64/64',
},
}; };
ReactDOM.render( ReactDOM.render(
<Comment <Comment
date={comment.date} date={comment.date}
text={comment.text} text={comment.text}
author={comment.author} />, author={comment.author}
document.getElementById('root') />,
); document.getElementById('root'),
);

4
examples/components-and-props/rendering-a-component.js

@ -5,5 +5,5 @@ function Welcome(props) {
const element = <Welcome name="Sara" />; const element = <Welcome name="Sara" />;
ReactDOM.render( ReactDOM.render(
element, element,
document.getElementById('root') document.getElementById('root'),
); );

6
examples/es5-syntax-example.js

@ -1,3 +1,5 @@
const element = <h1>Hello, world!</h1>; const element = <h1>Hello, world!</h1>;
const container = document.getElementById('root'); const container = document.getElementById(
ReactDOM.render(element, container); 'root',
);
ReactDOM.render(element, container);

2
examples/hello-world.js

@ -1,4 +1,4 @@
ReactDOM.render( ReactDOM.render(
<h1>Hello, world!</h1>, <h1>Hello, world!</h1>,
document.getElementById('root') document.getElementById('root'),
); );

10
examples/introducing-jsx.js

@ -1,5 +1,7 @@
function formatName(user) { function formatName(user) {
return user.firstName + ' ' + user.lastName; return (
user.firstName + ' ' + user.lastName
);
} }
const user = { const user = {
@ -8,12 +10,10 @@ const user = {
}; };
const element = ( const element = (
<h1> <h1>Hello, {formatName(user)}!</h1>
Hello, {formatName(user)}!
</h1>
); );
ReactDOM.render( ReactDOM.render(
element, element,
document.getElementById('root') document.getElementById('root'),
); );

2
examples/jsx-simple-example.js

@ -1,3 +1,3 @@
function hello() { function hello() {
return <div>Hello world!</div>; return <div>Hello world!</div>;
} }

111
examples/reconciliation/index-used-as-key.js

@ -1,8 +1,16 @@
const ToDo = (props) => ( const ToDo = props => (
<tr> <tr>
<td><label>{props.id}</label></td> <td>
<td><input/></td> <label>{props.id}</label>
<td><label>{props.createdAt.toTimeString()}</label></td> </td>
<td>
<input />
</td>
<td>
<label>
{props.createdAt.toTimeString()}
</label>
</td>
</tr> </tr>
); );
@ -14,90 +22,111 @@ class ToDoList extends React.Component {
this.state = { this.state = {
todoCounter: todoCounter, todoCounter: todoCounter,
list: [ list: [
{ id: todoCounter, createdAt: date }, {
] id: todoCounter,
} createdAt: date,
},
],
};
} }
sortByEarliest() { sortByEarliest() {
const sortedList = this.state.list.sort((a, b) => { const sortedList = this.state.list.sort(
return a.createdAt - b.createdAt; (a, b) => {
}); return a.createdAt - b.createdAt;
},
);
this.setState({ this.setState({
list: [...sortedList] list: [...sortedList],
}) });
} }
sortByLatest() { sortByLatest() {
const sortedList = this.state.list.sort((a, b) => { const sortedList = this.state.list.sort(
return b.createdAt - a.createdAt; (a, b) => {
}); return b.createdAt - a.createdAt;
},
);
this.setState({ this.setState({
list: [...sortedList] list: [...sortedList],
}) });
} }
addToEnd() { addToEnd() {
const date = new Date(); const date = new Date();
const nextId = this.state.todoCounter + 1; const nextId =
this.state.todoCounter + 1;
const newList = [ const newList = [
...this.state.list, ...this.state.list,
{ id: nextId, createdAt: date } {id: nextId, createdAt: date},
]; ];
this.setState({ this.setState({
list: newList, list: newList,
todoCounter: nextId todoCounter: nextId,
}); });
} }
addToStart() { addToStart() {
const date = new Date(); const date = new Date();
const nextId = this.state.todoCounter + 1; const nextId =
this.state.todoCounter + 1;
const newList = [ const newList = [
{ id: nextId, createdAt: date }, {id: nextId, createdAt: date},
...this.state.list ...this.state.list,
]; ];
this.setState({ this.setState({
list: newList, list: newList,
todoCounter: nextId todoCounter: nextId,
}); });
} }
render() { render() {
return( return (
<div> <div>
<code>key=index</code><br/> <code>key=index</code>
<button onClick={this.addToStart.bind(this)}> <br />
<button
onClick={this.addToStart.bind(
this,
)}>
Add New to Start Add New to Start
</button> </button>
<button onClick={this.addToEnd.bind(this)}> <button
onClick={this.addToEnd.bind(
this,
)}>
Add New to End Add New to End
</button> </button>
<button onClick={this.sortByEarliest.bind(this)}> <button
onClick={this.sortByEarliest.bind(
this,
)}>
Sort by Earliest Sort by Earliest
</button> </button>
<button onClick={this.sortByLatest.bind(this)}> <button
onClick={this.sortByLatest.bind(
this,
)}>
Sort by Latest Sort by Latest
</button> </button>
<table> <table>
<tr> <tr>
<th>ID</th><th></th><th>created at</th> <th>ID</th>
<th />
<th>created at</th>
</tr> </tr>
{ {this.state.list.map(
this.state.list.map((todo, index) => ( (todo, index) => (
<ToDo <ToDo key={index} {...todo} />
key={index} ),
{...todo} )}
/>
))
}
</table> </table>
</div> </div>
) );
} }
} }
ReactDOM.render( ReactDOM.render(
<ToDoList />, <ToDoList />,
document.getElementById('root') document.getElementById('root'),
); );

106
examples/reconciliation/no-index-used-as-key.js

@ -1,8 +1,16 @@
const ToDo = (props) => ( const ToDo = props => (
<tr> <tr>
<td><label>{props.id}</label></td> <td>
<td><input/></td> <label>{props.id}</label>
<td><label>{props.createdAt.toTimeString()}</label></td> </td>
<td>
<input />
</td>
<td>
<label>
{props.createdAt.toTimeString()}
</label>
</td>
</tr> </tr>
); );
@ -13,91 +21,115 @@ class ToDoList extends React.Component {
const toDoCounter = 1; const toDoCounter = 1;
this.state = { this.state = {
list: [ list: [
{ id: toDoCounter, createdAt: date }, {
id: toDoCounter,
createdAt: date,
},
], ],
toDoCounter: toDoCounter toDoCounter: toDoCounter,
} };
} }
sortByEarliest() { sortByEarliest() {
const sortedList = this.state.list.sort((a, b) => { const sortedList = this.state.list.sort(
return a.createdAt - b.createdAt; (a, b) => {
}); return a.createdAt - b.createdAt;
},
);
this.setState({ this.setState({
list: [...sortedList] list: [...sortedList],
}) });
} }
sortByLatest() { sortByLatest() {
const sortedList = this.state.list.sort((a, b) => { const sortedList = this.state.list.sort(
return b.createdAt - a.createdAt; (a, b) => {
}); return b.createdAt - a.createdAt;
},
);
this.setState({ this.setState({
list: [...sortedList] list: [...sortedList],
}) });
} }
addToEnd() { addToEnd() {
const date = new Date(); const date = new Date();
const nextId = this.state.toDoCounter + 1; const nextId =
this.state.toDoCounter + 1;
const newList = [ const newList = [
...this.state.list, ...this.state.list,
{ id: nextId, createdAt: date } {id: nextId, createdAt: date},
]; ];
this.setState({ this.setState({
list: newList, list: newList,
toDoCounter: nextId toDoCounter: nextId,
}); });
} }
addToStart() { addToStart() {
const date = new Date(); const date = new Date();
const nextId = this.state.toDoCounter + 1; const nextId =
this.state.toDoCounter + 1;
const newList = [ const newList = [
{ id: nextId, createdAt: date }, {id: nextId, createdAt: date},
...this.state.list ...this.state.list,
]; ];
this.setState({ this.setState({
list: newList, list: newList,
toDoCounter: nextId toDoCounter: nextId,
}); });
} }
render() { render() {
return( return (
<div> <div>
<code>key=id</code><br/> <code>key=id</code>
<button onClick={this.addToStart.bind(this)}> <br />
<button
onClick={this.addToStart.bind(
this,
)}>
Add New to Start Add New to Start
</button> </button>
<button onClick={this.addToEnd.bind(this)}> <button
onClick={this.addToEnd.bind(
this,
)}>
Add New to End Add New to End
</button> </button>
<button onClick={this.sortByEarliest.bind(this)}> <button
onClick={this.sortByEarliest.bind(
this,
)}>
Sort by Earliest Sort by Earliest
</button> </button>
<button onClick={this.sortByLatest.bind(this)}> <button
onClick={this.sortByLatest.bind(
this,
)}>
Sort by Latest Sort by Latest
</button> </button>
<table> <table>
<tr> <tr>
<th>ID</th><th></th><th>created at</th> <th>ID</th>
<th />
<th>created at</th>
</tr> </tr>
{ {this.state.list.map(
this.state.list.map((todo, index) => ( (todo, index) => (
<ToDo <ToDo
key={todo.id} key={todo.id}
{...todo} {...todo}
/> />
)) ),
} )}
</table> </table>
</div> </div>
) );
} }
} }
ReactDOM.render( ReactDOM.render(
<ToDoList />, <ToDoList />,
document.getElementById('root') document.getElementById('root'),
); );

2
examples/tutorial-expanded-version.js

@ -5,4 +5,4 @@
<li>WhatsApp</li> <li>WhatsApp</li>
<li>Oculus</li> <li>Oculus</li>
</ul> </ul>
</div> </div>;

8
package.json

@ -79,10 +79,14 @@
"ci-check": "npm-run-all prettier:diff --parallel lint flow", "ci-check": "npm-run-all prettier:diff --parallel lint flow",
"dev": "gatsby develop -H 0.0.0.0", "dev": "gatsby develop -H 0.0.0.0",
"flow": "flow", "flow": "flow",
"format:source": "prettier --config .prettierrc --write \"{gatsby-*.js,{flow-typed,plugins,src}/**/*.js}\"",
"format:examples": "prettier --config .prettierrc --write --print-width 44 \"examples/**/*.js\"",
"lint": "eslint .", "lint": "eslint .",
"netlify": "yarn install && yarn build", "netlify": "yarn install && yarn build",
"prettier": "prettier --config .prettierrc --write \"{gatsby-*.js,{flow-typed,plugins,src}/**/*.js}\"", "nit:source": "prettier --config .prettierrc --list-different \"{gatsby-*.js,{flow-typed,plugins,src}/**/*.js}\"",
"prettier:diff": "prettier --config .prettierrc --list-different \"{gatsby-*.js,{flow-typed,plugins,src}/**/*.js}\"", "nit:examples": "prettier --config .prettierrc --list-different --print-width 44 \"examples/**/*.js\"",
"prettier": "yarn format:source && yarn format:examples",
"prettier:diff": "yarn nit:source && yarn nit:examples",
"reset": "rimraf ./.cache" "reset": "rimraf ./.cache"
}, },
"devDependencies": { "devDependencies": {

Loading…
Cancel
Save