Commit c10ca6f3 authored by 偏右's avatar 偏右 Committed by niko

Remove dva from component (#26)

* update footer link hover color

* Use linkElement

* remove module-resolver
parent 0148ddb3
...@@ -7,12 +7,7 @@ ...@@ -7,12 +7,7 @@
"transform-runtime", "transform-runtime",
"transform-decorators-legacy", "transform-decorators-legacy",
"transform-class-properties", "transform-class-properties",
["import", { "libraryName": "antd", "style": true }], ["import", { "libraryName": "antd", "style": true }]
["module-resolver", {
"alias": {
"react-router": "dva/router"
}
}]
] ]
}, },
"production": { "production": {
...@@ -20,12 +15,7 @@ ...@@ -20,12 +15,7 @@
"transform-runtime", "transform-runtime",
"transform-decorators-legacy", "transform-decorators-legacy",
"transform-class-properties", "transform-class-properties",
["import", { "libraryName": "antd", "style": true }], ["import", { "libraryName": "antd", "style": true }]
["module-resolver", {
"alias": {
"react-router": "dva/router"
}
}]
] ]
} }
}, },
......
import React, { PureComponent } from 'react'; import React, { PureComponent, createElement } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Link } from 'react-router';
import { Button, Icon } from 'antd'; import { Button, Icon } from 'antd';
import styles from './index.less'; import styles from './index.less';
...@@ -9,27 +8,31 @@ import styles from './index.less'; ...@@ -9,27 +8,31 @@ import styles from './index.less';
class EditableLinkGroup extends PureComponent { class EditableLinkGroup extends PureComponent {
static defaultProps = { static defaultProps = {
links: [], links: [],
onAdd: () => { onAdd: () => {},
}, linkElement: 'a',
}
state = {
links: this.props.links,
}; };
handleOnClick() { static propTypes = {
const { onAdd } = this.props; links: PropTypes.array,
onAdd(); onAdd: PropTypes.func,
} linkElement: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
};
render() { render() {
const { links } = this.state; const { links, linkElement, onAdd } = this.props;
return ( return (
<div className={styles.linkGroup}> <div className={styles.linkGroup}>
{ {
links.map(link => <Link key={`linkGroup-item-${link.id || link.title}`} to={link.href}>{link.title}</Link>) links.map(link => (
createElement(linkElement, {
key: `linkGroup-item-${link.id || link.title}`,
to: link.href,
href: link.href,
}, link.title)
))
} }
{ {
<Button size="small" onClick={() => this.handleOnClick()}> <Button size="small" onClick={onAdd}>
<Icon type="plus" />添加 <Icon type="plus" />添加
</Button> </Button>
} }
...@@ -38,9 +41,4 @@ class EditableLinkGroup extends PureComponent { ...@@ -38,9 +41,4 @@ class EditableLinkGroup extends PureComponent {
} }
} }
EditableLinkGroup.propTypes = {
links: PropTypes.array,
onAdd: PropTypes.func,
};
export default EditableLinkGroup; export default EditableLinkGroup;
import React from 'react'; import React, { createElement } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import { Button } from 'antd'; import { Button } from 'antd';
import { Link } from 'react-router';
import config from './typeConfig'; import config from './typeConfig';
import styles from './index.less'; import styles from './index.less';
export default ({ className, linkElement = 'a', type, title, desc, img, actions, ...rest }) => {
export default ({ className, type, title, desc, img, actions, ...rest }) => {
const pageType = type in config ? type : '404'; const pageType = type in config ? type : '404';
const clsString = classNames(styles.exception, className); const clsString = classNames(styles.exception, className);
return ( return (
...@@ -21,7 +19,13 @@ export default ({ className, type, title, desc, img, actions, ...rest }) => { ...@@ -21,7 +19,13 @@ export default ({ className, type, title, desc, img, actions, ...rest }) => {
<h1>{title || config[pageType].title}</h1> <h1>{title || config[pageType].title}</h1>
<div className={styles.desc}>{desc || config[pageType].desc}</div> <div className={styles.desc}>{desc || config[pageType].desc}</div>
<div className={styles.actions}> <div className={styles.actions}>
{actions || <Link to="/"><Button type="primary">返回首页</Button></Link>} {
actions ||
createElement(linkElement, {
to: '/',
href: '/',
}, <Button type="primary">返回首页</Button>)
}
</div> </div>
</div> </div>
</div> </div>
......
...@@ -16,3 +16,4 @@ cols: 1 ...@@ -16,3 +16,4 @@ cols: 1
| desc | 补充描述 | ReactNode | - | | desc | 补充描述 | ReactNode | - |
| img | 背景图片地址 | string | - | | img | 背景图片地址 | string | - |
| actions | 建议操作,配置此属性时默认的『返回首页』按钮不生效 | ReactNode | - | | actions | 建议操作,配置此属性时默认的『返回首页』按钮不生效 | ReactNode | - |
| linkElement | 定义链接的元素,默认为 `a` | string\|ReactElement | - |
...@@ -10,10 +10,15 @@ ...@@ -10,10 +10,15 @@
a { a {
color: @text-color-secondary; color: @text-color-secondary;
transition: all .3s;
&:not(:last-child) { &:not(:last-child) {
margin-right: 40px; margin-right: 40px;
} }
&:hover {
color: @text-color;
}
} }
} }
......
import React, { PureComponent } from 'react'; import React, { PureComponent, createElement } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Breadcrumb, Tabs } from 'antd'; import { Breadcrumb, Tabs } from 'antd';
import { Link } from 'react-router';
import classNames from 'classnames'; import classNames from 'classnames';
import styles from './index.less'; import styles from './index.less';
const { TabPane } = Tabs; const { TabPane } = Tabs;
function itemRender(route, params, routes, paths) {
const last = routes.indexOf(route) === routes.length - 1;
return (last || !route.component)
? <span>{route.breadcrumbName}</span>
: <Link to={paths.join('/') || '/'}>{route.breadcrumbName}</Link>;
}
export default class PageHeader extends PureComponent { export default class PageHeader extends PureComponent {
static contextTypes = { static contextTypes = {
routes: PropTypes.array, routes: PropTypes.array,
...@@ -34,10 +26,22 @@ export default class PageHeader extends PureComponent { ...@@ -34,10 +26,22 @@ export default class PageHeader extends PureComponent {
breadcrumbNameMap: this.props.breadcrumbNameMap || this.context.breadcrumbNameMap, breadcrumbNameMap: this.props.breadcrumbNameMap || this.context.breadcrumbNameMap,
}; };
}; };
itemRender = (route, params, routes, paths) => {
const { linkElement = 'a' } = this.props;
const last = routes.indexOf(route) === routes.length - 1;
return (last || !route.component)
? <span>{route.breadcrumbName}</span>
: createElement(linkElement, {
href: paths.join('/') || '/',
to: paths.join('/') || '/',
}, route.breadcrumbName);
}
render() { render() {
const { routes, params, location, breadcrumbNameMap } = this.getBreadcrumbProps(); const { routes, params, location, breadcrumbNameMap } = this.getBreadcrumbProps();
const { title, logo, action, content, extraContent, const {
breadcrumbList, tabList, className } = this.props; title, logo, action, content, extraContent,
breadcrumbList, tabList, className, linkElement = 'a',
} = this.props;
const clsString = classNames(styles.pageHeader, className); const clsString = classNames(styles.pageHeader, className);
let breadcrumb; let breadcrumb;
if (routes && params) { if (routes && params) {
...@@ -46,7 +50,7 @@ export default class PageHeader extends PureComponent { ...@@ -46,7 +50,7 @@ export default class PageHeader extends PureComponent {
className={styles.breadcrumb} className={styles.breadcrumb}
routes={routes.filter(route => route.breadcrumbName)} routes={routes.filter(route => route.breadcrumbName)}
params={params} params={params}
itemRender={itemRender} itemRender={this.itemRender}
/> />
); );
} else if (location && location.pathname) { } else if (location && location.pathname) {
...@@ -55,15 +59,19 @@ export default class PageHeader extends PureComponent { ...@@ -55,15 +59,19 @@ export default class PageHeader extends PureComponent {
const url = `/${pathSnippets.slice(0, index + 1).join('/')}`; const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
return ( return (
<Breadcrumb.Item key={url}> <Breadcrumb.Item key={url}>
<Link to={url}> {createElement(linkElement, {
{breadcrumbNameMap[url] || breadcrumbNameMap[url.replace('/', '')] || url} to: url,
</Link> href: url,
}, breadcrumbNameMap[url] || breadcrumbNameMap[url.replace('/', '')] || url)}
</Breadcrumb.Item> </Breadcrumb.Item>
); );
}); });
const breadcrumbItems = [( const breadcrumbItems = [(
<Breadcrumb.Item key="home"> <Breadcrumb.Item key="home">
<Link to="/">Home</Link> {createElement(linkElement, {
to: '/',
href: '/',
}, '首页')}
</Breadcrumb.Item> </Breadcrumb.Item>
)].concat(extraBreadcrumbItems); )].concat(extraBreadcrumbItems);
breadcrumb = ( breadcrumb = (
......
...@@ -21,5 +21,6 @@ cols: 1 ...@@ -21,5 +21,6 @@ cols: 1
| breadcrumbList | 面包屑数据,配置了 `routes` `params` 时此属性无效 | array<{title: ReactNode, href?: string}> | - | | breadcrumbList | 面包屑数据,配置了 `routes` `params` 时此属性无效 | array<{title: ReactNode, href?: string}> | - |
| tabList | tab 标题列表 | array<{key: string, tab: ReactNode}> | - | | tabList | tab 标题列表 | array<{key: string, tab: ReactNode}> | - |
| onTabChange | 切换面板的回调 | (key) => void | - | | onTabChange | 切换面板的回调 | (key) => void | - |
| linkElement | 定义链接的元素,默认为 `a`,可传入 react-router 的 Link | string\|ReactElement | - |
> 面包屑的配置方式有两种,一是结合 `react-router`,通过配置 `routes` 及 `params` 实现,类似 [面包屑 Demo](https://ant.design/components/breadcrumb-cn/#components-breadcrumb-demo-router);二是直接配置 `breadcrumbList`。 你也可以将 `routes` 及 `params` 放到 context 中,`PageHeader` 组件会自动获取。 > 面包屑的配置方式有两种,一是结合 `react-router`,通过配置 `routes` 及 `params` 实现,类似 [面包屑 Demo](https://ant.design/components/breadcrumb-cn/#components-breadcrumb-demo-router);二是直接配置 `breadcrumbList`。 你也可以将 `routes` 及 `params` 放到 context 中,`PageHeader` 组件会自动获取。
import React from 'react'; import React from 'react';
import { Link } from 'dva/router';
import PageHeader from '../components/PageHeader'; import PageHeader from '../components/PageHeader';
export default ({ children, wrapperClassName, top, ...restProps }) => ( export default ({ children, wrapperClassName, top, ...restProps }) => (
<div style={{ margin: '-24px -24px 0' }} className={wrapperClassName}> <div style={{ margin: '-24px -24px 0' }} className={wrapperClassName}>
{top} {top}
<PageHeader {...restProps} /> <PageHeader {...restProps} linkElement={Link} />
{children ? <div style={{ margin: '24px 24px 0' }}>{children}</div> : null} {children ? <div style={{ margin: '24px 24px 0' }}>{children}</div> : null}
</div> </div>
); );
...@@ -212,6 +212,7 @@ export default class Workplace extends PureComponent { ...@@ -212,6 +212,7 @@ export default class Workplace extends PureComponent {
<EditableLinkGroup <EditableLinkGroup
onAdd={() => {}} onAdd={() => {}}
links={links} links={links}
linkElement={Link}
/> />
</Card> </Card>
<Card <Card
......
import React from 'react'; import React from 'react';
import { Link } from 'dva/router';
import Exception from '../../components/Exception'; import Exception from '../../components/Exception';
export default () => <Exception type="403" style={{ minHeight: 500, height: '80%' }} />; export default () => (
<Exception type="403" style={{ minHeight: 500, height: '80%' }} linkElement={Link} />
);
import React from 'react'; import React from 'react';
import { Link } from 'dva/router';
import Exception from '../../components/Exception'; import Exception from '../../components/Exception';
export default () => <Exception type="404" style={{ minHeight: 500, height: '80%' }} />; export default () => (
<Exception type="404" style={{ minHeight: 500, height: '80%' }} linkElement={Link} />
);
import React from 'react'; import React from 'react';
import { Link } from 'dva/router';
import Exception from '../../components/Exception'; import Exception from '../../components/Exception';
export default () => <Exception type="500" style={{ minHeight: 500, height: '80%' }} />; export default () => (
<Exception type="500" style={{ minHeight: 500, height: '80%' }} linkElement={Link} />
);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment