import React from 'react'; import classNames from 'classnames'; import omit from 'omit.js'; import Icon from '../icon'; export interface SiderProps { style?: React.CSSProperties; prefixCls?: string; className?: string; collapsible?: boolean; collapsed?: boolean; defaultCollapsed?: boolean; onCollapse?: (collapsed: boolean) => void; trigger?: React.ReactNode; width?: number | string; collapsedWidth?: number; } export default class Sider extends React.Component { static defaultProps = { prefixCls: 'ant-layout-sider', collapsible: false, defaultCollapsed: false, width: 200, collapsedWidth: 64, style: {}, }; constructor(props) { super(props); let collapsed; if ('collapsed' in props) { collapsed = props.collapsed; } else { collapsed = props.defaultCollapsed; } this.state = { collapsed, }; } componentWillReceiveProps(nextProps) { if ('collapsed' in nextProps) { this.setState({ collapsed: nextProps.collapsed, }); } } setCollapsed = (collapsed) => { if (!('collapsed' in this.props)) { this.setState({ collapsed, }); } const { onCollapse } = this.props; if (onCollapse) { onCollapse(collapsed); } } toggle = () => { const collapsed = !this.state.collapsed; this.setCollapsed(collapsed); } render() { const { prefixCls, className, collapsible, trigger, style, width, collapsedWidth, ...others, } = this.props; const divProps = omit(others, ['collapsed', 'defaultCollapsed', 'onCollapse']); const siderCls = classNames(className, prefixCls, { [`${prefixCls}-collapsed`]: !!this.state.collapsed, [`${prefixCls}-has-trigger`]: !!trigger, }); const divStyle = { ...style, flex: `0 0 ${this.state.collapsed ? collapsedWidth : width}px`, }; const iconObj = { 'expanded': , 'collapsed': , }; const status = this.state.collapsed ? 'collapsed' : 'expanded'; const defaultTrigger = iconObj[status]; const triggerDom = ( trigger !== null ? (
{trigger || defaultTrigger}
) : null ); return (
{this.props.children} {collapsible && triggerDom}
); } }