Skip to content

Commit

Permalink
add trigger prop for Picker.Fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed May 19, 2015
1 parent 45df326 commit c25693c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# History
----

## 3.4.0 / 2015-05-19

`new` [#21](https://github.com/react-component/calendar/issues/21) add trigger prop for DatePicker

## 3.3.0 / 2015-05-13

use jsx as extension name
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ For details to see: [https://github.com/yiminghe/learning-react/tree/master/exam
<td></td>
<td>use to format/parse value to/from input</td>
</tr>
<tr>
<td>trigger</td>
<td>React.Element</td>
<td></td>
<td>additional trigger appended to picker</td>
</tr>
<tr>
<td>value</td>
<td>GregorianCalendar</td>
Expand Down
13 changes: 4 additions & 9 deletions examples/ant-design-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var style = `
.datepicker-icon {
position: absolute;
-webkit-user-select:none;
}
.datepicker-icon:after {
Expand All @@ -72,12 +73,6 @@ var style = `
`;

var Test = React.createClass({
open: function () {
this.refs.picker.setState({
open: true
});
},

handleChange: function (value) {
console.log('DatePicker change: ' + (value && this.props.formatter.format(value)));
},
Expand Down Expand Up @@ -127,12 +122,12 @@ var Test = React.createClass({
showTime</span>
</div>
<div className="date-picker-wrap">
<DatePicker ref='picker' formatter={this.props.formatter} calendar={calendar}
<DatePicker
trigger={<span className="datepicker-icon" />}
formatter={this.props.formatter} calendar={calendar}
value={state.value} onChange={this.handleChange}>
<input type="text" className="datepicker-input" style={{background: 'white', cursor: 'pointer'}}/>
</DatePicker>
<span className="datepicker-icon" onClick={this.open}>
</span>
</div>
</div>;
}
Expand Down
18 changes: 12 additions & 6 deletions examples/picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ var GregorianCalendar = require('gregorian-calendar');
var CalendarLocale = require('../lib/locale/zh-cn');

var Test = React.createClass({
open: function () {
this.refs.picker.setState({
open: true
});
toggle: function () {
this.refs.picker.toggle();
},

handleChange: function (value) {
Expand Down Expand Up @@ -55,7 +53,7 @@ var Test = React.createClass({
var state = this.state;
var calendar = <Calendar locale={CalendarLocale}
orient={['top', 'left']}
showTime={this.state.showTime} onSelect={this.handleCalendarSelect} onClear={this.handleCalendarSelect.bind(this,null)} showClear={true}/>;
showTime={this.state.showTime} onSelect={this.handleCalendarSelect} onClear={this.handleCalendarSelect.bind(this, null)} showClear={true}/>;
return <div className="form-group" style={{width: 400, margin: 20}} data-time={this.state.time}>
<div className="input-group">
<span>
Expand All @@ -67,14 +65,22 @@ var Test = React.createClass({
value={state.value} onChange={this.handleChange}>
<input type="text" className="form-control" style={{background: 'white', cursor: 'pointer'}}/>
</DatePicker>
<span className="input-group-addon" onClick={this.open}>
<span className="input-group-addon"
style={{'-webkit-user-select': 'none'}}
onMouseDown={prevent}
unselectable="unselectable"
onClick={this.toggle}>
<span className="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>;
}
});

function prevent(e) {
e.preventDefault();
}

React.render(<div>
<h1>zh-cn</h1>
<Test />
Expand Down
32 changes: 27 additions & 5 deletions lib/Picker.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var React = require('react');
var DateTimeFormat = require('gregorian-calendar-format');
var rcUtil = require('rc-util');
Expand Down Expand Up @@ -39,7 +38,8 @@ class Picker extends React.Component {

// bind methods
[
'handleInputClick', 'handleCalendarBlur', 'handleCalendarClear', 'handleCalendarKeyDown',
'handleInputClick', 'handleCalendarBlur', 'handleTriggerClick',
'handleCalendarClear', 'handleCalendarKeyDown',
'handleKeyDown', 'handleCalendarSelect'
].forEach(m => {
this[m] = this[m].bind(this);
Expand Down Expand Up @@ -69,14 +69,24 @@ class Picker extends React.Component {
}

handleInputClick() {
this.open();
this.toggle();
}

handleTriggerClick() {
this.toggle();
}

toggle(callback) {
this.setState({
open: !this.state.open
}, callback);
}

handleKeyDown(e) {
// down
if (e.keyCode === KeyCode.DOWN) {
e.preventDefault();
this.handleInputClick();
this.open();
}
}

Expand Down Expand Up @@ -219,10 +229,22 @@ class Picker extends React.Component {
if (state.open) {
classes.push(props.prefixCls + '-open');
}
return <span className={classes.join(' ')}>{[input, calendar]}</span>;
var trigger = props.trigger;
if (trigger) {
trigger = React.cloneElement(trigger, {
onClick: this.handleTriggerClick,
unselectable: true,
onMouseDown: prevent
});
}
return <span className={classes.join(' ')}>{[input, calendar, trigger]}</span>;
}
}

function prevent(e) {
e.preventDefault();
}

Picker.propTypes = {
onChange: React.PropTypes.func
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-calendar",
"version": "3.3.0",
"version": "3.4.0",
"description": "calendar ui component for react",
"keywords": [
"react",
Expand Down

0 comments on commit c25693c

Please sign in to comment.