2021-06-26 01:35:40 +00:00
|
|
|
import type { ExtractPropTypes, HTMLAttributes } from 'vue';
|
2021-12-11 14:29:29 +00:00
|
|
|
import { computed, createVNode, defineComponent, provide, ref } from 'vue';
|
2021-06-07 09:35:03 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import { SiderHookProviderKey } from './injectionKey';
|
2020-10-15 07:54:52 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export const basicProps = () => ({
|
|
|
|
prefixCls: String,
|
|
|
|
hasSider: { type: Boolean, default: undefined },
|
|
|
|
tagName: String,
|
|
|
|
});
|
2020-10-15 07:54:52 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export type BasicProps = Partial<ExtractPropTypes<ReturnType<typeof basicProps>>> & HTMLAttributes;
|
2020-11-26 05:27:42 +00:00
|
|
|
|
2020-10-15 07:54:52 +00:00
|
|
|
type GeneratorArgument = {
|
|
|
|
suffixCls: string;
|
2021-06-07 09:35:03 +00:00
|
|
|
tagName: 'header' | 'footer' | 'main' | 'section';
|
2020-10-15 07:54:52 +00:00
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
function generator({ suffixCls, tagName, name }: GeneratorArgument) {
|
|
|
|
return (BasicComponent: typeof Basic) => {
|
2021-06-07 09:35:03 +00:00
|
|
|
const Adapter = defineComponent({
|
2020-10-15 07:54:52 +00:00
|
|
|
name,
|
2022-03-26 14:52:54 +00:00
|
|
|
props: basicProps(),
|
2020-10-15 07:54:52 +00:00
|
|
|
setup(props, { slots }) {
|
2021-06-07 09:35:03 +00:00
|
|
|
const { prefixCls } = useConfigInject(suffixCls, props);
|
2020-10-15 07:54:52 +00:00
|
|
|
return () => {
|
|
|
|
const basicComponentProps = {
|
2021-06-08 03:36:58 +00:00
|
|
|
...props,
|
2021-06-07 09:35:03 +00:00
|
|
|
prefixCls: prefixCls.value,
|
2020-10-15 07:54:52 +00:00
|
|
|
tagName,
|
|
|
|
};
|
2021-12-11 14:29:29 +00:00
|
|
|
return <BasicComponent {...basicComponentProps} v-slots={slots}></BasicComponent>;
|
2020-10-15 07:54:52 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
2020-11-26 05:27:42 +00:00
|
|
|
return Adapter;
|
2020-10-15 07:54:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const Basic = defineComponent({
|
2022-03-26 14:52:54 +00:00
|
|
|
props: basicProps(),
|
2020-10-15 07:54:52 +00:00
|
|
|
setup(props, { slots }) {
|
2021-12-11 14:29:29 +00:00
|
|
|
return () => createVNode(props.tagName, { class: props.prefixCls }, slots);
|
2020-10-15 07:54:52 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const BasicLayout = defineComponent({
|
2022-03-26 14:52:54 +00:00
|
|
|
props: basicProps(),
|
2020-10-15 07:54:52 +00:00
|
|
|
setup(props, { slots }) {
|
2021-06-07 09:35:03 +00:00
|
|
|
const { direction } = useConfigInject('', props);
|
2020-10-15 07:54:52 +00:00
|
|
|
const siders = ref<string[]>([]);
|
2021-06-07 09:35:03 +00:00
|
|
|
const siderHookProvider = {
|
|
|
|
addSider: (id: string) => {
|
2020-10-15 07:54:52 +00:00
|
|
|
siders.value = [...siders.value, id];
|
|
|
|
},
|
2021-06-07 09:35:03 +00:00
|
|
|
removeSider: (id: string) => {
|
2020-10-15 07:54:52 +00:00
|
|
|
siders.value = siders.value.filter(currentId => currentId !== id);
|
|
|
|
},
|
|
|
|
};
|
2021-06-07 09:35:03 +00:00
|
|
|
|
|
|
|
provide(SiderHookProviderKey, siderHookProvider);
|
2021-12-11 14:29:29 +00:00
|
|
|
const divCls = computed(() => {
|
|
|
|
const { prefixCls, hasSider } = props;
|
|
|
|
return {
|
|
|
|
[`${prefixCls}`]: true,
|
2020-10-15 07:54:52 +00:00
|
|
|
[`${prefixCls}-has-sider`]:
|
|
|
|
typeof hasSider === 'boolean' ? hasSider : siders.value.length > 0,
|
2021-06-07 09:35:03 +00:00
|
|
|
[`${prefixCls}-rtl`]: direction.value === 'rtl',
|
2021-12-11 14:29:29 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
return () => {
|
|
|
|
const { tagName } = props;
|
|
|
|
return createVNode(tagName, { class: divCls.value }, slots);
|
2020-10-15 07:54:52 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-11-10 02:06:59 +00:00
|
|
|
export { Header, Footer, Content };
|
2020-10-15 07:54:52 +00:00
|
|
|
|
2021-11-10 02:06:59 +00:00
|
|
|
export default Layout;
|