index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import React, { PureComponent } from 'react';
  2. import { Select, message, Drawer, List, Switch, Divider, Icon, Button, Alert, Tooltip } from 'antd';
  3. import { formatMessage } from 'umi/locale';
  4. import { CopyToClipboard } from 'react-copy-to-clipboard';
  5. import { connect } from 'dva';
  6. import omit from 'omit.js';
  7. import styles from './index.less';
  8. import ThemeColor from './ThemeColor';
  9. import BlockCheckbox from './BlockCheckbox';
  10. const { Option } = Select;
  11. const Body = ({ children, title, style }) => (
  12. <div
  13. style={{
  14. ...style,
  15. marginBottom: 24,
  16. }}
  17. >
  18. <h3 className={styles.title}>{title}</h3>
  19. {children}
  20. </div>
  21. );
  22. @connect(({ setting }) => ({ setting }))
  23. class SettingDrawer extends PureComponent {
  24. state = {
  25. collapse: false,
  26. };
  27. getLayoutSetting = () => {
  28. const {
  29. setting: { contentWidth, fixedHeader, layout, autoHideHeader, fixSiderbar },
  30. } = this.props;
  31. return [
  32. {
  33. title: formatMessage({ id: 'app.setting.content-width' }),
  34. action: (
  35. <Select
  36. value={contentWidth}
  37. size="small"
  38. onSelect={value => this.changeSetting('contentWidth', value)}
  39. style={{ width: 80 }}
  40. getPopupContainer={triggerNode => triggerNode.parentNode}
  41. >
  42. {layout === 'sidemenu' ? null : (
  43. <Option value="Fixed">
  44. {formatMessage({ id: 'app.setting.content-width.fixed' })}
  45. </Option>
  46. )}
  47. <Option value="Fluid">
  48. {formatMessage({ id: 'app.setting.content-width.fluid' })}
  49. </Option>
  50. </Select>
  51. ),
  52. },
  53. {
  54. title: formatMessage({ id: 'app.setting.fixedheader' }),
  55. action: (
  56. <Switch
  57. size="small"
  58. checked={!!fixedHeader}
  59. onChange={checked => this.changeSetting('fixedHeader', checked)}
  60. />
  61. ),
  62. },
  63. {
  64. title: formatMessage({ id: 'app.setting.hideheader' }),
  65. disabled: !fixedHeader,
  66. disabledReason: formatMessage({ id: 'app.setting.hideheader.hint' }),
  67. action: (
  68. <Switch
  69. size="small"
  70. checked={!!autoHideHeader}
  71. onChange={checked => this.changeSetting('autoHideHeader', checked)}
  72. />
  73. ),
  74. },
  75. {
  76. title: formatMessage({ id: 'app.setting.fixedsidebar' }),
  77. disabled: layout === 'topmenu',
  78. disabledReason: formatMessage({ id: 'app.setting.fixedsidebar.hint' }),
  79. action: (
  80. <Switch
  81. size="small"
  82. checked={!!fixSiderbar}
  83. onChange={checked => this.changeSetting('fixSiderbar', checked)}
  84. />
  85. ),
  86. },
  87. ];
  88. };
  89. changeSetting = (key, value) => {
  90. const { setting } = this.props;
  91. const nextState = { ...setting };
  92. nextState[key] = value;
  93. if (key === 'layout') {
  94. nextState.contentWidth = value === 'topmenu' ? 'Fixed' : 'Fluid';
  95. } else if (key === 'fixedHeader' && !value) {
  96. nextState.autoHideHeader = false;
  97. }
  98. this.setState(nextState, () => {
  99. const { dispatch } = this.props;
  100. dispatch({
  101. type: 'setting/changeSetting',
  102. payload: this.state,
  103. });
  104. });
  105. };
  106. togglerContent = () => {
  107. const { collapse } = this.state;
  108. this.setState({ collapse: !collapse });
  109. };
  110. renderLayoutSettingItem = item => {
  111. const action = React.cloneElement(item.action, {
  112. disabled: item.disabled,
  113. });
  114. return (
  115. <Tooltip title={item.disabled ? item.disabledReason : ''} placement="left">
  116. <List.Item actions={[action]}>
  117. <span style={{ opacity: item.disabled ? '0.5' : '' }}>{item.title}</span>
  118. </List.Item>
  119. </Tooltip>
  120. );
  121. };
  122. render() {
  123. const { setting } = this.props;
  124. const { navTheme, primaryColor, layout, colorWeak } = setting;
  125. const { collapse } = this.state;
  126. return (
  127. <Drawer
  128. visible={collapse}
  129. width={300}
  130. onClose={this.togglerContent}
  131. placement="right"
  132. handler={
  133. <div className={styles.handle}>
  134. <Icon
  135. type={collapse ? 'close' : 'setting'}
  136. style={{
  137. color: '#fff',
  138. fontSize: 20,
  139. }}
  140. />
  141. </div>
  142. }
  143. onHandleClick={this.togglerContent}
  144. style={{
  145. zIndex: 999,
  146. }}
  147. >
  148. <div className={styles.content}>
  149. <Body title={formatMessage({ id: 'app.setting.pagestyle' })}>
  150. <BlockCheckbox
  151. list={[
  152. {
  153. key: 'dark',
  154. url: 'https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg',
  155. title: formatMessage({ id: 'app.setting.pagestyle.dark' }),
  156. },
  157. {
  158. key: 'light',
  159. url: 'https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg',
  160. title: formatMessage({ id: 'app.setting.pagestyle.light' }),
  161. },
  162. ]}
  163. value={navTheme}
  164. onChange={value => this.changeSetting('navTheme', value)}
  165. />
  166. </Body>
  167. <ThemeColor
  168. title={formatMessage({ id: 'app.setting.themecolor' })}
  169. value={primaryColor}
  170. onChange={color => this.changeSetting('primaryColor', color)}
  171. />
  172. <Divider />
  173. <Body title={formatMessage({ id: 'app.setting.navigationmode' })}>
  174. <BlockCheckbox
  175. list={[
  176. {
  177. key: 'sidemenu',
  178. url: 'https://gw.alipayobjects.com/zos/rmsportal/JopDzEhOqwOjeNTXkoje.svg',
  179. title: formatMessage({ id: 'app.setting.sidemenu' }),
  180. },
  181. {
  182. key: 'topmenu',
  183. url: 'https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg',
  184. title: formatMessage({ id: 'app.setting.topmenu' }),
  185. },
  186. ]}
  187. value={layout}
  188. onChange={value => this.changeSetting('layout', value)}
  189. />
  190. </Body>
  191. <List
  192. split={false}
  193. dataSource={this.getLayoutSetting()}
  194. renderItem={this.renderLayoutSettingItem}
  195. />
  196. <Divider />
  197. <Body title={formatMessage({ id: 'app.setting.othersettings' })}>
  198. <List.Item
  199. actions={[
  200. <Switch
  201. size="small"
  202. checked={!!colorWeak}
  203. onChange={checked => this.changeSetting('colorWeak', checked)}
  204. />,
  205. ]}
  206. >
  207. {formatMessage({ id: 'app.setting.weakmode' })}
  208. </List.Item>
  209. </Body>
  210. <Divider />
  211. <CopyToClipboard
  212. text={JSON.stringify(omit(setting, ['colorWeak']), null, 2)}
  213. onCopy={() => message.success(formatMessage({ id: 'app.setting.copyinfo' }))}
  214. >
  215. <Button block icon="copy">
  216. {formatMessage({ id: 'app.setting.copy' })}
  217. </Button>
  218. </CopyToClipboard>
  219. <Alert
  220. type="warning"
  221. className={styles.productionHint}
  222. message={
  223. <div>
  224. {formatMessage({ id: 'app.setting.production.hint' })}{' '}
  225. <a
  226. href="https://u.ant.design/pro-v2-default-settings"
  227. target="_blank"
  228. rel="noopener noreferrer"
  229. >
  230. src/defaultSettings.js
  231. </a>
  232. </div>
  233. }
  234. />
  235. </div>
  236. </Drawer>
  237. );
  238. }
  239. }
  240. export default SettingDrawer;