2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-12-30 21:41:28 +08:00
|
|
|
import classNames from 'classnames';
|
2019-06-05 14:00:42 +08:00
|
|
|
import createContext from '@ant-design/create-react-context';
|
2017-05-28 03:20:05 +08:00
|
|
|
import { SiderProps } from './Sider';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2016-12-30 21:41:28 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
export interface GeneratorProps {
|
|
|
|
suffixCls: string;
|
2019-02-14 17:51:52 +08:00
|
|
|
tagName: 'header' | 'footer' | 'main' | 'section';
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2018-03-06 15:51:06 +08:00
|
|
|
export interface BasicProps extends React.HTMLAttributes<HTMLDivElement> {
|
2016-12-30 21:41:28 +08:00
|
|
|
prefixCls?: string;
|
2018-01-12 20:44:35 +08:00
|
|
|
hasSider?: boolean;
|
2019-03-05 14:39:43 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
export interface LayoutContextProps {
|
|
|
|
siderHook: {
|
|
|
|
addSider: (id: string) => void;
|
|
|
|
removeSider: (id: string) => void;
|
|
|
|
};
|
|
|
|
}
|
2019-06-05 14:00:42 +08:00
|
|
|
export const LayoutContext = createContext<LayoutContextProps>({
|
2019-04-05 16:15:01 +08:00
|
|
|
siderHook: {
|
|
|
|
addSider: () => null,
|
|
|
|
removeSider: () => null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-03-05 14:39:43 +08:00
|
|
|
interface BasicPropsWithTagName extends BasicProps {
|
2019-02-14 17:51:52 +08:00
|
|
|
tagName: 'header' | 'footer' | 'main' | 'section';
|
2016-12-30 21:41:28 +08:00
|
|
|
}
|
|
|
|
|
2019-02-14 17:51:52 +08:00
|
|
|
function generator({ suffixCls, tagName }: GeneratorProps) {
|
2019-03-05 14:39:43 +08:00
|
|
|
return (BasicComponent: React.ComponentClass<BasicPropsWithTagName>): any => {
|
2016-12-30 21:41:28 +08:00
|
|
|
return class Adapter extends React.Component<BasicProps, any> {
|
|
|
|
static Header: any;
|
|
|
|
static Footer: any;
|
|
|
|
static Content: any;
|
|
|
|
static Sider: any;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
renderComponent = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const { prefixCls: customizePrefixCls } = this.props;
|
|
|
|
const prefixCls = getPrefixCls(suffixCls, customizePrefixCls);
|
|
|
|
|
2019-02-14 17:51:52 +08:00
|
|
|
return <BasicComponent prefixCls={prefixCls} tagName={tagName} {...this.props} />;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderComponent}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-12-30 21:41:28 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-05 14:39:43 +08:00
|
|
|
class Basic extends React.Component<BasicPropsWithTagName, any> {
|
2016-12-30 21:41:28 +08:00
|
|
|
render() {
|
2019-03-05 14:39:43 +08:00
|
|
|
const { prefixCls, className, children, tagName, ...others } = this.props;
|
2019-02-14 17:51:52 +08:00
|
|
|
const classString = classNames(className, prefixCls);
|
2019-03-05 14:39:43 +08:00
|
|
|
return React.createElement(tagName, { className: classString, ...others }, children);
|
2017-11-09 20:04:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 03:40:36 +08:00
|
|
|
interface BasicLayoutState {
|
|
|
|
siders: string[];
|
|
|
|
}
|
|
|
|
|
2019-03-05 14:39:43 +08:00
|
|
|
class BasicLayout extends React.Component<BasicPropsWithTagName, BasicLayoutState> {
|
2017-11-09 20:04:34 +08:00
|
|
|
state = { siders: [] };
|
|
|
|
|
2019-04-05 16:15:01 +08:00
|
|
|
getSiderHook() {
|
2017-11-09 20:04:34 +08:00
|
|
|
return {
|
2019-04-05 16:15:01 +08:00
|
|
|
addSider: (id: string) => {
|
|
|
|
this.setState(state => ({
|
|
|
|
siders: [...state.siders, id],
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
removeSider: (id: string) => {
|
|
|
|
this.setState(state => ({
|
|
|
|
siders: state.siders.filter(currentId => currentId !== id),
|
|
|
|
}));
|
2017-11-09 20:04:34 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-04-05 16:15:01 +08:00
|
|
|
const { prefixCls, className, children, hasSider, tagName: Tag, ...others } = this.props;
|
2019-02-14 17:51:52 +08:00
|
|
|
const classString = classNames(className, prefixCls, {
|
2019-03-14 11:46:47 +08:00
|
|
|
[`${prefixCls}-has-sider`]:
|
|
|
|
typeof hasSider === 'boolean' ? hasSider : this.state.siders.length > 0,
|
2017-01-08 17:03:47 +08:00
|
|
|
});
|
2019-04-05 16:15:01 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<LayoutContext.Provider value={{ siderHook: this.getSiderHook() }}>
|
|
|
|
<Tag className={classString} {...others}>
|
|
|
|
{children}
|
|
|
|
</Tag>
|
|
|
|
</LayoutContext.Provider>
|
|
|
|
);
|
2016-12-30 21:41:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 03:20:05 +08:00
|
|
|
const Layout: React.ComponentClass<BasicProps> & {
|
|
|
|
Header: React.ComponentClass<BasicProps>;
|
|
|
|
Footer: React.ComponentClass<BasicProps>;
|
|
|
|
Content: React.ComponentClass<BasicProps>;
|
|
|
|
Sider: React.ComponentClass<SiderProps>;
|
|
|
|
} = generator({
|
2018-12-05 19:12:18 +08:00
|
|
|
suffixCls: 'layout',
|
2019-02-14 17:51:52 +08:00
|
|
|
tagName: 'section',
|
2017-11-09 20:04:34 +08:00
|
|
|
})(BasicLayout);
|
2016-12-30 21:41:28 +08:00
|
|
|
|
|
|
|
const Header = generator({
|
2018-12-05 19:12:18 +08:00
|
|
|
suffixCls: 'layout-header',
|
2019-02-14 17:51:52 +08:00
|
|
|
tagName: 'header',
|
2016-12-30 21:41:28 +08:00
|
|
|
})(Basic);
|
|
|
|
|
|
|
|
const Footer = generator({
|
2018-12-05 19:12:18 +08:00
|
|
|
suffixCls: 'layout-footer',
|
2019-02-14 17:51:52 +08:00
|
|
|
tagName: 'footer',
|
2016-12-30 21:41:28 +08:00
|
|
|
})(Basic);
|
|
|
|
|
|
|
|
const Content = generator({
|
2018-12-05 19:12:18 +08:00
|
|
|
suffixCls: 'layout-content',
|
2019-02-14 17:51:52 +08:00
|
|
|
tagName: 'main',
|
2016-12-30 21:41:28 +08:00
|
|
|
})(Basic);
|
|
|
|
|
|
|
|
Layout.Header = Header;
|
|
|
|
Layout.Footer = Footer;
|
|
|
|
Layout.Content = Content;
|
|
|
|
|
|
|
|
export default Layout;
|