king
2023-06-14 08cce3334a2dc81d690b518136b0aaea64e48b0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Form, Input } from 'antd'
import { UserOutlined, LockOutlined } from '@ant-design/icons'
import zhCN from '@/locales/zh-CN/login.js'
import enUS from '@/locales/en-US/login.js'
import './index.scss'
 
class HeaderLoginForm extends Component {
  static propTpyes = {
    handleSubmit: PropTypes.func
  }
 
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS
  }
 
  handleConfirm = () => {
    // 表单提交时检查输入值是否正确
    return new Promise((resolve, reject) => {
      this.props.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          values.cloudusername = values.cloudusername.replace(/\t*|\v*|\s*/g, '')
          values.cloudpassword = values.cloudpassword.replace(/\t*|\v*|\s*/g, '')
          resolve(values)
        } else {
          reject(err)
        }
      })
    })
  }
 
  handleSubmit = (e, key) => {
    e.preventDefault()
    if (e.target.value) {
      if (!this.props.form.getFieldValue(key)) {
        const input = document.getElementById(key)
        if (input) {
          input.focus()
        }
        return
      }
      this.props.handleSubmit()
    } else {
      this.handleConfirm()
    }
  }
 
  componentDidMount () {
    const input = document.getElementById('cloudusername')
    if (input) {
      input.focus()
    }
  }
 
  render() {
    const { getFieldDecorator } = this.props.form
 
    return (
      <Form style={{margin: '0px 10px'}}>
        <Form.Item>
          {getFieldDecorator('cloudusername', {
            rules: [{ required: true, message: this.state.dict['login.username.empty'] }],
            initialValue: '',
          })(
            <Input
              prefix={<UserOutlined style={{ color: 'rgba(0,0,0,.25)' }} />}
              placeholder={this.state.dict['login.username']}
              autoComplete="off"
              onPressEnter={(e) => {this.handleSubmit(e, 'cloudpassword')}}
            />
          )}
        </Form.Item>
        <Form.Item>
          {getFieldDecorator('cloudpassword', {
            initialValue: '',
            rules: [
              {
                required: true,
                message: this.state.dict['login.password.empty'],
              }
            ]
          })(<Input.Password onPressEnter={(e) => {this.handleSubmit(e, 'cloudusername')}} placeholder={this.state.dict['login.password']} prefix={<LockOutlined style={{ color: 'rgba(0,0,0,.25)' }} />} />)}
        </Form.Item>
      </Form>
    )
  }
}
 
export default Form.create()(HeaderLoginForm)