App.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import * as React from 'react';
  2. import { RouterProvider } from 'react-router-dom';
  3. import {
  4. ConfigProvider as AntdConfigProvider,
  5. App as AntdApp,
  6. message,
  7. notification,
  8. } from 'antd';
  9. import zhCN from 'antd/locale/zh_CN';
  10. import router from '@/router';
  11. interface ConfigProviderProps {
  12. children: React.ReactNode,
  13. }
  14. // Ant-Design全局化配置
  15. const ConfigProvider: React.FC<ConfigProviderProps> = (props: ConfigProviderProps) => {
  16. function getDesignToken() {
  17. // 获取元素
  18. const root = document.getElementById('root');
  19. // 获取元素计算样式
  20. const styles = getComputedStyle(root!);
  21. // 获取Ant-Design主题-属性
  22. const colorPrimary = styles.getPropertyValue('--primary-color');
  23. const colorTextBase = styles.getPropertyValue('--text-color');
  24. const borderRadiusArray = styles.getPropertyValue('--border-radius').match(/\d+/);
  25. return {
  26. colorPrimary: colorPrimary || undefined,
  27. colorTextBase: colorTextBase || undefined,
  28. borderRadius: borderRadiusArray ? Number(borderRadiusArray[0]) : undefined,
  29. }
  30. }
  31. const designToken = getDesignToken();
  32. return (
  33. <AntdConfigProvider
  34. // 组件-中文
  35. locale={zhCN}
  36. // 警告等级
  37. warning={{
  38. strict: true,// 严格模式
  39. }}
  40. // 渲染父节点到body上
  41. getPopupContainer={() => document.body}
  42. // 设置主题
  43. theme={{
  44. token: {
  45. colorPrimary: designToken.colorPrimary,// 主题颜色
  46. colorTextBase: designToken.colorTextBase,// 文本颜色
  47. borderRadius: designToken.borderRadius,// 圆角大小
  48. },
  49. components: {
  50. Table: {
  51. headerBg: '#F7F8FA',
  52. headerSplitColor: 'transparent',
  53. },
  54. },
  55. }}
  56. /* 组件配置 */
  57. button={{
  58. autoInsertSpace: false,// 移除按钮两个汉字之间的空格
  59. }}
  60. >
  61. {props.children}
  62. </AntdConfigProvider>
  63. );
  64. }
  65. const App: React.FC = () => {
  66. // 全局提示
  67. message.config({
  68. duration: 2,// 2秒自动关闭
  69. maxCount: 1,// 最多显示1条
  70. });
  71. // 通知提醒
  72. notification.config({
  73. duration: 2,// 2秒自动关闭
  74. maxCount: 1,// 最多显示1条
  75. });
  76. // 全局配置
  77. AntdConfigProvider.config({
  78. // 静态方法
  79. holderRender: (children) => (
  80. <ConfigProvider>
  81. {children}
  82. </ConfigProvider>
  83. )
  84. });
  85. return (
  86. <ConfigProvider>
  87. <AntdApp style={{ height: '100%', lineHeight: 'normal' }}>
  88. <RouterProvider router={router} />
  89. </AntdApp>
  90. </ConfigProvider>
  91. );
  92. };
  93. export default App;