-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ui): renders live document title (#5115)
- Loading branch information
1 parent
ad24900
commit 1bbbcb3
Showing
11 changed files
with
96 additions
and
101 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
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
This file was deleted.
Oops, something went wrong.
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,56 @@ | ||
'use client' | ||
import { useEffect } from 'react' | ||
import { useDocumentInfo } from '../../../providers/DocumentInfo' | ||
import { useTranslation } from '../../../providers/Translation' | ||
import { ClientConfig } from 'payload/types' | ||
import { useFormFields } from '../../../forms/Form/context' | ||
import { formatDate } from '../../..' | ||
import { getTranslation } from '@payloadcms/translations' | ||
|
||
export const SetDocumentTitle: React.FC<{ | ||
config?: ClientConfig | ||
globalConfig?: ClientConfig['globals'][0] | ||
collectionConfig?: ClientConfig['collections'][0] | ||
}> = (props) => { | ||
const { config, globalConfig, collectionConfig } = props | ||
|
||
const dateFormatFromConfig = config?.admin?.dateFormat | ||
|
||
const useAsTitle = collectionConfig?.admin?.useAsTitle | ||
|
||
const field = useFormFields(([fields]) => (useAsTitle && fields && fields?.[useAsTitle]) || null) | ||
|
||
const { i18n } = useTranslation() | ||
|
||
const { setDocumentTitle } = useDocumentInfo() | ||
|
||
let title: string | ||
|
||
if (typeof field === 'string') { | ||
title = field | ||
} else if (typeof field === 'number') { | ||
title = String(field) | ||
} else { | ||
title = field?.value as string | ||
} | ||
|
||
if (collectionConfig && useAsTitle) { | ||
const fieldConfig = collectionConfig.fields.find((f) => 'name' in f && f.name === useAsTitle) | ||
const isDate = fieldConfig?.type === 'date' | ||
|
||
if (title && isDate) { | ||
const dateFormat = fieldConfig?.admin?.date?.displayFormat || dateFormatFromConfig | ||
title = formatDate(title, dateFormat, i18n.language) | ||
} | ||
} | ||
|
||
if (globalConfig) { | ||
title = getTranslation(globalConfig?.label, i18n) || globalConfig?.slug | ||
} | ||
|
||
useEffect(() => { | ||
setDocumentTitle(title) | ||
}, [setDocumentTitle, title]) | ||
|
||
return null | ||
} |
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