Skip to content

Commit 57d0109

Browse files
committed
Stateless DatePicker
1 parent 7aec185 commit 57d0109

File tree

3 files changed

+54
-46
lines changed

3 files changed

+54
-46
lines changed

components/date_picker/dialog.jsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,30 @@ import Dialog from '../dialog';
66

77
class CalendarDialog extends React.Component {
88
static propTypes = {
9+
active: React.PropTypes.bool,
910
initialDate: React.PropTypes.object,
10-
onDateSelected: React.PropTypes.func
11+
onCancel: React.PropTypes.func,
12+
onChange: React.PropTypes.func,
13+
onSelect: React.PropTypes.func
1114
};
1215

1316
static defaultProps = {
17+
active: false,
1418
initialDate: new Date()
1519
};
1620

1721
state = {
18-
active: false,
1922
date: this.props.initialDate,
2023
display: 'months',
2124
actions: [
22-
{ label: 'Cancel', className: style.button, onClick: this.onDateCancel.bind(this) },
23-
{ label: 'Ok', className: style.button, onClick: this.onDateSelected.bind(this) }
25+
{ label: 'Cancel', className: style.button, onClick: this.handleCancel.bind(this) },
26+
{ label: 'Ok', className: style.button, onClick: this.handleSelect.bind(this) }
2427
]
2528
};
2629

2730
handleCalendarChange = (date) => {
2831
this.setState({date, display: 'months'});
32+
if (this.props.onChange) this.props.onChange(date);
2933
};
3034

3135
displayMonths = () => {
@@ -36,25 +40,20 @@ class CalendarDialog extends React.Component {
3640
this.setState({display: 'years'});
3741
};
3842

39-
onDateCancel () {
40-
this.setState({active: false});
41-
}
42-
43-
onDateSelected () {
44-
if (this.props.onDateSelected) this.props.onDateSelected(this.state.date);
45-
this.setState({active: false});
43+
handleCancel () {
44+
if (this.props.onCancel) this.props.onCancel(this.state.date);
4645
}
4746

48-
show () {
49-
this.setState({active: true});
47+
handleSelect () {
48+
if (this.props.onSelect) this.props.onSelect(this.state.date);
5049
}
5150

5251
render () {
5352
const display = `display-${this.state.display}`;
5453
const headerClassName = `${style.header} ${style[display]}`;
5554

5655
return (
57-
<Dialog active={this.state.active} type="custom" className={style.dialog} actions={this.state.actions}>
56+
<Dialog active={this.props.active} type="custom" className={style.dialog} actions={this.state.actions}>
5857
<header className={headerClassName}>
5958
<span className={style.weekday}>
6059
{time.getFullDayOfWeek(this.state.date.getDay())}

components/date_picker/index.jsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import style from './style';
88
class DatePicker extends React.Component {
99
static propTypes = {
1010
className: React.PropTypes.string,
11+
onChange: React.PropTypes.func,
1112
value: React.PropTypes.object
1213
};
1314

@@ -16,17 +17,22 @@ class DatePicker extends React.Component {
1617
};
1718

1819
state = {
19-
value: this.props.value
20+
value: this.props.value,
21+
dialog: false
2022
};
2123

22-
handleDateSelected = (value) => {
23-
this.refs.input.setValue(this.formatDate(value));
24-
this.setState({value});
24+
handleCalendarCancel = () => {
25+
this.setState({dialog: false});
26+
};
27+
28+
handleCalendarChange = (value) => {
29+
this.setState({dialog: false, value: value});
30+
if (this.props.onChange) this.props.onChange(value);
2531
};
2632

2733
handleMouseDown = (event) => {
2834
events.pauseEvent(event);
29-
this.refs.dialog.show();
35+
this.setState({dialog: true});
3036
};
3137

3238
formatDate (date) {
@@ -47,20 +53,15 @@ class DatePicker extends React.Component {
4753
/>
4854
<CalendarDialog
4955
ref='dialog'
56+
active={this.state.dialog}
5057
initialDate={this.state.value}
51-
onDateSelected={this.handleDateSelected}
58+
onCancel={this.handleCalendarCancel}
59+
onChange={this.handleCalendarChange}
60+
onSelect={this.handleCalendarChange}
5261
/>
5362
</div>
5463
);
5564
}
56-
57-
getValue () {
58-
return this.state.value;
59-
}
60-
61-
setValue (value) {
62-
this.setState({value});
63-
}
6465
}
6566

6667
export default DatePicker;

spec/components/pickers.jsx

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@ import React from 'react';
22
import DatePicker from '../../components/date_picker';
33
import TimePicker from '../../components/time_picker';
44

5-
const PickersTest = () => {
6-
const datetime = new Date(1995, 11, 17);
7-
datetime.setHours(17);
8-
datetime.setMinutes(28);
9-
10-
return (
11-
<section>
12-
<h5>Pickers</h5>
13-
<p>Date pickers and time pickers with Material flavour.</p>
14-
15-
<DatePicker />
16-
<DatePicker value={datetime} />
17-
18-
<TimePicker />
19-
<TimePicker value={datetime} format='ampm' />
20-
</section>
21-
);
22-
};
5+
const datetime = new Date(1995, 11, 17);
6+
datetime.setHours(17);
7+
datetime.setMinutes(28);
8+
9+
class PickersTest extends React.Component {
10+
11+
handleDatePickerChange = (value) => {
12+
console.log('handleDatePickerChange', value);
13+
};
14+
15+
render () {
16+
return (
17+
<section>
18+
<h5>Pickers</h5>
19+
<p>Date pickers and time pickers with Material flavour.</p>
20+
21+
<DatePicker onChange={this.handleDatePickerChange}/>
22+
<DatePicker value={datetime} />
23+
24+
<TimePicker />
25+
<TimePicker value={datetime} format='ampm' />
26+
</section>
27+
);
28+
}
29+
30+
}
2331

2432
export default PickersTest;

0 commit comments

Comments
 (0)