New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
edit / show title prop: add 'false' and callback choices #9770
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -37,16 +37,24 @@ export const EditView = (props: EditViewProps) => { | |||||
if (!children) { | ||||||
return null; | ||||||
} | ||||||
let titleComponent = null; | ||||||
if (title !== false) { | ||||||
const newTitle = typeof title === 'function' ? title(record) : title; | ||||||
|
||||||
titleComponent = ( | ||||||
<Title | ||||||
title={newTitle} | ||||||
defaultTitle={defaultTitle} | ||||||
preferenceKey={`${resource}.show.title`} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue:
Suggested change
|
||||||
/> | ||||||
); | ||||||
} | ||||||
return ( | ||||||
<Root | ||||||
className={clsx('edit-page', className)} | ||||||
{...sanitizeRestProps(rest)} | ||||||
> | ||||||
<Title | ||||||
title={title} | ||||||
defaultTitle={defaultTitle} | ||||||
preferenceKey={`${resource}.edit.title`} | ||||||
/> | ||||||
{titleComponent} | ||||||
{finalActions} | ||||||
<div | ||||||
className={clsx(EditClasses.main, { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,13 @@ import { Route, Routes } from 'react-router-dom'; | |
import { render, screen, waitFor } from '@testing-library/react'; | ||
|
||
import { AdminContext } from '../AdminContext'; | ||
import { Default, Actions, Basic, Component } from './Show.stories'; | ||
import { | ||
Default, | ||
Actions, | ||
Basic, | ||
Component, | ||
CustomTitle, | ||
} from './Show.stories'; | ||
import { Show } from './Show'; | ||
|
||
describe('<Show />', () => { | ||
|
@@ -126,6 +132,11 @@ describe('<Show />', () => { | |
expect(screen.getByTestId('custom-component')).toBeDefined(); | ||
}); | ||
|
||
it('should display a custom title', async () => { | ||
render(<CustomTitle />); | ||
await screen.findByText('book_1_test'); | ||
}); | ||
|
||
Comment on lines
+135
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Why not add tests for Edit too? |
||
describe('defaultTitle', () => { | ||
const defaultShowProps = { | ||
id: '123', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,17 @@ import PropTypes from 'prop-types'; | |
import { Card } from '@mui/material'; | ||
import { styled } from '@mui/material/styles'; | ||
import clsx from 'clsx'; | ||
import { useShowContext, useResourceDefinition } from 'ra-core'; | ||
import { useShowContext, useResourceDefinition, RaRecord } from 'ra-core'; | ||
|
||
import { ShowProps } from '../types'; | ||
import { ShowActions } from './ShowActions'; | ||
import { Title } from '../layout'; | ||
|
||
const defaultActions = <ShowActions />; | ||
|
||
export const ShowView = (props: ShowViewProps) => { | ||
export const ShowView = <RecordType extends RaRecord = any>( | ||
props: ShowViewProps<RecordType> | ||
) => { | ||
const { | ||
actions, | ||
aside, | ||
|
@@ -32,16 +34,24 @@ export const ShowView = (props: ShowViewProps) => { | |
if (!children || (!record && emptyWhileLoading)) { | ||
return null; | ||
} | ||
let titleComponent = null; | ||
if (title !== false) { | ||
const newTitle = typeof title === 'function' ? title(record) : title; | ||
|
||
titleComponent = ( | ||
<Title | ||
title={newTitle} | ||
defaultTitle={defaultTitle} | ||
preferenceKey={`${resource}.show.title`} | ||
/> | ||
); | ||
} | ||
return ( | ||
<Root | ||
className={clsx('show-page', className)} | ||
{...sanitizeRestProps(rest)} | ||
> | ||
<Title | ||
title={title} | ||
defaultTitle={defaultTitle} | ||
preferenceKey={`${resource}.show.title`} | ||
/> | ||
{titleComponent} | ||
{finalActions !== false && finalActions} | ||
<div | ||
className={clsx(ShowClasses.main, { | ||
|
@@ -55,7 +65,9 @@ export const ShowView = (props: ShowViewProps) => { | |
); | ||
}; | ||
|
||
export type ShowViewProps = ShowProps; | ||
export type ShowViewProps<RecordType extends RaRecord = any> = ShowProps< | ||
RecordType | ||
>; | ||
Comment on lines
+68
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Why did you need that change? |
||
|
||
ShowView.propTypes = { | ||
actions: PropTypes.oneOfType([PropTypes.element, PropTypes.bool]), | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -32,7 +32,11 @@ export interface EditProps< | |||||
redirect?: RedirectionSideEffect; | ||||||
resource?: string; | ||||||
transform?: TransformData; | ||||||
title?: string | ReactElement; | ||||||
title?: | ||||||
| string | ||||||
| ReactElement | ||||||
| false | ||||||
| ((arg0: RecordType | undefined) => string); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. polish:
Suggested change
|
||||||
sx?: SxProps; | ||||||
} | ||||||
|
||||||
|
@@ -73,7 +77,11 @@ export interface ShowProps<RecordType extends RaRecord = any> { | |||||
id?: Identifier; | ||||||
queryOptions?: UseQueryOptions<RecordType> & { meta?: any }; | ||||||
resource?: string; | ||||||
title?: string | ReactElement; | ||||||
title?: | ||||||
| string | ||||||
| ReactElement | ||||||
| false | ||||||
| ((arg0: RecordType | undefined) => string); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. polish:
Suggested change
|
||||||
sx?: SxProps; | ||||||
} | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick (non-blocking): You could also add another example in the
title
section of this doc, just like you did forEdit
.