Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit c191135

Browse files
committed
expose initialFocusedRange, focusedRange, onRangeFocusChange
expose preview, showPreview, onPreviewChange rename showSelectionPreview to showPreview
1 parent 8febab5 commit c191135

File tree

7 files changed

+54
-36
lines changed

7 files changed

+54
-36
lines changed

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Momentjs: `moment(dateString).toDate()`
111111
- `disableDaysBeforeToday` prop removed. use `minDate={new Date()}` instead.
112112
- `firstDayOfWeek` prop removed. It is auto detecting from locale prop.
113113
- `init` prop removed.
114+
- `specialDays` prop removed.
114115

115116
### Added
116117
- `DefinedRanges` component: It's a set of date presets. Receives `inputRanges`, `staticRanges` for setting date ranges.
@@ -123,4 +124,16 @@ Momentjs: `moment(dateString).toDate()`
123124
<DateRangePicker scroll={horizontalScroll} />
124125
<DateRangePicker scroll={verticalScroll} months={2} />
125126
```
126-
127+
- `showPreview` prop added to control visibility of preview. Default value is `true`.
128+
- `preview` prop added: It displays a preview range and overwrite DateRange's default preview. You can set a controlled preview with below shape of object.
129+
```js
130+
{
131+
startDate: [Date Object] || null,
132+
endDate: [Date Object] || null,
133+
color: '#fed14c',
134+
}
135+
```
136+
- `onPreviewChange(date)` prop added: Callback function for preview changes. You can set controlled custom previews with `preview` prop.
137+
- `focusedRange` prop added: It defines which range and step are focused. Common initial value is `[0, 0]`; first value is index of ranges, second value is which step on date range(startDate or endDate).
138+
- `initialFocusedRange` prop added: Initial value for focused range. See `focusedRange` for usage.
139+
- `onRangeFocusChange` prop added: Callback function for focus changes by user.

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ shownDate | Date | | initial fo
108108
minDate | Date | | defines minimum date. Disabled earlier dates
109109
maxDate | Date | | defines maximum date. Disabled later dates
110110
direction | String | 'vertical' | direction of calendar months. can be `vertical` or `horizontal`
111-
scroll | Object | { enabled: false }| infinite scroll behaviour configuration. Checkout [Infinite Scroll](#infinite-scrolled-mode) section
111+
scroll | Object | { enabled: false }| infinite scroll behaviour configuration. Check out [Infinite Scroll](#infinite-scrolled-mode) section
112112
showMonthArrow | Boolean | true | show/hide month arrow button
113113
navigatorRenderer | Func | | renderer for focused date navigation area. fn(currentFocusedDate: Date, changeShownDate: func, props: object)
114114
ranges | *Object[] | [] | Defines ranges. array of range object
@@ -118,7 +118,13 @@ onChange(DateRange) | Func | | callback f
118118
color(Calendar) | String | `#3d91ff` | defines color for selected date in Calendar
119119
date(Calendar) | Date | | date value for Calendar
120120
showDateDisplay(DateRange) | Boolean | true | show/hide selection display row. Uses `dateDisplayFormat` for formatter
121-
dateDisplayFormat(DateRange) | String | `MMM D,YYYY` | selected range preview formatter. checkout [date-fns's format option](https://date-fns.org/v2.0.0-alpha.7/docs/format)
121+
initialFocusedRange(DateRange) | Object | | Initial value for focused range. See `focusedRange` for usage.
122+
focusedRange(DateRange) | Object | | It defines which range and step are focused. Common initial value is `[0, 0]`; first value is index of ranges, second one is which step on date range(startDate or endDate).
123+
onRangeFocusChange(DateRange) | Object | | Callback function for focus changes by user
124+
preview(DateRange) | Object | | displays a preview range and overwrite DateRange's default preview. Expected shape: `{ startDate: Date, endDate: Date, color: String }`
125+
showPreview(DateRange) | bool | true | visibility of preview
126+
onPreviewChange(DateRange) | Object | | Callback function for preview changes
127+
dateDisplayFormat(DateRange) | String | `MMM D, YYYY` | selected range preview formatter. Check out [date-fns's format option](https://date-fns.org/v2.0.0-alpha.7/docs/format)
122128
staticRanges(`DefinedRange`, `DateRangePicker`) | Array | [default preDefined ranges](https://github.com/Adphorus/react-date-range/blob/master/src/defaultRanges.js) | -
123129
inputRanges(`DefinedRange`, `DateRangePicker`) | Array | [default input ranges](https://github.com/Adphorus/react-date-range/blob/master/src/defaultRanges.js) | -
124130

@@ -138,8 +144,8 @@ inputRanges(`DefinedRange`, `DateRangePicker`) | Array | [default input ranges
138144

139145
#### Infinite Scrolled Mode
140146

141-
To enable infinite scroll set `scroll={{enabled: true}}` basically. Infinite scroll feature is affected by `direction`(rendering direction for months) and `months`(for rendered months count) props directly.
142-
If you prefer, you can overwrite calendar sizes with `calendarWidth`/`calendarHeight` or each month's height/withs with `monthWidth`/`monthHeight`/`longMonthHeight` at `scroll` prop.
147+
To enable infinite scroll set `scroll={{enabled: true}}` basically. Infinite scroll feature is affected by `direction`(rendering direction for months) and `months`(for rendered months count) props directly.
148+
If you prefer, you can overwrite calendar sizes with `calendarWidth`/`calendarHeight` or each month's height/withs with `monthWidth`/`monthHeight`/`longMonthHeight` at `scroll` prop.
143149

144150
```js
145151
// shape of scroll prop

src/components/Calendar.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ Calendar.defaultProps = {
448448
dateDisplayFormat: 'MMM D, YYYY',
449449
monthDisplayFormat: 'MMM YYYY',
450450
showDateDisplay: true,
451-
showSelectionPreview: true,
451+
showPreview: true,
452452
displayMode: 'date',
453453
months: 1,
454454
color: '#3d91ff',
@@ -477,15 +477,16 @@ Calendar.propTypes = {
477477
preview: PropTypes.shape({
478478
startDate: PropTypes.object,
479479
endDate: PropTypes.object,
480+
color: PropTypes.string,
480481
}),
481-
previewColor: PropTypes.string,
482482
dateDisplayFormat: PropTypes.string,
483483
monthDisplayFormat: PropTypes.string,
484484
focusedRange: PropTypes.arrayOf(PropTypes.number),
485+
initialFocusedRange: PropTypes.arrayOf(PropTypes.number),
485486
months: PropTypes.number,
486487
className: PropTypes.string,
487488
showDateDisplay: PropTypes.bool,
488-
showSelectionPreview: PropTypes.bool,
489+
showPreview: PropTypes.bool,
489490
displayMode: PropTypes.oneOf(['dateRange', 'date']),
490491
color: PropTypes.string,
491492
updateRange: PropTypes.func,

src/components/DateRange.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class DateRange extends Component {
1515
this.updatePreview = this.updatePreview.bind(this);
1616
this.calcNewSelection = this.calcNewSelection.bind(this);
1717
this.state = {
18-
focusedRange: [findNextRangeIndex(props.ranges), 0],
18+
focusedRange: props.initialFocusedRange || [findNextRangeIndex(props.ranges), 0],
1919
preview: null,
2020
};
2121
this.styles = generateStyles([coreStyles, props.classNames]);
2222
}
2323
calcNewSelection(value, isSingleValue = true) {
24-
const { focusedRange } = this.state;
24+
const focusedRange = this.props.focusedRange || this.state.focusedRange;
2525
const { ranges, onChange, maxDate, moveRangeOnFirstSelection } = this.props;
2626
const focusedRangeIndex = focusedRange[0];
2727
const selectedRange = ranges[focusedRangeIndex];
@@ -58,8 +58,8 @@ class DateRange extends Component {
5858
};
5959
}
6060
setSelection(value, isSingleValue) {
61-
const { onChange, ranges } = this.props;
62-
const { focusedRange } = this.state;
61+
const { onChange, ranges, onRangeFocusChange } = this.props;
62+
const focusedRange = this.props.focusedRange || this.state.focusedRange;
6363
const focusedRangeIndex = focusedRange[0];
6464
const selectedRange = ranges[focusedRangeIndex];
6565
if (!selectedRange) return;
@@ -74,6 +74,7 @@ class DateRange extends Component {
7474
focusedRange: newSelection.nextFocusRange,
7575
preview: null,
7676
});
77+
onRangeFocusChange && onRangeFocusChange(newSelection.nextFocusRange);
7778
}
7879
handleRangeFocusChange(focusedRange) {
7980
this.setState({ focusedRange });
@@ -85,24 +86,24 @@ class DateRange extends Component {
8586
return;
8687
}
8788
const { rangeColors, ranges } = this.props;
88-
const { focusedRange } = this.state;
89+
const focusedRange = this.props.focusedRange || this.state.focusedRange;
8990
const color = ranges[focusedRange[0]].color || rangeColors[focusedRange[0]] || color;
9091
this.setState({ preview: { ...val, color } });
9192
}
9293
render() {
9394
return (
9495
<Calendar
95-
{...this.props}
96-
displayMode="dateRange"
97-
className={classnames(this.styles.dateRangeWrapper, this.props.className)}
98-
onChange={this.setSelection}
9996
focusedRange={this.state.focusedRange}
10097
onRangeFocusChange={this.handleRangeFocusChange}
10198
preview={this.state.preview}
102-
updateRange={val => this.setSelection(val, false)}
10399
onPreviewChange={value => {
104100
this.updatePreview(value ? this.calcNewSelection(value).range : null);
105101
}}
102+
{...this.props}
103+
displayMode="dateRange"
104+
className={classnames(this.styles.dateRangeWrapper, this.props.className)}
105+
onChange={this.setSelection}
106+
updateRange={val => this.setSelection(val, false)}
106107
ref={target => {
107108
this.calendar = target;
108109
}}

src/components/DateRangePicker.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,21 @@ class DateRangePicker extends Component {
1515
this.styles = generateStyles([coreStyles, props.classNames]);
1616
}
1717
render() {
18-
const focusedRangeIndex = this.state.focusedRange[0];
18+
const { focusedRange } = this.state;
1919
return (
2020
<div className={classnames(this.styles.dateRangePickerWrapper, this.props.className)}>
2121
<DefinedRange
22+
focusedRange={focusedRange}
23+
onPreviewChange={value => this.dateRange.updatePreview(value)}
2224
{...this.props}
23-
range={this.props.ranges[focusedRangeIndex] || {}}
24-
onPreviewChange={value => {
25-
this.dateRange.updatePreview(value);
26-
}}
27-
focusedRangeIndex={focusedRangeIndex}
25+
range={this.props.ranges[focusedRange[0]]}
2826
className={undefined}
2927
/>
3028
<DateRange
31-
{...this.props}
32-
ref={t => {
33-
this.dateRange = t;
34-
}}
3529
onRangeFocusChange={focusedRange => this.setState({ focusedRange })}
30+
focusedRange={focusedRange}
31+
{...this.props}
32+
ref={t => (this.dateRange = t)}
3633
className={undefined}
3734
/>
3835
</div>

src/components/DefinedRange.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class DefinedRanges extends Component {
1515
this.handleRangeChange = this.handleRangeChange.bind(this);
1616
}
1717
handleRangeChange(range) {
18-
const { onChange, ranges, focusedRangeIndex } = this.props;
19-
const selectedRange = ranges[focusedRangeIndex];
18+
const { onChange, ranges, focusedRange } = this.props;
19+
const selectedRange = ranges[focusedRange[0]];
2020
if (!onChange || !selectedRange) return;
2121
onChange({
22-
[selectedRange.key || `range${focusedRangeIndex + 1}`]: { ...selectedRange, ...range },
22+
[selectedRange.key || `range${focusedRange[0] + 1}`]: { ...selectedRange, ...range },
2323
});
2424
}
2525
getSelectedRange(ranges, staticRange) {
@@ -81,7 +81,7 @@ class DefinedRanges extends Component {
8181
max={99999}
8282
value={
8383
rangeOption.getCurrentValue
84-
? rangeOption.getCurrentValue(ranges[this.props.focusedRangeIndex] || {})
84+
? rangeOption.getCurrentValue(ranges[this.props.focusedRange[0]] || {})
8585
: '-'
8686
}
8787
/>
@@ -99,7 +99,7 @@ DefinedRanges.propTypes = {
9999
inputRanges: PropTypes.array,
100100
staticRanges: PropTypes.array,
101101
ranges: PropTypes.arrayOf(rangeShape),
102-
focusedRangeIndex: PropTypes.number,
102+
focusedRange: PropTypes.arrayOf(PropTypes.number),
103103
onPreviewChange: PropTypes.func,
104104
onChange: PropTypes.func,
105105
footerContent: PropTypes.any,
@@ -113,7 +113,7 @@ DefinedRanges.defaultProps = {
113113
staticRanges: defaultStaticRanges,
114114
ranges: [],
115115
rangeColors: ['#3d91ff', '#3ecf8e', '#fed14c'],
116-
focusedRangeIndex: 0,
116+
focusedRange: [0, 0],
117117
};
118118

119119
export default DefinedRanges;

src/components/Month.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Month extends PureComponent {
5252
};
5353
});
5454
}
55-
const showPreview = this.props.showSelectionPreview && !drag.disablePreview;
55+
const showPreview = this.props.showPreview && !drag.disablePreview;
5656
return (
5757
<div className={styles.month} style={this.props.style}>
5858
{this.props.showMonthName ? (
@@ -116,7 +116,7 @@ Month.propTypes = {
116116
startDate: PropTypes.object,
117117
endDate: PropTypes.object,
118118
}),
119-
showSelectionPreview: PropTypes.bool,
119+
showPreview: PropTypes.bool,
120120
displayMode: PropTypes.oneOf(['dateRange', 'date']),
121121
minDate: PropTypes.object,
122122
maxDate: PropTypes.object,

0 commit comments

Comments
 (0)