ant-design/components/layout/Sider.tsx

257 lines
6.8 KiB
TypeScript
Raw Normal View History

import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
if (typeof window !== 'undefined') {
2018-09-30 16:07:01 +08:00
const matchMediaPolyfill = (mediaQuery: string) => {
return {
media: mediaQuery,
matches: false,
addListener() {
},
removeListener() {
},
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
}
import * as React from 'react';
import { polyfill } from 'react-lifecycles-compat';
import classNames from 'classnames';
import omit from 'omit.js';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
import Icon from '../icon';
2018-07-19 15:39:47 +08:00
import isNumeric from '../_util/isNumeric';
const dimensionMap = {
xs: '480px',
sm: '576px',
md: '768px',
lg: '992px',
xl: '1200px',
xxl: '1600px',
};
2017-11-21 19:02:23 +08:00
export type CollapseType = 'clickTrigger' | 'responsive';
export type SiderTheme = 'light' | 'dark';
export interface SiderProps extends React.HTMLAttributes<HTMLDivElement> {
prefixCls?: string;
collapsible?: boolean;
collapsed?: boolean;
defaultCollapsed?: boolean;
reverseArrow?: boolean;
2017-11-21 19:02:23 +08:00
onCollapse?: (collapsed: boolean, type: CollapseType) => void;
trigger?: React.ReactNode;
width?: number | string;
collapsedWidth?: number | string;
breakpoint?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
theme?: SiderTheme;
onBreakpoint?: (broken: boolean) => void;
}
2018-04-01 01:15:42 +08:00
export interface SiderState {
2017-11-21 19:02:23 +08:00
collapsed?: boolean;
below: boolean;
belowShow?: boolean;
}
2018-04-01 01:15:42 +08:00
export interface SiderContext {
siderCollapsed: boolean;
}
const generateId = (() => {
let i = 0;
return (prefix: string = '') => {
i += 1;
return `${prefix}${i}`;
};
})();
class Sider extends React.Component<SiderProps, SiderState> {
static __ANT_LAYOUT_SIDER: any = true;
static defaultProps = {
collapsible: false,
defaultCollapsed: false,
reverseArrow: false,
width: 200,
collapsedWidth: 80,
style: {},
theme: 'dark' as SiderTheme,
};
static childContextTypes = {
siderCollapsed: PropTypes.bool,
collapsedWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
};
static contextTypes = {
siderHook: PropTypes.object,
};
static getDerivedStateFromProps(nextProps: SiderProps) {
if ('collapsed' in nextProps) {
return {
collapsed: nextProps.collapsed,
};
}
return null;
}
2018-11-09 17:38:19 +08:00
context: any;
2017-11-21 19:02:23 +08:00
private mql: MediaQueryList;
private uniqueId: string;
2017-11-21 19:02:23 +08:00
constructor(props: SiderProps) {
super(props);
this.uniqueId = generateId('ant-sider-');
let matchMedia;
if (typeof window !== 'undefined') {
matchMedia = window.matchMedia;
}
if (matchMedia && props.breakpoint && props.breakpoint in dimensionMap) {
this.mql = matchMedia(`(max-width: ${dimensionMap[props.breakpoint]})`);
}
let collapsed;
if ('collapsed' in props) {
collapsed = props.collapsed;
} else {
collapsed = props.defaultCollapsed;
}
this.state = {
collapsed,
below: false,
};
}
getChildContext() {
return {
siderCollapsed: this.state.collapsed,
collapsedWidth: this.props.collapsedWidth,
};
}
componentDidMount() {
if (this.mql) {
this.mql.addListener(this.responsiveHandler);
this.responsiveHandler(this.mql);
}
if (this.context.siderHook) {
this.context.siderHook.addSider(this.uniqueId);
}
}
componentWillUnmount() {
if (this.mql) {
2018-09-30 21:39:12 +08:00
this.mql.removeListener(this.responsiveHandler as any);
}
if (this.context.siderHook) {
this.context.siderHook.removeSider(this.uniqueId);
}
}
2018-09-30 21:39:12 +08:00
responsiveHandler = (mql: MediaQueryListEvent | MediaQueryList) => {
this.setState({ below: mql.matches });
const { onBreakpoint } = this.props;
if (onBreakpoint) {
onBreakpoint(mql.matches);
}
if (this.state.collapsed !== mql.matches) {
this.setCollapsed(mql.matches, 'responsive');
}
}
2017-11-21 19:02:23 +08:00
setCollapsed = (collapsed: boolean, type: CollapseType) => {
if (!('collapsed' in this.props)) {
this.setState({
collapsed,
});
}
const { onCollapse } = this.props;
if (onCollapse) {
onCollapse(collapsed, type);
}
}
toggle = () => {
const collapsed = !this.state.collapsed;
this.setCollapsed(collapsed, 'clickTrigger');
}
belowShowChange = () => {
this.setState({ belowShow: !this.state.belowShow });
}
renderSider = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
className, theme,
collapsible, reverseArrow, trigger, style, width, collapsedWidth,
...others
} = this.props;
const prefixCls = getPrefixCls('layout-sider', customizePrefixCls);
const divProps = omit(others, ['collapsed',
'defaultCollapsed', 'onCollapse', 'breakpoint', 'onBreakpoint']);
const rawWidth = this.state.collapsed ? collapsedWidth : width;
// use "px" as fallback unit for width
const siderWidth = isNumeric(rawWidth) ? `${rawWidth}px` : String(rawWidth);
// special trigger when collapsedWidth == 0
const zeroWidthTrigger = parseFloat(String(collapsedWidth || 0)) === 0 ? (
<span onClick={this.toggle} className={`${prefixCls}-zero-width-trigger`}>
<Icon type="bars" />
</span>
) : null;
const iconObj = {
'expanded': reverseArrow ? <Icon type="right" /> : <Icon type="left" />,
'collapsed': reverseArrow ? <Icon type="left" /> : <Icon type="right" />,
};
const status = this.state.collapsed ? 'collapsed' : 'expanded';
const defaultTrigger = iconObj[status];
const triggerDom = (
trigger !== null ?
zeroWidthTrigger || (
<div className={`${prefixCls}-trigger`} onClick={this.toggle} style={{ width: siderWidth }}>
{trigger || defaultTrigger}
</div>
) : null
);
const divStyle = {
...style,
flex: `0 0 ${siderWidth}`,
maxWidth: siderWidth, // Fix width transition bug in IE11
minWidth: siderWidth, // https://github.com/ant-design/ant-design/issues/6349
width: siderWidth,
};
const siderCls = classNames(className, prefixCls, `${prefixCls}-${theme}`, {
[`${prefixCls}-collapsed`]: !!this.state.collapsed,
[`${prefixCls}-has-trigger`]: collapsible && trigger !== null && !zeroWidthTrigger,
[`${prefixCls}-below`]: !!this.state.below,
[`${prefixCls}-zero-width`]: parseFloat(siderWidth) === 0,
});
return (
<div className={siderCls} {...divProps} style={divStyle}>
<div className={`${prefixCls}-children`}>{this.props.children}</div>
{collapsible || (this.state.below && zeroWidthTrigger) ? triggerDom : null}
</div>
);
}
render() {
return (
<ConfigConsumer>
{this.renderSider}
</ConfigConsumer>
);
}
}
polyfill(Sider);
export default Sider;