Skip to content

Commit 73fb8f3

Browse files
committed
Merge pull request react-toolbox#116 from react-toolbox/datepicker
Datepicker
2 parents e285625 + 0e54523 commit 73fb8f3

File tree

13 files changed

+89
-55
lines changed

13 files changed

+89
-55
lines changed

components/date_picker/_config.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ $datepicker-primary-color: unquote("rgb(#{$datepicker-primary})") !default;
55
$datepicker-primary-hover-color: unquote("rgba(#{$datepicker-primary}, 0.20)") !default;
66
$datepicker-primary-contrast-color: unquote("rgb(#{$datepicker-primary-contrast})") !default;
77
$datepicker-primary-dark-color: unquote("rgb(#{$datepicker-primary-dark})") !default;
8-
$datepicker-dialog-width: 30 * $unit;
8+
$datepicker-dialog-width: 33 * $unit;
99
$datepicker-inactive-opacity: .6;
1010
$datepicker-weekday-line-height: 2 * $unit;
1111
$datepicker-weekday-font-size: $font-size-small;
1212
$datepicker-month-font-size: $font-size-big;
1313
$datepicker-day-font-size: 5 * $unit;
1414
$datepicker-day-line-height: 4 * $unit;
15-
$datepicker-year-font-size: 1.8 * $unit;
15+
$datepicker-year-font-size: $font-size-small;

components/date_picker/calendar/_config.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ $calendar-arrows-font-size: 2 * $unit;
88
$calendar-arrows-ripple-duration: 450ms;
99
$calendar-year-font-size: 2.4;
1010
$calendar-day-font-size: 1.3 * $unit;
11+
$calendar-day-disable-opacity: 0.25;
1112
$calendar-row-height: 3 * $unit;
1213
$calendar-day-padding: .2 * $unit;
1314
$calendar-total-height: $calendar-row-height * 8 + $calendar-day-padding * 12;

components/date_picker/calendar/day.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import style from './style';
55
class Day extends React.Component {
66
static propTypes = {
77
day: React.PropTypes.number,
8+
disabled: React.PropTypes.bool,
89
onClick: React.PropTypes.func,
910
selectedDate: React.PropTypes.object,
1011
viewDate: React.PropTypes.object
@@ -26,7 +27,9 @@ class Day extends React.Component {
2627
}
2728

2829
render () {
29-
const className = this.isSelected() ? `${style.day} ${style.active}` : style.day;
30+
let className = this.isSelected() ? `${style.day} ${style.active}` : style.day;
31+
if (this.props.disabled) className += ` ${style.disabled}`;
32+
3033
return (
3134
<div className={className} style={this.dayStyle()}>
3235
<span onClick={this.props.onClick}>

components/date_picker/calendar/index.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import style from './style';
1010
class Calendar extends React.Component {
1111
static propTypes = {
1212
display: React.PropTypes.oneOf(['months', 'years']),
13+
maxDate: React.PropTypes.object,
14+
minDate: React.PropTypes.object,
1315
onChange: React.PropTypes.func,
1416
selectedDate: React.PropTypes.object,
1517
viewDate: React.PropTypes.object
@@ -98,6 +100,8 @@ class Calendar extends React.Component {
98100
<CSSTransitionGroup transitionName={animation} transitionEnterTimeout={350} transitionLeaveTimeout={350}>
99101
<Month
100102
key={this.state.viewDate.getMonth()}
103+
maxDate={this.props.maxDate}
104+
minDate={this.props.minDate}
101105
viewDate={this.state.viewDate}
102106
selectedDate={this.props.selectedDate}
103107
onDayClick={this.handleDayClick} />

components/date_picker/calendar/month.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import style from './style';
55

66
class Month extends React.Component {
77
static propTypes = {
8+
maxDate: React.PropTypes.object,
9+
minDate: React.PropTypes.object,
810
onDayClick: React.PropTypes.func,
911
selectedDate: React.PropTypes.object,
1012
viewDate: React.PropTypes.object
@@ -22,11 +24,15 @@ class Month extends React.Component {
2224

2325
renderDays () {
2426
return utils.range(1, utils.time.getDaysInMonth(this.props.viewDate) + 1).map(i => {
27+
const date = new Date(this.props.viewDate.getFullYear(), this.props.viewDate.getMonth(), i);
28+
const disabled = utils.time.dateOutOfRange(date, this.props.minDate, this.props.maxDate);
29+
2530
return (
2631
<Day
2732
key={i}
2833
day={i}
29-
onClick={this.handleDayClick.bind(this, i)}
34+
disabled={disabled}
35+
onClick={!disabled ? this.handleDayClick.bind(this, i) : null}
3036
selectedDate={this.props.selectedDate}
3137
viewDate={this.props.viewDate}
3238
/>

components/date_picker/calendar/style.scss

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,19 @@
5050
.week {
5151
display: flex;
5252
height: $calendar-row-height;
53+
flex-wrap: wrap;
5354
font-size: $calendar-day-font-size;
5455
line-height: $calendar-row-height;
5556
opacity: .5;
56-
57-
flex-wrap: wrap;
5857
> span {
5958
flex: 0 0 (100% / 7);
6059
}
6160
}
6261

6362
.days {
6463
display: flex;
65-
font-size: $calendar-day-font-size;
66-
6764
flex-wrap: wrap;
65+
font-size: $calendar-day-font-size;
6866
}
6967

7068
.day {
@@ -75,17 +73,22 @@
7573
width: $calendar-row-height;
7674
height: $calendar-row-height;
7775
line-height: $calendar-row-height;
78-
cursor: pointer;
7976
border-radius: 50%;
8077
}
81-
&:hover:not(.active) > span {
78+
&:hover:not(.active):not(.disabled) > span {
8279
color: $calendar-primary-contrast-color;
8380
background: $calendar-primary-hover-color;
8481
}
8582
&.active > span {
8683
color: $calendar-primary-contrast-color;
8784
background: $calendar-primary-color;
8885
}
86+
&:hover:not(.disabled) > span {
87+
cursor: pointer;
88+
}
89+
&.disabled {
90+
opacity: $calendar-day-disable-opacity;
91+
}
8992
}
9093

9194
.month {

components/date_picker/dialog.jsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React from 'react';
2-
import style from './style';
3-
import time from '../utils/time';
42
import Calendar from './calendar';
53
import Dialog from '../dialog';
4+
import style from './style';
5+
import time from '../utils/time';
66

77
class CalendarDialog extends React.Component {
88
static propTypes = {
99
active: React.PropTypes.bool,
10+
maxDate: React.PropTypes.object,
11+
minDate: React.PropTypes.object,
1012
onDismiss: React.PropTypes.func,
1113
onSelect: React.PropTypes.func,
1214
value: React.PropTypes.object
@@ -23,7 +25,11 @@ class CalendarDialog extends React.Component {
2325
};
2426

2527
handleCalendarChange = (value) => {
26-
this.setState({date: value, display: 'months'});
28+
const state = {display: 'months', date: value};
29+
if (time.dateOutOfRange(value, this.props.minDate, this.props.maxDate)) {
30+
state.date = this.props.maxDate || this.props.minDate;
31+
}
32+
this.setState(state);
2733
};
2834

2935
handleSelect = () => {
@@ -46,21 +52,19 @@ class CalendarDialog extends React.Component {
4652
return (
4753
<Dialog active={this.props.active} type="custom" className={style.dialog} actions={this.actions}>
4854
<header className={headerClassName}>
49-
<span className={style.weekday}>
50-
{time.getFullDayOfWeek(this.state.date.getDay())}
51-
</span>
52-
<div onClick={this.handleSwitchDisplay.bind(this, 'months')}>
53-
<span className={style.month}>{time.getShortMonth(this.state.date)}</span>
54-
<span className={style.day}>{this.state.date.getDate()}</span>
55-
</div>
5655
<span className={style.year} onClick={this.handleSwitchDisplay.bind(this, 'years')}>
5756
{this.state.date.getFullYear()}
5857
</span>
58+
<h3 className={style.date} onClick={this.handleSwitchDisplay.bind(this, 'months')}>
59+
{time.getShortDayOfWeek(this.state.date.getDay())}, {time.getShortMonth(this.state.date)} {this.state.date.getDate()}
60+
</h3>
5961
</header>
6062

6163
<div className={style.wrapper}>
6264
<Calendar
6365
display={this.state.display}
66+
maxDate={this.props.maxDate}
67+
minDate={this.props.minDate}
6468
onChange={this.handleCalendarChange}
6569
selectedDate={this.state.date} />
6670
</div>

components/date_picker/index.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React from 'react';
22
import CalendarDialog from './dialog';
3-
import Input from '../input';
43
import events from '../utils/events';
5-
import time from '../utils/time';
4+
import Input from '../input';
65
import style from './style';
6+
import time from '../utils/time';
77

88
class DatePicker extends React.Component {
99
static propTypes = {
10+
maxDate: React.PropTypes.object,
11+
minDate: React.PropTypes.object,
1012
onChange: React.PropTypes.func,
1113
value: React.PropTypes.object
1214
};
@@ -32,6 +34,7 @@ class DatePicker extends React.Component {
3234
render () {
3335
const { value } = this.props;
3436
const date = value ? `${value.getDate()} ${time.getFullMonth(value)} ${value.getFullYear()}` : null;
37+
3538
return (
3639
<div data-toolbox='date-picker'>
3740
<Input
@@ -44,6 +47,8 @@ class DatePicker extends React.Component {
4447
/>
4548
<CalendarDialog
4649
active={this.state.active}
50+
maxDate={this.props.maxDate}
51+
minDate={this.props.minDate}
4752
onDismiss={this.handleDismiss}
4853
onSelect={this.handleSelect}
4954
value={this.props.value}

components/date_picker/readme.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ A [dialog](https://www.google.com/design/spec/components/pickers.html#pickers-da
66
```jsx
77
import DatePicker from 'react-toolbox/lib/date_picker';
88

9-
const datetime = new Date(1995, 11, 17);
9+
const datetime = new Date(2015, 10, 16);
10+
const min_datetime = new Date(new Date(datetime).setDate(8));
1011
datetime.setHours(17);
1112
datetime.setMinutes(28);
1213

@@ -25,7 +26,7 @@ class DatePickerTest extends React.Component {
2526
return (
2627
<section>
2728
<DatePicker value={this.state.date1} onChange={this.handleChange.bind(this, 'date1')} />
28-
<DatePicker value={this.state.date2} onChange={this.handleChange.bind(this, 'date2')} />
29+
<DatePicker minDate={min_datetime} value={this.state.date2} onChange={this.handleChange.bind(this, 'date2')} />
2930
</section>
3031
);
3132
}
@@ -36,5 +37,7 @@ class DatePickerTest extends React.Component {
3637

3738
| Name | Type | Default | Description|
3839
|:-----|:-----|:-----|:-----|
40+
| `maxDate` | `Date` | | Date object with the maximum selectable date. |
41+
| `minDate` | `Date` | | Date object with the minimum selectable date. |
3942
| `onChange` | `Function` | | Callback called when the picker value is changed.|
4043
| `value` | `Date` | | Date object with the currently selected date. |

components/date_picker/style.scss

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,36 @@
66
}
77

88
.header {
9-
padding-bottom: $unit;
9+
padding: 1.6 * $unit 2 * $unit;
1010
color: $datepicker-primary-contrast-color;
11-
text-align: center;
1211
background-color: $datepicker-primary-color;
13-
}
14-
15-
.weekday {
16-
display: block;
17-
width: 100%;
18-
font-size: $datepicker-weekday-font-size;
19-
line-height: $datepicker-weekday-line-height;
20-
background-color: $datepicker-primary-dark-color;
21-
}
22-
23-
.month {
24-
display: block;
25-
padding: $unit;
26-
font-size: $datepicker-month-font-size;
27-
text-transform: uppercase;
2812
cursor: pointer;
2913
}
3014

31-
.day {
32-
display: block;
33-
font-size: $datepicker-day-font-size;
34-
line-height: $datepicker-day-line-height;
35-
cursor: pointer;;
36-
}
37-
3815
.year {
3916
display: inline-block;
40-
margin-top: $unit;
4117
font-size: $datepicker-year-font-size;
42-
cursor: pointer;
18+
transition: opacity, font-size $animation-duration $animation-curve-default;
19+
}
20+
21+
.date {
22+
display: block;
23+
font-weight: $font-weight-semi-bold;
24+
text-transform: capitalize;
25+
transition: opacity $animation-duration $animation-curve-default;
4326
}
4427

4528
.wrapper {
4629
padding: $unit .5 * $unit 0;
4730
}
4831

4932
.display-years {
50-
.day, .month {
33+
.date {
5134
opacity: $datepicker-inactive-opacity;
5235
}
36+
.year {
37+
font-size: $font-size-normal;
38+
}
5339
}
5440

5541
.display-months {

0 commit comments

Comments
 (0)