Skip to content

Commit ed3ba8e

Browse files
authored
Merge pull request #6205 from marmelab/update-tutorial
[Doc] Update tutorial images
2 parents 557ffff + 4ab672a commit ed3ba8e

13 files changed

+19
-9
lines changed

docs/Tutorial.md

+16-8
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,23 @@ export const UserList = props => (
225225

226226
![Url Field](./img/tutorial_url_field.png)
227227

228-
In react-admin, fields are simple React components. At runtime, they receive the `record` fetched from the API (e.g. `{ "id": 2, "name": "Ervin Howell", "website": "anastasia.net", ... }`), and the `source` field they should display (e.g. `website`).
228+
In react-admin, fields are simple React components. At runtime, they grab the `record` fetched from the API (e.g. `{ "id": 2, "name": "Ervin Howell", "website": "anastasia.net", ... }`) with a custom hook, and use the `source` field (e.g. `website`) to get the value they should display (e.g. "anastasia.net").
229229

230230
That means that writing a custom Field component is really straightforward. For instance, here is a simplified version of the `UrlField`:
231231

232232
```jsx
233233
// in src/MyUrlField.js
234234
import * as React from "react";
235+
import { useRecordContext } from 'react-admin';
235236

236-
const MyUrlField = ({ record = {}, source }) =>
237-
<a href={record[source]}>
238-
{record[source]}
239-
</a>;
237+
const MyUrlField = ({ source }) => {
238+
const record = useRecordContext();
239+
return record ? (
240+
<a href={record[source]}>
241+
{record[source]}
242+
</a>
243+
) : null;
244+
}
240245

241246
export default MyUrlField;
242247
```
@@ -274,6 +279,7 @@ The `MyUrlField` component is a perfect opportunity to illustrate how to customi
274279
```jsx
275280
// in src/MyUrlField.js
276281
import * as React from "react";
282+
import { useRecordContext } from 'react-admin';
277283
import { makeStyles } from '@material-ui/core/styles';
278284
import LaunchIcon from '@material-ui/icons/Launch';
279285

@@ -283,18 +289,20 @@ const useStyles = makeStyles({
283289
},
284290
icon: {
285291
width: '0.5em',
292+
height: '0.5em',
286293
paddingLeft: 2,
287294
},
288295
});
289296

290-
const MyUrlField = ({ record = {}, source }) => {
297+
const MyUrlField = ({ source }) => {
298+
const record = useRecordContext();
291299
const classes = useStyles();
292-
return (
300+
return record ? (
293301
<a href={record[source]} className={classes.link}>
294302
{record[source]}
295303
<LaunchIcon className={classes.icon} />
296304
</a>
297-
);
305+
) : null;
298306
}
299307

300308
export default MyUrlField;

docs/img/tutorial_custom_styles.png

-19.4 KB
Loading

docs/img/tutorial_edit_guesser.gif

-47.7 KB
Loading

docs/img/tutorial_guessed_list.png

-3.58 KB
Loading
-9.77 KB
Loading

docs/img/tutorial_list_user_name.png

-21.3 KB
Loading
-66.9 KB
Loading

docs/img/tutorial_url_field.png

-19.5 KB
Loading

docs/img/tutorial_users_list.png

-16.3 KB
Loading
Loading

examples/tutorial/.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SKIP_PREFLIGHT_CHECK=true

examples/tutorial/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
.env.development.local
1616
.env.test.local
1717
.env.production.local
18+
.eslintcache
1819

1920
npm-debug.log*
2021
yarn-debug.log*

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"lint": "eslint --ext .js,.ts,.tsx \"./packages/**/src/**/*.{js,ts,tsx}\" \"./examples/**/src/**/*.{js,ts,tsx}\" \"./cypress/**/*.{js,ts,tsx}\"",
1515
"prettier": "prettier --config ./.prettierrc.js --write --list-different \"packages/*/src/**/*.{js,json,ts,tsx,css,md}\" \"examples/*/src/**/*.{js,ts,json,tsx,css,md}\" \"cypress/**/*.{js,ts,json,tsx,css,md}\"",
1616
"run-simple": "cd examples/simple && yarn -s start",
17-
"run-tutorial": "yarn run -s build && cd examples/tutorial && yarn -s start",
17+
"run-tutorial": "cd examples/tutorial && yarn -s start",
1818
"run-demo": "cd examples/demo && cross-env REACT_APP_DATA_PROVIDER=rest yarn -s start",
1919
"run-graphql-demo": "cd examples/demo && cross-env REACT_APP_DATA_PROVIDER=graphql yarn -s start",
2020
"run-demo-watch": "concurrently \"yarn run watch\" \"yarn run run-demo\"",

0 commit comments

Comments
 (0)