index.js 5.96 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent, Fragment } from 'react';
jim's avatar
jim committed
2
import { Select, message, List, Switch, Divider, Radio } from 'antd';
jim's avatar
jim committed
3
import DrawerMenu from 'rc-drawer-menu';
jim's avatar
jim committed
4
import { connect } from 'dva';
jim's avatar
jim committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import styles from './index.less';
import ThemeColor from './ThemeColor';
import LayoutSeting from './LayoutSetting';

const RadioGroup = Radio.Group;

const ColorBlock = ({ color, title }) => (
  <Fragment>
    <div
      className={styles.color_block}
      style={{
        backgroundColor: color,
      }}
    />
    <div className={styles.color_block_title}>{title}</div>
  </Fragment>
);

const Body = ({ children, title, style }) => (
  <div
    style={{
      padding: 15,
      ...style,
    }}
  >
    <h3 className={styles.bodyTitle}>{title}</h3>
    {children}
  </div>
);
jim's avatar
jim committed
34
@connect(({ setting }) => ({ setting }))
jim's avatar
jim committed
35
class Sidebar extends PureComponent {
jim's avatar
jim committed
36
  componentDidMount() {
jim's avatar
jim committed
37 38 39 40
    const { themeColor } = this.props.setting;
    if (themeColor !== '#1890FF') {
      this.colorChange(themeColor);
    }
jim's avatar
jim committed
41
  }
jim's avatar
jim committed
42
  getLayOutSetting = () => {
jim's avatar
jim committed
43
    const { grid, fixedHeader, autoHideHeader, fixSiderbar, layout } = this.props.setting;
jim's avatar
jim committed
44 45 46 47 48 49
    return [
      {
        title: '栅格模式',
        isShow: true,
        action: [
          <Select
jim's avatar
jim committed
50
            value={grid}
jim's avatar
jim committed
51 52 53 54 55 56 57 58 59 60 61 62 63
            onSelect={value => this.changeSetting('grid', value)}
            style={{ width: 120 }}
          >
            <Select.Option value="Wide">Wide</Select.Option>
            <Select.Option value="Fluid">Fluid</Select.Option>
          </Select>,
        ],
      },
      {
        title: 'Fixed Header',
        isShow: true,
        action: [
          <Switch
jim's avatar
jim committed
64
            checked={!!fixedHeader}
jim's avatar
jim committed
65 66 67 68 69 70 71 72 73
            onChange={checked => this.changeSetting('fixedHeader', checked)}
          />,
        ],
      },
      {
        title: '↳ 下滑时隐藏 Header',
        isShow: true,
        action: [
          <Switch
jim's avatar
jim committed
74
            checked={!!autoHideHeader}
jim's avatar
jim committed
75 76 77 78 79 80 81
            onChange={checked => this.changeSetting('autoHideHeader', checked)}
          />,
        ],
      },
      {
        title: 'Fix Siderbar',
        isShow: layout === 'sidemenu',
jim's avatar
jim committed
82
        action: [<Switch checked={!!fixSiderbar} onChange={this.fixSiderbar} />],
jim's avatar
jim committed
83 84 85
      },
    ].filter(item => item.isShow);
  };
jim's avatar
jim committed
86
  fixSiderbar = checked => {
jim's avatar
jim committed
87 88 89
    this.changeSetting('fixSiderbar', checked);
  };
  changeSetting = (key, value) => {
jim's avatar
jim committed
90
    const nextState = { ...this.props.setting };
jim's avatar
jim committed
91 92 93 94 95 96 97 98 99
    nextState[key] = value;
    if (key === 'layout') {
      if (value === 'topmenu') {
        nextState.grid = 'Wide';
      } else {
        nextState.grid = 'Fluid';
      }
    }
    this.setState(nextState, () => {
jim's avatar
jim committed
100 101 102 103
      this.props.dispatch({
        type: 'setting/changeSetting',
        payload: this.state,
      });
jim's avatar
jim committed
104 105 106
    });
  };
  togglerContent = () => {
jim's avatar
jim committed
107
    this.changeSetting('collapse', !this.props.setting.collapse);
jim's avatar
jim committed
108
  };
jim's avatar
jim committed
109 110
  colorChange = color => {
    this.changeSetting('themeColor', color);
jim's avatar
jim committed
111 112 113 114 115 116 117 118 119 120 121
    const hideMessage = message.loading('正在编译主题!', 0);
    window.less
      .modifyVars({
        '@primary-color': color,
      })
      .then(() => {
        hideMessage();
      })
      .catch(() => {
        message.error(`Failed to update theme`);
      });
jim's avatar
jim committed
122
  };
jim's avatar
jim committed
123 124 125 126
  render() {
    const radioStyle = {
      display: 'block',
    };
jim's avatar
jim committed
127
    const { collapse, silderTheme, themeColor, layout, colorWeak } = this.props.setting;
jim's avatar
jim committed
128
    return (
jim's avatar
jim committed
129 130 131 132 133 134
      <div className={styles.sidebar}>
        <div className={styles.mini_bar} onClick={this.togglerContent}>
          <img
            alt="logo"
            src="https://gw.alipayobjects.com/zos/rmsportal/ApQgLmeZDNJMomKNvavq.svg"
          />
jim's avatar
jim committed
135
        </div>
jim's avatar
jim committed
136 137 138 139
        <DrawerMenu
          parent={null}
          level={null}
          iconChild={null}
jim's avatar
jim committed
140
          open={collapse}
jim's avatar
jim committed
141 142 143 144 145 146 147 148 149 150 151 152 153
          placement="right"
          width="336px"
          onMaskClick={this.togglerContent}
        >
          <div className={styles.content}>
            <Body
              title="整体风格设置"
              style={{
                paddingBottom: 10,
              }}
            >
              <RadioGroup
                onChange={({ target }) => this.changeSetting('silderTheme', target.value)}
jim's avatar
jim committed
154
                value={silderTheme}
jim's avatar
jim committed
155 156 157 158
              >
                <Radio style={radioStyle} value="dark">
                  <ColorBlock color="#002140" title="深色导航" />
                </Radio>
jim's avatar
jim committed
159
                <Radio style={radioStyle} value="light">
jim's avatar
jim committed
160 161 162
                  <ColorBlock color="#E9E9E9" title="浅色导航" />
                </Radio>
              </RadioGroup>
jim's avatar
jim committed
163
              <ThemeColor value={themeColor} onChange={this.colorChange} />
jim's avatar
jim committed
164 165 166 167
            </Body>
            <Divider style={{ margin: 0 }} />
            <Body title="导航设置 ">
              <LayoutSeting
jim's avatar
jim committed
168 169
                value={layout}
                onChange={value => this.changeSetting('layout', value)}
jim's avatar
jim committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
              />
              <List
                split={false}
                dataSource={this.getLayOutSetting()}
                renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
              />
            </Body>
            <Divider style={{ margin: 0 }} />
            <Body title="其他设置">
              <List
                split={false}
                dataSource={[
                  {
                    title: '色弱模式',
                    action: [
                      <Select
jim's avatar
jim committed
186
                        value={colorWeak}
jim's avatar
jim committed
187 188 189 190
                        onSelect={value => this.changeSetting('colorWeak', value)}
                        style={{ width: 120 }}
                      >
                        <Select.Option value="open">打开</Select.Option>
jim's avatar
jim committed
191
                        <Select.Option value="close">关闭</Select.Option>
jim's avatar
jim committed
192 193 194 195 196 197 198 199 200
                      </Select>,
                    ],
                  },
                ]}
                renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
              />
            </Body>
          </div>
        </DrawerMenu>
jim's avatar
jim committed
201
      </div>
jim's avatar
jim committed
202 203 204 205 206
    );
  }
}

export default Sidebar;