Git Product home page Git Product logo

plugin-layout's Introduction

@umijs/plugin-layout

Umi plugin for build-in ant-design-pro-layout .

为了进一步降低研发成本,我们尝试将布局通过 umi 插件的方式内置,只需通过简单的配置即可拥有 Ant Design Pro 风格的布局,包括导航以及侧边栏。从而做到用户无需关心布局。

Features

  • ✔︎ 支持 pro 的全部配置项。
  • ✔︎ 侧边栏数据根据路由中的 menu 配置自动生成。
  • ✔︎ 可根据路由中的 layout 配置决定是否渲染默认布局,同时支持只渲染导航或者侧边栏。
  • ✔︎ 默认的路由 403/404 处理,error boundary。
  • ✔︎ 搭配 @umijs/plugin-access 插件一起使用,可以完成对路由权限的控制,且同时拥有默认的权限处理 UI。

Prerequisites

使用此插件之前,您需要安装并启用

Install

# or yarn
$ npm install @umijs/plugin-layout --save

Usage

  1. 配置 .umirc.js | config/config.js

    启用 layout 插件以及依赖的相关插件,注意必须是以下顺序。

    export default {
      plugins: [
        ['@umijs/plugin-layout'],
        ['@umijs/plugin-initial-state'],
        ['@umijs/plugin-model'],
      ],
    };
  2. 新建 src/app.js 文件并导出 getInitialState()

    您可以异步或同步获取一些数据,然后在 getInitialState()函数中返回任何值,umi 将返回的值保存为初始状态(基本信息)。

    约定: layout 插件导航头右上角的默认 UI 会获取初始状态中的用户名(name)以及 头像(avatar)字段,并展示。

    // src/app.js
    
    export async function getInitialState() {
      const { userId, fole } = await getCurrentRole();
      return {
        name, // 默认 layout 导航右上角展示的用户名字段
        avatar,  // 默认 layout 导航右上角展示的用户头像字段
        ...
      };
    }

Plugin API

1. 编译时配置

name

  • Type: String
  • Default: packageName 产品名,默认值为包名。

logo

  • Type: URL
  • Default: 金融科技小蚂蚁 LOGO 产品 logo。

locale

  • Type: Boolean
  • Default: false 是否开始国际化配置。

开启后:路由里配置的菜单名会被当作菜单名国际化的 key,插件会去 locales 文件中查找 'menu.[key]' 对应的文案。默认值为改 key。

关闭时:路由菜单中的菜单名直接为设置的 name 。

🌰:

// .umirc.js | config/config.js
export default {
  plugins: [
    ['@umijs/plugin-layout',{
      name: '服务网格'; 
      locale: true;
    }],
    ['@umijs/plugin-initial-state'],
    ['@umijs/plugin-model'],
  ],
};

2. 运行时配置

logout

  • Type: () => void
  • Default: null 用于运行时配置默认 Layout 的 UI 中,点击退出登录的处理逻辑。默认不做处理。

rightRender

  • Type: (initialState) => React.ReactNode

    用于运行时自定义配置导航头右侧的展示内容。

  • 默认情况下: 展示用户名 & 头像 & 退出登录

    ⚠️ 需搭配 @umijs/plugin-initial-state 一起使用。

    一起使用时,会将 initialState 注入函数作为入参,也是默认主题的用户名等信息的数据来源。合适的数据格式为:

    initialState: {name: string , userId: string, avatar?:url}

🌰:

// src/app.js
export const layout = { 
  logout: () => {}, // do something 
  rightRender:(initInfo)=> { return 'hahah'; },// return string || ReactNode; 
};

childrenRender

  • Type: (children) => React.ReactNode 用于对内容区做自定义的包裹处理,将 children 作为参数传给此函数。

路由配置

Layout 插件会基于 umi 的配置式路由,封装了更多的配置项,支持更多配置式的能力。新增:

  • 侧边栏菜单配置。
  • 布局路由级别展示/隐藏相关配置。
  • 与权限插件结合,配置式实现权限路由的功能。

每一个 routeItem 都新增如下配置项:

  • menu
  • layout
  • access

比如:

//config/route.ts
export const routes =  [
  {
    path: '/welcome',
    component: 'IndexPage',
    menu: {
      name: '欢迎', // 兼容此写法
      icon: 'testicon',
    },
    layout:{
      hideNav: true,
    },
    access: 'canRead',
  }
]

更多路由配置参考,可前往:https://github.com/umijs/plugin-layout/tree/master/test/routes

name

  • Type: string

菜单上显示的名称,没有则不显示。

icon

  • Type: string

菜单上显示的 Icon。

menu

  • Type: false |IRouteMenuConfig
  • Default: false

SideMenu 相关配置。默认为 false,表示在菜单中隐藏此项包括子项。

menu 的可配置项包括:

  1. name

    • Type: string

    当前菜单名,无默认值,必选,不填则表示不展示。

  2. icon

    • Type: string

    当前菜单的左侧 icon,可选 antd 的 icon name 和 url,可选。

  3. hideChildren

    • Type: boolean

    在菜单中隐藏他的子项,只展示自己。

  4. flatMenu

    • Type: boolean

    默认为false 在菜单中只隐藏此项,子项往上提,仍旧展示。

layout

  • Type: false | IRouteLayoutConfig
  • Default: false Layout 相关配置。 默认为 false, 默认展示选择的 layout 主题。

layout 的可配置项包括:

  1. hideMenu

    • Type: boolean
    • Default: false

    当前路由隐藏左侧菜单,默认不隐藏。

  2. hideNav

    • Type: boolean
    • Default: false

    当前路由隐藏导航头,默认不隐藏。

access

  • Type: string

可选。

🌈当 layout 插件配合 access 插件使用时生效。

权限插件会将用户在这里配置的 access 字符串与当前用户所有权限做匹配,如果找到相同的项,并当该权限的值为 false,则当用户访问该路由时,默认展示 403 模版页面。

LICENSE

MIT

plugin-layout's People

Contributors

ariel-cheng avatar ycjcl868 avatar yutingzhao1991 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

plugin-layout's Issues

hash 路由模式时菜单不能正常选中

按照 umi3 的「快速上手」创建项目,添加路由。

export default defineConfig({
  nodeModulesTransform: {
    type: 'none',
  },
  layout: {
    name: 'AAAA',
    fixedHeader: true,
    fixSiderbar: true,
    // locale: true,
    navTheme: 'light',
  },
 // 使用 hash 路由时菜单无法正常选中
  history: {
    type: 'hash',
  },
  routes: [
    { path: '/', redirect: '/home' },
    { path: '/home', component: '@/pages/index', name: '首页' },
    { path: '/list', component: '@/pages/list', name: '列表' },
  ],
});
{
  "private": true,
  "scripts": {
    "start": "umi dev",
    "build": "umi build",
    "postinstall": "umi generate tmp",
    "prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'",
    "test": "umi-test",
    "test:coverage": "umi-test --coverage"
  },
  "gitHooks": {
    "pre-commit": "lint-staged"
  },
  "lint-staged": {
    "*.{js,jsx,less,md,json}": [
      "prettier --write"
    ],
    "*.ts?(x)": [
      "prettier --parser=typescript --write"
    ]
  },
  "dependencies": {
    "@ant-design/pro-layout": "^5.0.12",
    "@umijs/preset-react": "1.x",
    "@umijs/test": "^3.2.2",
    "lint-staged": "^10.0.7",
    "prettier": "^1.19.1",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "umi": "^3.2.2",
    "yorkie": "^2.0.0"
  }
}

相似问题:添加 base 或者开启 hash 时菜单不能正常选中

已修复

plugins/packages/plugin-layout/CHANGELOG.md

0.11.7 (2020-06-03)
Bug Fixes
layout: fix hash router no work bugfix (#247) (2c35415)

查看详情

[feature] suggest to support i18n Exception [需求] 建议支持 Exception 页面 i18n

403, 404, 500 错误页为中文

const Exception404 = () => (
<Exception
exceptionImg={
<img
src="https://img.alicdn.com/tfs/TB1_3MMg1H2gK0jSZJnXXaT1FXa-1323-801.png"
width="438"
alt="desc"
/>
}
title="404"
description="抱歉,你访问的页面不存在"
footer={
<Button type="primary" onClick={backToHome}>
返回首页
</Button>
}
/>
);
const Exception500 = () => (
<Exception
exceptionImg="https://img.alicdn.com/tfs/TB1Pt.Mg.z1gK0jSZLeXXb9kVXa-1446-795.png"
title="500"
description="抱歉,服务器出错了"
footer={
<Button type="primary" onClick={backToHome}>
返回首页
</Button>
}
/>
);
const Exception403 = () => (
<Exception
exceptionImg={
<img
src="https://img.alicdn.com/tfs/TB1uK.QgW61gK0jSZFlXXXDKFXa-1311-825.png"
width="438"
alt="desc"
/>
}
title="403"
description="抱歉,你无权访问该页面"
footer={
<Button type="primary" onClick={backToHome}>
返回首页
</Button>
}
/>
);

bug不能正确获得menu信息

// umi运行时配置,routes前加路由后
export function patchRoutes({ routes }) {
  routes.unshift({
    path: '/foo',
    exact: true,
    component: require('@/extraRoutes/foo').default,
  });
}
// layout/index.tsx 中如下代码不能正确得到menu信息了
 const menus = patchMenus(getMenuDataFromRoutes(_routes[0].routes)); //  _routes[0] 已经变为新加的 /foo了

如何扩展BasicLayout的内容区域?

plugin-layout没有可以扩展Tab页的功能,需要再layout中的children外面嵌套Tab页需要如何实现?目前看没有相关的接口来实现。。请问是否能支持,或有没有支持计划?

[需求]支持路由配置不同layout

插件路由配置项layout目前只能配置布尔值,是否应该提供一个api以支持不同的layout?例如 loginLayout之类的, 可以是本地自己写的layout,或者pro之前提供的其他layout?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.