@jdcfe/yep-react
Version:
一套移动端的React组件库
191 lines (161 loc) • 6.34 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) { 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 _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; } }
var __rest = this && this.__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;
};
import * as React from 'react';
import classNames from 'classnames';
import ToolTip from '../tooltip';
import InputItem from '../input-item';
var AutoComplete = /*#__PURE__*/function (_React$PureComponent) {
_inherits(AutoComplete, _React$PureComponent);
var _super = _createSuper(AutoComplete);
function AutoComplete(props) {
var _this;
_classCallCheck(this, AutoComplete);
_this = _super.call(this, props);
_this.handleInput = function (value) {
_this.lastValue = value;
var state = {
value: value
};
if (!value) {
state.open = true;
state.result = _this.props.source;
_this.setState(state);
} else {
// reset tab index
state.index = -1;
state.result = _this.props.source.filter(function (item) {
return item.indexOf(value) > -1;
});
state.open = !!state.result.length;
_this.setState(state);
}
_this.props.onChange && _this.props.onChange(value);
};
_this.handleKeyDown = function (e) {
var _this$state = _this.state,
open = _this$state.open,
result = _this$state.result;
if (open) {
var input = e.target;
var key = e.key;
var index = _this.state.index;
if (key === 'ArrowDown' || key === 'ArrowUp') {
if (key === 'ArrowDown') {
if (index === result.length - 1) index = -1;else index++;
}
if (key === 'ArrowUp') {
e.preventDefault();
if (index === -1) index = result.length - 1;else index--;
}
_this.setState({
index: index,
value: result[index] || _this.lastValue
});
}
if (key === 'Enter') {
_this.handleSelect(result[index]);
input.blur();
}
}
};
_this.state = {
open: false,
index: -1,
result: props.source,
value: props.defaultValue || props.value || ''
};
return _this;
}
_createClass(AutoComplete, [{
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
'value' in nextProps && this.setState({
value: nextProps.value
});
'source' in nextProps && this.setState({
result: nextProps.source
});
}
}, {
key: "handleSelect",
value: function handleSelect(value) {
this.setState({
value: value,
open: false
});
this.props.onChange && this.props.onChange(value);
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$state2 = this.state,
open = _this$state2.open,
index = _this$state2.index,
result = _this$state2.result,
value = _this$state2.value;
var _a = this.props,
prefixCls = _a.prefixCls,
source = _a.source,
onChange = _a.onChange,
disabled = _a.disabled,
isOpen = _a.isOpen,
matchedStyle = _a.matchedStyle,
other = __rest(_a, ["prefixCls", "source", "onChange", "disabled", "isOpen", "matchedStyle"]);
var inputProps = Object.assign(Object.assign({}, other), {
onKeyDown: this.handleKeyDown
});
return /*#__PURE__*/React.createElement(ToolTip, {
open: isOpen || open,
disabled: disabled,
aligned: true,
onToggle: function onToggle(open) {
return _this2.setState({
open: open
});
},
overlay: /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "__popover")
}, /*#__PURE__*/React.createElement("ul", {
className: "".concat(prefixCls, "__result")
}, result.map(function (item, i) {
return /*#__PURE__*/React.createElement("li", {
key: item,
className: classNames(_defineProperty({}, "".concat(prefixCls, "__option--active"), index === i)),
onClick: _this2.handleSelect.bind(_this2, item)
}, /*#__PURE__*/React.createElement("div", {
dangerouslySetInnerHTML: {
__html: value === '' ? item : "".concat(item.replace(value, '<span style="' + matchedStyle + '">' + value + '</span>'))
}
}));
})))
}, /*#__PURE__*/React.createElement(InputItem, _extends({
value: value,
onChange: this.handleInput,
disabled: disabled
}, inputProps)));
}
}]);
return AutoComplete;
}(React.PureComponent);
AutoComplete.defaultProps = {
prefixCls: 'Yep-auto-complete'
};
export default AutoComplete;