Skip to content

Commit e467f3c

Browse files
committed
code, README, LICENSE, grunt
1 parent c04fa09 commit e467f3c

File tree

9 files changed

+425
-2
lines changed

9 files changed

+425
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Gruntfile.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module.exports = function (grunt) {
2+
grunt.initConfig({
3+
pkg: grunt.file.readJSON('package.json'),
4+
browserify: {
5+
dist: {
6+
files: {
7+
'dist/react-input-placeholder.js': ['src/umd.js']
8+
},
9+
}
10+
},
11+
uglify: {
12+
options: {
13+
banner: '/*! <%= pkg.name %> - v <%= pkg.version %> - <%= pkg.author %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
14+
},
15+
dist: {
16+
files: {
17+
'dist/react-input-placeholder.min.js': ['dist/react-input-placeholder.js']
18+
}
19+
}
20+
},
21+
jshint: {
22+
options: {
23+
indent: 2,
24+
camelcase: true,
25+
nonew: true,
26+
plusplus: true,
27+
quotmark: true,
28+
bitwise: true,
29+
forin: true,
30+
curly: true,
31+
eqeqeq: true,
32+
immed: true,
33+
latedef: true,
34+
newcap: true,
35+
noarg: true,
36+
undef: true,
37+
unused: true,
38+
regexp: true,
39+
trailing: true,
40+
node: true,
41+
browser: true
42+
},
43+
gruntfile: {
44+
files: {
45+
src: ['Gruntfile.js']
46+
}
47+
},
48+
dev: {
49+
files: {
50+
src: ['src/**/*.js']
51+
},
52+
options: {
53+
debug: true,
54+
devel: true
55+
}
56+
},
57+
dist: {
58+
files: {
59+
src: ['src/**/*.js']
60+
}
61+
}
62+
}
63+
});
64+
65+
grunt.loadNpmTasks('grunt-contrib-jshint');
66+
grunt.loadNpmTasks('grunt-browserify');
67+
grunt.loadNpmTasks('grunt-contrib-uglify');
68+
69+
grunt.registerTask('lint', ['jshint']);
70+
grunt.registerTask('dist', ['browserify', 'uglify']);
71+
grunt.registerTask('default', ['jshint', 'dist']);
72+
};

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Enigma Technologies, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,64 @@
1-
react-input-placeholder
1+
React Input with Placeholder Shim
22
=======================
33

4-
Input is a small wrapper around React.DOM.input that shims in placeholder functionality for browsers that don't natively support it.
4+
`Input` is a small wrapper around `React.DOM.input` that shims in `placeholder` functionality for browsers that don't natively support it. Currently only tested with IE9.
5+
6+
Demo: http://jsfiddle.net/gb4xq/6/
7+
8+
## Getting Started
9+
10+
### Browserify
11+
12+
Install: `npm install react-input-placeholder`
13+
14+
Require: `Input = require('react-input-placeholder')(React);`
15+
16+
### No module
17+
18+
The compiled component sits in the `dist` folder, and places itself in `React.addons`.
19+
20+
```
21+
<script src='dist/react-input-placeholder.min.js'></script>
22+
<script>
23+
var Input = React.addons.Input;
24+
</script>
25+
```
26+
27+
## Usage
28+
29+
You can use `Input` 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).
30+
31+
When the placeholder text is visible, the `placeholder` CSS class will be added to the `input` element so you can style it, e.g.
32+
```
33+
input.placeholder {
34+
color: gray;
35+
font-style: italic;
36+
}
37+
```
38+
39+
### Before
40+
41+
Placeholder doesn't show on IE9.
42+
43+
```
44+
<input placeholder="Enter text here..." value={this.state.value} onChange={this.handleChange} />
45+
```
46+
47+
### After
48+
49+
Works on IE9!
50+
51+
```
52+
<Input placeholder="Enter text here..." value={this.state.value} onChange={this.handleChange} />
53+
```
54+
55+
## Building yourself
56+
57+
```
58+
npm install
59+
grunt dist
60+
```
61+
62+
## License
63+
64+
MIT

dist/react-input-placeholder.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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){
2+
var isPlaceholderSupported = 'placeholder' in document.createElement('input');
3+
4+
/**
5+
* Input is a wrapper around React.DOM.input with a `placeholder` shim for IE9.
6+
* NOTE: only supports "controlled" inputs (http://facebook.github.io/react/docs/forms.html#controlled-components)
7+
*/
8+
module.exports = function(React) {
9+
return React.createClass({
10+
displayName: 'Input',
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(prevProps, prevState, node) {
81+
this.setSelectionIfNeeded(node);
82+
},
83+
84+
render: function() {
85+
var input = this.transferPropsTo(React.DOM.input());
86+
if (this.needsPlaceholding) {
87+
// override valueLink and event handlers
88+
input.props.onFocus = this.onFocus;
89+
input.props.onBlur = this.onBlur;
90+
input.props.onChange = this.onChange;
91+
input.props.onSelect = this.onSelect;
92+
input.props.valueLink = undefined;
93+
94+
var value = this.getValue();
95+
if (!value) {
96+
this.isPlaceholding = true;
97+
value = this.props.placeholder;
98+
input.props.type = 'text';
99+
input.props.className += ' placeholder';
100+
} else {
101+
this.isPlaceholding = false;
102+
}
103+
input.props.value = value;
104+
}
105+
return input;
106+
}
107+
});
108+
};
109+
110+
},{}],2:[function(require,module,exports){
111+
/*global define*/
112+
var reactInputPlaceholder = require('./react-input-placeholder');
113+
if (typeof define === 'function' && define.amd) {
114+
define(['react'], function (React) {
115+
return reactInputPlaceholder(React);
116+
});
117+
} else {
118+
window.React.addons = window.React.addons || {};
119+
window.React.addons.Input = reactInputPlaceholder(window.React);
120+
}
121+
122+
},{"./react-input-placeholder":1}]},{},[2])

dist/react-input-placeholder.min.js

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

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "react-input-placeholder",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "src/react-input-placeholder.js",
6+
"scripts": {
7+
"test": "grunt lint"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git://github.com/enigma-io/react-input-placeholder.git"
12+
},
13+
"keywords": [
14+
"placeholder",
15+
"input",
16+
"react"
17+
],
18+
"author": "enigma.io",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/enigma-io/react-input-placeholder/issues"
22+
},
23+
"devDependencies": {
24+
"grunt": "~0.4.1",
25+
"grunt-contrib-jshint": "~0.7.0",
26+
"grunt-contrib-uglify": "~0.2.4",
27+
"grunt-browserify": "~1.2.9"
28+
}
29+
}

0 commit comments

Comments
 (0)