UNPKG

frc-ui

Version:

React Web UI

213 lines (162 loc) 8.31 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _input = _interopRequireDefault(require("antd/es/input")); var _icon = _interopRequireDefault(require("../icon")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var __rest = void 0 && (void 0).__rest || function (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; } if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var SInput = /*#__PURE__*/function (_React$Component) { _inherits(SInput, _React$Component); var _super = _createSuper(SInput); function SInput(props) { var _this; _classCallCheck(this, SInput); _this = _super.call(this, props); _this.clearing = false; _this.handleClear = function (ev) { if (_this.timer) { clearTimeout(_this.timer); _this.timer = null; } _this.clearing = true; var timer = setTimeout(function () { clearTimeout(timer); _this.clearing = false; _this.input.focus(); }, 100); _this.setValue('', ev, function () { _this.input.focus(); }); }; _this.handleChange = function (ev) { _this.setValue(ev.target.value, ev); }; _this.onFocus = function (ev) { var onFocus = _this.props.onFocus; typeof onFocus === 'function' && onFocus(ev); _this.setState({ focus: true }); if (_this.timer) { clearTimeout(_this.timer); _this.timer = null; } }; _this.onBlur = function (ev) { if (_this.clearing) return ev; var onBlur = _this.props.onBlur; typeof onBlur === 'function' && onBlur(ev); _this.timer = setTimeout(function () { _this.setState({ focus: false }); }, 100); }; _this.saveInput = function (node) { _this.input = node; }; _this.state = { value: props.value, showClear: false, focus: false }; return _this; } _createClass(SInput, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { var props = this.props; if (nextProps.value !== props.value) { this.setState({ value: nextProps.value }); } } }, { key: "setValue", value: function setValue(value, e, callback) { if (!('value' in this.props)) { this.setState({ value: value }, callback); } var onChange = this.props.onChange; if (onChange) { var event = e; if (e.type === 'click') { // click clear icon event = Object.create(e); event.target = this.input; event.currentTarget = this.input; var originalInputValue = this.input.value; // change input value cause e.target.value should be '' when clear input this.input.value = ''; onChange(event); // reset input value this.input.value = originalInputValue; return; } onChange(event); } } }, { key: "render", value: function render() { var _this$state = this.state, value = _this$state.value, focus = _this$state.focus; var cls = 'swc-input'; var _a = this.props, className = _a.className, allowClear = _a.allowClear, other = __rest(_a, ["className", "allowClear"]); cls = (0, _classnames["default"])(cls, className, _defineProperty({}, "".concat(cls, "-focus"), focus)); if (allowClear && focus) { var iconClass = (0, _classnames["default"])('swc-input-clear', _defineProperty({}, 'swc-input-clear-focus', focus)); other.suffix = /*#__PURE__*/_react["default"].createElement("span", { onMouseDown: this.handleClear }, /*#__PURE__*/_react["default"].createElement(_icon["default"], { className: iconClass, type: 'close-square', style: { fontSize: 'inherit' } })); } other.value = value; other.onChange = this.handleChange; other.onBlur = this.onBlur; other.onFocus = this.onFocus; return /*#__PURE__*/_react["default"].createElement(_input["default"], Object.assign({ ref: this.saveInput, className: cls }, other)); } }]); return SInput; }(_react["default"].Component); var _default = SInput; exports["default"] = _default;