react-lacona
Version:
React Component to provide a friendly interface for lacona
142 lines (127 loc) • 6.2 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Input = undefined;
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Input = exports.Input = function (_React$Component) {
_inherits(Input, _React$Component);
function Input() {
_classCallCheck(this, Input);
return _possibleConstructorReturn(this, (Input.__proto__ || Object.getPrototypeOf(Input)).apply(this, arguments));
}
_createClass(Input, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (this.props.autoFocus) {
(0, _reactDom.findDOMNode)(this).focus();
}
}
}, {
key: 'change',
value: function change(e) {
this.props.update(e.target.value);
}
}, {
key: 'focus',
value: function focus() {
(0, _reactDom.findDOMNode)(this.refs.input).focus();
}
}, {
key: 'focusEnd',
value: function focusEnd() {
var elem = (0, _reactDom.findDOMNode)(this.refs.input);
var pos = this.props.userInput.length;
if (elem.createTextRange) {
var range = elem.createTextRange();
range.move('character', pos);
range.select();
} else {
if (elem.selectionStart) {
elem.focus();
elem.setSelectionRange(pos, pos);
} else {
elem.focus();
}
}
elem.scrollLeft = elem.scrollWidth; //just pretty big
}
}, {
key: 'blur',
value: function blur() {
(0, _reactDom.findDOMNode)(this.refs.input).blur();
}
}, {
key: 'keyDown',
value: function keyDown(e) {
if (e.keyCode === 9) {
// tab
this.props.completeSelection();
} else if (!e.shiftKey && !e.metaKey && !e.altKey && !e.ctrlKey && e.keyCode === 38 || !e.shiftKey && !e.metaKey && !e.altKey && e.ctrlKey && e.keyCode === 75) {
// up or ^k
this.props.moveSelection(-1);
} else if (!e.shiftKey && !e.metaKey && !e.altKey && !e.ctrlKey && e.keyCode === 40 || !e.shiftKey && !e.metaKey && !e.altKey && e.ctrlKey && e.keyCode === 74) {
//down or ^j
this.props.moveSelection(1);
} else if (e.keyCode === 13) {
// return
this.props.execute();
} else if (e.keyCode === 27) {
// escape
this.props.cancel();
} else if (!e.shiftKey && !e.metaKey && !e.altKey && !e.ctrlKey && e.keyCode === 39 || !e.shiftKey && !e.metaKey && !e.altKey && e.ctrlKey && e.keyCode === 76) {
//right or ^l
var node = (0, _reactDom.findDOMNode)(this.refs.input);
if (node.selectionStart === node.selectionEnd && node.selectionStart === this.props.userInput.length) {
this.props.completeSelection();
} else {
if (node.selectionStart === node.selectionEnd) {
node.setSelectionRange(node.selectionStart + 1, node.selectionStart + 1);
} else {
node.setSelectionRange(node.selectionEnd, node.selectionEnd);
}
}
} else if (!e.shiftKey && !e.metaKey && !e.altKey && !e.ctrlKey && e.keyCode === 37 || !e.shiftKey && !e.metaKey && !e.altKey && e.ctrlKey && e.keyCode === 72) {
//left or ^h
var _node = (0, _reactDom.findDOMNode)(this.refs.input);
if (_node.selectionStart === _node.selectionEnd && _node.selectionStart >= 1) {
_node.setSelectionRange(_node.selectionStart - 1, _node.selectionStart - 1);
} else {
_node.setSelectionRange(_node.selectionStart, _node.selectionStart);
}
} else if (e.keyCode >= 49 && e.keyCode <= 57 && (this.props.selectKey === 'ctrl' && !e.altKey && !e.metaKey && e.ctrlKey || this.props.selectKey === 'alt' && e.altKey && !e.metaKey && !e.ctrlKey || this.props.selectKey === 'cmd' && !e.altKey && e.metaKey && !e.ctrlKey)) {
this.props.execute(e.keyCode - 49);
} else {
return;
}
e.preventDefault();
e.stopPropagation();
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement('input', {
type: 'text',
className: 'input ' + (this.props.empty ? 'empty' : ''),
ref: 'input',
tabIndex: this.props.tabIndex,
autoCorrect: false,
spellCheck: false,
autoCapitalize: false,
value: this.props.userInput,
onChange: this.change.bind(this),
onKeyDown: this.keyDown.bind(this),
onFocus: this.props.onFocus,
onBlur: this.props.onBlur,
placeholder: this.props.placeholder });
}
}]);
return Input;
}(_react2.default.Component);