UNPKG

2.44 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6
7var _class, _temp2;
8
9import React from 'react';
10import PropTypes from 'prop-types';
11
12import Input from './input';
13import Icon from '../icon/index';
14
15function preventDefault(e) {
16 e.preventDefault();
17}
18var Password = (_temp2 = _class = function (_Input) {
19 _inherits(Password, _Input);
20
21 function Password() {
22 var _temp, _this, _ret;
23
24 _classCallCheck(this, Password);
25
26 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
27 args[_key] = arguments[_key];
28 }
29
30 return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Input.call.apply(_Input, [this].concat(args))), _this), _this.state = {
31 hint: 'eye-close',
32 htmlType: 'password'
33 }, _this.toggleEye = function (e) {
34 e.preventDefault();
35 if (_this.props.disabled) return;
36 var eyeClose = _this.state.hint === 'eye';
37
38 _this.setState({
39 hint: eyeClose ? 'eye-close' : 'eye',
40 htmlType: eyeClose || !_this.props.showToggle ? 'password' : 'text'
41 });
42 }, _temp), _possibleConstructorReturn(_this, _ret);
43 }
44
45 Password.prototype.render = function render() {
46 var _props = this.props,
47 showToggle = _props.showToggle,
48 others = _objectWithoutProperties(_props, ['showToggle']);
49
50 var _state = this.state,
51 hint = _state.hint,
52 htmlType = _state.htmlType;
53
54
55 var extra = showToggle ? React.createElement(Icon, { type: hint, onClick: this.toggleEye, onMouseDown: preventDefault }) : null;
56
57 return React.createElement(Input, _extends({}, others, { extra: extra, htmlType: htmlType }));
58 };
59
60 return Password;
61}(Input), _class.getDerivedStateFromProps = Input.getDerivedStateFromProps, _class.propTypes = _extends({}, Input.propTypes, {
62 /**
63 * 是否展示切换按钮
64 */
65 showToggle: PropTypes.bool
66}), _class.defaultProps = _extends({}, Input.defaultProps, {
67 showToggle: true
68}), _temp2);
69export { Password as default };
\No newline at end of file