feat: support set document title

This commit is contained in:
chenos 2021-08-25 22:05:53 +08:00
parent a48f2a5159
commit 06fa41fffc
8 changed files with 37 additions and 5 deletions

View File

@ -1,6 +1,7 @@
export const login = { export const login = {
key: 'dtf9j0b8p9u', key: 'dtf9j0b8p9u',
type: 'object', type: 'object',
title: '登录',
properties: { properties: {
email: { email: {
type: 'string', type: 'string',

View File

@ -1,6 +1,7 @@
export const register = { export const register = {
key: '46qlxqam3xk', key: '46qlxqam3xk',
type: 'object', type: 'object',
title: '注册',
properties: { properties: {
email: { email: {
type: 'string', type: 'string',

View File

@ -30,6 +30,11 @@ export function SystemSettingsProvider(props) {
); );
} }
export const useSystemSettings = () => {
const ctx = useContext(SystemSettingsContext);
return ctx?.service?.data || {};
};
export const SiteTitle = () => { export const SiteTitle = () => {
const { service = {} } = useContext(SystemSettingsContext); const { service = {} } = useContext(SystemSettingsContext);
const { loading, data } = service; const { loading, data } = service;

View File

@ -38,8 +38,13 @@ import { uid } from '@formily/shared';
import { Permissions } from './Permissions'; import { Permissions } from './Permissions';
import { More } from './More'; import { More } from './More';
import { UserInfo } from './UserInfo'; import { UserInfo } from './UserInfo';
import { SiteTitle, SystemSettingsProvider } from './SiteTitle'; import {
SiteTitle,
SystemSettingsProvider,
useSystemSettings,
} from './SiteTitle';
import { AuthProvider } from './Auth'; import { AuthProvider } from './Auth';
import { Helmet } from 'react-helmet';
function DesignableToggle() { function DesignableToggle() {
const { designable, setDesignable } = useDesignableSwitchContext(); const { designable, setDesignable } = useDesignableSwitchContext();
@ -69,6 +74,7 @@ function LayoutWithMenu(props: LayoutWithMenuProps) {
const match = useRouteMatch<any>(); const match = useRouteMatch<any>();
const sideMenuRef = useRef(); const sideMenuRef = useRef();
const history = useHistory(); const history = useHistory();
const { title } = useSystemSettings();
const [activeKey, setActiveKey] = useState(match.params.name); const [activeKey, setActiveKey] = useState(match.params.name);
const [, setPageTitle] = usePageTitleContext(); const [, setPageTitle] = usePageTitleContext();
const onSelect = (info) => { const onSelect = (info) => {
@ -91,6 +97,7 @@ function LayoutWithMenu(props: LayoutWithMenuProps) {
const onMenuItemRemove = () => { const onMenuItemRemove = () => {
history.push(`/admin`); history.push(`/admin`);
}; };
console.log({ activeKey });
return ( return (
<Layout> <Layout>
<Layout.Header className={'site-header'} style={{ display: 'flex' }}> <Layout.Header className={'site-header'} style={{ display: 'flex' }}>
@ -117,7 +124,13 @@ function LayoutWithMenu(props: LayoutWithMenuProps) {
width={200} width={200}
></Layout.Sider> ></Layout.Sider>
<Layout.Content style={{ minHeight: 'calc(100vh - 46px)' }}> <Layout.Content style={{ minHeight: 'calc(100vh - 46px)' }}>
{activeKey && <Content activeKey={activeKey} />} {activeKey ? (
<Content activeKey={activeKey} />
) : (
<Helmet>
<title>{title}</title>
</Helmet>
)}
</Layout.Content> </Layout.Content>
</Layout> </Layout>
</Layout> </Layout>

View File

@ -1,6 +1,7 @@
import { Card } from 'antd'; import { Card } from 'antd';
import React from 'react'; import React from 'react';
import { useLocation, useHistory } from 'react-router-dom'; import { useLocation, useHistory } from 'react-router-dom';
import { SystemSettingsProvider } from '../admin-layout/SiteTitle';
export function AuthLayout({ children, route }: any) { export function AuthLayout({ children, route }: any) {
const location = useLocation(); const location = useLocation();
@ -8,7 +9,7 @@ export function AuthLayout({ children, route }: any) {
return ( return (
<div style={{ maxWidth: 320, margin: '0 auto', paddingTop: '20vh' }}> <div style={{ maxWidth: 320, margin: '0 auto', paddingTop: '20vh' }}>
<h1>NocoBase</h1> <h1>NocoBase</h1>
{children} <SystemSettingsProvider>{children}</SystemSettingsProvider>
</div> </div>
); );
} }

View File

@ -5,6 +5,7 @@ import { useRequest } from 'ahooks';
import { request, SchemaRenderer } from '../../schemas'; import { request, SchemaRenderer } from '../../schemas';
import { useForm } from '@formily/react'; import { useForm } from '@formily/react';
import { useHistory, useLocation } from 'react-router-dom'; import { useHistory, useLocation } from 'react-router-dom';
import { useSystemSettings } from '../admin-layout/SiteTitle';
function Div(props) { function Div(props) {
return <div {...props}></div>; return <div {...props}></div>;
@ -56,11 +57,15 @@ export function RouteSchemaRenderer({ route }) {
formatResult: (result) => result?.data, formatResult: (result) => result?.data,
}, },
); );
const { title } = useSystemSettings();
if (loading) { if (loading) {
return <Spin />; return <Spin />;
} }
return ( return (
<div> <div>
<Helmet>
<title>{title ? `${data.title} - ${title}` : data.title}</title>
</Helmet>
<SchemaRenderer <SchemaRenderer
components={{ Div }} components={{ Div }}
scope={{ useLogin, useRegister }} scope={{ useLogin, useRegister }}

View File

@ -1,12 +1,15 @@
import constate from 'constate'; import constate from 'constate';
import { useState } from 'react'; import { useState } from 'react';
import { useSystemSettings } from '../components/admin-layout/SiteTitle';
export interface PageTitleProviderProps { export interface PageTitleProviderProps {
defaultPageTitle?: string; defaultPageTitle?: string;
} }
const [PageTitleProvider, usePageTitleContext] = constate(({ defaultPageTitle }: PageTitleProviderProps) => { const [PageTitleProvider, usePageTitleContext] = constate(({ defaultPageTitle }: PageTitleProviderProps) => {
return useState(defaultPageTitle); const { title } = useSystemSettings();
const [pageTitle, setPageTitle] = useState(defaultPageTitle);
return [title ? `${pageTitle} - ${title}` : pageTitle, setPageTitle];
}); });
export { PageTitleProvider, usePageTitleContext }; export { PageTitleProvider, usePageTitleContext };

View File

@ -2,13 +2,16 @@ import React from 'react';
import { PageHeader as AntdPageHeader } from 'antd'; import { PageHeader as AntdPageHeader } from 'antd';
import { observer } from '@formily/react'; import { observer } from '@formily/react';
import { usePageTitleContext } from '../../constate/PageTitle'; import { usePageTitleContext } from '../../constate/PageTitle';
import { Helmet } from 'react-helmet';
export const Page = observer((props) => { export const Page = observer((props) => {
const { children, ...others } = props; const { children, ...others } = props;
const [pageTitle] = usePageTitleContext(); const [pageTitle] = usePageTitleContext();
return ( return (
<> <>
<Helmet>
<title>{pageTitle}</title>
</Helmet>
<AntdPageHeader ghost={false} title={pageTitle} {...others} /> <AntdPageHeader ghost={false} title={pageTitle} {...others} />
<div style={{margin: 24}}> <div style={{margin: 24}}>
{children} {children}