UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

510 lines (439 loc) 16.8 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); 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 _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _classnames = _interopRequireDefault(require("classnames")); var _omit = _interopRequireDefault(require("lodash/omit")); var _icon = _interopRequireDefault(require("../icon")); var _enum = require("../_util/enum"); var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext")); 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 = (0, _getPrototypeOf2["default"])(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; } function fixControlledValue(value) { if (typeof value === 'undefined' || value === null) { return ''; } return value; } var Input = /*#__PURE__*/ function (_Component) { (0, _inherits2["default"])(Input, _Component); var _super = _createSuper(Input); function Input(props, context) { var _this; (0, _classCallCheck2["default"])(this, Input); _this = _super.call(this, props, context); _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; } (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() { 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; 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; 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, "-has-copy"), copy), _classNames)); } }, { key: "renderCopyIcon", value: function renderCopyIcon() { var copy = this.props.copy; var prefixCls = this.getPrefixCls(); return copy ? _react["default"].createElement("span", { className: "".concat(prefixCls, "-icon"), onClick: this.handleCopy }, _react["default"].createElement(_icon["default"], { 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["default"].createElement("span", { className: "".concat(prefixCls, "-icon"), onClick: this.handleTogglePassword }, _react["default"].createElement(_icon["default"], { 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 = (0, _omit["default"])(this.props, ['placeholder', 'prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'label', 'copy', 'style', 'focused', 'showLengthInfo', 'showPasswordEye', 'size', 'border']); return _react["default"].createElement("input", (0, _extends2["default"])({}, otherProps, { value: fixControlledValue(value), className: (0, _classnames["default"])(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["default"].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["default"].createElement("div", { className: "".concat(prefixCls, "-label-wrapper") }, _react["default"].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 (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: "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["default"].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["default"].createElement("span", { ref: this.savePrefix, className: this.getSizeClassName('prefix') }, props.prefix) : null; var suffix = props.suffix ? _react["default"].createElement("span", { ref: this.saveSuffix, className: this.getSizeClassName('suffix') }, props.suffix) : null; var className = (0, _classnames["default"])("".concat(prefixCls, "-wrapper"), (_classNames3 = {}, (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-value"), this.hasValue()), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-focused"), focused), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-label"), !!label), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-prefix"), !!prefix), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-suffix"), !!suffix), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-has-border"), border), _classNames3)); return _react["default"].createElement("span", { className: className, style: style }, _react["default"].createElement("div", { className: "".concat(prefixCls, "-content") }, _react["default"].createElement("div", { className: "".concat(prefixCls, "-rendered-wrapper") }, prefix, _react["default"].createElement("div", { className: this.getSizeClassName('rendered'), ref: this.saveRenderedRef }, this.renderPlaceholder(), this.renderInput(), this.renderFloatLabel(), this.renderCopyIcon(), showPasswordEye ? this.renderShowPassword() : null), suffix), this.getLengthInfo())); } }], [{ 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 }; Input.propTypes = { type: _propTypes["default"].string, id: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]), label: _propTypes["default"].node, size: _propTypes["default"].oneOf([_enum.Size.small, _enum.Size["default"], _enum.Size.large]), maxLength: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]), disabled: _propTypes["default"].bool, value: _propTypes["default"].any, defaultValue: _propTypes["default"].any, className: _propTypes["default"].string, addonBefore: _propTypes["default"].node, addonAfter: _propTypes["default"].node, prefixCls: _propTypes["default"].string, autosize: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].object]), onPressEnter: _propTypes["default"].func, onKeyDown: _propTypes["default"].func, onKeyUp: _propTypes["default"].func, onFocus: _propTypes["default"].func, onBlur: _propTypes["default"].func, prefix: _propTypes["default"].node, suffix: _propTypes["default"].node, copy: _propTypes["default"].bool, onCopy: _propTypes["default"].func, readOnly: _propTypes["default"].bool, focused: _propTypes["default"].bool, border: _propTypes["default"].bool, showLengthInfo: _propTypes["default"].bool, showPasswordEye: _propTypes["default"].bool }; //# sourceMappingURL=Input.js.map