choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
484 lines (426 loc) • 14.9 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import omit from 'lodash/omit';
import Icon from '../icon';
import { Size } from '../_util/enum';
import { getPrefixCls as _getPrefixCls } from '../configure';
function fixControlledValue(value) {
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
}
var Input =
/*#__PURE__*/
function (_Component) {
_inherits(Input, _Component);
var _super = _createSuper(Input);
function Input(props) {
var _this;
_classCallCheck(this, Input);
_this = _super.call(this, props);
_this.handleKeyDown = function (e) {
var _this$props = _this.props,
onPressEnter = _this$props.onPressEnter,
onKeyDown = _this$props.onKeyDown;
if (e.keyCode === 13 && onPressEnter) {
onPressEnter(e);
}
if (onKeyDown) {
onKeyDown(e);
}
};
_this.handleFocus = function (e) {
var _this$props2 = _this.props,
onFocus = _this$props2.onFocus,
readOnly = _this$props2.readOnly;
if (!readOnly) {
_this.setState({
focused: true
});
}
if (onFocus) {
onFocus(e);
}
};
_this.handleBlur = function (e) {
var _this$props3 = _this.props,
onBlur = _this$props3.onBlur,
readOnly = _this$props3.readOnly;
if (!readOnly) {
_this.setState({
focused: false
});
}
if (onBlur) {
onBlur(e);
}
};
_this.handleChange = function (e) {
var onChange = _this.props.onChange;
if (!('value' in _this.props)) {
_this.setState({
value: e.target.value
});
}
if (onChange) {
onChange(e);
}
};
_this.handleCopy = function () {
var onCopy = _this.props.onCopy;
_this.input.select();
document.execCommand('Copy');
if (onCopy) {
onCopy(_this.input.value);
}
};
_this.handleTogglePassword = function () {
var showPassword = _this.state.showPassword;
_this.setState({
showPassword: !showPassword
});
};
_this.saveInput = function (node) {
_this.input = node;
};
_this.saveRenderedRef = function (node) {
_this.rendered = node;
};
_this.savePrefix = function (node) {
_this.prefix = node;
};
_this.saveSuffix = function (node) {
_this.suffix = node;
};
_this.state = {
value: typeof props.value === 'undefined' ? props.defaultValue : props.value,
focused: false,
showPassword: false
};
return _this;
}
_createClass(Input, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this$props4 = this.props,
focused = _this$props4.focused,
autoFocus = _this$props4.autoFocus;
if (autoFocus) {
this.setState({
focused: true
});
}
if (typeof focused === 'boolean') {
this.setState({
focused: focused
});
}
this.setRenderedStyle();
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
var value = this.state.value;
if ('value' in nextProps && value !== nextProps.value) {
this.setState({
value: nextProps.value
});
}
if (nextProps.autoFocus) {
this.setState({
focused: true
});
}
if (typeof nextProps.focused === 'boolean') {
this.setState({
focused: nextProps.focused
});
}
if (nextProps.type !== 'password') {
this.setState({
showPassword: false
});
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.setRenderedStyle();
}
}, {
key: "setRenderedStyle",
value: function setRenderedStyle() {
var rendered = this.rendered,
suffix = this.suffix,
prefix = this.prefix;
var suffixWidth;
var prefixWidth;
var margin = '0';
var width = '100%';
if (suffix && prefix) {
suffixWidth = "".concat((suffix.clientWidth || -2) + 2, "px");
prefixWidth = "".concat((prefix.clientWidth || -2) + 2, "px");
margin = "0 ".concat(suffixWidth, " 0 ").concat(prefixWidth);
width = "calc(100% - ".concat(suffixWidth, " - ").concat(prefixWidth, ")");
} else if (suffix) {
suffixWidth = "".concat((suffix.clientWidth || -2) + 2, "px");
margin = "0 ".concat(suffixWidth, " 0 0");
width = "calc(100% - ".concat(suffixWidth, ")");
} else if (prefix) {
prefixWidth = "".concat((prefix.clientWidth || -2) + 2, "px");
margin = "0 0 0 ".concat(prefixWidth);
width = "calc(100% - ".concat(prefixWidth, ")");
}
rendered.style.margin = margin;
rendered.style.width = width;
}
}, {
key: "focus",
value: function focus() {
this.input.focus();
}
}, {
key: "blur",
value: function blur() {
this.input.blur();
}
}, {
key: "getPrefixCls",
value: function getPrefixCls() {
var prefixCls = this.props.prefixCls;
return _getPrefixCls('input', prefixCls);
}
}, {
key: "getInputClassName",
value: function getInputClassName() {
var _classNames;
var _this$props5 = this.props,
size = _this$props5.size,
copy = _this$props5.copy;
var prefixCls = this.getPrefixCls();
return classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-sm"), size === Size.small), _defineProperty(_classNames, "".concat(prefixCls, "-lg"), size === Size.large), _defineProperty(_classNames, "".concat(prefixCls, "-has-copy"), copy), _classNames));
}
}, {
key: "renderCopyIcon",
value: function renderCopyIcon() {
var copy = this.props.copy;
var prefixCls = this.getPrefixCls();
return copy ? React.createElement("span", {
className: "".concat(prefixCls, "-icon"),
onClick: this.handleCopy
}, React.createElement(Icon, {
className: "".concat(prefixCls, "-icon-copy"),
type: "library_books"
})) : null;
}
}, {
key: "renderShowPassword",
value: function renderShowPassword() {
var type = this.props.type;
var prefixCls = this.getPrefixCls();
var showPassword = this.state.showPassword;
return type === 'password' ? React.createElement("span", {
className: "".concat(prefixCls, "-icon"),
onClick: this.handleTogglePassword
}, React.createElement(Icon, {
className: "".concat(prefixCls, "-icon-copy"),
type: showPassword ? 'visibility' : 'visibility_off'
})) : null;
}
}, {
key: "renderInput",
value: function renderInput() {
var _this$props6 = this.props,
className = _this$props6.className,
type = _this$props6.type;
var _this$state = this.state,
showPassword = _this$state.showPassword,
value = _this$state.value; // Fix https://fb.me/react-unknown-prop
var otherProps = omit(this.props, ['placeholder', 'prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'label', 'copy', 'style', 'focused', 'showLengthInfo', 'showPasswordEye', 'size', 'border']);
return React.createElement("input", _extends({}, otherProps, {
value: fixControlledValue(value),
className: classNames(this.getInputClassName(), className),
onKeyDown: this.handleKeyDown,
ref: this.saveInput,
onFocus: this.handleFocus,
onBlur: this.handleBlur,
onChange: this.handleChange,
type: showPassword ? 'text' : type
}));
}
}, {
key: "getLengthInfo",
value: function getLengthInfo() {
var _this$props7 = this.props,
maxLength = _this$props7.maxLength,
showLengthInfo = _this$props7.showLengthInfo;
var prefixCls = this.getPrefixCls();
var value = this.state.value;
var inputLength = value ? value.length : 0;
return maxLength && showLengthInfo || maxLength && maxLength > 0 && inputLength === maxLength ? React.createElement("div", {
className: "".concat(prefixCls, "-length-info")
}, "".concat(inputLength, "/").concat(maxLength)) : null;
}
}, {
key: "getLabel",
value: function getLabel() {
var focused = this.state.focused;
var _this$props8 = this.props,
placeholder = _this$props8.placeholder,
label = _this$props8.label;
if (!this.hasValue() && !focused && placeholder) {
return placeholder;
}
return label;
}
}, {
key: "renderFloatLabel",
value: function renderFloatLabel() {
var label = this.getLabel();
var border = this.props.border;
if (label && border) {
var prefixCls = this.getPrefixCls();
return React.createElement("div", {
className: "".concat(prefixCls, "-label-wrapper")
}, React.createElement("div", {
className: "".concat(prefixCls, "-label")
}, label));
}
}
}, {
key: "getSizeClassName",
value: function getSizeClassName(name) {
var _classNames2;
var size = this.props.size;
var prefixCls = this.getPrefixCls();
return classNames("".concat(prefixCls, "-").concat(name), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-").concat(name, "-sm"), size === Size.small), _defineProperty(_classNames2, "".concat(prefixCls, "-").concat(name, "-lg"), size === Size.large), _classNames2));
}
}, {
key: "hasValue",
value: function hasValue() {
var value = this.state.value;
return value && value.length !== 0;
}
}, {
key: "renderPlaceholder",
value: function renderPlaceholder() {
var _this$props9 = this.props,
placeholder = _this$props9.placeholder,
border = _this$props9.border;
if (!border && placeholder) {
var prefixCls = this.getPrefixCls();
return React.createElement("div", {
className: "".concat(prefixCls, "-placeholder")
}, placeholder);
}
}
}, {
key: "render",
value: function render() {
var _classNames3;
var props = this.props;
var _this$props10 = this.props,
disabled = _this$props10.disabled,
label = _this$props10.label,
style = _this$props10.style,
showPasswordEye = _this$props10.showPasswordEye,
border = _this$props10.border;
var prefixCls = this.getPrefixCls();
var focused = this.state.focused;
var prefix = props.prefix ? React.createElement("span", {
ref: this.savePrefix,
className: this.getSizeClassName('prefix')
}, props.prefix) : null;
var suffix = props.suffix ? React.createElement("span", {
ref: this.saveSuffix,
className: this.getSizeClassName('suffix')
}, props.suffix) : null;
var className = classNames("".concat(prefixCls, "-wrapper"), (_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefixCls, "-has-value"), this.hasValue()), _defineProperty(_classNames3, "".concat(prefixCls, "-focused"), focused), _defineProperty(_classNames3, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames3, "".concat(prefixCls, "-has-label"), !!label), _defineProperty(_classNames3, "".concat(prefixCls, "-has-prefix"), !!prefix), _defineProperty(_classNames3, "".concat(prefixCls, "-has-suffix"), !!suffix), _defineProperty(_classNames3, "".concat(prefixCls, "-has-border"), border), _classNames3));
return React.createElement("span", {
className: className,
style: style
}, React.createElement("div", {
className: "".concat(prefixCls, "-content")
}, React.createElement("div", {
className: "".concat(prefixCls, "-rendered-wrapper")
}, prefix, React.createElement("div", {
className: this.getSizeClassName('rendered'),
ref: this.saveRenderedRef
}, this.renderPlaceholder(), this.renderInput(), this.renderFloatLabel(), this.renderCopyIcon(), showPasswordEye ? this.renderShowPassword() : null), suffix), this.getLengthInfo()));
}
}]);
return Input;
}(Component);
export { Input as default };
Input.displayName = 'Input';
Input.defaultProps = {
type: 'text',
disabled: false,
readOnly: false,
showLengthInfo: true,
showPasswordEye: false,
border: true
};
Input.propTypes = {
type: PropTypes.string,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
label: PropTypes.node,
size: PropTypes.oneOf([Size.small, Size["default"], Size.large]),
maxLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
disabled: PropTypes.bool,
value: PropTypes.any,
defaultValue: PropTypes.any,
className: PropTypes.string,
addonBefore: PropTypes.node,
addonAfter: PropTypes.node,
prefixCls: PropTypes.string,
autosize: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
onPressEnter: PropTypes.func,
onKeyDown: PropTypes.func,
onKeyUp: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
prefix: PropTypes.node,
suffix: PropTypes.node,
copy: PropTypes.bool,
onCopy: PropTypes.func,
readOnly: PropTypes.bool,
focused: PropTypes.bool,
border: PropTypes.bool,
showLengthInfo: PropTypes.bool,
showPasswordEye: PropTypes.bool
};
//# sourceMappingURL=Input.js.map