Skip to content
13 changes: 2 additions & 11 deletions packages/ra-ui-materialui/src/layout/PageTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import * as React from 'react';
import { useRecordContext, useTranslate, usePreference } from 'ra-core';
import { useTranslate } from 'ra-core';

export const PageTitle = ({ title, defaultTitle, className, ...rest }: any) => {
const [titleFromPreferences] = usePreference();
const translate = useTranslate();
const record = useRecordContext();

return titleFromPreferences ? (
<span className={className} {...rest}>
{translate(titleFromPreferences, {
...record,
_: titleFromPreferences,
})}
</span>
) : (
return (
<span className={className}>
{!title ? (
<span {...rest}>{defaultTitle}</span>
Expand Down
26 changes: 24 additions & 2 deletions packages/ra-ui-materialui/src/layout/PageTitleConfigurable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as React from 'react';
import { useLocation } from 'react-router-dom';
import { usePreferenceInput } from 'ra-core';
import {
usePreferenceInput,
usePreference,
useRecordContext,
useTranslate,
} from 'ra-core';
import { TextField } from '@mui/material';

import { Configurable } from '../preferences';
Expand Down Expand Up @@ -34,7 +39,24 @@ export const PageTitleConfigurable = ({ preferenceKey, ...props }) => {
},
}}
>
<PageTitle {...props} />
<PageTitleConfigurableInner {...props} />
</Configurable>
);
};

const PageTitleConfigurableInner = props => {
const [titleFromPreferences] = usePreference();
const translate = useTranslate();
const record = useRecordContext();

return titleFromPreferences ? (
<span className={props.className}>
{translate(titleFromPreferences, {
...record,
_: titleFromPreferences,
})}
</span>
) : (
<PageTitle {...props} />
);
};
24 changes: 12 additions & 12 deletions packages/ra-ui-materialui/src/layout/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ReactElement } from 'react';
import { createPortal } from 'react-dom';
import { RaRecord, TitleComponent, warning } from 'ra-core';

import { PageTitle } from './PageTitle';
import { PageTitleConfigurable } from './PageTitleConfigurable';

export const Title = (props: TitleProps) => {
Expand Down Expand Up @@ -31,19 +32,18 @@ export const Title = (props: TitleProps) => {

warning(!defaultTitle && !title, 'Missing title prop in <Title> element');

return (
<>
{createPortal(
<PageTitleConfigurable
title={title}
defaultTitle={defaultTitle}
preferenceKey={preferenceKey}
{...rest}
/>,
container
)}
</>
const pageTitle = preferenceKey ? (
<PageTitleConfigurable
title={title}
defaultTitle={defaultTitle}
preferenceKey={preferenceKey}
{...rest}
/>
) : (
<PageTitle title={title} defaultTitle={defaultTitle} {...rest} />
);

return <>{createPortal(pageTitle, container)}</>;
};

export interface TitleProps {
Expand Down
20 changes: 18 additions & 2 deletions packages/ra-ui-materialui/src/layout/TitlePortal.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as React from 'react';
import { TestMemoryRouter } from 'ra-core';
import { PreferencesEditorContextProvider } from 'ra-core';
import { PreferencesEditorContextProvider, TestMemoryRouter } from 'ra-core';

import { TitlePortal } from './TitlePortal';
import { Title } from './Title';
import { InspectorButton } from '../preferences/InspectorButton';
import { Inspector } from '../preferences/Inspector';
import { Configurable } from '../preferences/Configurable';
import { PageTitleEditor } from './PageTitleConfigurable';

export default {
title: 'ra-ui-materialui/layout/TitlePortal',
Expand Down Expand Up @@ -35,3 +38,16 @@ export const Sx = () => (
</PreferencesEditorContextProvider>
</TestMemoryRouter>
);

export const NonConfigurable = () => (
<TestMemoryRouter>
<PreferencesEditorContextProvider>
<Inspector />
<InspectorButton />
<>
<TitlePortal variant="body1" />
<Title title="Hello, world" preferenceKey={undefined} />
</>
</PreferencesEditorContextProvider>
</TestMemoryRouter>
);