Skip to content

Commit 6c299c1

Browse files
committed
Tests, example, and documentation for disabled property.
1 parent 57bb367 commit 6c299c1

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ DatePicker component. Renders as a [React-Bootstrap InputGroup](https://react-bo
7878
* **Optional**
7979
* **Type:** `string`
8080
* **Example:** `"2016-05-19T16:00:00.000Z"`
81+
* `disabled` - Whether or not component is disabled.
82+
* **Optional**
83+
* **Type:** `bool`
84+
* **Example:** `false`
8185
* `onChange` - Focus callback function.
8286
* **Optional**
8387
* **Type:** `function`
@@ -107,7 +111,7 @@ DatePicker component. Renders as a [React-Bootstrap InputGroup](https://react-bo
107111
* **Example:** `"×"`
108112
* `showClearButton` - Toggles the visibility of the clearButton
109113
* **Optional**
110-
* **Type:** `bool`
114+
* **Type:** `bool`
111115
* **Example:** `false`
112116
* `onClear` - Defines what happens when clear button is clicked.
113117
* **Optional**

lib/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ exports.default = _react2.default.createClass({
169169
monthLabels: _react2.default.PropTypes.array,
170170
onChange: _react2.default.PropTypes.func,
171171
onClear: _react2.default.PropTypes.func,
172+
disabled: _react2.default.PropTypes.bool,
172173
weekStartsOnMonday: _react2.default.PropTypes.bool,
173174
clearButtonElement: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.object]),
174175
showClearButton: _react2.default.PropTypes.bool,
@@ -190,7 +191,8 @@ exports.default = _react2.default.createClass({
190191
nextButtonElement: ">",
191192
calendarPlacement: "bottom",
192193
dateFormat: dateFormat,
193-
showClearButton: true
194+
showClearButton: true,
195+
disabled: false
194196
};
195197
},
196198
getInitialState: function getInitialState() {
@@ -426,7 +428,7 @@ exports.default = _react2.default.createClass({
426428
dateFormat: this.props.dateFormat });
427429
return _react2.default.createElement(
428430
_reactBootstrap.InputGroup,
429-
{ ref: 'inputGroup', bsClass: this.props.bsClass, bsSize: this.props.bsSize, id: this.props.id ? this.props.id + "_group" : null },
431+
{ ref: 'inputGroup', bsClass: this.props.showClearButton ? this.props.bsClass : "", bsSize: this.props.bsSize, id: this.props.id ? this.props.id + "_group" : null },
430432
_react2.default.createElement(
431433
_reactBootstrap.Overlay,
432434
{ rootClose: true, onHide: this.handleHide, show: this.state.focused, container: function container() {
@@ -447,15 +449,21 @@ exports.default = _react2.default.createClass({
447449
value: this.state.inputValue || '',
448450
ref: 'input',
449451
type: 'text',
452+
disabled: this.props.disabled,
450453
placeholder: this.state.focused ? this.props.dateFormat : this.state.placeholder,
451454
onFocus: this.handleFocus,
452455
onBlur: this.handleBlur,
453-
onChange: this.handleInputChange
456+
onChange: this.handleInputChange,
457+
style: { width: "100%" }
454458
}),
455459
this.props.showClearButton && _react2.default.createElement(
456460
_reactBootstrap.InputGroup.Addon,
457-
{ onClick: this.clear, style: { cursor: this.state.inputValue ? "pointer" : "not-allowed" } },
458-
this.props.clearButtonElement
461+
{ onClick: this.props.disabled ? null : this.clear, style: { cursor: this.state.inputValue && !this.props.disabled ? "pointer" : "not-allowed" } },
462+
_react2.default.createElement(
463+
'div',
464+
{ style: { opacity: this.state.inputValue && !this.props.disabled ? 1 : 0.5 } },
465+
this.props.clearButtonElement
466+
)
459467
)
460468
);
461469
}

test/core.test.jsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,46 @@ describe("Date Picker", function() {
447447
assert.notEqual(document.querySelector("#calendarContainer #calendar"), null);
448448
ReactDOM.unmountComponentAtNode(container);
449449
}));
450+
it("should disable the input.", co.wrap(function *(){
451+
const id = UUID.v4();
452+
const value = new Date().toISOString();
453+
const App = React.createClass({
454+
render: function(){
455+
return <div>
456+
<DatePicker id={id} value={value} disabled={true}/>
457+
</div>;
458+
}
459+
});
460+
yield new Promise(function(resolve, reject){
461+
ReactDOM.render(<App />, container, resolve);
462+
});
463+
const hiddenInputElement = document.getElementById(id);
464+
const inputElement = document.querySelector("input.form-control");
465+
assert.equal(inputElement.disabled, true);
466+
ReactDOM.unmountComponentAtNode(container);
467+
}));
468+
it("should disable the input.", co.wrap(function *(){
469+
const id = UUID.v4();
470+
let value = new Date().toISOString();
471+
let originalValue = value;
472+
const App = React.createClass({
473+
handleChange: function(newValue){
474+
value = newValue;
475+
},
476+
render: function(){
477+
return <div>
478+
<DatePicker id={id} onChange={this.handleChange} disabled={true} />
479+
</div>;
480+
}
481+
});
482+
yield new Promise(function(resolve, reject){
483+
ReactDOM.render(<App />, container, resolve);
484+
});
485+
const inputElement = document.querySelector("input.form-control");
486+
assert.equal(inputElement.disabled, true);
487+
const clearButtonElement = document.querySelector("span.input-group-addon");
488+
TestUtils.Simulate.click(clearButtonElement);
489+
assert.equal(value, originalValue);
490+
ReactDOM.unmountComponentAtNode(container);
491+
}));
450492
});

0 commit comments

Comments
 (0)