|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import classnames from 'classnames'; |
| 4 | +import { withRouter } from 'react-router-dom'; |
| 5 | +import { observer } from 'mobx-react'; |
| 6 | +import { translate } from 'react-i18next'; |
| 7 | +import _ from 'lodash'; |
| 8 | +import Loading from 'components/Loading'; |
| 9 | + |
| 10 | +import { DocLink, Icon } from 'components/Base'; |
| 11 | + |
| 12 | +import styles from './index.scss'; |
| 13 | + |
| 14 | +@translate() |
| 15 | +@observer |
| 16 | +class LayoutStepper extends Component { |
| 17 | + static propTypes = { |
| 18 | + children: PropTypes.node, |
| 19 | + className: PropTypes.string, |
| 20 | + name: PropTypes.string, |
| 21 | + stepOption: PropTypes.shape({ |
| 22 | + activeStep: PropTypes.number, |
| 23 | + steps: PropTypes.number, |
| 24 | + prevStep: PropTypes.func, |
| 25 | + disableNextStep: PropTypes.bool, |
| 26 | + isLoading: PropTypes.bool, |
| 27 | + nextStep: PropTypes.func |
| 28 | + }) |
| 29 | + }; |
| 30 | + |
| 31 | + renderTopProgress() { |
| 32 | + const { stepOption } = this.props; |
| 33 | + const { steps, activeStep } = stepOption; |
| 34 | + const width = `${activeStep * 100 / steps}%`; |
| 35 | + const className = activeStep > steps ? 'headerStepNotFinished' : 'headerStepNotFinished'; |
| 36 | + |
| 37 | + const style = { |
| 38 | + width |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <div |
| 43 | + style={style} |
| 44 | + className={classnames(styles.headerStep, styles[className])} |
| 45 | + /> |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + renderTopNav() { |
| 50 | + const { |
| 51 | + name, stepOption, t, history |
| 52 | + } = this.props; |
| 53 | + const { |
| 54 | + activeStep, steps, prevStep, goBack |
| 55 | + } = stepOption; |
| 56 | + const funcBack = _.isFunction(goBack) ? goBack : history.goBack; |
| 57 | + |
| 58 | + if (activeStep > steps) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + let text = ''; |
| 63 | + if (activeStep > 1 && !!name) { |
| 64 | + text = t(`STEPPER_HEADER_${name.toUpperCase()}_${activeStep}`); |
| 65 | + } |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className={styles.operate}> |
| 69 | + {activeStep > 1 && ( |
| 70 | + <label onClick={prevStep}> |
| 71 | + ← {t('Back')} |
| 72 | + <span className={styles.operateText}>{text}</span> |
| 73 | + </label> |
| 74 | + )} |
| 75 | + <label className="pull-right" onClick={funcBack}> |
| 76 | + {t('Esc')} |
| 77 | + <Icon name="close" size={20} type="dark" /> |
| 78 | + </label> |
| 79 | + </div> |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + renderTitle() { |
| 84 | + const { name, stepOption, t } = this.props; |
| 85 | + const { activeStep, steps } = stepOption; |
| 86 | + |
| 87 | + if (activeStep > steps) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + const nameKey = name.toUpperCase(); |
| 92 | + const header = t(`STEPPER_NAME_${nameKey}_HEADER`, { |
| 93 | + activeStep, |
| 94 | + steps |
| 95 | + }); |
| 96 | + const title = t(`STEPPER_TITLE_${nameKey}_${activeStep}`); |
| 97 | + return ( |
| 98 | + <div className={classnames(styles.stepContent)}> |
| 99 | + <div className={styles.stepName}>{header}</div> |
| 100 | + <div className={styles.stepExplain}>{title}</div> |
| 101 | + </div> |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + renderFooter() { |
| 106 | + const { |
| 107 | + t, stepOption, name, disableNextStep |
| 108 | + } = this.props; |
| 109 | + const { activeStep, steps, nextStep } = stepOption; |
| 110 | + |
| 111 | + if (activeStep > steps) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + const keyName = `STEPPER_FOOTER_${name.toLocaleUpperCase()}_${activeStep}`; |
| 116 | + const tipText = t(keyName); |
| 117 | + const tipLink = <DocLink name={keyName} />; |
| 118 | + |
| 119 | + const buttonText = t('Go on'); |
| 120 | + |
| 121 | + return ( |
| 122 | + <div className={styles.footer}> |
| 123 | + <span className={styles.footerTips}> |
| 124 | + <span className={styles.footerTipsButton}>{t('Tips')}</span> |
| 125 | + {tipText} |
| 126 | + {tipLink} |
| 127 | + </span> |
| 128 | + <button |
| 129 | + className={classnames(styles.button, { |
| 130 | + [styles.buttonActived]: !disableNextStep |
| 131 | + })} |
| 132 | + type="primary" |
| 133 | + onClick={nextStep} |
| 134 | + > |
| 135 | + {/* |
| 136 | + {activeStep === steps && <Icon className={styles.icon} name="checked-icon" size={20} />} |
| 137 | + */} |
| 138 | + <span>{buttonText}</span> |
| 139 | + <Icon className={styles.icon} name="next-icon" size={20} /> |
| 140 | + {/* |
| 141 | + {activeStep !== steps && <Icon className={styles.icon} name="next-icon" size={20} />} |
| 142 | + */} |
| 143 | + </button> |
| 144 | + </div> |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + render() { |
| 149 | + const { className, children, stepOption } = this.props; |
| 150 | + return ( |
| 151 | + <div className={classnames(styles.layout)}> |
| 152 | + {this.renderTopProgress()} |
| 153 | + {this.renderTopNav()} |
| 154 | + {this.renderTitle()} |
| 155 | + <Loading isLoading={stepOption.isLoading}> |
| 156 | + <div className={className}>{children}</div> |
| 157 | + </Loading> |
| 158 | + {this.renderFooter()} |
| 159 | + </div> |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +export default withRouter(LayoutStepper); |
0 commit comments