Skip to content

Commit 02e9cff

Browse files
Merge pull request HurricaneJames#1 from rikukissa/master
Usage with a React class as the type attribute
2 parents f6cc4b9 + ad92878 commit 02e9cff

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/LazyInput.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ var React = require('react');
33
var LazyInput = React.createClass({
44
displayName: "LazyInput",
55
propTypes: {
6-
type: React.PropTypes.string, // ['text'] type of input/textarea
6+
type: React.PropTypes.oneOfType([ // by defaut it will use a React.DOM.input (type='text')
7+
React.PropTypes.string, // ['text'] type of input ('textarea' will create a textarea element, anything else will pass to input)
8+
React.PropTypes.func // a React component class
9+
]),
710
lazyLevel: React.PropTypes.number // [1000] number of ms to wait before responding to changes in prop.value
811
// note: passes through everything but lazyLevel
912
},
@@ -56,7 +59,12 @@ var LazyInput = React.createClass({
5659
return props;
5760
},
5861
render: function() {
59-
return React.createElement(this.props.type === "textarea" ? "textarea" : "input", this.getProps());
62+
var type = this.props.type;
63+
if(!type || type === "text") {
64+
type = "input";
65+
}
66+
67+
return React.createElement(type, this.getProps());
6068
}
6169

6270
});

src/__tests__/LazyInput-test.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ describe('LazyInput', function() {
3232
var input = TestUtils.renderIntoDocument(<LazyInput type="textarea" />);
3333
expect(TestUtils.findRenderedDOMComponentWithTag(input, 'textarea')).not.to.be(undefined);
3434
});
35+
it('should render a custom type when a React class is given', function() {
36+
var CustomType = React.createClass({
37+
render: function() {
38+
return <input className="custom" {...this.props} />
39+
}
40+
});
41+
42+
var input = TestUtils.renderIntoDocument(<LazyInput type={CustomType} />);
43+
expect(TestUtils.findRenderedDOMComponentWithClass(input, 'custom')).not.to.be(undefined);
44+
});
3545

3646
describe("props", function() {
3747
// specificially import props

0 commit comments

Comments
 (0)