react-predictive-text
Version:
A React component that finishes users' words.
269 lines (219 loc) • 10.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(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); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } 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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var PTextItem =
/*#__PURE__*/
function (_React$Component) {
_inherits(PTextItem, _React$Component);
function PTextItem(props) {
var _this;
_classCallCheck(this, PTextItem);
_this = _possibleConstructorReturn(this, _getPrototypeOf(PTextItem).call(this, props));
_this.state = {
styles: _this.props.style
};
return _this;
}
_createClass(PTextItem, [{
key: "handleMouseEnter",
value: function handleMouseEnter() {
var selectedStyles = {
backgroundColor: this.props.color
};
this.setState({
styles: Object.assign({}, this.state.styles, selectedStyles)
});
}
}, {
key: "handleMouseLeave",
value: function handleMouseLeave() {
var unselectedStyles = {
backgroundColor: 'white'
};
this.setState({
styles: Object.assign({}, this.state.styles, unselectedStyles)
});
}
}, {
key: "handleClick",
value: function handleClick(e) {
this.props.onSelect(this.props.value);
}
}, {
key: "render",
value: function render() {
return _react.default.createElement("div", {
style: this.state.styles,
onMouseDown: this.handleClick.bind(this),
onMouseEnter: this.handleMouseEnter.bind(this),
onMouseLeave: this.handleMouseLeave.bind(this)
}, this.props.value);
}
}]);
return PTextItem;
}(_react.default.Component);
PTextItem.propTypes = {};
PTextItem.defaultProps = {
color: "#E6E6FA" // props:
// onSelect -- callback to be invoked when an item is clicked.
// color -- the color which a hovered item will turn.
// style -- the style for an unhovered item.
// value -- the text of the item.
};
var _default = PTextItem;
exports.default = _default;
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _Trie = _interopRequireDefault(require("./Trie.js"));
var _reactPredictiveTextItem = _interopRequireDefault(require("./react-predictive-text-item.jsx"));
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(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); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
var PText =
/*#__PURE__*/
function (_React$Component) {
_inherits(PText, _React$Component);
function PText(props) {
var _this;
_classCallCheck(this, PText);
_this = _possibleConstructorReturn(this, _getPrototypeOf(PText).call(this, props));
_this.uniqueId = 0;
_this.state = {
value: "",
predictions: [],
cache: [],
width: 100
};
_this._trie = new _Trie.default();
_this.props.options.forEach(function (opt) {
_this._trie.add(opt);
});
_this.handleOptionSelect = _this.handleOptionSelect.bind(_assertThisInitialized(_assertThisInitialized(_this)));
return _this;
}
_createClass(PText, [{
key: "handleTextInput",
value: function handleTextInput(e) {
var fragment = e.target.value;
var newPredictions = this._trie.retrieve(e.target.value);
this.setState({
value: e.target.value,
predictions: newPredictions
});
}
}, {
key: "handleOptionSelect",
value: function handleOptionSelect(option) {
this.setState({
value: option,
predictions: []
}, this.props.onSelect(option));
}
}, {
key: "handleResize",
value: function handleResize(e) {
var bbox = e.target.getBoundingClientRect();
this.setState({
width: bbox.width
});
}
}, {
key: "handleBlur",
value: function handleBlur(e) {
this.setState({
cache: this.state.predictions,
predictions: []
});
}
}, {
key: "handleFocus",
value: function handleFocus(e) {
this.setState({
predictions: this.state.cache
});
}
}, {
key: "render",
value: function render() {
var _this2 = this;
return _react.default.createElement("div", {
onBlur: this.handleBlur.bind(this),
onFocus: this.handleFocus.bind(this),
style: this.props.wrapperStyles
}, _react.default.createElement("textarea", {
styles: this.props.textStyles,
id: this.props.options.id,
value: this.state.value,
onChange: this.handleTextInput.bind(this),
onMouseUp: this.handleResize.bind(this)
}), _react.default.createElement("div", {
style: {
width: this.state.width,
maxHeight: "10em",
overflow: "scroll",
position: "absolute",
zIndex: 99
}
}, this.state.predictions.map(function (pred) {
return _react.default.createElement(_reactPredictiveTextItem.default, {
style: _this2.props.optionStyles,
key: _this2.uniqueId++,
color: _this2.props.optionColor,
onSelect: _this2.handleOptionSelect,
value: pred
});
})));
}
}]);
return PText;
}(_react.default.Component);
PText.propTypes = {
optionStyles: _propTypes.default.object,
optionColor: _propTypes.default.string,
textStyles: _propTypes.default.object,
wrapperStyles: _propTypes.default.object,
onSelect: _propTypes.default.func
};
PText.defaultProps = {
optionStyles: {
border: '1px solid lightgrey',
fontSize: '0.25em',
padding: '0.25em'
},
optionColor: '#E6E6FA',
textStyles: {},
wrapperStyles: {},
onSelect: function onSelect() {} // props:
// optionStyles -- a style object for every item in the recommendation list.
// textStyles -- a style object for the textarea element.
// wrapperStyles -- a style object for the wrapper div.
// optionColor -- a string representing the color a hovered item will be colored.
// onSelect -- a function to be called when an item is selected.
};
var _default = PText;
exports.default = _default;