ResetPasswordForm.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import React from 'react'
  2. import { Col, Row, Input, Button, Form } from 'antd'
  3. import { FormComponentProps } from 'antd/lib/form/Form'
  4. const FormItem = Form.Item
  5. const styles = require('../Profile/profile.less')
  6. interface IResetPasswordProps {
  7. submit: () => any
  8. }
  9. export class ResetPasswordForm extends React.PureComponent<IResetPasswordProps & FormComponentProps, {}> {
  10. public componentDidMount () {
  11. this.props.form.validateFields()
  12. }
  13. private checkPasswordConfirm = (rule, value, callback) => {
  14. if (value && value !== this.props.form.getFieldValue('password')) {
  15. callback('两次输入的密码不一致')
  16. } else {
  17. callback()
  18. }
  19. }
  20. private forceCheckConfirm = (rule, value, callback) => {
  21. const { form } = this.props
  22. if (form.getFieldValue('confirmPassword')) {
  23. form.validateFields(['confirmPassword'], { force: true })
  24. }
  25. callback()
  26. }
  27. private hasErrors = (fieldsError) => {
  28. return Object.keys(fieldsError).some((field) => fieldsError[field])
  29. }
  30. public render () {
  31. const { getFieldDecorator, getFieldsError, getFieldError, isFieldTouched } = this.props.form
  32. const commonFormItemStyle = {
  33. labelCol: { span: 4 },
  34. wrapperCol: { span: 18 }
  35. }
  36. const oldPassError = isFieldTouched('oldPassword') && getFieldError('oldPassword')
  37. const newPassError = isFieldTouched('password') && getFieldError('password')
  38. const confirmPasswordError = isFieldTouched('confirmPassword') && getFieldError('confirmPassword')
  39. const isSubmit = this.hasErrors(getFieldsError())
  40. return (
  41. <Form className={styles.formView}>
  42. <Row>
  43. <Col>
  44. <FormItem
  45. {...commonFormItemStyle}
  46. className={styles.hide}
  47. >
  48. {getFieldDecorator('id', {})(
  49. <Input />
  50. )}
  51. </FormItem>
  52. <FormItem
  53. label="旧密码"
  54. {...commonFormItemStyle}
  55. validateStatus={oldPassError ? 'error' : 'success'}
  56. help={oldPassError || ''}
  57. >
  58. {getFieldDecorator('oldPassword', {
  59. rules: [{
  60. required: true,
  61. message: '旧密码不能为空'
  62. }]
  63. })(
  64. <Input type="password" placeholder="旧密码" />
  65. )}
  66. </FormItem>
  67. </Col>
  68. <Col>
  69. <FormItem
  70. label="新密码"
  71. {...commonFormItemStyle}
  72. validateStatus={newPassError ? 'error' : 'success'}
  73. help={newPassError || ''}
  74. >
  75. {getFieldDecorator('password', {
  76. rules: [{
  77. required: true,
  78. message: '新密码不能为空'
  79. }, {
  80. min: 6,
  81. max: 20,
  82. message: '密码长度为6-20位'
  83. }, {
  84. validator: this.forceCheckConfirm
  85. }]
  86. })(
  87. <Input type="password" placeholder="新密码" />
  88. )}
  89. </FormItem>
  90. </Col>
  91. <Col>
  92. <FormItem
  93. label="确认新密码"
  94. {...commonFormItemStyle}
  95. validateStatus={confirmPasswordError ? 'error' : 'success'}
  96. help={confirmPasswordError || ''}
  97. >
  98. {getFieldDecorator('confirmPassword', {
  99. rules: [{
  100. required: true,
  101. message: '请确认密码'
  102. }, {
  103. validator: this.checkPasswordConfirm
  104. }]
  105. })(
  106. <Input type="password" placeholder="确认密码" />
  107. )}
  108. </FormItem>
  109. </Col>
  110. <Col offset={4}>
  111. <Button size="large" type="primary" disabled={isSubmit} onClick={this.props.submit}>确认修改</Button>
  112. </Col>
  113. </Row>
  114. </Form>
  115. )
  116. }
  117. }
  118. export default Form.create<IResetPasswordProps & FormComponentProps>()(ResetPasswordForm)