Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor refs to make react 15 happy #208

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
11 changes: 7 additions & 4 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var TypeaheadTokenizer = React.createClass({
},

focus: function(){
this.refs.typeahead.focus();
this._typeahead.focus();
},

getSelectedTokens: function(){
Expand Down Expand Up @@ -148,7 +148,7 @@ var TypeaheadTokenizer = React.createClass({

// Remove token ONLY when bksp pressed at beginning of line
// without a selection
var entry = this.refs.typeahead.refs.entry;
var entry = this._typeahead._entry;
if (entry.selectionStart == entry.selectionEnd &&
entry.selectionStart == 0) {
this._removeTokenForValue(
Expand All @@ -175,7 +175,7 @@ var TypeaheadTokenizer = React.createClass({
}
this.state.selected.push(value);
this.setState({selected: this.state.selected});
this.refs.typeahead.setEntryText("");
this._typeahead.setEntryText("");
this.props.onTokenAdd(value);
},

Expand All @@ -187,10 +187,13 @@ var TypeaheadTokenizer = React.createClass({
tokenizerClasses[this.props.className] = !!this.props.className;
var tokenizerClassList = classNames(tokenizerClasses)

var _this = this;
var typeaheadRef = function(c){_this._typeahead = c};

return (
<div className={tokenizerClassList}>
{ this._renderTokens() }
<Typeahead ref="typeahead"
<Typeahead ref={typeaheadRef}
className={classList}
placeholder={this.props.placeholder}
disabled={this.props.disabled}
Expand Down
20 changes: 14 additions & 6 deletions src/typeahead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ var Typeahead = React.createClass({
},

setEntryText: function(value) {
this.refs.entry.value = value;
this._entry.value = value;
this._onTextEntryUpdated();
},

focus: function(){
this.refs.entry.focus()
this._entry.focus()
},

_hasCustomValue: function() {
Expand Down Expand Up @@ -160,9 +160,12 @@ var Typeahead = React.createClass({
return "";
}

var _this = this;
var selRef = function(c){ _this.sel = c};

return (
<this.props.customListComponent
ref="sel" options={this.props.maxVisible ? this.state.searchResults.slice(0, this.props.maxVisible) : this.state.searchResults}
ref={selRef} options={this.props.maxVisible ? this.state.searchResults.slice(0, this.props.maxVisible) : this.state.searchResults}
areResultsTruncated={this.props.maxVisible && this.state.searchResults.length > this.props.maxVisible}
resultsTruncatedMessage={this.props.resultsTruncatedMessage}
onOptionSelected={this._onOptionSelected}
Expand All @@ -188,7 +191,7 @@ var Typeahead = React.createClass({
},

_onOptionSelected: function(option, event) {
var nEntry = this.refs.entry;
var nEntry = this._entry;
nEntry.focus();

var displayOption = Accessor.generateOptionToStringFor(this.props.inputDisplayOption || this.props.displayOption);
Expand All @@ -206,7 +209,7 @@ var Typeahead = React.createClass({
},

_onTextEntryUpdated: function() {
var value = this.refs.entry.value;
var value = this._entry.value;
this.setState({searchResults: this.getOptionsForValue(value, this.props.options),
selection: '',
entryValue: value});
Expand Down Expand Up @@ -324,10 +327,15 @@ var Typeahead = React.createClass({
var classList = classNames(classes);

var InputElement = this.props.textarea ? 'textarea' : 'input';

var _this = this;
var entryRef = function(c) { _this._entry = c};
return (
<div className={classList}>
{ this._renderHiddenInput() }
<InputElement ref="entry" type="text"
<InputElement
ref={entryRef}
type="text"
disabled={this.props.disabled}
{...this.props.inputProps}
placeholder={this.props.placeholder}
Expand Down
2 changes: 1 addition & 1 deletion src/typeahead/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var TypeaheadOption = React.createClass({

return (
<li className={classList} onClick={this._onClick}>
<a href="javascript: void 0;" className={this._getClasses()} ref="anchor">
<a href="javascript: void 0;" className={this._getClasses()}>
{ this.props.children }
</a>
</li>
Expand Down
4 changes: 2 additions & 2 deletions src/typeahead/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var TypeaheadSelector = React.createClass({
if (this.props.customValue !== null) {
customValueOffset++;
customValue = (
<TypeaheadOption ref={this.props.customValue} key={this.props.customValue}
<TypeaheadOption key={this.props.customValue}
hover={this.props.selectionIndex === 0}
customClasses={this.props.customClasses}
customValue={this.props.customValue}
Expand All @@ -63,7 +63,7 @@ var TypeaheadSelector = React.createClass({
var displayString = this.props.displayOption(result, i);
var uniqueKey = displayString + '_' + i;
return (
<TypeaheadOption ref={uniqueKey} key={uniqueKey}
<TypeaheadOption key={uniqueKey}
hover={this.props.selectionIndex === i + customValueOffset}
customClasses={this.props.customClasses}
onClick={this._onClick.bind(this, result)}>
Expand Down
48 changes: 24 additions & 24 deletions test/tokenizer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ var Keyevent = require('../src/keyevent');
var TestUtils = require('react-addons-test-utils');

function simulateTextInput(component, value) {
var node = component.refs.entry;
var node = component._entry;
node.value = value;
TestUtils.Simulate.change(node);
return TestUtils.scryRenderedComponentsWithType(component, TypeaheadOption);
}

function simulateTokenInput(component, value) {
var typeahead = component.refs.typeahead;
var typeahead = component._typeahead;
return simulateTextInput(typeahead, value);
}

Expand Down Expand Up @@ -82,7 +82,7 @@ describe('TypeaheadTokenizer Component', function() {

it('should have custom and default token classes', function() {
simulateTokenInput(this.component, 'o');
var entry = this.component.refs.typeahead.refs.entry;
var entry = this.component._typeahead._entry;
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_RETURN });

Expand All @@ -104,7 +104,7 @@ describe('TypeaheadTokenizer Component', function() {
}
}
/>);
var input = ReactDOM.findDOMNode(component.refs.typeahead.refs.entry);
var input = ReactDOM.findDOMNode(component._typeahead._entry);
TestUtils.Simulate.keyDown(input, { keyCode: 87 });
});
});
Expand All @@ -120,7 +120,7 @@ describe('TypeaheadTokenizer Component', function() {
}
/>);

var input = ReactDOM.findDOMNode(component.refs.typeahead.refs.entry);
var input = ReactDOM.findDOMNode(component._typeahead._entry);
TestUtils.Simulate.keyPress(input, { keyCode: 87 });
});
});
Expand All @@ -136,7 +136,7 @@ describe('TypeaheadTokenizer Component', function() {
}
/>);

var input = ReactDOM.findDOMNode(component.refs.typeahead.refs.entry);
var input = ReactDOM.findDOMNode(component._typeahead._entry);
TestUtils.Simulate.keyUp(input, { keyCode: 87 });
});
});
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('TypeaheadTokenizer Component', function() {
/>);
var results = simulateTokenInput(component, 'john');

var entry = component.refs.typeahead.refs.entry;
var entry = component._typeahead._entry;
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_RETURN });

Expand All @@ -201,7 +201,7 @@ describe('TypeaheadTokenizer Component', function() {
/>);
var results = simulateTokenInput(component, 'john');

var entry = component.refs.typeahead.refs.entry;
var entry = component._typeahead._entry;
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_RETURN });

Expand All @@ -221,7 +221,7 @@ describe('TypeaheadTokenizer Component', function() {
/>);
results = simulateTokenInput(component, 'john');

var entry = component.refs.typeahead.refs.entry;
var entry = component._typeahead._entry;
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_RETURN });

Expand All @@ -243,15 +243,15 @@ describe('TypeaheadTokenizer Component', function() {
});

it('focuses the typeahead', function() {
this.sinon.spy(this.component.refs.typeahead, 'focus');
this.sinon.spy(this.component._typeahead, 'focus');
this.component.focus();
assert.equal(this.component.refs.typeahead.focus.calledOnce, true);
assert.equal(this.component._typeahead.focus.calledOnce, true);
});
});

it('should provide an exposed component function to get the selected tokens', function() {
simulateTokenInput(this.component, 'o');
var entry = this.component.refs.typeahead.refs.entry;
var entry = this.component._typeahead._entry;
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_RETURN });

Expand All @@ -263,7 +263,7 @@ describe('TypeaheadTokenizer Component', function() {
it('down arrow + return creates a token', function() {
var results = simulateTokenInput(this.component, 'o');
var secondItem = ReactDOM.findDOMNode(results[1]).innerText;
var node = this.component.refs.typeahead.refs.entry;
var node = this.component._typeahead._entry;
TestUtils.Simulate.keyDown(node, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(node, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(node, { keyCode: Keyevent.DOM_VK_RETURN });
Expand All @@ -274,7 +274,7 @@ describe('TypeaheadTokenizer Component', function() {
it('up arrow + return navigates and creates a token', function() {
var results = simulateTokenInput(this.component, 'o');
var firstItem = ReactDOM.findDOMNode(results[0]).innerText;
var node = this.component.refs.typeahead.refs.entry;
var node = this.component._typeahead._entry;
TestUtils.Simulate.keyDown(node, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(node, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(node, { keyCode: Keyevent.DOM_VK_UP });
Expand All @@ -286,7 +286,7 @@ describe('TypeaheadTokenizer Component', function() {
it('should remove a token when BKSPC is pressed on an empty input', function() {
// Select two items
simulateTokenInput(this.component, 'o');
var entry = this.component.refs.typeahead.refs.entry;
var entry = this.component._typeahead._entry;
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(entry, { keyCode: Keyevent.DOM_VK_RETURN });

Expand All @@ -309,7 +309,7 @@ describe('TypeaheadTokenizer Component', function() {
});

it('should not remove a token on BKSPC when input is not empty', function() {
var input = this.component.refs.typeahead.refs.entry;
var input = this.component._typeahead._entry;
var startLength = getTokens(this.component).length;

input.value = "hello";
Expand All @@ -323,7 +323,7 @@ describe('TypeaheadTokenizer Component', function() {
it('tab to choose first item', function() {
var results = simulateTokenInput(this.component, 'o');
var itemText = ReactDOM.findDOMNode(results[0]).innerText;
var node = this.component.refs.typeahead.refs.entry;
var node = this.component._typeahead._entry;
var tokens = getTokens(this.component);

// Need to check Token list for props.children
Expand All @@ -337,7 +337,7 @@ describe('TypeaheadTokenizer Component', function() {
it('tab to selected current item', function() {
var results = simulateTokenInput(this.component, 'o');
var itemText = ReactDOM.findDOMNode(results[1]).innerText;
var node = this.component.refs.typeahead.refs.entry;
var node = this.component._typeahead._entry;
TestUtils.Simulate.keyDown(node, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(node, { keyCode: Keyevent.DOM_VK_DOWN });
TestUtils.Simulate.keyDown(node, { keyCode: Keyevent.DOM_VK_TAB });
Expand Down Expand Up @@ -405,7 +405,7 @@ describe('TypeaheadTokenizer Component', function() {

it('should allow selection of custom token', function() {
var results = simulateTokenInput(this.component, "abzz");
var input = this.component.refs.typeahead.refs.entry;
var input = this.component._typeahead._entry;
var tokens = getTokens(this.component);

TestUtils.Simulate.keyDown(input, {keyCode: Keyevent.DOM_VK_DOWN})
Expand All @@ -417,7 +417,7 @@ describe('TypeaheadTokenizer Component', function() {

it('should call onTokenAdd for custom token', function() {
var results = simulateTokenInput(this.component, "abzz");
var input = this.component.refs.typeahead.refs.entry;
var input = this.component._typeahead._entry;
var tokens = getTokens(this.component);

TestUtils.Simulate.keyDown(input, {keyCode: Keyevent.DOM_VK_DOWN})
Expand All @@ -429,7 +429,7 @@ describe('TypeaheadTokenizer Component', function() {

it('should call onTokenRemove for custom token', function() {
var results = simulateTokenInput(this.component, "abzz");
var input = this.component.refs.typeahead.refs.entry;
var input = this.component._typeahead._entry;
var tokens = getTokens(this.component);

TestUtils.Simulate.keyDown(input, {keyCode: Keyevent.DOM_VK_DOWN})
Expand All @@ -447,7 +447,7 @@ describe('TypeaheadTokenizer Component', function() {

it('should not return undefined for a custom token when not selected', function() {
var results = simulateTokenInput(this.component, "abzz");
var input = this.component.refs.typeahead.refs.entry;
var input = this.component._typeahead._entry;
var tokens = getTokens(this.component);
TestUtils.Simulate.keyDown(input, {keyCode: Keyevent.DOM_VK_TAB})

Expand All @@ -460,7 +460,7 @@ describe('TypeaheadTokenizer Component', function() {

it('should not select value for a custom token when too short', function() {
var results = simulateTokenInput(this.component, "abz");
var input = this.component.refs.typeahead.refs.entry;
var input = this.component._typeahead._entry;
var tokens = getTokens(this.component);
TestUtils.Simulate.keyDown(input, {keyCode: Keyevent.DOM_VK_TAB})

Expand All @@ -479,7 +479,7 @@ describe('TypeaheadTokenizer Component', function() {
/>);

assert.notOk(ReactDOM.findDOMNode(component).classList.contains("tokenizer-typeahead"));
assert.equal(false, component.refs.typeahead.props.defaultClassNames);
assert.equal(false, component._typeahead.props.defaultClassNames);
});
});
});
Loading