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)
|