2020-11-26 05:27:42 +00:00
|
|
|
import {
|
|
|
|
createVNode,
|
|
|
|
defineComponent,
|
|
|
|
inject,
|
|
|
|
provide,
|
|
|
|
toRefs,
|
|
|
|
ref,
|
|
|
|
ExtractPropTypes,
|
|
|
|
HTMLAttributes,
|
|
|
|
} from 'vue';
|
2020-10-15 07:54:52 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import classNames from '../_util/classNames';
|
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-10-25 12:44:26 +00:00
|
|
|
import { flattenChildren } from '../_util/props-util';
|
2020-10-15 07:54:52 +00:00
|
|
|
|
2020-11-26 05:27:42 +00:00
|
|
|
export const basicProps = {
|
2020-10-15 07:54:52 +00:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
hasSider: PropTypes.looseBool,
|
|
|
|
tagName: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
2020-11-26 05:27:42 +00:00
|
|
|
export type BasicProps = Partial<ExtractPropTypes<typeof basicProps>> & HTMLAttributes;
|
|
|
|
|
2020-10-15 07:54:52 +00:00
|
|
|
export interface SiderHookProvider {
|
|
|
|
addSider?: (id: string) => void;
|
|
|
|
removeSider?: (id: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
type GeneratorArgument = {
|
|
|
|
suffixCls: string;
|
|
|
|
tagName: string;
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
function generator({ suffixCls, tagName, name }: GeneratorArgument) {
|
|
|
|
return (BasicComponent: typeof Basic) => {
|
2020-11-26 05:27:42 +00:00
|
|
|
const Adapter = defineComponent<BasicProps>({
|
2020-10-15 07:54:52 +00:00
|
|
|
name,
|
|
|
|
setup(props, { slots }) {
|
|
|
|
const { getPrefixCls } = inject('configProvider', defaultConfigProvider);
|
|
|
|
return () => {
|
|
|
|
const { prefixCls: customizePrefixCls } = props;
|
|
|
|
const prefixCls = getPrefixCls(suffixCls, customizePrefixCls);
|
|
|
|
const basicComponentProps = {
|
|
|
|
prefixCls,
|
|
|
|
...props,
|
|
|
|
tagName,
|
|
|
|
};
|
2020-10-25 12:44:26 +00:00
|
|
|
return (
|
|
|
|
<BasicComponent {...basicComponentProps}>
|
|
|
|
{flattenChildren(slots.default?.())}
|
|
|
|
</BasicComponent>
|
|
|
|
);
|
2020-10-15 07:54:52 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
2020-11-26 05:27:42 +00:00
|
|
|
Adapter.props = basicProps;
|
|
|
|
return Adapter;
|
2020-10-15 07:54:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const Basic = defineComponent({
|
2020-11-26 05:27:42 +00:00
|
|
|
props: basicProps,
|
2020-10-15 07:54:52 +00:00
|
|
|
setup(props, { slots }) {
|
|
|
|
const { prefixCls, tagName } = toRefs(props);
|
|
|
|
return () => createVNode(tagName.value, { class: prefixCls.value }, slots.default?.());
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const BasicLayout = defineComponent({
|
2020-11-26 05:27:42 +00:00
|
|
|
props: basicProps,
|
2020-10-15 07:54:52 +00:00
|
|
|
setup(props, { slots }) {
|
|
|
|
const siders = ref<string[]>([]);
|
|
|
|
const siderHookProvider: SiderHookProvider = {
|
|
|
|
addSider: id => {
|
|
|
|
siders.value = [...siders.value, id];
|
|
|
|
},
|
|
|
|
removeSider: id => {
|
|
|
|
siders.value = siders.value.filter(currentId => currentId !== id);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
provide('siderHook', siderHookProvider);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
const { prefixCls, hasSider, tagName } = props;
|
|
|
|
const divCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-has-sider`]:
|
|
|
|
typeof hasSider === 'boolean' ? hasSider : siders.value.length > 0,
|
|
|
|
});
|
|
|
|
return createVNode(tagName, { class: divCls }, slots.default?.());
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const Layout = generator({
|
|
|
|
suffixCls: 'layout',
|
|
|
|
tagName: 'section',
|
|
|
|
name: 'ALayout',
|
|
|
|
})(BasicLayout);
|
|
|
|
|
|
|
|
const Header = generator({
|
|
|
|
suffixCls: 'layout-header',
|
|
|
|
tagName: 'header',
|
|
|
|
name: 'ALayoutHeader',
|
|
|
|
})(Basic);
|
|
|
|
|
|
|
|
const Footer = generator({
|
|
|
|
suffixCls: 'layout-footer',
|
|
|
|
tagName: 'footer',
|
|
|
|
name: 'ALayoutFooter',
|
|
|
|
})(Basic);
|
|
|
|
|
|
|
|
const Content = generator({
|
|
|
|
suffixCls: 'layout-content',
|
|
|
|
tagName: 'main',
|
|
|
|
name: 'ALayoutContent',
|
|
|
|
})(Basic);
|
|
|
|
|
|
|
|
Layout.Header = Header;
|
|
|
|
Layout.Footer = Footer;
|
|
|
|
Layout.Content = Content;
|
|
|
|
|
|
|
|
export default Layout as typeof Layout & {
|
|
|
|
readonly Header: typeof Header;
|
|
|
|
readonly Footer: typeof Footer;
|
|
|
|
readonly Content: typeof Content;
|
|
|
|
};
|