Skip to content

Commit 1744417

Browse files
Niftwehriam
authored andcommitted
Added functionality to support the clear button not being visible (#37)
Added test case Updated README Updated examples
1 parent 672360d commit 1744417

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ DatePicker component. Renders as a [React-Bootstrap InputGroup](https://react-bo
105105
* **Optional**
106106
* **Type:** `string` or `ReactClass`
107107
* **Example:** `"×"`
108+
* `showClearButton` - Toggles the visibility of the clearButton
109+
* **Optional**
110+
* **Type:** `bool`
111+
* **Example:** `false`
108112
* `onClear` - Defines what happens when clear button is clicked.
109113
* **Optional**
110114
* **Type:** `function`

example/app.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,19 @@ const App = React.createClass({
289289
</FormGroup>
290290
</Col>
291291
</Row>
292+
<Row>
293+
<Col xs={12}>
294+
<h2>No clear button visible</h2>
295+
</Col>
296+
</Row>
297+
<Row>
298+
<Col sm={6}>
299+
<FormGroup controlId="no_clear_button">
300+
<DatePicker placeholder="Placeholder" value={this.state.date}
301+
id="no_clear_button_example" showClearButton={false}/>
302+
</FormGroup>
303+
</Col>
304+
</Row>
292305
</Grid>;
293306
}
294307
});

lib/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,12 @@ exports.default = _react2.default.createClass({
171171
onClear: _react2.default.PropTypes.func,
172172
weekStartsOnMonday: _react2.default.PropTypes.bool,
173173
clearButtonElement: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.object]),
174+
showClearButton: _react2.default.PropTypes.bool,
174175
previousButtonElement: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.object]),
175176
nextButtonElement: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.object]),
176177
calendarPlacement: _react2.default.PropTypes.string,
177178
dateFormat: _react2.default.PropTypes.string // 'MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY/MM/DD', 'DD-MM-YYYY'
179+
178180
},
179181
getDefaultProps: function getDefaultProps() {
180182
var language = typeof window !== "undefined" && window.navigator ? (window.navigator.userLanguage || window.navigator.language || '').toLowerCase() : '';
@@ -187,7 +189,8 @@ exports.default = _react2.default.createClass({
187189
previousButtonElement: "<",
188190
nextButtonElement: ">",
189191
calendarPlacement: "bottom",
190-
dateFormat: dateFormat
192+
dateFormat: dateFormat,
193+
showClearButton: true
191194
};
192195
},
193196
getInitialState: function getInitialState() {
@@ -449,7 +452,7 @@ exports.default = _react2.default.createClass({
449452
onBlur: this.handleBlur,
450453
onChange: this.handleInputChange
451454
}),
452-
_react2.default.createElement(
455+
this.props.showClearButton && _react2.default.createElement(
453456
_reactBootstrap.InputGroup.Addon,
454457
{ onClick: this.clear, style: { cursor: this.state.inputValue ? "pointer" : "not-allowed" } },
455458
this.props.clearButtonElement

src/index.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export default React.createClass({
128128
React.PropTypes.string,
129129
React.PropTypes.object
130130
]),
131+
showClearButton: React.PropTypes.bool,
131132
previousButtonElement: React.PropTypes.oneOfType([
132133
React.PropTypes.string,
133134
React.PropTypes.object
@@ -138,6 +139,7 @@ export default React.createClass({
138139
]),
139140
calendarPlacement: React.PropTypes.string,
140141
dateFormat: React.PropTypes.string // 'MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY/MM/DD', 'DD-MM-YYYY'
142+
141143
},
142144
getDefaultProps() {
143145
const language = typeof window !== "undefined" && window.navigator ? (window.navigator.userLanguage || window.navigator.language || '').toLowerCase() : '';
@@ -152,7 +154,8 @@ export default React.createClass({
152154
previousButtonElement: "<",
153155
nextButtonElement: ">",
154156
calendarPlacement: "bottom",
155-
dateFormat: dateFormat
157+
dateFormat: dateFormat,
158+
showClearButton: true,
156159
}
157160
},
158161
getInitialState() {
@@ -383,7 +386,7 @@ export default React.createClass({
383386
onChange={this.onChangeMonth}
384387
monthLabels={this.props.monthLabels}
385388
dateFormat={this.props.dateFormat} />;
386-
return <InputGroup ref="inputGroup" bsClass={this.props.bsClass} bsSize={this.props.bsSize} id={this.props.id ? this.props.id + "_group" : null}>
389+
return <InputGroup ref="inputGroup" bsClass={this.props.showClearButton ? this.props.bsClass : ""} bsSize={this.props.bsSize} id={this.props.id ? this.props.id + "_group" : null}>
387390
<Overlay rootClose={true} onHide={this.handleHide} show={this.state.focused} container={() => this.props.calendarContainer || ReactDOM.findDOMNode(this.refs.overlayContainer)} target={() => ReactDOM.findDOMNode(this.refs.input)} placement={this.props.calendarPlacement} delayHide={200}>
388391
<Popover id="calendar" title={calendarHeader}>
389392
<Calendar cellPadding={this.props.cellPadding} selectedDate={this.state.selectedDate} displayDate={this.state.displayDate} onChange={this.onChangeDate} dayLabels={this.state.dayLabels} weekStartsOnMonday={this.props.weekStartsOnMonday} />
@@ -400,8 +403,9 @@ export default React.createClass({
400403
onFocus={this.handleFocus}
401404
onBlur={this.handleBlur}
402405
onChange={this.handleInputChange}
406+
style={{width: "100%"}}
403407
/>
404-
<InputGroup.Addon onClick={this.clear} style={{cursor:this.state.inputValue ? "pointer" : "not-allowed"}}>{this.props.clearButtonElement}</InputGroup.Addon>
408+
{this.props.showClearButton && <InputGroup.Addon onClick={this.clear} style={{cursor:this.state.inputValue ? "pointer" : "not-allowed"}}>{this.props.clearButtonElement}</InputGroup.Addon>}
405409
</InputGroup>;
406410
}
407411
});

test/core.test.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,26 @@ describe("Date Picker", function() {
184184
assert.notEqual("Mx", -1);
185185
ReactDOM.unmountComponentAtNode(container);
186186
}));
187+
it("should render without clear button element", co.wrap(function *(){
188+
const id = UUID.v4();
189+
const clearButtonElement = <div id="clear-button-element"></div>;
190+
const App = React.createClass({
191+
render: function(){
192+
return <div>
193+
<DatePicker
194+
id={id}
195+
clearButtonElement={clearButtonElement}
196+
showClearButton={false}
197+
/>
198+
</div>;
199+
}
200+
});
201+
yield new Promise(function(resolve, reject){
202+
ReactDOM.render(<App />, container, resolve);
203+
});
204+
assert.equal(document.getElementById("clear-button-element"), null);
205+
ReactDOM.unmountComponentAtNode(container);
206+
}));
187207
it("should go to the previous and next month.", co.wrap(function *(){
188208
const id = UUID.v4();
189209
const App = React.createClass({

0 commit comments

Comments
 (0)