choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
721 lines (611 loc) • 23.9 kB
JavaScript
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _isString = _interopRequireDefault(require("lodash/isString"));
var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
var _omit = _interopRequireDefault(require("lodash/omit"));
var _icon = _interopRequireDefault(require("../icon"));
var _enum = require("../_util/enum");
var _browser = require("../_util/browser");
var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));
function fixControlledValue(value) {
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
}
function isNeedTriggerAfterComposition() {
return (0, _browser.isChrome)() || (0, _browser.getIeVersion)() === 11;
}
function upperCaseString(value) {
if ((0, _isString["default"])(value)) {
return value.toUpperCase();
}
return undefined;
}
function lowerCaseString(value) {
if ((0, _isString["default"])(value)) {
return value.toLowerCase();
}
return undefined;
}
/**
* 判断是否全角
*/
function isDbc(s) {
var dbc = false;
if ((0, _isString["default"])(s)) {
for (var i = 0; i < s.length; i++) {
var c = s.charCodeAt(i);
if (c > 65248 || c === 12288) {
dbc = true;
break;
}
}
}
return dbc;
}
/**
* 全角转半角
*/
function dbcToSbc(str) {
var result = [];
if ((0, _isString["default"])(str)) {
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i); // 获取当前字符的unicode编码
if (code >= 65281 && code <= 65373) {
// 在这个unicode编码范围中的是所有的英文字母已及各种字符
result.push(String.fromCharCode(code - 65248)); // 把全角字符的unicode编码转换为对应半角字符的unicode码
} else if (code === 12288) {
// 空格
result.push(' ');
} else {
result.push(str.charAt(i));
}
}
}
return result.join('');
}
var Input = /*#__PURE__*/function (_Component) {
(0, _inherits2["default"])(Input, _Component);
var _super = (0, _createSuper2["default"])(Input);
function Input(props, context) {
var _this;
(0, _classCallCheck2["default"])(this, Input);
_this = _super.call(this, props, context);
_this.isOnComposition = false;
_this.handleComposition = function (e) {
if (e.type === 'compositionend') {
// composition is end
_this.isOnComposition = false;
if (isNeedTriggerAfterComposition()) {
_this.handleChange(e);
}
} else {
// in composition
_this.isOnComposition = true;
}
};
_this.handleFocus = function (e) {
var _this$props = _this.props,
onFocus = _this$props.onFocus,
readOnly = _this$props.readOnly;
if (!readOnly) {
_this.setState({
focused: true
});
}
if (onFocus) {
onFocus(e);
}
};
_this.handleBlur = function (e) {
var value = e.target.value;
var _this$props2 = _this.props,
onChange = _this$props2.onChange,
trim = _this$props2.trim,
trimAll = _this$props2.trimAll,
onBlur = _this$props2.onBlur,
readOnly = _this$props2.readOnly;
if (!readOnly) {
_this.setState({
focused: false
});
}
var trimValue = value;
if (trim && (0, _isString["default"])(value)) {
trimValue = value.trim();
}
if (trimAll && (0, _isString["default"])(value)) {
trimValue = value.replace(/\s/g, '');
}
if (trimValue !== value) {
_this.input.value = trimValue;
if (onChange && (0, _isFunction["default"])(onChange)) {
e.target.value = trimValue;
onChange(e);
}
}
if (onBlur && (0, _isFunction["default"])(onBlur)) {
onBlur(e);
}
};
_this.handleChange = function (e) {
var onChange = _this.props.onChange;
if (!_this.isOnComposition) {
// 在 onChange 时记录光标的位置
if (_this.input) {
_this.inputSelection = {
start: _this.input.selectionStart,
end: _this.input.selectionEnd
};
}
var transformValue = _this.transformValue(e.target.value);
if (transformValue !== e.target.value) {
e.target.value = _this.transformValue(e.target.value);
if (_this.inputSelection && (_this.inputSelection.start || _this.inputSelection.end)) {
e.target.setSelectionRange(_this.inputSelection.start, _this.inputSelection.end);
_this.inputSelection = null;
}
}
}
if (!('value' in _this.props)) {
_this.setState({
value: e.target.value
});
}
if (onChange && (0, _isFunction["default"])(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.handleShowPassword = function () {
_this.setState({
showPassword: true
});
};
_this.handleHidePassword = function () {
_this.setState({
showPassword: false
});
};
_this.handleKeyDown = function (e) {
var _this$props3 = _this.props,
onPressEnter = _this$props3.onPressEnter,
onKeyDown = _this$props3.onKeyDown;
if (e.keyCode === 13 && onPressEnter) {
onPressEnter(e);
}
if (onKeyDown) {
onKeyDown(e);
}
};
_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;
}
(0, _createClass2["default"])(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(prevProps) {
var inputSelection = this.inputSelection;
var value = prevProps.value;
if (inputSelection && value && !this.isOnComposition) {
// 在 didUpdate 时根据情况恢复光标的位置
// 如果光标的位置小于值的长度,那么可以判定属于中间编辑的情况
// 此时恢复光标的位置
// 当如果不是 onComposiotionend 触发的事件时不用修改位置
if (inputSelection.start && this.transformValue(value).length && inputSelection.start < this.transformValue(value).length) {
var input = this.input;
input.selectionStart = inputSelection.start;
input.selectionEnd = inputSelection.end;
this.inputSelection = null;
}
}
this.setRenderedStyle();
}
}, {
key: "setRenderedStyle",
value: function setRenderedStyle() {
var rendered = this.rendered,
input = this.input,
suffix = this.suffix,
prefix = this.prefix;
if (rendered || input) {
var suffixWidth;
var prefixWidth;
var marginRight = '';
var marginLeft = '';
var width = '100%';
if (suffix && prefix) {
suffixWidth = "".concat((suffix.clientWidth || -2) + 2, "px");
prefixWidth = "".concat((prefix.clientWidth || -2) + 2, "px");
marginRight = suffixWidth;
marginLeft = prefixWidth;
width = "calc(100% - ".concat(suffixWidth, " - ").concat(prefixWidth, ")");
} else if (suffix) {
suffixWidth = "".concat((suffix.clientWidth || -2) + 2, "px");
marginRight = suffixWidth;
width = "calc(100% - ".concat(suffixWidth, ")");
} else if (prefix) {
prefixWidth = "".concat((prefix.clientWidth || -2) + 2, "px");
marginLeft = prefixWidth;
width = "calc(100% - ".concat(prefixWidth, ")");
}
if (rendered) {
rendered.style.marginRight = marginRight;
rendered.style.marginLeft = marginLeft;
rendered.style.width = width;
} else if (input) {
input.style.paddingRight = marginRight;
input.style.paddingLeft = marginLeft;
}
}
}
}, {
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;
var getPrefixCls = this.context.getPrefixCls;
return getPrefixCls('input', prefixCls);
}
}, {
key: "getInputClassName",
value: function getInputClassName() {
var _classNames;
var _this$props5 = this.props,
size = _this$props5.size,
copy = _this$props5.copy,
disabled = _this$props5.disabled,
typeCase = _this$props5.typeCase,
showPasswordEye = _this$props5.showPasswordEye,
type = _this$props5.type;
var prefixCls = this.getPrefixCls();
return (0, _classnames["default"])(prefixCls, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-sm"), size === _enum.Size.small), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-lg"), size === _enum.Size.large), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-upper"), typeCase === 'upper'), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-lower"), typeCase === 'lower'), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-has-copy"), copy), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-has-eye"), showPasswordEye && type === 'password'), _classNames));
}
}, {
key: "transformValue",
value: function transformValue(v) {
var _this$props6 = this.props,
typeCase = _this$props6.typeCase,
_this$props6$dbc2sbc = _this$props6.dbc2sbc,
dbc2sbc = _this$props6$dbc2sbc === void 0 ? true : _this$props6$dbc2sbc,
inputChinese = _this$props6.inputChinese;
var value = v;
if (typeCase === 'upper') {
value = upperCaseString(v);
} else if (typeCase === 'lower') {
value = lowerCaseString(v);
}
if (dbc2sbc && isDbc(v)) {
value = dbcToSbc(v);
}
if (!inputChinese && (0, _isString["default"])(value)) {
value = value.replace(/[\u4e00-\u9fa5]/g, '');
}
return value;
}
}, {
key: "renderCopyIcon",
value: function renderCopyIcon(prefixCls) {
var copy = this.props.copy;
return copy ? /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefixCls, "-icon "),
onClick: this.handleCopy
}, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
className: "".concat(prefixCls, "-icon-copy"),
type: "library_books"
})) : null;
}
}, {
key: "renderShowPassword",
value: function renderShowPassword(prefixCls) {
var _this$props7 = this.props,
type = _this$props7.type,
showPasswordEye = _this$props7.showPasswordEye;
if (showPasswordEye && type === 'password') {
var showPassword = this.state.showPassword;
var props = {};
if (showPasswordEye === 'nohold') {
props.onMouseDown = this.handleShowPassword;
props.onMouseLeave = this.handleHidePassword;
props.onMouseUp = this.handleHidePassword;
} else {
props.onClick = this.handleTogglePassword;
}
return /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({
className: "".concat(prefixCls, "-icon ").concat(prefixCls, "-icon-eye")
}, props), /*#__PURE__*/_react["default"].createElement(_icon["default"], {
className: "".concat(prefixCls, "-icon-copy"),
type: showPassword ? 'visibility' : 'visibility_off'
}));
}
}
}, {
key: "getLengthInfo",
value: function getLengthInfo(prefixCls) {
var _this$props8 = this.props,
maxLength = _this$props8.maxLength,
showLengthInfo = _this$props8.showLengthInfo;
var value = this.state.value;
var inputLength = value ? value.length : 0;
return showLengthInfo !== 'never' && (maxLength && showLengthInfo === true || maxLength && maxLength > 0 && inputLength === maxLength) ? /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefixCls, "-length-info")
}, "".concat(inputLength, "/").concat(maxLength)) : null;
}
}, {
key: "renderFloatLabel",
value: function renderFloatLabel(prefixCls) {
var label = this.props.label;
if (label) {
return /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefixCls, "-label-wrapper")
}, /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefixCls, "-label")
}, label));
}
}
}, {
key: "getSizeClassName",
value: function getSizeClassName(name, prefixCls) {
var _classNames2;
var size = this.props.size;
return (0, _classnames["default"])("".concat(prefixCls, "-").concat(name), (_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-").concat(name, "-sm"), size === _enum.Size.small), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-").concat(name, "-lg"), size === _enum.Size.large), _classNames2));
}
}, {
key: "hasValue",
value: function hasValue() {
var value = this.state.value;
return value && value.length !== 0;
}
}, {
key: "renderLabeledIcon",
value: function renderLabeledIcon(children, prefixCls) {
var props = this.props;
var hasBorder = props.border && props.labelLayout === 'float';
var passwordEye = this.renderShowPassword(prefixCls);
var copyIcon = this.renderCopyIcon(prefixCls);
var floatLabel = hasBorder && this.renderFloatLabel(prefixCls);
var className = props.className;
var prefix = props.prefix ? /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefixCls, "-prefix"),
ref: this.savePrefix
}, props.prefix) : null;
var $suffix = props.showPasswordEye === 'nohold' && passwordEye ? passwordEye : props.suffix;
var $passwordEye = props.showPasswordEye === 'nohold' ? undefined : passwordEye;
var suffix = $suffix ? /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefixCls, "-suffix"),
ref: this.saveSuffix
}, $suffix) : null;
if (hasBorder) {
var focused = this.state.focused;
var preProps = children.props;
children = /*#__PURE__*/(0, _react.cloneElement)(children, {
className: (0, _classnames["default"])(preProps.className, className),
placeholder: !floatLabel || focused ? preProps.placeholder : null
});
}
if ($passwordEye || copyIcon || floatLabel) {
children = /*#__PURE__*/_react["default"].createElement("div", {
className: this.getSizeClassName('rendered', prefixCls),
ref: this.saveRenderedRef
}, children, floatLabel, copyIcon, $passwordEye);
}
if (prefix || suffix) {
var _classNames3;
var affixWrapperCls = (0, _classnames["default"])(this.getSizeClassName('affix-wrapper', prefixCls), (_classNames3 = {}, (0, _defineProperty2["default"])(_classNames3, "".concat(className), className && !hasBorder), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-border"), hasBorder), _classNames3));
return /*#__PURE__*/_react["default"].createElement("span", {
className: affixWrapperCls,
style: props.style
}, prefix, /*#__PURE__*/(0, _react.cloneElement)(children, {
style: null
}), suffix);
}
if (hasBorder) {
return /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefixCls, "-has-border"),
style: props.style
}, /*#__PURE__*/(0, _react.cloneElement)(children, {
style: null
}));
}
return /*#__PURE__*/(0, _react.cloneElement)(children, {
className: (0, _classnames["default"])(className, children.props.className),
style: props.style
});
}
}, {
key: "renderInput",
value: function renderInput(prefixCls) {
var type = this.props.type;
var _this$state = this.state,
value = _this$state.value,
showPassword = _this$state.showPassword;
var omits = ['prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'label', 'labelLayout', 'copy', 'style', 'focused', 'showLengthInfo', 'showPasswordEye', 'size', 'border', 'form', 'onChange', 'dbc2sbc', 'typeCase', 'trim', 'trimAll', 'inputChinese', 'type'];
var otherProps = (0, _omit["default"])(this.props, omits);
return this.renderLabeledIcon( /*#__PURE__*/_react["default"].createElement("input", (0, _extends2["default"])({}, otherProps, {
value: fixControlledValue(value),
className: this.getInputClassName(),
onKeyDown: this.handleKeyDown,
ref: this.saveInput,
onFocus: this.handleFocus,
onBlur: this.handleBlur,
onChange: this.handleChange,
onCompositionStart: this.handleComposition,
onCompositionUpdate: this.handleComposition,
onCompositionEnd: this.handleComposition,
type: showPassword ? 'text' : type
})), prefixCls);
}
}, {
key: "getWrapperClassName",
value: function getWrapperClassName(prefixCls) {
var _classNames4;
var _this$props9 = this.props,
disabled = _this$props9.disabled,
label = _this$props9.label,
prefix = _this$props9.prefix,
suffix = _this$props9.suffix;
var focused = this.state.focused;
return (0, _classnames["default"])((_classNames4 = {}, (0, _defineProperty2["default"])(_classNames4, "".concat(prefixCls, "-has-value"), this.hasValue()), (0, _defineProperty2["default"])(_classNames4, "".concat(prefixCls, "-focused"), focused), (0, _defineProperty2["default"])(_classNames4, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames4, "".concat(prefixCls, "-has-label"), !!label), (0, _defineProperty2["default"])(_classNames4, "".concat(prefixCls, "-has-prefix"), !!prefix), (0, _defineProperty2["default"])(_classNames4, "".concat(prefixCls, "-has-suffix"), !!suffix), _classNames4));
}
}, {
key: "renderLabeledInput",
value: function renderLabeledInput(children, prefixCls) {
var props = this.props;
var wrapperClassName = "".concat(prefixCls, "-group");
var addonClassName = "".concat(wrapperClassName, "-addon");
var addonBefore = props.addonBefore ? /*#__PURE__*/_react["default"].createElement("span", {
className: addonClassName
}, props.addonBefore) : null;
var addonAfter = props.addonAfter ? /*#__PURE__*/_react["default"].createElement("span", {
className: addonClassName
}, props.addonAfter) : null;
var lengthInfo = this.getLengthInfo(prefixCls);
var className = (0, _classnames["default"])("".concat(prefixCls, "-wrapper"), this.getWrapperClassName(prefixCls), (0, _defineProperty2["default"])({}, wrapperClassName, addonBefore || addonAfter));
var groupClassName = this.getSizeClassName('group-wrapper', prefixCls); // Need another wrapper for changing display:table to display:inline-block
// and put style prop in wrapper
if (addonBefore || addonAfter) {
return /*#__PURE__*/_react["default"].createElement("span", {
className: groupClassName,
style: props.style
}, /*#__PURE__*/_react["default"].createElement("span", {
className: className
}, addonBefore, /*#__PURE__*/(0, _react.cloneElement)(children, {
style: null
}), addonAfter), lengthInfo);
}
if (lengthInfo) {
return /*#__PURE__*/_react["default"].createElement("span", {
className: className,
style: props.style
}, /*#__PURE__*/(0, _react.cloneElement)(children, {
style: null
}), lengthInfo);
}
return /*#__PURE__*/(0, _react.cloneElement)(children, {
className: (0, _classnames["default"])(children.props.className, className)
});
}
}, {
key: "render",
value: function render() {
var prefixCls = this.getPrefixCls();
return this.renderLabeledInput(this.renderInput(prefixCls), prefixCls);
}
}], [{
key: "contextType",
get: function get() {
return _ConfigContext["default"];
}
}]);
return Input;
}(_react.Component);
exports["default"] = Input;
Input.displayName = 'Input';
Input.defaultProps = {
type: 'text',
disabled: false,
readOnly: false,
showLengthInfo: true,
showPasswordEye: false,
border: true,
dbc2sbc: false,
trim: false,
trimAll: false,
inputChinese: true,
labelLayout: 'float'
};
//# sourceMappingURL=Input.js.map