mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-26 04:00:13 +08:00
2bced36f0c
* Tweak button padding
* upgrade input and button size
* update form controls size
* update components size
* Add lemon color and update rate color
* Add new icons
* update layout style
* breadcrumb and dropdown
* update menu arrow width
* update layout and menu
* update steps
* fix var name
* update cascasder style
* Update DatePicker
* update InputNumber and Mention
* radio and switch
* select and transfer
* TimePicker & Upload
* more components
* calendar and list
* Tree Timeline Popover Tag
* divider modal popover
* update search input
* update card style
* update switch disabled opacity
* update shoadow and mask
* Add v2-compatible-reset.less
* Fix undefined className
* update snahshotssssssssssssssssss 👻
159 lines
4.3 KiB
TypeScript
159 lines
4.3 KiB
TypeScript
import React from 'react';
|
|
import classNames from 'classnames';
|
|
import omit from 'omit.js';
|
|
import PropTypes from 'prop-types';
|
|
import Icon from '../icon';
|
|
|
|
export interface SiderProps {
|
|
style?: React.CSSProperties;
|
|
prefixCls?: string;
|
|
className?: string;
|
|
collapsible?: boolean;
|
|
collapsed?: boolean;
|
|
defaultCollapsed?: boolean;
|
|
reverseArrow?: boolean;
|
|
onCollapse?: (collapsed: boolean, type: 'clickTrigger' | 'responsive') => void;
|
|
trigger?: React.ReactNode;
|
|
width?: number | string;
|
|
collapsedWidth?: number | string;
|
|
breakpoint?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
}
|
|
|
|
export default class Sider extends React.Component<SiderProps, any> {
|
|
static __ANT_LAYOUT_SIDER: any = true;
|
|
|
|
static defaultProps = {
|
|
prefixCls: 'ant-layout-sider',
|
|
collapsible: false,
|
|
defaultCollapsed: false,
|
|
reverseArrow: false,
|
|
width: 200,
|
|
collapsedWidth: 80,
|
|
style: {},
|
|
};
|
|
|
|
static childContextTypes = {
|
|
siderCollapsed: PropTypes.bool,
|
|
};
|
|
|
|
private mql: any;
|
|
|
|
constructor(props) {
|
|
super(props);
|
|
let collapsed;
|
|
if ('collapsed' in props) {
|
|
collapsed = props.collapsed;
|
|
} else {
|
|
collapsed = props.defaultCollapsed;
|
|
}
|
|
this.state = {
|
|
collapsed,
|
|
below: false,
|
|
};
|
|
}
|
|
|
|
getChildContext() {
|
|
return {
|
|
siderCollapsed: this.state.collapsed,
|
|
};
|
|
}
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
if ('collapsed' in nextProps) {
|
|
this.setState({
|
|
collapsed: nextProps.collapsed,
|
|
});
|
|
}
|
|
}
|
|
|
|
componentDidMount() {
|
|
if (this.mql) {
|
|
this.mql.addListener(this.responsiveHandler);
|
|
this.responsiveHandler(this.mql);
|
|
}
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
if (this.mql) {
|
|
this.mql.removeListener(this.responsiveHandler);
|
|
}
|
|
}
|
|
|
|
responsiveHandler = (mql) => {
|
|
this.setState({ below: mql.matches });
|
|
if (this.state.collapsed !== mql.matches) {
|
|
this.setCollapsed(mql.matches, 'responsive');
|
|
}
|
|
}
|
|
|
|
setCollapsed = (collapsed, type) => {
|
|
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 });
|
|
}
|
|
|
|
render() {
|
|
const { prefixCls, className,
|
|
collapsible, reverseArrow, trigger, style, width, collapsedWidth,
|
|
...others,
|
|
} = this.props;
|
|
const divProps = omit(others, ['collapsed',
|
|
'defaultCollapsed', 'onCollapse', 'breakpoint']);
|
|
const siderWidth = this.state.collapsed ? collapsedWidth : width;
|
|
// special trigger when collapsedWidth == 0
|
|
const zeroWidthTrigger = collapsedWidth === 0 || collapsedWidth === '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}px`,
|
|
maxWidth: `${siderWidth}px`, // Fix width transition bug in IE11
|
|
minWidth: `${siderWidth}px`, // https://github.com/ant-design/ant-design/issues/6349
|
|
width: `${siderWidth}px`,
|
|
};
|
|
const siderCls = classNames(className, prefixCls, {
|
|
[`${prefixCls}-collapsed`]: !!this.state.collapsed,
|
|
[`${prefixCls}-has-trigger`]: !!trigger,
|
|
[`${prefixCls}-below`]: !!this.state.below,
|
|
[`${prefixCls}-zero-width`]: siderWidth === 0 || 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>
|
|
);
|
|
}
|
|
}
|