Skip to content

Commit 51dc2e4

Browse files
committed
[Doc] update some js and jsx snippet to ts and tsx
1 parent e047cd1 commit 51dc2e4

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

docs/Tutorial.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,13 @@ But on desktop, `<SimpleList>` takes too much space for a low information densit
273273

274274
To do so, we'll use [the `useMediaQuery` hook](https://mui.com/material-ui/react-use-media-query/) from Material UI:
275275

276-
```jsx
276+
```tsx
277277
// in src/users.tsx
278-
import { useMediaQuery } from "@mui/material";
278+
import { useMediaQuery, Theme } from "@mui/material";
279279
import { List, SimpleList, Datagrid, TextField, EmailField } from "react-admin";
280280

281281
export const UserList = () => {
282-
const isSmall = useMediaQuery((theme) => theme.breakpoints.down("sm"));
282+
const isSmall = useMediaQuery<Theme>((theme) => theme.breakpoints.down("sm"));
283283
return (
284284
<List>
285285
{isSmall ? (
@@ -372,11 +372,11 @@ In react-admin, fields are just React components. When rendered, they grab the `
372372

373373
That means that you can do the same to write a custom Field. For instance, here is a simplified version of the `<UrlField>`:
374374

375-
```jsx
375+
```tsx
376376
// in src/MyUrlField.tsx
377377
import { useRecordContext } from "react-admin";
378378

379-
const MyUrlField = ({ source }) => {
379+
const MyUrlField = ({ source }: { source: string }) => {
380380
const record = useRecordContext();
381381
if (!record) return null;
382382
return <a href={record[source]}>{record[source]}</a>;
@@ -415,13 +415,13 @@ The `<MyUrlField>` component is a perfect opportunity to illustrate how to custo
415415
React-admin relies on [Material UI](https://mui.com/material-ui/getting-started/overview/), a set of React components modeled after Google's [Material Design Guidelines](https://material.io/). All Material UI components (and most react-admin components) support a prop called `sx`, which allows custom inline styles. Let's take advantage of the `sx` prop to remove the underline from the link and add an icon:
416416

417417
{% raw %}
418-
```jsx
418+
```tsx
419419
// in src/MyUrlField.tsx
420420
import { useRecordContext } from "react-admin";
421421
import { Link } from "@mui/material";
422422
import LaunchIcon from "@mui/icons-material/Launch";
423423

424-
const MyUrlField = ({ source }) => {
424+
const MyUrlField = ({ source }: { source: string }) => {
425425
const record = useRecordContext();
426426
return record ? (
427427
<Link href={record[source]} sx={{ textDecoration: "none" }}>
@@ -877,13 +877,13 @@ For this tutorial, since there is no public authentication API, we can use a fak
877877

878878
The `authProvider` must expose 5 methods, each returning a `Promise`:
879879

880-
```js
880+
```ts
881881
// in src/authProvider.ts
882+
import { AuthProvider } from "react-admin";
882883

883-
// TypeScript users must reference the type: `AuthProvider`
884-
export const authProvider = {
884+
export const authProvider: AuthProvider = {
885885
// called when the user attempts to log in
886-
login: ({ username }) => {
886+
login: ({ username }: { username: string }) => {
887887
localStorage.setItem("username", username);
888888
// accept all username/password combinations
889889
return Promise.resolve();
@@ -894,7 +894,7 @@ export const authProvider = {
894894
return Promise.resolve();
895895
},
896896
// called when the API returns an error
897-
checkError: ({ status }) => {
897+
checkError: ({ status }: { status: number }) => {
898898
if (status === 401 || status === 403) {
899899
localStorage.removeItem("username");
900900
return Promise.reject();
@@ -962,16 +962,15 @@ React-admin calls the Data Provider with one method for each of the actions of t
962962

963963
The code for a Data Provider for the `my.api.url` API is as follows:
964964

965-
```js
965+
```ts
966966
// in src/dataProvider.ts
967-
import { fetchUtils } from "react-admin";
967+
import { DataProvider, fetchUtils } from "react-admin";
968968
import { stringify } from "query-string";
969969

970970
const apiUrl = 'https://my.api.com/';
971971
const httpClient = fetchUtils.fetchJson;
972972

973-
// TypeScript users must reference the type `DataProvider`
974-
export const dataProvider = {
973+
export const dataProvider: DataProvider = {
975974
getList: (resource, params) => {
976975
const { page, perPage } = params.pagination;
977976
const { field, order } = params.sort;

0 commit comments

Comments
 (0)