Skip to content

Commit 1e1e268

Browse files
author
Evan Jacobs
committed
Merge pull request #9 from yaycmyk/react-compatibility
Update React compatibility, bump to 1.1.0
2 parents 0440f10 + 7db07cb commit 1e1e268

File tree

8 files changed

+327
-230
lines changed

8 files changed

+327
-230
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## 1.1.0
2+
### Bugfixes
3+
- [Make module compatible with newer versions of React](https://github.com/enigma-io/react-input-placeholder/pull/7)
4+
- [Check for document before using it](https://github.com/enigma-io/react-input-placeholder/pull/6)
5+
6+
### Deprecations
7+
- Support for React versions `< 0.13.0` will be removed in `2.0.0`
8+
9+
## 1.0.0
10+
- Initial commit

Gruntfile.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function (grunt) {
1010
},
1111
uglify: {
1212
options: {
13-
banner: '/*! <%= pkg.name %> - v <%= pkg.version %> - <%= pkg.author %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
13+
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> (<%= grunt.template.today("yyyy-mm-dd") %>) - <%= pkg.author %> */\n'
1414
},
1515
dist: {
1616
files: {
@@ -20,7 +20,6 @@ module.exports = function (grunt) {
2020
},
2121
jshint: {
2222
options: {
23-
indent: 2,
2423
camelcase: true,
2524
nonew: true,
2625
plusplus: true,

LICENSE renamed to LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Enigma Technologies, Inc.
3+
Copyright (c) 2015 Enigma Technologies, Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
React Input and Textarea with Placeholder Shim
22
=======================
33

4-
`PlaceholderShim` provides `Input` and `Textarea`, small wrappers around `React.DOM.input` and `React.DOM.textarea` respectively that shims in `placeholder` functionality for browsers that don't natively support it. Currently only tested with IE9.
4+
`PlaceholderShim` provides `Input` and `Textarea`, small wrappers around `React.createElement('input')` and `React.createElement('textarea')` respectively that shims in `placeholder` functionality for browsers that don't natively support it. Currently only tested with IE9.
55

6-
Demo: http://jsfiddle.net/gb4xq/12/
6+
Demo: https://jsfiddle.net/yaycmyk/69z2wepo/10029/
77

88
## Getting Started
9-
109
### Browserify
1110

1211
Install: `npm install react-input-placeholder`
1312

1413
Require:
15-
```
14+
15+
```js
1616
Input = require('react-input-placeholder')(React).Input;
1717
Textarea = require('react-input-placeholder')(React).Textarea;
1818
```
1919

2020
### No module
2121

22-
The compiled component sits in the `dist` folder.
22+
The compiled bundle sits in the `dist/` folder.
2323

24-
```
24+
```js
2525
<script src='dist/react-input-placeholder.min.js'></script>
2626
<script>
2727
var Input = PlaceholderShim.Input;
@@ -31,11 +31,13 @@ The compiled component sits in the `dist` folder.
3131

3232
## Usage
3333

34-
You can use `Input` or `Textarea` exactly the same way you'd use `React.DOM.Input`. All attributes will be passed on, and all event callbacks will be called. However, please note that the placeholder shim only works on [controlled](http://facebook.github.io/react/docs/forms.html#controlled-components) inputs (i.e., you must provide a `value` or `valueLink` prop).
34+
You can use `Input` or `Textarea` exactly the same way you'd use `React.createElement('input')` or `<input />` in JSX. All attributes will be passed on, and all event callbacks will be called. However, please note that the placeholder shim only works on [controlled](http://facebook.github.io/react/docs/forms.html#controlled-components) inputs (i.e., you must provide a `value` or `valueLink` prop).
3535

3636
When the placeholder text is visible, the `placeholder` CSS class will be added to the `input` element so you can style it, e.g.
37-
```
38-
input.placeholder, textarea.placeholder {
37+
38+
```css
39+
input.placeholder,
40+
textarea.placeholder {
3941
color: gray;
4042
font-style: italic;
4143
}
@@ -45,21 +47,21 @@ input.placeholder, textarea.placeholder {
4547

4648
Placeholder doesn't show on IE9.
4749

48-
```
50+
```html
4951
<input placeholder="Enter text here..." value={this.state.value} onChange={this.handleChange} />
5052
```
5153

5254
### After
5355

5456
Works on IE9!
5557

56-
```
58+
```html
5759
<Input placeholder="Enter text here..." value={this.state.value} onChange={this.handleChange} />
5860
```
5961

6062
## Building yourself
6163

62-
```
64+
```shell
6365
npm install
6466
grunt dist
6567
```

dist/react-input-placeholder.js

Lines changed: 145 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,157 @@
1-
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
1+
;(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
22
var isPlaceholderSupported = (typeof document !== 'undefined') && 'placeholder' in document.createElement('input');
33

44
/**
55
* Input is a wrapper around React.DOM.input with a `placeholder` shim for IE9.
66
* NOTE: only supports "controlled" inputs (http://facebook.github.io/react/docs/forms.html#controlled-components)
77
*/
88
var createShimmedElement = function(React, elementConstructor, name) {
9-
return React.createClass({
10-
displayName: name,
11-
12-
componentWillMount: function() {
13-
this.needsPlaceholding = this.props.placeholder && !isPlaceholderSupported;
14-
},
15-
componentWillReceiveProps: function(props) {
16-
this.needsPlaceholding = props.placeholder && !isPlaceholderSupported;
17-
},
18-
19-
// this component supports valueLink or value/onChange.
20-
// borrowed from LinkedValueMixin.js
21-
getValue: function() {
22-
if (this.props.valueLink) {
23-
return this.props.valueLink.value;
24-
}
25-
return this.props.value;
26-
},
27-
getOnChange: function() {
28-
if (this.props.valueLink) {
29-
return this._handleLinkedValueChange;
30-
}
31-
return this.props.onChange;
32-
},
33-
_handleLinkedValueChange: function(e) {
34-
this.props.valueLink.requestChange(e.target.value);
35-
},
36-
37-
// keep track of focus
38-
onFocus: function(e) {
39-
this.hasFocus = true;
40-
this.setSelectionIfNeeded(e.target);
41-
if (this.props.onFocus) { return this.props.onFocus(e); }
42-
},
43-
onBlur: function(e) {
44-
this.hasFocus = false;
45-
if (this.props.onBlur) { return this.props.onBlur(e); }
46-
},
47-
48-
setSelectionIfNeeded: function(node) {
49-
// if placeholder is visible, ensure cursor is at start of input
50-
if (this.needsPlaceholding && this.hasFocus && this.isPlaceholding &&
51-
(node.selectionStart !== 0 || node.selectionEnd !== 0)) {
52-
node.setSelectionRange(0, 0);
53-
return true;
54-
}
55-
return false;
56-
},
57-
58-
onChange: function(e) {
59-
if (this.isPlaceholding) {
60-
// remove placeholder when text is added
61-
var value = e.target.value,
62-
i = value.indexOf(this.props.placeholder);
63-
if (i !== -1) {
64-
e.target.value = value.slice(0, i);
65-
}
66-
}
67-
var onChange = this.getOnChange();
68-
if (onChange) { return onChange(e); }
69-
},
70-
71-
onSelect: function(e) {
72-
if (this.isPlaceholding) {
73-
this.setSelectionIfNeeded(e.target);
74-
return false;
75-
} else if (this.props.onSelect) {
76-
return this.props.onSelect(e);
77-
}
78-
},
79-
80-
componentDidUpdate: function() {
81-
this.setSelectionIfNeeded(this.getDOMNode());
82-
},
83-
84-
render: function() {
85-
var element = this.transferPropsTo(elementConstructor());
86-
if (this.needsPlaceholding) {
87-
// override valueLink and event handlers
88-
element.props.onFocus = this.onFocus;
89-
element.props.onBlur = this.onBlur;
90-
element.props.onChange = this.onChange;
91-
element.props.onSelect = this.onSelect;
92-
element.props.valueLink = undefined;
93-
94-
var value = this.getValue();
95-
if (!value) {
96-
this.isPlaceholding = true;
97-
value = this.props.placeholder;
98-
element.props.type = 'text';
99-
element.props.className += ' placeholder';
100-
} else {
101-
this.isPlaceholding = false;
9+
return React.createClass({
10+
displayName: name,
11+
12+
componentWillMount: function() {
13+
this.needsPlaceholding = this.props.placeholder && !isPlaceholderSupported;
14+
},
15+
componentWillReceiveProps: function(props) {
16+
this.needsPlaceholding = props.placeholder && !isPlaceholderSupported;
17+
},
18+
19+
// this component supports valueLink or value/onChange.
20+
// borrowed from LinkedValueMixin.js
21+
getValue: function() {
22+
if (this.props.valueLink) {
23+
return this.props.valueLink.value;
24+
}
25+
26+
return this.props.value;
27+
},
28+
29+
getOnChange: function() {
30+
if (this.props.valueLink) {
31+
return this._handleLinkedValueChange;
32+
}
33+
34+
return this.props.onChange;
35+
},
36+
37+
_handleLinkedValueChange: function(e) {
38+
this.props.valueLink.requestChange(e.target.value);
39+
},
40+
41+
// keep track of focus
42+
onFocus: function(e) {
43+
this.hasFocus = true;
44+
this.setSelectionIfNeeded(e.target);
45+
46+
if (this.props.onFocus) {
47+
return this.props.onFocus(e);
48+
}
49+
},
50+
onBlur: function(e) {
51+
this.hasFocus = false;
52+
53+
if (this.props.onBlur) {
54+
return this.props.onBlur(e);
55+
}
56+
},
57+
58+
setSelectionIfNeeded: function(node) {
59+
// if placeholder is visible, ensure cursor is at start of input
60+
if (this.needsPlaceholding &&
61+
this.hasFocus &&
62+
this.isPlaceholding &&
63+
(node.selectionStart !== 0 || node.selectionEnd !== 0)) {
64+
node.setSelectionRange(0, 0);
65+
66+
return true;
67+
}
68+
69+
return false;
70+
},
71+
72+
onChange: function(e) {
73+
var onChange = this.getOnChange();
74+
var value;
75+
var index;
76+
77+
if (this.isPlaceholding) {
78+
// remove placeholder when text is added
79+
value = e.target.value;
80+
index = value.indexOf(this.props.placeholder);
81+
82+
if (index !== -1) {
83+
e.target.value = value.slice(0, index);
84+
}
85+
}
86+
87+
if (onChange) {
88+
return onChange(e);
89+
}
90+
},
91+
92+
onSelect: function(e) {
93+
if (this.isPlaceholding) {
94+
this.setSelectionIfNeeded(e.target);
95+
96+
return false;
97+
} else if (this.props.onSelect) {
98+
return this.props.onSelect(e);
99+
}
100+
},
101+
102+
componentDidUpdate: function() {
103+
this.setSelectionIfNeeded(this.getDOMNode());
104+
},
105+
106+
render: function() {
107+
var element;
108+
var value;
109+
110+
if (!('createElement' in React)) { /* start -- to be removed in 2.0.0 */
111+
element = this.transferPropsTo(elementConstructor());
112+
} else { /* -- end */
113+
element = React.createElement(elementConstructor, this.props, this.props.children);
114+
}
115+
116+
if (this.needsPlaceholding) {
117+
// override valueLink and event handlers
118+
element.props.onFocus = this.onFocus;
119+
element.props.onBlur = this.onBlur;
120+
element.props.onChange = this.onChange;
121+
element.props.onSelect = this.onSelect;
122+
element.props.valueLink = undefined;
123+
124+
value = this.getValue();
125+
126+
if (!value) {
127+
this.isPlaceholding = true;
128+
value = this.props.placeholder;
129+
element.props.type = 'text';
130+
element.props.className += ' placeholder';
131+
} else {
132+
this.isPlaceholding = false;
133+
}
134+
135+
element.props.value = value;
136+
}
137+
138+
return element;
102139
}
103-
element.props.value = value;
104-
}
105-
return element;
106-
}
107-
});
140+
});
108141
};
109142

110143
module.exports = function(React) {
111-
return {
112-
Input: createShimmedElement(React, React.DOM.input, 'Input'),
113-
Textarea: createShimmedElement(React, React.DOM.textarea, 'Textarea')
114-
};
144+
if (!('createElement' in React)) { /* start -- to be removed in 2.0.0 */
145+
return {
146+
Input: createShimmedElement(React, React.DOM.input, 'Input'),
147+
Textarea: createShimmedElement(React, React.DOM.textarea, 'Textarea')
148+
};
149+
} else { /* -- end */
150+
return {
151+
Input: React.createFactory(createShimmedElement(React, 'input', 'Input')),
152+
Textarea: React.createFactory(createShimmedElement(React, 'textarea', 'Textarea'))
153+
};
154+
}
115155
};
116156

117157
},{}],2:[function(require,module,exports){
@@ -124,4 +164,5 @@ if (typeof define === 'function' && define.amd) {
124164
} else {
125165
window.PlaceholderShim = reactInputPlaceholder(window.React);
126166
}
127-
},{"./react-input-placeholder":1}]},{},[2])
167+
},{"./react-input-placeholder":1}]},{},[2])
168+
;

dist/react-input-placeholder.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)