index.js 5.83 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
陈帅's avatar
陈帅 committed
2
import { Select, message, Drawer, List, Switch, Divider, Icon, Button } from 'antd';
陈帅's avatar
陈帅 committed
3
import { CopyToClipboard } from 'react-copy-to-clipboard';
jim's avatar
jim committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import { connect } from 'dva';
import styles from './index.less';
import ThemeColor from './ThemeColor';
import BlockChecbox from './BlockChecbox';

const Body = ({ children, title, style }) => (
  <div
    style={{
      ...style,
      marginBottom: 24,
    }}
  >
    <h3 className={styles.title}>{title}</h3>
    {children}
  </div>
);

@connect(({ setting }) => ({ setting }))
class SettingDarwer extends PureComponent {
  getLayOutSetting = () => {
陈帅's avatar
陈帅 committed
24
    const {
25
      setting: { grid, fixedHeader, layout, autoHideHeader, fixSiderbar },
陈帅's avatar
陈帅 committed
26
    } = this.props;
jim's avatar
jim committed
27 28 29 30 31 32 33 34 35 36
    return [
      {
        title: '栅格模式',
        action: [
          <Select
            value={grid}
            size="small"
            onSelect={value => this.changeSetting('grid', value)}
            style={{ width: 80 }}
          >
jim's avatar
jim committed
37 38
            <Select.Option value="Wide">定宽</Select.Option>
            <Select.Option value="Fluid">流式</Select.Option>
jim's avatar
jim committed
39 40 41 42
          </Select>,
        ],
      },
      {
jim's avatar
jim committed
43
        title: '固定 Header',
jim's avatar
jim committed
44 45 46 47 48 49 50 51 52 53
        action: [
          <Switch
            size="small"
            checked={!!fixedHeader}
            onChange={checked => this.changeSetting('fixedHeader', checked)}
          />,
        ],
      },
      {
        title: '下滑时隐藏 Header',
54
        hide: !fixedHeader,
jim's avatar
jim committed
55 56 57 58 59 60 61 62 63
        action: [
          <Switch
            size="small"
            checked={!!autoHideHeader}
            onChange={checked => this.changeSetting('autoHideHeader', checked)}
          />,
        ],
      },
      {
jim's avatar
jim committed
64
        title: '固定 Siderbar',
65
        hide: layout === 'topmenu',
jim's avatar
jim committed
66 67 68 69 70 71 72 73
        action: [
          <Switch
            size="small"
            checked={!!fixSiderbar}
            onChange={checked => this.changeSetting('fixSiderbar', checked)}
          />,
        ],
      },
jim's avatar
jim committed
74 75 76
    ].filter(item => {
      return !item.hide;
    });
jim's avatar
jim committed
77
  };
陈帅's avatar
陈帅 committed
78

jim's avatar
jim committed
79
  changeSetting = (key, value) => {
陈帅's avatar
陈帅 committed
80 81
    const { setting } = this.props;
    const nextState = { ...setting };
jim's avatar
jim committed
82 83 84 85 86 87 88 89
    nextState[key] = value;
    if (key === 'layout') {
      if (value === 'topmenu') {
        nextState.grid = 'Wide';
      } else {
        nextState.grid = 'Fluid';
      }
    }
jim's avatar
jim committed
90
    if (key === 'fixedHeader') {
91
      if (!value) {
jim's avatar
jim committed
92 93 94
        nextState.autoHideHeader = false;
      }
    }
jim's avatar
jim committed
95
    this.setState(nextState, () => {
陈帅's avatar
陈帅 committed
96 97
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
98 99 100 101 102
        type: 'setting/changeSetting',
        payload: this.state,
      });
    });
  };
陈帅's avatar
陈帅 committed
103

jim's avatar
jim committed
104
  togglerContent = () => {
陈帅's avatar
陈帅 committed
105 106
    const { setting } = this.props;
    this.changeSetting('collapse', !setting.collapse);
jim's avatar
jim committed
107
  };
陈帅's avatar
陈帅 committed
108

jim's avatar
jim committed
109
  render() {
陈帅's avatar
陈帅 committed
110 111
    const { setting } = this.props;
    const { collapse, silderTheme, themeColor, layout, colorWeak } = setting;
jim's avatar
jim committed
112
    return (
陈帅's avatar
陈帅 committed
113
      <Drawer
陈帅's avatar
陈帅 committed
114
        firstEnter={true}
陈帅's avatar
陈帅 committed
115 116 117
        visible={collapse}
        width={273}
        onClose={this.togglerContent}
陈帅's avatar
陈帅 committed
118 119 120 121 122
        placement="right"
        style={{
          zIndex: 999,
        }}
      >
陈帅's avatar
陈帅 committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        <div className={styles.handle} onClick={this.togglerContent}>
          {!collapse ? (
            <Icon
              type="setting"
              style={{
                color: '#FFF',
                fontSize: 20,
              }}
            />
          ) : (
            <Icon
              type="close"
              style={{
                color: '#FFF',
                fontSize: 20,
              }}
            />
          )}
        </div>
陈帅's avatar
陈帅 committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        <div className={styles.content}>
          <Body title="整体风格设置">
            <BlockChecbox
              list={[
                {
                  key: 'dark',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg',
                },
                {
                  key: 'light',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg',
                },
              ]}
              value={silderTheme}
              onChange={value => this.changeSetting('silderTheme', value)}
            />
          </Body>
jim's avatar
jim committed
159

160 161 162 163
          <ThemeColor
            value={themeColor}
            onChange={color => this.changeSetting('themeColor', color)}
          />
jim's avatar
jim committed
164

陈帅's avatar
陈帅 committed
165
          <Divider />
jim's avatar
jim committed
166

陈帅's avatar
陈帅 committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180
          <Body title="导航设置 ">
            <BlockChecbox
              list={[
                {
                  key: 'sidemenu',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/JopDzEhOqwOjeNTXkoje.svg',
                },
                {
                  key: 'topmenu',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg',
                },
              ]}
              value={layout}
              onChange={value => this.changeSetting('layout', value)}
jim's avatar
jim committed
181
            />
陈帅's avatar
陈帅 committed
182
          </Body>
jim's avatar
jim committed
183

陈帅's avatar
陈帅 committed
184 185 186 187 188
          <List
            split={false}
            dataSource={this.getLayOutSetting()}
            renderItem={item => <List.Item actions={item.action}>{item.title}</List.Item>}
          />
jim's avatar
jim committed
189

陈帅's avatar
陈帅 committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
          <Divider />

          <Body title="其他设置 ">
            <List.Item
              actions={[
                <Switch
                  size="small"
                  checked={!!colorWeak}
                  onChange={checked => this.changeSetting('colorWeak', checked)}
                />,
              ]}
            >
              色弱模式
            </List.Item>
          </Body>
陈帅's avatar
陈帅 committed
205 206 207 208 209 210 211 212 213 214 215 216 217
          <Divider />
          <CopyToClipboard
            text={JSON.stringify(setting)}
            onCopy={() => message.success('copy success')}
          >
            <Button
              style={{
                width: 224,
              }}
            >
              <Icon type="copy" />拷贝代码
            </Button>
          </CopyToClipboard>
陈帅's avatar
陈帅 committed
218
        </div>
陈帅's avatar
陈帅 committed
219
      </Drawer>
jim's avatar
jim committed
220 221 222 223 224
    );
  }
}

export default SettingDarwer;