UNPKG

918 BJavaScriptView Raw
1import React, { PureComponent } from 'react'
2import PropTypes from 'prop-types'
3import Input from './Input'
4
5class Password extends PureComponent {
6 constructor(props) {
7 super(props)
8 this.handleChange = this.handleChange.bind(this)
9 }
10
11 handleChange(val) {
12 const { value, point } = this.props
13 const newValue = []
14 val.split('').forEach((v, i) => {
15 newValue.push(v === point ? value[i] : v)
16 })
17
18 this.props.onChange(newValue.join(''))
19 }
20
21 render() {
22 const { point, ...others } = this.props
23 const value = Array.from({ length: this.props.value.length }, () => point).join('')
24 return (
25 <Input {...others} type="text" value={value} onChange={this.handleChange} />
26 )
27 }
28}
29
30Password.propTypes = {
31 onChange: PropTypes.func,
32 point: PropTypes.string,
33 value: PropTypes.string,
34}
35
36Password.defaultProps = {
37 value: '',
38 point: '•',
39}
40
41export default Password