ant-design-vue/components/layout/layout.jsx

115 lines
2.4 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
import classNames from 'classnames';
2020-01-19 16:58:38 +08:00
import { getOptionProps, getListeners } from '../_util/props-util';
2019-04-10 09:08:29 +08:00
import { ConfigConsumerProps } from '../config-provider';
2018-04-20 12:52:31 +08:00
export const BasicProps = {
prefixCls: PropTypes.string,
hasSider: PropTypes.boolean,
2019-01-12 11:33:27 +08:00
};
2018-04-20 12:52:31 +08:00
2019-01-12 11:33:27 +08:00
function generator(props, name) {
return BasicComponent => {
2018-04-20 12:52:31 +08:00
return {
name,
props: BasicComponent.props,
2019-04-10 09:08:29 +08:00
inject: {
2019-09-11 22:35:25 +08:00
configProvider: { default: () => ConfigConsumerProps },
2019-04-10 09:08:29 +08:00
},
2019-01-12 11:33:27 +08:00
render() {
2019-04-10 09:08:29 +08:00
const { suffixCls } = props;
const { prefixCls: customizePrefixCls } = this.$props;
2019-09-11 22:35:25 +08:00
const getPrefixCls = this.configProvider.getPrefixCls;
2019-04-10 09:08:29 +08:00
const prefixCls = getPrefixCls(suffixCls, customizePrefixCls);
2018-04-20 12:52:31 +08:00
const basicComponentProps = {
props: {
prefixCls,
...getOptionProps(this),
},
2020-01-19 16:58:38 +08:00
on: getListeners(this),
2019-01-12 11:33:27 +08:00
};
return <BasicComponent {...basicComponentProps}>{this.$slots.default}</BasicComponent>;
2018-04-20 12:52:31 +08:00
},
2019-01-12 11:33:27 +08:00
};
};
2018-04-20 12:52:31 +08:00
}
const Basic = {
props: BasicProps,
2019-01-12 11:33:27 +08:00
render() {
2020-01-19 16:58:38 +08:00
const { prefixCls, $slots } = this;
2018-04-20 12:52:31 +08:00
const divProps = {
class: prefixCls,
2020-01-19 16:58:38 +08:00
on: getListeners(this),
2019-01-12 11:33:27 +08:00
};
return <div {...divProps}>{$slots.default}</div>;
2018-04-20 12:52:31 +08:00
},
2019-01-12 11:33:27 +08:00
};
2018-04-20 12:52:31 +08:00
const BasicLayout = {
props: BasicProps,
2019-01-12 11:33:27 +08:00
data() {
2018-04-20 12:52:31 +08:00
return {
siders: [],
2019-01-12 11:33:27 +08:00
};
2018-04-20 12:52:31 +08:00
},
2019-01-12 11:33:27 +08:00
provide() {
2018-04-20 12:52:31 +08:00
return {
siderHook: {
2019-01-12 11:33:27 +08:00
addSider: id => {
this.siders = [...this.siders, id];
2018-04-20 12:52:31 +08:00
},
2019-01-12 11:33:27 +08:00
removeSider: id => {
this.siders = this.siders.filter(currentId => currentId !== id);
2018-04-20 12:52:31 +08:00
},
},
2019-01-12 11:33:27 +08:00
};
2018-04-20 12:52:31 +08:00
},
2019-01-12 11:33:27 +08:00
render() {
2020-01-19 16:58:38 +08:00
const { prefixCls, $slots, hasSider } = this;
2018-04-20 12:52:31 +08:00
const divCls = classNames(prefixCls, {
[`${prefixCls}-has-sider`]: hasSider || this.siders.length > 0,
2019-01-12 11:33:27 +08:00
});
2018-04-20 12:52:31 +08:00
const divProps = {
class: divCls,
2020-01-19 16:58:38 +08:00
on: getListeners,
2019-01-12 11:33:27 +08:00
};
return <div {...divProps}>{$slots.default}</div>;
2018-04-20 12:52:31 +08:00
},
2019-01-12 11:33:27 +08:00
};
2018-04-20 12:52:31 +08:00
2019-01-12 11:33:27 +08:00
const Layout = generator(
{
2019-04-10 09:08:29 +08:00
suffixCls: 'layout',
2019-01-12 11:33:27 +08:00
},
'ALayout',
)(BasicLayout);
2018-04-20 12:52:31 +08:00
2019-01-12 11:33:27 +08:00
const Header = generator(
{
2019-04-10 09:08:29 +08:00
suffixCls: 'layout-header',
2019-01-12 11:33:27 +08:00
},
'ALayoutHeader',
)(Basic);
2018-04-20 12:52:31 +08:00
2019-01-12 11:33:27 +08:00
const Footer = generator(
{
2019-04-10 09:08:29 +08:00
suffixCls: 'layout-footer',
2019-01-12 11:33:27 +08:00
},
'ALayoutFooter',
)(Basic);
2018-04-20 12:52:31 +08:00
2019-01-12 11:33:27 +08:00
const Content = generator(
{
2019-04-10 09:08:29 +08:00
suffixCls: 'layout-content',
2019-01-12 11:33:27 +08:00
},
'ALayoutContent',
)(Basic);
2018-04-20 12:52:31 +08:00
2019-01-12 11:33:27 +08:00
Layout.Header = Header;
Layout.Footer = Footer;
Layout.Content = Content;
2018-04-20 12:52:31 +08:00
2019-01-12 11:33:27 +08:00
export default Layout;