Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

[added] passed through event to onSelect prop callback #87

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 23 additions & 35 deletions build/lib/Autocomplete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var React = require('react');
Expand All @@ -10,8 +12,11 @@ var _debugStates = [];
var Autocomplete = React.createClass({
displayName: 'Autocomplete',


propTypes: {
value: React.PropTypes.any,
onFocus: React.PropTypes.func,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
onSelect: React.PropTypes.func,
shouldItemRender: React.PropTypes.func,
Expand All @@ -33,13 +38,16 @@ var Autocomplete = React.createClass({
},
inputProps: {},
onChange: function onChange() {},
onFocus: function onFocus() {},
onBlur: function onBlur() {},
onSelect: function onSelect(value, item) {},
renderMenu: function renderMenu(items, value, style) {
return React.createElement('div', { style: _extends({}, style, this.menuStyle), children: items });
},
shouldItemRender: function shouldItemRender() {
return true;
},

menuStyle: {
borderRadius: '3px',
boxShadow: '0 2px 12px rgba(0, 0, 0, 0.1)',
Expand All @@ -51,25 +59,20 @@ var Autocomplete = React.createClass({
maxHeight: '50%' }
};
},

// TODO: don't cheat, let it flow to the bottom
getInitialState: function getInitialState() {
return {
isOpen: false,
highlightedIndex: null
};
},

componentWillMount: function componentWillMount() {
this._ignoreBlur = false;
this._performAutoCompleteOnUpdate = false;
this._performAutoCompleteOnKeyUp = false;
},

componentWillReceiveProps: function componentWillReceiveProps() {
this._performAutoCompleteOnUpdate = true;
},

componentDidUpdate: function componentDidUpdate(prevProps, prevState) {
if (this.state.isOpen === true && prevState.isOpen === false) this.setMenuPositions();

Expand All @@ -80,20 +83,18 @@ var Autocomplete = React.createClass({

this.maybeScrollItemIntoView();
},

maybeScrollItemIntoView: function maybeScrollItemIntoView() {
if (this.state.isOpen === true && this.state.highlightedIndex !== null) {
var itemNode = this.refs['item-' + this.state.highlightedIndex];
var menuNode = this.refs.menu;
scrollIntoView(itemNode, menuNode, { onlyScrollIfNeeded: true });
}
},

handleKeyDown: function handleKeyDown(event) {
var _this = this;

if (this.keyDownHandlers[event.key]) this.keyDownHandlers[event.key].call(this, event);else {
var _ret = (function () {
var _ret = function () {
var _event$target = event.target;
var selectionStart = _event$target.selectionStart;
var value = _event$target.value;
Expand All @@ -102,7 +103,7 @@ var Autocomplete = React.createClass({
// Nothing changed, no need to do anything. This also prevents
// our workaround below from nuking user-made selections
return {
v: undefined
v: void 0
};
_this.setState({
highlightedIndex: null,
Expand All @@ -112,24 +113,23 @@ var Autocomplete = React.createClass({
// to work around a setSelectionRange bug in IE (#80)
_this.refs.input.selectionStart = selectionStart;
});
})();
}();

if (typeof _ret === 'object') return _ret.v;
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
},

handleChange: function handleChange(event) {
this._performAutoCompleteOnKeyUp = true;
this.props.onChange(event, event.target.value);
},

handleKeyUp: function handleKeyUp() {
if (this._performAutoCompleteOnKeyUp) {
this._performAutoCompleteOnKeyUp = false;
this.maybeAutoCompleteText();
}
},


keyDownHandlers: {
ArrowDown: function ArrowDown(event) {
event.preventDefault();
Expand All @@ -144,7 +144,6 @@ var Autocomplete = React.createClass({
isOpen: true
});
},

ArrowUp: function ArrowUp(event) {
event.preventDefault();
var itemsLength = this.getFilteredItems().length;
Expand All @@ -158,7 +157,6 @@ var Autocomplete = React.createClass({
isOpen: true
});
},

Enter: function Enter(event) {
var _this2 = this;

Expand All @@ -183,11 +181,10 @@ var Autocomplete = React.createClass({
}, function () {
//this.refs.input.focus() // TODO: file issue
_this2.refs.input.setSelectionRange(value.length, value.length);
_this2.props.onSelect(value, item);
_this2.props.onSelect(value, item, event);
});
}
},

Escape: function Escape(event) {
this.setState({
highlightedIndex: null,
Expand Down Expand Up @@ -215,7 +212,6 @@ var Autocomplete = React.createClass({

return items;
},

maybeAutoCompleteText: function maybeAutoCompleteText() {
var _this4 = this;

Expand All @@ -236,7 +232,6 @@ var Autocomplete = React.createClass({
if (highlightedIndex === null) this.setState({ highlightedIndex: 0 }, setSelection);else setSelection();
}
},

setMenuPositions: function setMenuPositions() {
var node = this.refs.input;
var rect = node.getBoundingClientRect();
Expand All @@ -250,29 +245,25 @@ var Autocomplete = React.createClass({
menuWidth: rect.width + marginLeft + marginRight
});
},

highlightItemFromMouse: function highlightItemFromMouse(index) {
this.setState({ highlightedIndex: index });
},

selectItemFromMouse: function selectItemFromMouse(item) {
selectItemFromMouse: function selectItemFromMouse(item, event) {
var _this5 = this;

var value = this.props.getItemValue(item);
this.setState({
isOpen: false,
highlightedIndex: null
}, function () {
_this5.props.onSelect(value, item);
_this5.props.onSelect(value, item, event);
_this5.refs.input.focus();
_this5.setIgnoreBlur(false);
});
},

setIgnoreBlur: function setIgnoreBlur(ignore) {
this._ignoreBlur = ignore;
},

renderMenu: function renderMenu() {
var _this6 = this;

Expand All @@ -285,8 +276,8 @@ var Autocomplete = React.createClass({
onMouseEnter: function onMouseEnter() {
return _this6.highlightItemFromMouse(index);
},
onClick: function onClick() {
return _this6.selectItemFromMouse(item);
onClick: function onClick(event) {
return _this6.selectItemFromMouse(item, event);
},
ref: 'item-' + index
});
Expand All @@ -299,29 +290,26 @@ var Autocomplete = React.createClass({
var menu = this.props.renderMenu(items, this.props.value, style);
return React.cloneElement(menu, { ref: 'menu' });
},

handleInputBlur: function handleInputBlur() {
this.props.onBlur({ ignoreBlur: this._ignoreBlur });
if (this._ignoreBlur) return;
this.setState({
isOpen: false,
highlightedIndex: null
});
},

handleInputFocus: function handleInputFocus() {
this.props.onFocus({ ignoreBlur: this._ignoreBlur });
if (this._ignoreBlur) return;
this.setState({ isOpen: true });
},

isInputFocused: function isInputFocused() {
var el = this.refs.input;
return el.ownerDocument && el === el.ownerDocument.activeElement;
},

handleInputClick: function handleInputClick() {
if (this.isInputFocused() && this.state.isOpen === false) this.setState({ isOpen: true });else if (this.state.highlightedIndex !== null) this.selectItemFromMouse(this.getFilteredItems()[this.state.highlightedIndex]);
},

render: function render() {
var _this7 = this;

Expand All @@ -343,13 +331,13 @@ var Autocomplete = React.createClass({
ref: 'input',
onFocus: this.handleInputFocus,
onBlur: this.handleInputBlur,
onChange: function (event) {
onChange: function onChange(event) {
return _this7.handleChange(event);
},
onKeyDown: function (event) {
onKeyDown: function onKeyDown(event) {
return _this7.handleKeyDown(event);
},
onKeyUp: function (event) {
onKeyUp: function onKeyUp(event) {
return _this7.handleKeyUp(event);
},
onClick: this.handleInputClick,
Expand Down
Loading