index.tsx 7.87 KB
Newer Older
陈小聪's avatar
陈小聪 committed
1
import React, { Component } from 'react';
afc163's avatar
afc163 committed
2
import { Select, message, Drawer, List, Switch, Divider, Icon, Button, Alert, Tooltip } from 'antd';
陈小聪's avatar
陈小聪 committed
3
import { formatMessage } from 'umi-plugin-locale';
陈帅's avatar
陈帅 committed
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
import styles from './index.less';
import ThemeColor from './ThemeColor';
9
import BlockCheckbox from './BlockCheckbox';
陈小聪's avatar
陈小聪 committed
10
import { DefaultSettings } from '../../../config/defaultSettings';
jim's avatar
jim committed
11

afc163's avatar
afc163 committed
12
const { Option } = Select;
陈小聪's avatar
陈小聪 committed
13 14 15 16
interface BodyProps {
  title: string;
  style?: React.CSSProperties;
}
afc163's avatar
afc163 committed
17

陈小聪's avatar
陈小聪 committed
18
const Body: React.SFC<BodyProps> = ({ children, title, style }) => (
jim's avatar
jim committed
19 20 21 22 23 24 25 26 27 28 29
  <div
    style={{
      ...style,
      marginBottom: 24,
    }}
  >
    <h3 className={styles.title}>{title}</h3>
    {children}
  </div>
);

陈小聪's avatar
陈小聪 committed
30 31 32 33 34 35
interface SettingDrawerProps {
  setting?: DefaultSettings;
  dispatch?: (args: any) => void;
}
interface SettingDrawerState {}

jim's avatar
jim committed
36
@connect(({ setting }) => ({ setting }))
陈小聪's avatar
陈小聪 committed
37
class SettingDrawer extends Component<SettingDrawerProps, SettingDrawerState> {
afc163's avatar
afc163 committed
38 39 40 41
  state = {
    collapse: false,
  };

afc163's avatar
afc163 committed
42
  getLayoutSetting = () => {
陈帅's avatar
陈帅 committed
43
    const {
afc163's avatar
afc163 committed
44
      setting: { contentWidth, fixedHeader, layout, autoHideHeader, fixSiderbar },
陈帅's avatar
陈帅 committed
45
    } = this.props;
jim's avatar
jim committed
46 47
    return [
      {
afc163's avatar
afc163 committed
48
        title: formatMessage({ id: 'app.setting.content-width' }),
afc163's avatar
afc163 committed
49
        action: (
jim's avatar
jim committed
50
          <Select
afc163's avatar
afc163 committed
51
            value={contentWidth}
jim's avatar
jim committed
52
            size="small"
afc163's avatar
afc163 committed
53
            onSelect={value => this.changeSetting('contentWidth', value)}
jim's avatar
jim committed
54 55
            style={{ width: 80 }}
          >
afc163's avatar
afc163 committed
56 57 58 59 60 61
            {layout === 'sidemenu' ? null : (
              <Option value="Fixed">
                {formatMessage({ id: 'app.setting.content-width.fixed' })}
              </Option>
            )}
            <Option value="Fluid">
afc163's avatar
afc163 committed
62
              {formatMessage({ id: 'app.setting.content-width.fluid' })}
afc163's avatar
afc163 committed
63 64 65
            </Option>
          </Select>
        ),
jim's avatar
jim committed
66 67
      },
      {
陈帅's avatar
陈帅 committed
68
        title: formatMessage({ id: 'app.setting.fixedheader' }),
afc163's avatar
afc163 committed
69
        action: (
jim's avatar
jim committed
70 71 72 73
          <Switch
            size="small"
            checked={!!fixedHeader}
            onChange={checked => this.changeSetting('fixedHeader', checked)}
afc163's avatar
afc163 committed
74 75
          />
        ),
jim's avatar
jim committed
76 77
      },
      {
陈帅's avatar
陈帅 committed
78
        title: formatMessage({ id: 'app.setting.hideheader' }),
afc163's avatar
afc163 committed
79
        disabled: !fixedHeader,
afc163's avatar
locales  
afc163 committed
80
        disabledReason: formatMessage({ id: 'app.setting.hideheader.hint' }),
afc163's avatar
afc163 committed
81
        action: (
jim's avatar
jim committed
82 83 84 85
          <Switch
            size="small"
            checked={!!autoHideHeader}
            onChange={checked => this.changeSetting('autoHideHeader', checked)}
afc163's avatar
afc163 committed
86 87
          />
        ),
jim's avatar
jim committed
88 89
      },
      {
陈帅's avatar
陈帅 committed
90
        title: formatMessage({ id: 'app.setting.fixedsidebar' }),
afc163's avatar
afc163 committed
91
        disabled: layout === 'topmenu',
afc163's avatar
locales  
afc163 committed
92
        disabledReason: formatMessage({ id: 'app.setting.fixedsidebar.hint' }),
afc163's avatar
afc163 committed
93
        action: (
jim's avatar
jim committed
94 95 96 97
          <Switch
            size="small"
            checked={!!fixSiderbar}
            onChange={checked => this.changeSetting('fixSiderbar', checked)}
afc163's avatar
afc163 committed
98 99
          />
        ),
jim's avatar
jim committed
100
      },
afc163's avatar
afc163 committed
101
    ];
jim's avatar
jim committed
102
  };
陈帅's avatar
陈帅 committed
103

jim's avatar
jim committed
104
  changeSetting = (key, value) => {
陈帅's avatar
陈帅 committed
105 106
    const { setting } = this.props;
    const nextState = { ...setting };
jim's avatar
jim committed
107 108
    nextState[key] = value;
    if (key === 'layout') {
afc163's avatar
afc163 committed
109
      nextState.contentWidth = value === 'topmenu' ? 'Fixed' : 'Fluid';
afc163's avatar
afc163 committed
110 111
    } else if (key === 'fixedHeader' && !value) {
      nextState.autoHideHeader = false;
jim's avatar
jim committed
112
    }
jim's avatar
jim committed
113
    this.setState(nextState, () => {
陈帅's avatar
陈帅 committed
114 115
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
116 117 118 119 120
        type: 'setting/changeSetting',
        payload: this.state,
      });
    });
  };
陈帅's avatar
陈帅 committed
121

jim's avatar
jim committed
122
  togglerContent = () => {
afc163's avatar
afc163 committed
123 124
    const { collapse } = this.state;
    this.setState({ collapse: !collapse });
jim's avatar
jim committed
125
  };
陈帅's avatar
陈帅 committed
126

afc163's avatar
afc163 committed
127 128 129 130 131 132 133
  renderLayoutSettingItem = item => {
    const action = React.cloneElement(item.action, {
      disabled: item.disabled,
    });
    return (
      <Tooltip title={item.disabled ? item.disabledReason : ''} placement="left">
        <List.Item actions={[action]}>
陈小聪's avatar
陈小聪 committed
134
          <span style={{ opacity: item.disabled ? 0.5 : 1 }}>{item.title}</span>
afc163's avatar
afc163 committed
135 136 137 138 139
        </List.Item>
      </Tooltip>
    );
  };

jim's avatar
jim committed
140
  render() {
陈帅's avatar
陈帅 committed
141
    const { setting } = this.props;
afc163's avatar
afc163 committed
142 143
    const { navTheme, primaryColor, layout, colorWeak } = setting;
    const { collapse } = this.state;
jim's avatar
jim committed
144
    return (
陈帅's avatar
陈帅 committed
145 146
      <Drawer
        visible={collapse}
147
        width={300}
陈帅's avatar
陈帅 committed
148
        onClose={this.togglerContent}
陈帅's avatar
陈帅 committed
149
        placement="right"
陈帅's avatar
陈帅 committed
150
        handler={
Yu's avatar
Yu committed
151
          <div className={styles.handle} onClick={this.togglerContent}>
afc163's avatar
afc163 committed
152 153 154 155 156 157 158
            <Icon
              type={collapse ? 'close' : 'setting'}
              style={{
                color: '#fff',
                fontSize: 20,
              }}
            />
陈帅's avatar
陈帅 committed
159 160
          </div>
        }
陈帅's avatar
陈帅 committed
161 162 163 164 165
        style={{
          zIndex: 999,
        }}
      >
        <div className={styles.content}>
陈帅's avatar
陈帅 committed
166
          <Body title={formatMessage({ id: 'app.setting.pagestyle' })}>
167
            <BlockCheckbox
陈帅's avatar
陈帅 committed
168 169 170 171
              list={[
                {
                  key: 'dark',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg',
afc163's avatar
afc163 committed
172
                  title: formatMessage({ id: 'app.setting.pagestyle.dark' }),
陈帅's avatar
陈帅 committed
173 174 175 176
                },
                {
                  key: 'light',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg',
afc163's avatar
afc163 committed
177
                  title: formatMessage({ id: 'app.setting.pagestyle.light' }),
陈帅's avatar
陈帅 committed
178 179
                },
              ]}
afc163's avatar
afc163 committed
180 181
              value={navTheme}
              onChange={value => this.changeSetting('navTheme', value)}
陈帅's avatar
陈帅 committed
182 183
            />
          </Body>
jim's avatar
jim committed
184

185
          <ThemeColor
陈帅's avatar
陈帅 committed
186
            title={formatMessage({ id: 'app.setting.themecolor' })}
afc163's avatar
afc163 committed
187 188
            value={primaryColor}
            onChange={color => this.changeSetting('primaryColor', color)}
189
          />
jim's avatar
jim committed
190

陈帅's avatar
陈帅 committed
191
          <Divider />
jim's avatar
jim committed
192

陈帅's avatar
陈帅 committed
193
          <Body title={formatMessage({ id: 'app.setting.navigationmode' })}>
194
            <BlockCheckbox
陈帅's avatar
陈帅 committed
195 196 197 198
              list={[
                {
                  key: 'sidemenu',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/JopDzEhOqwOjeNTXkoje.svg',
afc163's avatar
afc163 committed
199
                  title: formatMessage({ id: 'app.setting.sidemenu' }),
陈帅's avatar
陈帅 committed
200 201 202 203
                },
                {
                  key: 'topmenu',
                  url: 'https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg',
afc163's avatar
afc163 committed
204
                  title: formatMessage({ id: 'app.setting.topmenu' }),
陈帅's avatar
陈帅 committed
205 206 207 208
                },
              ]}
              value={layout}
              onChange={value => this.changeSetting('layout', value)}
jim's avatar
jim committed
209
            />
陈帅's avatar
陈帅 committed
210
          </Body>
jim's avatar
jim committed
211

陈帅's avatar
陈帅 committed
212 213
          <List
            split={false}
afc163's avatar
afc163 committed
214
            dataSource={this.getLayoutSetting()}
afc163's avatar
afc163 committed
215
            renderItem={this.renderLayoutSettingItem}
陈帅's avatar
陈帅 committed
216
          />
jim's avatar
jim committed
217

陈帅's avatar
陈帅 committed
218 219
          <Divider />

陈帅's avatar
陈帅 committed
220
          <Body title={formatMessage({ id: 'app.setting.othersettings' })}>
陈帅's avatar
陈帅 committed
221 222 223 224 225 226 227 228 229
            <List.Item
              actions={[
                <Switch
                  size="small"
                  checked={!!colorWeak}
                  onChange={checked => this.changeSetting('colorWeak', checked)}
                />,
              ]}
            >
陈帅's avatar
陈帅 committed
230
              {formatMessage({ id: 'app.setting.weakmode' })}
陈帅's avatar
陈帅 committed
231 232
            </List.Item>
          </Body>
陈帅's avatar
陈帅 committed
233 234
          <Divider />
          <CopyToClipboard
afc163's avatar
afc163 committed
235
            text={JSON.stringify(omit(setting, ['colorWeak']), null, 2)}
陈帅's avatar
陈帅 committed
236
            onCopy={() => message.success(formatMessage({ id: 'app.setting.copyinfo' }))}
陈帅's avatar
陈帅 committed
237
          >
afc163's avatar
afc163 committed
238
            <Button block icon="copy">
陈帅's avatar
陈帅 committed
239
              {formatMessage({ id: 'app.setting.copy' })}
陈帅's avatar
陈帅 committed
240 241
            </Button>
          </CopyToClipboard>
afc163's avatar
afc163 committed
242 243 244
          <Alert
            type="warning"
            className={styles.productionHint}
afc163's avatar
afc163 committed
245 246 247 248 249 250 251 252 253 254 255 256
            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
257
          />
陈帅's avatar
陈帅 committed
258
        </div>
陈帅's avatar
陈帅 committed
259
      </Drawer>
jim's avatar
jim committed
260 261 262 263
    );
  }
}

264
export default SettingDrawer;