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

Add a separate prop for format placeholders #65

Open
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -130,6 +130,14 @@ DatePicker component. Renders as a [React-Bootstrap InputGroup](https://react-bo
* **Optional**
* **Type:** `string`
* **Examples:** `"MM/DD/YYYY"`, `"YYYY/MM/DD"`, `"MM-DD-YYYY"`, or `"DD MM YYYY"`
* `placeholder` - Placeholder to show for empty non-focused input.
* **Optional**
* **Type:** `string`
* **Examples:** `"Event date"`
* `formatPlaceholder` - Placeholder to show for focused input.
* **Optional**
* **Type:** `string`
* **Examples:** `"month/day/year"`
* `clearButtonElement` - Character or component to use for the clear button.
* **Optional**
* **Type:** `string` or `ReactClass`
Expand Down
24 changes: 24 additions & 0 deletions example/app.jsx
Expand Up @@ -97,6 +97,30 @@ const App = React.createClass({
</FormGroup>
</Col>
</Row>
<Row>
<Col xs={6}>
<h2>Auto Focus</h2>
</Col>
<Col xs={6}>
<h2>Different placeholder on focus</h2>
</Col>
</Row>
<Row>
<Col sm={6}>
<FormGroup>
<ControlLabel>Auto-focused</ControlLabel>
<DatePicker autoFocus onChange={this.handleChange} weekStartsOnMonday placeholder="Placeholder" />
<HelpBlock>Help</HelpBlock>
</FormGroup>
</Col>
<Col sm={6}>
<FormGroup>
<ControlLabel>With format placeholder</ControlLabel>
<DatePicker onChange={this.handleChange} weekStartsOnMonday placeholder="Placeholder" formatPlaceholder="month/day/year"/>
<HelpBlock>Help</HelpBlock>
</FormGroup>
</Col>
</Row>
<Row>
<Col xs={12}>
<h2>Styles</h2>
Expand Down
7 changes: 5 additions & 2 deletions lib/index.js
Expand Up @@ -217,6 +217,7 @@ exports.default = _react2.default.createClass({
style: _react2.default.PropTypes.object,
cellPadding: _react2.default.PropTypes.string,
placeholder: _react2.default.PropTypes.string,
formatPlaceholder: _react2.default.PropTypes.string,
dayLabels: _react2.default.PropTypes.array,
monthLabels: _react2.default.PropTypes.array,
onChange: _react2.default.PropTypes.func,
Expand Down Expand Up @@ -517,10 +518,12 @@ exports.default = _react2.default.createClass({
monthLabels: this.props.monthLabels,
dateFormat: this.props.dateFormat });

var placeholder = this.state.focused ? this.props.formatPlaceholder || this.props.dateFormat : this.state.placeholder;

var control = this.props.customControl ? _react2.default.cloneElement(this.props.customControl, {
onKeyDown: this.handleKeyDown,
value: this.state.inputValue || '',
placeholder: this.state.focused ? this.props.dateFormat : this.state.placeholder,
placeholder: placeholder,
ref: 'input',
disabled: this.props.disabled,
onFocus: this.handleFocus,
Expand All @@ -537,7 +540,7 @@ exports.default = _react2.default.createClass({
style: this.props.style,
autoFocus: this.props.autoFocus,
disabled: this.props.disabled,
placeholder: this.state.focused ? this.props.dateFormat : this.state.placeholder,
placeholder: placeholder,
onFocus: this.handleFocus,
onBlur: this.handleBlur,
onChange: this.handleInputChange
Expand Down
9 changes: 7 additions & 2 deletions src/index.jsx
Expand Up @@ -175,6 +175,7 @@ export default React.createClass({
style: React.PropTypes.object,
cellPadding: React.PropTypes.string,
placeholder: React.PropTypes.string,
formatPlaceholder: React.PropTypes.string,
dayLabels: React.PropTypes.array,
monthLabels: React.PropTypes.array,
onChange: React.PropTypes.func,
Expand Down Expand Up @@ -499,11 +500,15 @@ export default React.createClass({
monthLabels={this.props.monthLabels}
dateFormat={this.props.dateFormat} />;

const placeholder = this.state.focused
? this.props.formatPlaceholder || this.props.dateFormat
: this.state.placeholder

const control = this.props.customControl
? React.cloneElement(this.props.customControl, {
onKeyDown: this.handleKeyDown,
value: this.state.inputValue || '',
placeholder: this.state.focused ? this.props.dateFormat : this.state.placeholder,
placeholder,
ref: 'input',
disabled: this.props.disabled,
onFocus: this.handleFocus,
Expand All @@ -521,7 +526,7 @@ export default React.createClass({
style={this.props.style}
autoFocus={this.props.autoFocus}
disabled={this.props.disabled}
placeholder={this.state.focused ? this.props.dateFormat : this.state.placeholder}
placeholder={placeholder}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onChange={this.handleInputChange}
Expand Down
47 changes: 47 additions & 0 deletions test/core.test.jsx
Expand Up @@ -840,4 +840,51 @@ describe("Date Picker", function() {
assert.equal(inputElement.style.backgroundColor, backgroundColor);
ReactDOM.unmountComponentAtNode(container);
}));
it("should set the plaheholder", co.wrap(function *(){
const App = React.createClass({
render: function(){
return <div>
<DatePicker placeholder="foo" />
</div>;
}
});
yield new Promise(function(resolve, reject){
ReactDOM.render(<App />, container, resolve);
});
const inputElement = document.querySelector("input.form-control");
assert.equal(inputElement.placeholder, "foo");
ReactDOM.unmountComponentAtNode(container);
}));
it("should show dateFormat as placeholder when focused", co.wrap(function *(){
const App = React.createClass({
render: function(){
return <div>
<DatePicker dateFormat="YYYY-MM-DD" placeholder="foo" />
</div>;
}
});
yield new Promise(function(resolve, reject){
ReactDOM.render(<App />, container, resolve);
});
const inputElement = document.querySelector("input.form-control");
TestUtils.Simulate.focus(inputElement);
assert.equal(inputElement.placeholder, "YYYY-MM-DD");
ReactDOM.unmountComponentAtNode(container);
}));
it("should show formatPlaceholder when focused", co.wrap(function *(){
const App = React.createClass({
render: function(){
return <div>
<DatePicker formatPlaceholder="bar" placeholder="foo" />
</div>;
}
});
yield new Promise(function(resolve, reject){
ReactDOM.render(<App />, container, resolve);
});
const inputElement = document.querySelector("input.form-control");
TestUtils.Simulate.focus(inputElement);
assert.equal(inputElement.placeholder, "bar");
ReactDOM.unmountComponentAtNode(container);
}));
});