index.js 5.9 KB
Newer Older
J
jim 已提交
1
import React, { PureComponent, Fragment } from 'react';
J
jim 已提交
2
import { Select, message, List, Switch, Divider, Radio } from 'antd';
J
jim 已提交
3
import DrawerMenu from 'rc-drawer-menu';
J
jim 已提交
4
import { connect } from 'dva';
J
jim 已提交
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>
);
J
jim 已提交
34
@connect(({ setting }) => ({ setting }))
J
jim 已提交
35
class Sidebar extends PureComponent {
J
jim 已提交
36
  componentDidMount() {
J
jim 已提交
37 38 39 40
    const { themeColor } = this.props.setting;
    if (themeColor !== '#1890FF') {
      this.colorChange(themeColor);
    }
J
jim 已提交
41
  }
J
jim 已提交
42
  getLayOutSetting = () => {
J
jim 已提交
43
    const { grid, fixedHeader, autoHideHeader, fixSiderbar, layout } = this.props.setting;
J
jim 已提交
44 45 46 47 48 49
    return [
      {
        title: '栅格模式',
        isShow: true,
        action: [
          <Select
J
jim 已提交
50
            value={grid}
J
jim 已提交
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
J
jim 已提交
64
            checked={!!fixedHeader}
J
jim 已提交
65 66 67 68 69 70 71 72 73
            onChange={checked => this.changeSetting('fixedHeader', checked)}
          />,
        ],
      },
      {
        title: '↳ 下滑时隐藏 Header',
        isShow: true,
        action: [
          <Switch
J
jim 已提交
74
            checked={!!autoHideHeader}
J
jim 已提交
75 76 77 78 79 80 81
            onChange={checked => this.changeSetting('autoHideHeader', checked)}
          />,
        ],
      },
      {
        title: 'Fix Siderbar',
        isShow: layout === 'sidemenu',
J
jim 已提交
82
        action: [<Switch checked={!!fixSiderbar} onChange={this.fixSiderbar} />],
J
jim 已提交
83 84 85
      },
    ].filter(item => item.isShow);
  };
J
jim 已提交
86
  fixSiderbar = checked => {
J
jim 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
    this.changeSetting('fixSiderbar', checked);
  };
  changeSetting = (key, value) => {
    const nextState = {};
    nextState[key] = value;
    if (key === 'layout') {
      if (value === 'topmenu') {
        nextState.grid = 'Wide';
      } else {
        nextState.grid = 'Fluid';
      }
    }
    this.setState(nextState, () => {
J
jim 已提交
100 101 102 103
      this.props.dispatch({
        type: 'setting/changeSetting',
        payload: this.state,
      });
J
jim 已提交
104 105 106
    });
  };
  togglerContent = () => {
J
jim 已提交
107
    this.changeSetting('collapse', !this.props.setting.collapse);
J
jim 已提交
108
  };
J
jim 已提交
109 110
  colorChange = color => {
    this.changeSetting('themeColor', color);
J
jim 已提交
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`);
      });
J
jim 已提交
122
  };
J
jim 已提交
123 124 125 126
  render() {
    const radioStyle = {
      display: 'block',
    };
J
jim 已提交
127
    const { collapse, silderTheme, themeColor, layout, colorWeak } = this.props.setting;
J
jim 已提交
128
    return (
J
jim 已提交
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"
          />
J
jim 已提交
135
        </div>
J
jim 已提交
136 137 138 139
        <DrawerMenu
          parent={null}
          level={null}
          iconChild={null}
J
jim 已提交
140
          open={collapse}
J
jim 已提交
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)}
J
jim 已提交
154
                value={silderTheme}
J
jim 已提交
155 156 157 158
              >
                <Radio style={radioStyle} value="dark">
                  <ColorBlock color="#002140" title="深色导航" />
                </Radio>
J
jim 已提交
159
                <Radio style={radioStyle} value="light">
J
jim 已提交
160 161 162
                  <ColorBlock color="#E9E9E9" title="浅色导航" />
                </Radio>
              </RadioGroup>
J
jim 已提交
163
              <ThemeColor value={themeColor} onChange={this.colorChange} />
J
jim 已提交
164 165 166 167
            </Body>
            <Divider style={{ margin: 0 }} />
            <Body title="导航设置 ">
              <LayoutSeting
J
jim 已提交
168 169
                value={layout}
                onChange={value => this.changeSetting('layout', value)}
J
jim 已提交
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
J
jim 已提交
186
                        value={colorWeak}
J
jim 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200
                        onSelect={value => this.changeSetting('colorWeak', value)}
                        style={{ width: 120 }}
                      >
                        <Select.Option value="open">打开</Select.Option>
                        <Select.Option value="colse">关闭</Select.Option>
                      </Select>,
                    ],
                  },
                ]}
                renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
              />
            </Body>
          </div>
        </DrawerMenu>
J
jim 已提交
201
      </div>
J
jim 已提交
202 203 204 205 206
    );
  }
}

export default Sidebar;