UNPKG

shineout

Version:

Shein 前端组件库

169 lines (136 loc) 5.77 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = void 0; var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _styles = require("../Input/styles"); var _element = require("../utils/dom/element"); var _utils = _interopRequireDefault(require("./utils")); var _styles2 = require("./styles"); var _locale = require("../locale"); var target = null; document.addEventListener('mousedown', function (e) { // eslint-disable-next-line prefer-destructuring target = e.target; }, true); var DefaultValue = { value: '' }; var Text = /*#__PURE__*/ function (_PureComponent) { (0, _inheritsLoose2.default)(Text, _PureComponent); function Text(props) { var _this; _this = _PureComponent.call(this, props) || this; (0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "element", void 0); _this.handleBlur = _this.handleBlur.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); _this.handleInput = _this.handleInput.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); _this.handleFocus = _this.handleFocus.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); _this.handleSpanClick = _this.handleSpanClick.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); _this.bindElement = _this.bindElement.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); return _this; } var _proto = Text.prototype; _proto.componentDidUpdate = function componentDidUpdate(prevProps) { if (prevProps.focus !== this.props.focus && this.props.focus && this.element && (this.props.focusElement === this.element || !this.props.focusElement)) { _element.focusElement.end(this.element); } }; _proto.getOptions = function getOptions() { var timeZone = this.props.timeZone; return { timeZone: timeZone, weekStartsOn: (0, _locale.getLocale)('startOfWeek') }; }; _proto.bindElement = function bindElement(el) { var onTextSpanRef = this.props.onTextSpanRef; this.element = el; if (onTextSpanRef) onTextSpanRef(el); }; _proto.handleBlur = function handleBlur(e) { var _this$props = this.props, format = _this$props.format, index = _this$props.index, onChange = _this$props.onChange, value = _this$props.value; var txt = e.target.innerText; if ((0, _element.getParent)(target, "." + (0, _styles2.datepickerClass)('picker'))) return; if (txt === value) return; if (txt.trim().length === 0) { onChange(undefined, index, e); } else { if (typeof format !== 'string') { console.error('formatResult should be string when inputable is true'); } var newValue = typeof format === 'string' ? _utils.default.toDateWithFormat(txt, format, undefined, this.getOptions()) : undefined; // if translate fail, clear if (!newValue) { // @ts-ignore this.element.innerText = null; } onChange(newValue, index, e); } }; _proto.handleFocus = function handleFocus(e) { var onTextSpanRef = this.props.onTextSpanRef; if (onTextSpanRef) onTextSpanRef(e.target); }; _proto.handleSpanClick = function handleSpanClick() { var onTextSpanRef = this.props.onTextSpanRef; if (onTextSpanRef && this.element) onTextSpanRef(this.element); }; _proto.handleInput = function handleInput(e) { if (e.keyCode === 13) { e.preventDefault(); e.stopPropagation(); this.element.blur(); this.handleBlur(e); // must wait for handleBlur to finish executing Promise.resolve().then(function () { document.dispatchEvent(new Event('mousedown', { bubbles: true })); }); } }; _proto.render = function render() { var _this$props2 = this.props, className = _this$props2.className, inputable = _this$props2.inputable, value = _this$props2.value, placeholder = _this$props2.placeholder, disabled = _this$props2.disabled, focus = _this$props2.focus; var showInput = inputable && !disabled && focus; var hideStyle = { display: 'none' }; return _react.default.createElement(_react.default.Fragment, null, inputable ? _react.default.createElement("span", { style: !showInput ? hideStyle : undefined, ref: this.bindElement, key: "edit", contentEditable: inputable, onBlur: this.handleBlur, onFocus: this.handleFocus, onKeyDown: this.handleInput, className: className, dangerouslySetInnerHTML: { __html: value } }) : null, _react.default.createElement("span", { style: showInput ? hideStyle : undefined, key: "placeholder", onClick: this.handleSpanClick, className: (0, _classnames.default)(!value && (0, _styles.inputClass)('placeholder'), className) }, value || placeholder)); }; return Text; }(_react.PureComponent); (0, _defineProperty2.default)(Text, "defaultProps", DefaultValue); var _default = Text; exports.default = _default;