-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9095 from marmelab/RecordRepresentation
Add RecordRepresentation component
- Loading branch information
Showing
9 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
layout: default | ||
title: "The RecordRepresentation Component" | ||
--- | ||
|
||
Render the record representation, leveraging the [`recordRepresentation`](./Resource.md#recordrepresentation) prop of the parent `<Resource>` component. | ||
|
||
You can also uses its hook version: [`<useGetRecordRepresentation>`](./useGetRecordRepresentation.md). | ||
|
||
## Usage | ||
|
||
```tsx | ||
// in src/posts/PostBreadcrumbs.tsx | ||
import * as React from 'react'; | ||
import { Breadcrumbs, Typography } from '@mui/material'; | ||
import { Link, RecordRepresentation } from 'react-admin'; | ||
|
||
export const PostBreadcrumbs = () => { | ||
return ( | ||
<div role="presentation"> | ||
<Breadcrumbs aria-label="breadcrumb"> | ||
<Link underline="hover" color="inherit" to="/"> | ||
Home | ||
</Link> | ||
<Link underline="hover" color="inherit" to="/posts"> | ||
Posts | ||
</Link> | ||
<Typography color="text.primary"> | ||
<RecordRepresentation /> | ||
</Typography> | ||
</Breadcrumbs> | ||
</div> | ||
); | ||
} | ||
|
||
// in src/posts/PostEdit.tsx | ||
import { EditBase, EditView, SimpleForm, TextInput } from 'react-admin'; | ||
import { PostBreadcrumbs } from './PostBreadcrumbs'; | ||
|
||
const PostEdit = () => ( | ||
<EditBase> | ||
<PostBreadcrumbs /> | ||
<EditView> | ||
<SimpleForm> | ||
<TextInput source="title" /> | ||
</SimpleForm> | ||
</EditView> | ||
</EditBase> | ||
) | ||
``` | ||
|
||
## Props | ||
|
||
Here are all the props you can set on the `<RecordRepresentation>` component: | ||
|
||
| Prop | Required | Type | Default | Description | | ||
| ---------- | -------- | ---------- | ------------------------------------------ | ----------------------| | ||
| `record` | Optional | `RaRecord` | Record from the parent `RecordContext` | The record to display | | ||
| `resource` | Optional | `string` | Resource from the parent `ResourceContext` | The record's resource | | ||
|
||
## `record` | ||
|
||
The record to display. Defaults to the record from the parent `RecordContext`. | ||
|
||
## `resource` | ||
|
||
The record's resource. Defaults to the resource from the parent `ResourceContext`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
layout: default | ||
title: "The useGetRecordRepresentation Component" | ||
--- | ||
|
||
Returns a function that get the record representation, leveraging the [`recordRepresentation`](./Resource.md#recordrepresentation) prop of the parent `<Resource>` component. | ||
|
||
You can also uses its component version: [`<RecordRepresentation>`](./RecordRepresentation.md). | ||
|
||
## Usage | ||
|
||
```tsx | ||
// in src/posts/PostBreadcrumbs.tsx | ||
import * as React from 'react'; | ||
import { Breadcrumbs, Typography } from '@mui/material'; | ||
import { Link, useGetRecordRepresentation, useRecordContext, useResourceContext } from 'react-admin'; | ||
|
||
export const PostBreadcrumbs = () => { | ||
const record = useRecordContext(); | ||
const resource = useResourceContext(); | ||
const getRecordRepresentation = useGetRecordRepresentation(resource); | ||
return ( | ||
<div role="presentation"> | ||
<Breadcrumbs aria-label="breadcrumb"> | ||
<Link underline="hover" color="inherit" to="/"> | ||
Home | ||
</Link> | ||
<Link underline="hover" color="inherit" to="/posts"> | ||
Posts | ||
</Link> | ||
<Typography color="text.primary"> | ||
{getRecordRepresentation(record)} | ||
</Typography> | ||
</Breadcrumbs> | ||
</div> | ||
); | ||
} | ||
|
||
// in src/posts/PostEdit.tsx | ||
import { EditBase, EditView, SimpleForm, TextInput } from 'react-admin'; | ||
import { PostBreadcrumbs } from './PostBreadcrumbs'; | ||
|
||
const PostEdit = () => ( | ||
<EditBase> | ||
<PostBreadcrumbs /> | ||
<EditView> | ||
<SimpleForm> | ||
<TextInput source="title" /> | ||
</SimpleForm> | ||
</EditView> | ||
</EditBase> | ||
) | ||
``` | ||
|
||
## Options | ||
|
||
Here are all the options you can set on the `useGetRecordRepresentation` hook: | ||
|
||
| Prop | Required | Type | Default | Description | | ||
| ---------- | -------- | ---------- | ------- | ----------------------| | ||
| `resource` | Required | `string` | | The record's resource | | ||
|
||
## `resource` | ||
|
||
The record's resource. |
37 changes: 37 additions & 0 deletions
37
packages/ra-core/src/controller/record/RecordRepresentation.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { | ||
ComponentRecordRepresentation, | ||
FunctionRecordRepresentation, | ||
NoRecordRepresentation, | ||
StringRecordRepresentation, | ||
} from './RecordRepresentation.stories'; | ||
|
||
describe('RecordRepresentation', () => { | ||
it('should render the record id when not provided on its parent <Resource>', async () => { | ||
render(<NoRecordRepresentation />); | ||
await screen.findByText('#1'); | ||
}); | ||
it('should render the record representation when provided as a field name on its parent <Resource>', async () => { | ||
render(<StringRecordRepresentation />); | ||
await screen.findByText("The Hitchhiker's Guide to the Galaxy"); | ||
}); | ||
it('should render the record representation when provided as a function on its parent <Resource>', async () => { | ||
render(<FunctionRecordRepresentation />); | ||
await screen.findByText( | ||
"The Hitchhiker's Guide to the Galaxy by Douglas Adams" | ||
); | ||
}); | ||
it('should render the record representation when provided as a component on its parent <Resource>', async () => { | ||
render(<ComponentRecordRepresentation />); | ||
await screen.findByText( | ||
(content, element) => { | ||
return ( | ||
element?.textContent === | ||
"The Hitchhiker's Guide to the Galaxy (by Douglas Adams) - 1979" | ||
); | ||
}, | ||
{ selector: 'p' } | ||
); | ||
}); | ||
}); |
117 changes: 117 additions & 0 deletions
117
packages/ra-core/src/controller/record/RecordRepresentation.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import * as React from 'react'; | ||
import { | ||
ResourceContextProvider, | ||
ResourceDefinitionContextProvider, | ||
} from '../../core'; | ||
import { RecordContextProvider } from './RecordContext'; | ||
import { RecordRepresentation } from './RecordRepresentation'; | ||
import { useRecordContext } from './useRecordContext'; | ||
export default { | ||
title: 'ra-core/controller/record/RecordRepresentation', | ||
}; | ||
|
||
const Book = { | ||
id: 1, | ||
title: "The Hitchhiker's Guide to the Galaxy", | ||
author: 'Douglas Adams', | ||
publishedAt: '1979-10-12', | ||
}; | ||
|
||
export const NoRecordRepresentation = () => ( | ||
<ResourceContextProvider value="books"> | ||
<ResourceDefinitionContextProvider | ||
definitions={{ | ||
books: { | ||
name: 'books', | ||
hasList: true, | ||
hasEdit: true, | ||
hasShow: true, | ||
hasCreate: true, | ||
}, | ||
}} | ||
> | ||
<RecordContextProvider value={Book}> | ||
<RecordRepresentation /> | ||
</RecordContextProvider> | ||
</ResourceDefinitionContextProvider> | ||
</ResourceContextProvider> | ||
); | ||
|
||
export const StringRecordRepresentation = () => ( | ||
<ResourceContextProvider value="books"> | ||
<ResourceDefinitionContextProvider | ||
definitions={{ | ||
books: { | ||
name: 'books', | ||
hasList: true, | ||
hasEdit: true, | ||
hasShow: true, | ||
hasCreate: true, | ||
recordRepresentation: 'title', | ||
}, | ||
}} | ||
> | ||
<RecordContextProvider value={Book}> | ||
<RecordRepresentation /> | ||
</RecordContextProvider> | ||
</ResourceDefinitionContextProvider> | ||
</ResourceContextProvider> | ||
); | ||
|
||
export const FunctionRecordRepresentation = () => ( | ||
<ResourceContextProvider value="books"> | ||
<ResourceDefinitionContextProvider | ||
definitions={{ | ||
books: { | ||
name: 'books', | ||
hasList: true, | ||
hasEdit: true, | ||
hasShow: true, | ||
hasCreate: true, | ||
recordRepresentation: record => | ||
`${record.title} by ${record.author}`, | ||
}, | ||
}} | ||
> | ||
<RecordContextProvider value={Book}> | ||
<RecordRepresentation /> | ||
</RecordContextProvider> | ||
</ResourceDefinitionContextProvider> | ||
</ResourceContextProvider> | ||
); | ||
|
||
const BookRepresentation = () => { | ||
const record = useRecordContext(); | ||
|
||
if (!record) return null; | ||
|
||
return ( | ||
<p> | ||
<b>{record.title}</b>{' '} | ||
<i> | ||
(by {record.author}) -{' '} | ||
{new Date(record.publishedAt).getFullYear()} | ||
</i> | ||
</p> | ||
); | ||
}; | ||
export const ComponentRecordRepresentation = () => ( | ||
<ResourceContextProvider value="books"> | ||
<ResourceDefinitionContextProvider | ||
definitions={{ | ||
books: { | ||
name: 'books', | ||
hasList: true, | ||
hasEdit: true, | ||
hasShow: true, | ||
hasCreate: true, | ||
recordRepresentation: <BookRepresentation />, | ||
}, | ||
}} | ||
> | ||
<RecordContextProvider value={Book}> | ||
<RecordRepresentation /> | ||
</RecordContextProvider> | ||
</ResourceDefinitionContextProvider> | ||
</ResourceContextProvider> | ||
); |
21 changes: 21 additions & 0 deletions
21
packages/ra-core/src/controller/record/RecordRepresentation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as React from 'react'; | ||
import { useGetRecordRepresentation, useResourceContext } from '../../core'; | ||
import { RaRecord } from '../../types'; | ||
import { useRecordContext } from './useRecordContext'; | ||
|
||
/** | ||
* Render the record representation as specified on its parent <Resource>. | ||
* @param props The component props | ||
* @param {string} props.resource The resource name | ||
* @param {RaRecord} props.record The record to render | ||
*/ | ||
export const RecordRepresentation = (props: { | ||
record?: RaRecord; | ||
resource?: string; | ||
}) => { | ||
const record = useRecordContext(props); | ||
const resource = useResourceContext(props); | ||
const getRecordRepresentation = useGetRecordRepresentation(resource); | ||
|
||
return <>{getRecordRepresentation(record)}</>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters