-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
<DocumentTitleHandler />
should populate label #6159
Co-authored-by: Ali Emir Şen <[email protected]> Co-authored-by: Batuhan Wilhelm <[email protected]>
- Loading branch information
1 parent
603c73e
commit ad401d5
Showing
16 changed files
with
1,075 additions
and
7 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,44 @@ | ||
--- | ||
"@refinedev/react-router-v6": minor | ||
--- | ||
|
||
feat: [`<DocumentTitleHandler/>`](https://refine.dev/docs/routing/integrations/react-router/#documenttitlehandler) should populated `resource.meta.label` field if it's not provided on the Refine's resource definition. | ||
From now on, users be able to use the `resource.meta.label` field to customize document title more easily. | ||
|
||
```tsx | ||
import { | ||
BrowserRouter, | ||
DocumentTitleHandler, | ||
} from "@refinedev/react-router-v6"; | ||
import { Refine } from "@refinedev/core"; | ||
|
||
const App = () => { | ||
return ( | ||
<BrowserRouter> | ||
<Refine | ||
/* ... */ | ||
> | ||
{/* ... */} | ||
<DocumentTitleHandler | ||
handler={({ action, params, resource }) => { | ||
const id = params?.id ?? ""; | ||
|
||
const actionPrefixMatcher = { | ||
create: "Create new ", | ||
clone: `#${id} Clone ${resource?.meta?.label}`, | ||
edit: `#${id} Edit ${resource?.meta?.label}`, | ||
show: `#${id} Show ${resource?.meta?.label}`, | ||
list: `${resource?.meta?.label}`, | ||
}; | ||
|
||
const suffix = " | <Company Name>"; | ||
const title = actionPrefixMatcher[action || "list"] + suffix; | ||
|
||
return title; | ||
}} | ||
/> | ||
</Refine> | ||
</BrowserRouter> | ||
); | ||
}; | ||
``` |
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,44 @@ | ||
--- | ||
"@refinedev/nextjs-router": minor | ||
--- | ||
|
||
feat: [`<DocumentTitleHandler/>`](https://refine.dev/docs/routing/integrations/next-js/#documenttitlehandler) should populated `resource.meta.label` field if it's not provided on the Refine's resource definition. | ||
From now on, users be able to use the `resource.meta.label` field to customize document title more easily. | ||
|
||
```tsx | ||
import { | ||
BrowserRouter, | ||
DocumentTitleHandler, | ||
} from "@refinedev/react-router-v6"; | ||
import { Refine } from "@refinedev/core"; | ||
|
||
const App = () => { | ||
return ( | ||
<BrowserRouter> | ||
<Refine | ||
/* ... */ | ||
> | ||
{/* ... */} | ||
<DocumentTitleHandler | ||
handler={({ action, params, resource }) => { | ||
const id = params?.id ?? ""; | ||
|
||
const actionPrefixMatcher = { | ||
create: "Create new ", | ||
clone: `#${id} Clone ${resource?.meta?.label}`, | ||
edit: `#${id} Edit ${resource?.meta?.label}`, | ||
show: `#${id} Show ${resource?.meta?.label}`, | ||
list: `${resource?.meta?.label}`, | ||
}; | ||
|
||
const suffix = " | <Company Name>"; | ||
const title = actionPrefixMatcher[action || "list"] + suffix; | ||
|
||
return title; | ||
}} | ||
/> | ||
</Refine> | ||
</BrowserRouter> | ||
); | ||
}; | ||
``` |
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
202 changes: 202 additions & 0 deletions
202
packages/nextjs-router/src/pages/document-title-handler.test.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,202 @@ | ||
import React, { type ReactNode } from "react"; | ||
|
||
import { DocumentTitleHandler } from "./document-title-handler"; | ||
import { render, TestWrapper, type ITestWrapperProps } from "../test/index"; | ||
import { mockRouterBindings } from "../test/dataMocks"; | ||
|
||
jest.mock("next/head", () => { | ||
return { | ||
__esModule: true, | ||
default: ({ children }: { children: Array<React.ReactElement> }) => { | ||
return <>{children}</>; | ||
}, | ||
}; | ||
}); | ||
|
||
const assertNextHeadTitle = (text: string) => { | ||
const title = document.querySelector("title"); | ||
expect(title?.textContent).toBe(text); | ||
}; | ||
|
||
const renderDocumentTitleHandler = ( | ||
children: ReactNode, | ||
wrapperProps: ITestWrapperProps = {}, | ||
) => { | ||
return render(<>{children}</>, { | ||
wrapper: TestWrapper(wrapperProps), | ||
}); | ||
}; | ||
|
||
describe("<DocumentTitleHandler />", () => { | ||
it("should render default list title", async () => { | ||
renderDocumentTitleHandler(<DocumentTitleHandler />, { | ||
resources: [{ name: "posts", list: "/posts" }], | ||
routerInitialEntries: ["/posts"], | ||
routerProvider: mockRouterBindings({ | ||
action: "list", | ||
resource: { name: "posts", list: "/posts" }, | ||
}), | ||
}); | ||
|
||
assertNextHeadTitle("Posts | Refine"); | ||
}); | ||
|
||
it("should render default create title", async () => { | ||
renderDocumentTitleHandler(<DocumentTitleHandler />, { | ||
resources: [{ name: "posts", create: "/posts/create" }], | ||
routerInitialEntries: ["/posts/create"], | ||
routerProvider: mockRouterBindings({ | ||
action: "create", | ||
resource: { name: "posts", create: "/posts/create" }, | ||
}), | ||
}); | ||
|
||
assertNextHeadTitle("Create new Post | Refine"); | ||
}); | ||
|
||
it("should render default edit title", async () => { | ||
renderDocumentTitleHandler(<DocumentTitleHandler />, { | ||
resources: [{ name: "posts", edit: "/posts/edit/:id" }], | ||
routerInitialEntries: ["/posts/edit/1"], | ||
routerProvider: mockRouterBindings({ | ||
action: "edit", | ||
resource: { name: "posts", edit: "/posts/edit/1" }, | ||
id: "1", | ||
}), | ||
}); | ||
|
||
assertNextHeadTitle("#1 Edit Post | Refine"); | ||
}); | ||
|
||
it("should render default show title", async () => { | ||
renderDocumentTitleHandler(<DocumentTitleHandler />, { | ||
resources: [{ name: "posts", show: "/posts/show/:id" }], | ||
routerInitialEntries: ["/posts/show/1"], | ||
routerProvider: mockRouterBindings({ | ||
action: "show", | ||
resource: { name: "posts", show: "/posts/show/1" }, | ||
id: "1", | ||
}), | ||
}); | ||
|
||
assertNextHeadTitle("#1 Show Post | Refine"); | ||
}); | ||
|
||
it("should render default clone title", async () => { | ||
renderDocumentTitleHandler(<DocumentTitleHandler />, { | ||
resources: [{ name: "posts", clone: "/posts/clone/:id" }], | ||
routerInitialEntries: ["/posts/clone/1"], | ||
routerProvider: mockRouterBindings({ | ||
action: "clone", | ||
resource: { name: "posts", clone: "/posts/clone/1" }, | ||
id: "1", | ||
}), | ||
}); | ||
|
||
assertNextHeadTitle("#1 Clone Post | Refine"); | ||
}); | ||
|
||
it("should render default title for unknown resource", async () => { | ||
renderDocumentTitleHandler(<DocumentTitleHandler />, { | ||
resources: [{ name: "posts" }], | ||
routerInitialEntries: ["/unknown"], | ||
routerProvider: mockRouterBindings({ | ||
action: "list", | ||
resource: undefined, | ||
}), | ||
}); | ||
|
||
assertNextHeadTitle("Refine"); | ||
}); | ||
|
||
it("should render default title for unknown action", async () => { | ||
renderDocumentTitleHandler(<DocumentTitleHandler />, { | ||
resources: [{ name: "posts" }], | ||
routerInitialEntries: ["/posts/unknown"], | ||
routerProvider: mockRouterBindings({ | ||
action: undefined, | ||
resource: { | ||
name: "posts", | ||
}, | ||
}), | ||
}); | ||
|
||
assertNextHeadTitle("Refine"); | ||
}); | ||
|
||
it("should use identifier", async () => { | ||
renderDocumentTitleHandler(<DocumentTitleHandler />, { | ||
resources: [ | ||
{ name: "posts", list: "/posts", identifier: "Awesome Posts" }, | ||
], | ||
routerInitialEntries: ["/posts"], | ||
routerProvider: mockRouterBindings({ | ||
action: "list", | ||
resource: { | ||
name: "posts", | ||
list: "/posts", | ||
identifier: "Awesome Posts", | ||
}, | ||
}), | ||
}); | ||
|
||
assertNextHeadTitle("Awesome posts | Refine"); | ||
}); | ||
|
||
it("should render custom title", async () => { | ||
renderDocumentTitleHandler( | ||
<DocumentTitleHandler | ||
handler={() => { | ||
return "Custom Title"; | ||
}} | ||
/>, | ||
{ | ||
resources: [{ name: "posts", list: "/posts" }], | ||
routerInitialEntries: ["/posts"], | ||
routerProvider: mockRouterBindings({ | ||
action: "list", | ||
resource: { name: "posts", list: "/posts" }, | ||
}), | ||
}, | ||
); | ||
|
||
assertNextHeadTitle("Custom Title"); | ||
}); | ||
|
||
it("should label be populated with friendly resource name on handler function", async () => { | ||
renderDocumentTitleHandler( | ||
<DocumentTitleHandler | ||
handler={({ resource, autoGeneratedTitle }) => { | ||
const label = resource?.label; | ||
const labelMeta = resource?.meta?.label; | ||
|
||
expect(labelMeta).toBe(label); | ||
expect(labelMeta).toBe("Posts"); | ||
|
||
return autoGeneratedTitle; | ||
}} | ||
/>, | ||
{ | ||
resources: [{ name: "posts", list: "/posts" }], | ||
routerInitialEntries: ["/posts"], | ||
routerProvider: mockRouterBindings({ | ||
action: "list", | ||
resource: { name: "posts", list: "/posts" }, | ||
}), | ||
}, | ||
); | ||
}); | ||
|
||
it("should use label from resource if its provided", async () => { | ||
renderDocumentTitleHandler(<DocumentTitleHandler />, { | ||
resources: [{ name: "posts", list: "/posts", label: "Labeled Posts" }], | ||
routerInitialEntries: ["/posts"], | ||
routerProvider: mockRouterBindings({ | ||
action: "list", | ||
resource: { name: "posts", list: "/posts", label: "Labeled Posts" }, | ||
}), | ||
}); | ||
|
||
assertNextHeadTitle("Labeled Posts | Refine"); | ||
}); | ||
}); |
Oops, something went wrong.