index.js 7 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
afc163's avatar
afc163 committed
2
import { Select, message, Drawer, List, Switch, Divider, Icon, Button, Alert } from 'antd';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
3
import { formatMessage } from 'umi/locale';
4
import { CopyToClipboard } from 'react-copy-to-clipboard';
jim's avatar
jim committed
5
import { connect } from 'dva';
afc163's avatar
afc163 committed
6
import omit from 'omit.js';
jim's avatar
jim committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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 }))
24
class SettingDrawer extends PureComponent {
afc163's avatar
afc163 committed
25 26 27 28
  state = {
    collapse: false,
  };

afc163's avatar
afc163 committed
29
  getLayoutSetting = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
30
    const {
afc163's avatar
afc163 committed
31
      setting: { contentWidth, fixedHeader, layout, autoHideHeader, fixSiderbar },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
32
    } = this.props;
jim's avatar
jim committed
33 34
    return [
      {
afc163's avatar
afc163 committed
35
        title: formatMessage({ id: 'app.setting.content-width' }),
jim's avatar
jim committed
36 37
        action: [
          <Select
afc163's avatar
afc163 committed
38
            value={contentWidth}
jim's avatar
jim committed
39
            size="small"
afc163's avatar
afc163 committed
40
            onSelect={value => this.changeSetting('contentWidth', value)}
jim's avatar
jim committed
41 42
            style={{ width: 80 }}
          >
afc163's avatar
afc163 committed
43
            <Select.Option value="Fixed">
afc163's avatar
afc163 committed
44
              {formatMessage({ id: 'app.setting.content-width.fixed' })}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
45 46
            </Select.Option>
            <Select.Option value="Fluid">
afc163's avatar
afc163 committed
47
              {formatMessage({ id: 'app.setting.content-width.fluid' })}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
48
            </Select.Option>
jim's avatar
jim committed
49 50 51 52
          </Select>,
        ],
      },
      {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
53
        title: formatMessage({ id: 'app.setting.fixedheader' }),
jim's avatar
jim committed
54 55 56 57 58 59 60 61 62
        action: [
          <Switch
            size="small"
            checked={!!fixedHeader}
            onChange={checked => this.changeSetting('fixedHeader', checked)}
          />,
        ],
      },
      {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
63
        title: formatMessage({ id: 'app.setting.hideheader' }),
64
        hide: !fixedHeader,
jim's avatar
jim committed
65 66 67 68 69 70 71 72 73
        action: [
          <Switch
            size="small"
            checked={!!autoHideHeader}
            onChange={checked => this.changeSetting('autoHideHeader', checked)}
          />,
        ],
      },
      {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
74
        title: formatMessage({ id: 'app.setting.fixedsidebar' }),
75
        hide: layout === 'topmenu',
jim's avatar
jim committed
76 77 78 79 80 81 82 83
        action: [
          <Switch
            size="small"
            checked={!!fixSiderbar}
            onChange={checked => this.changeSetting('fixSiderbar', checked)}
          />,
        ],
      },
84
    ].filter(item => !item.hide);
jim's avatar
jim committed
85
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
86

jim's avatar
jim committed
87
  changeSetting = (key, value) => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
88 89
    const { setting } = this.props;
    const nextState = { ...setting };
jim's avatar
jim committed
90 91
    nextState[key] = value;
    if (key === 'layout') {
afc163's avatar
afc163 committed
92
      nextState.contentWidth = value === 'topmenu' ? 'Fixed' : 'Fluid';
afc163's avatar
afc163 committed
93 94
    } else if (key === 'fixedHeader' && !value) {
      nextState.autoHideHeader = false;
jim's avatar
jim committed
95
    }
jim's avatar
jim committed
96
    this.setState(nextState, () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
97 98
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
99 100 101 102 103
        type: 'setting/changeSetting',
        payload: this.state,
      });
    });
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
104

jim's avatar
jim committed
105
  togglerContent = () => {
afc163's avatar
afc163 committed
106 107
    const { collapse } = this.state;
    this.setState({ collapse: !collapse });
jim's avatar
jim committed
108
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
109

jim's avatar
jim committed
110
  render() {
111
    const { setting } = this.props;
afc163's avatar
afc163 committed
112 113
    const { navTheme, primaryColor, layout, colorWeak } = setting;
    const { collapse } = this.state;
jim's avatar
jim committed
114
    return (
115 116
      <Drawer
        visible={collapse}
117
        width={300}
118
        onClose={this.togglerContent}
119
        placement="right"
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
120 121
        handler={
          <div className={styles.handle}>
afc163's avatar
afc163 committed
122 123 124 125 126 127 128
            <Icon
              type={collapse ? 'close' : 'setting'}
              style={{
                color: '#fff',
                fontSize: 20,
              }}
            />
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
129 130 131
          </div>
        }
        onHandleClick={this.togglerContent}
132 133 134 135 136
        style={{
          zIndex: 999,
        }}
      >
        <div className={styles.content}>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
137
          <Body title={formatMessage({ id: 'app.setting.pagestyle' })}>
138 139 140 141 142
            <BlockChecbox
              list={[
                {
                  key: 'dark',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg',
afc163's avatar
afc163 committed
143
                  title: formatMessage({ id: 'app.setting.pagestyle.dark' }),
144 145 146 147
                },
                {
                  key: 'light',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg',
afc163's avatar
afc163 committed
148
                  title: formatMessage({ id: 'app.setting.pagestyle.light' }),
149 150
                },
              ]}
afc163's avatar
afc163 committed
151 152
              value={navTheme}
              onChange={value => this.changeSetting('navTheme', value)}
153 154
            />
          </Body>
jim's avatar
jim committed
155

156
          <ThemeColor
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
157
            title={formatMessage({ id: 'app.setting.themecolor' })}
afc163's avatar
afc163 committed
158 159
            value={primaryColor}
            onChange={color => this.changeSetting('primaryColor', color)}
160
          />
jim's avatar
jim committed
161

162
          <Divider />
jim's avatar
jim committed
163

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
164
          <Body title={formatMessage({ id: 'app.setting.navigationmode' })}>
165 166 167 168 169
            <BlockChecbox
              list={[
                {
                  key: 'sidemenu',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/JopDzEhOqwOjeNTXkoje.svg',
afc163's avatar
afc163 committed
170
                  title: formatMessage({ id: 'app.setting.sidemenu' }),
171 172 173 174
                },
                {
                  key: 'topmenu',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg',
afc163's avatar
afc163 committed
175
                  title: formatMessage({ id: 'app.setting.topmenu' }),
176 177 178 179
                },
              ]}
              value={layout}
              onChange={value => this.changeSetting('layout', value)}
jim's avatar
jim committed
180
            />
181
          </Body>
jim's avatar
jim committed
182

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

189 190
          <Divider />

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
191
          <Body title={formatMessage({ id: 'app.setting.othersettings' })}>
192 193 194 195 196 197 198 199 200
            <List.Item
              actions={[
                <Switch
                  size="small"
                  checked={!!colorWeak}
                  onChange={checked => this.changeSetting('colorWeak', checked)}
                />,
              ]}
            >
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
201
              {formatMessage({ id: 'app.setting.weakmode' })}
202 203
            </List.Item>
          </Body>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
204 205
          <Divider />
          <CopyToClipboard
afc163's avatar
afc163 committed
206
            text={JSON.stringify(omit(setting, ['colorWeak']), null, 2)}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
207
            onCopy={() => message.success(formatMessage({ id: 'app.setting.copyinfo' }))}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
208
          >
afc163's avatar
afc163 committed
209
            <Button block icon="copy">
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
210
              {formatMessage({ id: 'app.setting.copy' })}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
211 212
            </Button>
          </CopyToClipboard>
afc163's avatar
afc163 committed
213 214 215
          <Alert
            type="warning"
            className={styles.productionHint}
afc163's avatar
afc163 committed
216 217 218 219 220 221 222 223 224 225 226 227
            message={
              <div>
                {formatMessage({ id: 'app.setting.production.hint' })}{' '}
                <a
                  href="https://u.ant.design/pro-v2-default-settings"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  src/defaultSettings.js
                </a>
              </div>
            }
afc163's avatar
afc163 committed
228
          />
229
        </div>
230
      </Drawer>
jim's avatar
jim committed
231 232 233 234
    );
  }
}

235
export default SettingDrawer;