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

Commit 9078d54

Browse files
author
Stephen Rivas Jr
committed
[release] 0.2.1 - Tests, README updates
1 parent 05aa0a0 commit 9078d54

11 files changed

+15586
-152
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
v0.2.1 - Wed, 12 Aug 2015 20:22:13 GMT
2+
--------------------------------------
3+
4+
- Added unit tests (thanks @ryanalane)
5+
- Updated README.md
6+
17
v0.1.4 - Wed, 12 Aug 2015 20:22:13 GMT
28
--------------------------------------
39

4-
-
10+
-
511

612

713
v0.1.3 - Wed, 12 Aug 2015 20:16:46 GMT
@@ -25,6 +31,6 @@ v0.1.1 - Wed, 12 Aug 2015 19:34:11 GMT
2531
v0.1.0 - Wed, 12 Aug 2015 19:22:26 GMT
2632
--------------------------------------
2733

28-
-
34+
-
2935

3036

build/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ React Autocomplete
33

44
Accessible, extensible, Autocomplete for React.js.
55

6-
Docs coming soon, for now just look at the `propTypes` and examples :)
6+
Docs coming soon, for now just look at the `propTypes` and [examples](https://reactjs.github.io/react-autocomplete/) :)
77

88
Trying to settle on the right API, and then focus hard on accessibility,
99
there are a few missing bits right now.
@@ -15,4 +15,13 @@ Stuff we need help with:
1515
pretty lean, on purpose, apps should style this however they'd like)
1616
- tests (eventually)
1717

18+
# Tests!
1819

20+
Run them:
21+
`npm test`
22+
23+
Write them:
24+
`lib/__tests__/Autocomplete-test.js`
25+
26+
Check your work:
27+
`npm run coverage`

build/lib/Autocomplete.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
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; };
44

55
var React = require('react');
6+
var lodash = require('lodash');
67
var scrollIntoView = require('dom-scroll-into-view');
78

89
var _debugStates = [];
@@ -17,12 +18,14 @@ var Autocomplete = React.createClass({
1718
shouldItemRender: React.PropTypes.func,
1819
renderItem: React.PropTypes.func.isRequired,
1920
menuStyle: React.PropTypes.object,
20-
inputProps: React.PropTypes.object
21+
inputProps: React.PropTypes.object,
22+
labelText: React.PropTypes.string
2123
},
2224

2325
getDefaultProps: function getDefaultProps() {
2426
return {
2527
inputProps: {},
28+
labelText: '',
2629
onChange: function onChange() {},
2730
onSelect: function onSelect(value, item) {},
2831
renderMenu: function renderMenu(items, value, style) {
@@ -53,6 +56,7 @@ var Autocomplete = React.createClass({
5356
},
5457

5558
componentWillMount: function componentWillMount() {
59+
this.id = lodash.uniqueId('autocomplete-');
5660
this._ignoreBlur = false;
5761
this._performAutoCompleteOnUpdate = false;
5862
this._performAutoCompleteOnKeyUp = false;
@@ -109,7 +113,7 @@ var Autocomplete = React.createClass({
109113
},
110114

111115
keyDownHandlers: {
112-
ArrowDown: function ArrowDown() {
116+
ArrowDown: function ArrowDown(event) {
113117
event.preventDefault();
114118
var highlightedIndex = this.state.highlightedIndex;
115119

@@ -137,16 +141,17 @@ var Autocomplete = React.createClass({
137141
var _this2 = this;
138142

139143
if (this.state.isOpen === false) {
140-
// already selected this, do nothing
144+
// menu is closed so there is no selection to accept -> do nothing
141145
return;
142146
} else if (this.state.highlightedIndex == null) {
143-
// hit enter after focus but before typing anything so no autocomplete attempt yet
147+
// input has focus but no menu item is selected + enter is hit -> close the menu, highlight whatever's in input
144148
this.setState({
145149
isOpen: false
146150
}, function () {
147151
_this2.refs.input.select();
148152
});
149153
} else {
154+
// text entered + menu item has been highlighted + enter is hit -> update value to that of selected menu item, close the menu
150155
var item = this.getFilteredItems()[this.state.highlightedIndex];
151156
this.setState({
152157
value: this.props.getItemValue(item),
@@ -212,10 +217,10 @@ var Autocomplete = React.createClass({
212217
setMenuPositions: function setMenuPositions() {
213218
var node = this.refs.input;
214219
var rect = node.getBoundingClientRect();
215-
var computedStyle = getComputedStyle(node);
216-
var marginBottom = parseInt(computedStyle.marginBottom, 10);
217-
var marginLeft = parseInt(computedStyle.marginLeft, 10);
218-
var marginRight = parseInt(computedStyle.marginRight, 10);
220+
var computedStyle = global.window.getComputedStyle(node);
221+
var marginBottom = parseInt(computedStyle.marginBottom, 10) || 0;
222+
var marginLeft = parseInt(computedStyle.marginLeft, 10) || 0;
223+
var marginRight = parseInt(computedStyle.marginRight, 10) || 0;
219224
this.setState({
220225
menuTop: rect.bottom + marginBottom,
221226
menuLeft: rect.left + marginLeft,
@@ -302,6 +307,11 @@ var Autocomplete = React.createClass({
302307
return React.createElement(
303308
'div',
304309
{ style: { display: 'inline-block' } },
310+
React.createElement(
311+
'label',
312+
{ htmlFor: this.id, ref: 'label' },
313+
this.props.labelText
314+
),
305315
React.createElement('input', _extends({}, this.props.inputProps, {
306316
role: 'combobox',
307317
'aria-autocomplete': 'both',
@@ -318,7 +328,8 @@ var Autocomplete = React.createClass({
318328
return _this7.handleKeyUp(event);
319329
},
320330
onClick: this.handleInputClick,
321-
value: this.state.value
331+
value: this.state.value,
332+
id: this.id
322333
})),
323334
this.state.isOpen && this.renderMenu(),
324335
this.props.debug && React.createElement(

0 commit comments

Comments
 (0)