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

Commit 8d0fec3

Browse files
committed
Change folder structure
1 parent 0dc22f3 commit 8d0fec3

File tree

26 files changed

+1669
-5119
lines changed

26 files changed

+1669
-5119
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"presets": [
33
["@babel/preset-env", { "targets": { "node": "current" } }],
4-
["@babel/preset-react"],
4+
["@babel/preset-react"]
55
],
66
"plugins": [
77
["@babel/plugin-proposal-class-properties"],

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
"description": "A React component for choosing dates and date ranges.",
55
"main": "dist/index.js",
66
"scripts": {
7+
"styleguide": "styleguidist server",
8+
"styleguide:build": "styleguidist build",
79
"dev": "NODE_ENV=development & webpack-dev-server --config webpack/webpack.config.dev.js",
810
"build": "NODE_ENV=production & yarn build-library & yarn build-demo",
911
"build-demo": "webpack --config webpack/webpack.config.prod.js",
10-
"build-library": "babel ./src --out-dir ./dist --ignore test.js & postcss 'src/styles.scss' -d dist --ext css & postcss 'src/theme/*.scss' -d 'dist/theme' --ext css",
12+
"build-library": "babel ./src --out-dir ./dist & postcss 'src/styles.scss' -d dist --ext css & postcss 'src/theme/*.scss' -d 'dist/theme' --ext css",
1113
"lint": "eslint 'src/**/*.js'",
1214
"test": "jest",
1315
"preversion": "yarn clear & yarn build"
@@ -68,14 +70,15 @@
6870
"eslint-plugin-jsx-a11y": "^6.2.3",
6971
"eslint-plugin-prettier": "^3.1.2",
7072
"eslint-plugin-react": "^7.17.0",
71-
"extract-text-webpack-plugin": "^4.0.0-beta.0",
7273
"html-webpack-plugin": "^3.2.0",
7374
"jest": "^24.9.0",
75+
"mini-css-extract-plugin": "^0.9.0",
7476
"node-sass": "^4.13.0",
7577
"postcss": "^7.0.25",
7678
"postcss-cli": "^6.1.3",
7779
"postcss-loader": "^3.0.0",
7880
"prettier": "^1.19.1",
81+
"react-styleguidist": "^10.4.0",
7982
"sass-loader": "^8.0.0",
8083
"style-loader": "^1.0.0",
8184
"webpack": "^4.31.0",

src/components/Calendar/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
DatePicker - Internationalization
2+
3+
```jsx inside Markdown
4+
import Calendar from 'react-date-range/components/Calendar';
5+
import * as locales from 'react-date-range/locale';
6+
//import '../../dist/styles.css'; // main style file
7+
//import 'react-date-range/dist/theme/default.css'; // theme css file
8+
9+
const [locale, setLocale] = React.useState('ja');
10+
<Calendar
11+
locale={locales[locale]}
12+
date={null}
13+
className={'PreviewArea'}
14+
/>
15+
16+
```
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { PureComponent } from 'react';
22
import PropTypes from 'prop-types';
3-
import { rangeShape } from './DayCell.js';
4-
import Month from './Month.js';
5-
import DateInput from './DateInput';
6-
import { calcFocusDate, generateStyles, getMonthDisplayRange } from '../utils';
3+
import { rangeShape } from '../DayCell';
4+
import Month from '../Month';
5+
import DateInput from '../DateInput';
6+
import { calcFocusDate, generateStyles, getMonthDisplayRange } from '../../utils';
77
import classnames from 'classnames';
88
import ReactList from 'react-list';
99
import {
@@ -26,7 +26,7 @@ import {
2626
max,
2727
} from 'date-fns';
2828
import defaultLocale from 'date-fns/locale/en-US';
29-
import coreStyles from '../styles';
29+
import coreStyles from '../../styles';
3030

3131
class Calendar extends PureComponent {
3232
constructor(props, context) {
@@ -47,6 +47,7 @@ class Calendar extends PureComponent {
4747
this.styles = generateStyles([coreStyles, props.classNames]);
4848
this.listSizeCache = {};
4949
this.monthNames = [...Array(12).keys()].map(i => props.locale.localize.month(i));
50+
this.isFirstRender = true;
5051
this.state = {
5152
focusedDate: calcFocusDate(null, props),
5253
drag: {
@@ -55,7 +56,6 @@ class Calendar extends PureComponent {
5556
disablePreview: false,
5657
},
5758
scrollArea: this.calcScrollArea(props),
58-
isFirstRender: true,
5959
};
6060
}
6161
calcScrollArea(props) {
@@ -88,6 +88,7 @@ class Calendar extends PureComponent {
8888
const targetMonthIndex = differenceInCalendarMonths(date, props.minDate, this.dateOptions);
8989
const visibleMonths = this.list.getVisibleRange();
9090
if (preventUnnecessary && visibleMonths.includes(targetMonthIndex)) return;
91+
this.isFirstRender = true;
9192
this.list.scrollTo(targetMonthIndex);
9293
this.setState({ focusedDate: date });
9394
}
@@ -155,7 +156,8 @@ class Calendar extends PureComponent {
155156
}
156157
handleScroll() {
157158
const { onShownDateChange, minDate } = this.props;
158-
const { focusedDate, isFirstRender } = this.state;
159+
const { focusedDate } = this.state;
160+
const { isFirstRender } = this;
159161

160162
const visibleMonths = this.list.getVisibleRange();
161163
// prevent scroll jump with wrong visible value
@@ -166,7 +168,7 @@ class Calendar extends PureComponent {
166168
this.setState({ focusedDate: visibleMonth });
167169
onShownDateChange && onShownDateChange(visibleMonth);
168170
}
169-
this.setState({ isFirstRender: false });
171+
this.isFirstRender = false;
170172
}
171173
renderMonthAndYear(focusedDate, changeShownDate, props) {
172174
const { showMonthArrow, minDate, maxDate, showMonthAndYearPickers } = props;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Calendar from './Calendar';
1+
import Calendar from'../Calendar';
22

33
describe('Calendar', () => {
44
test('Should resolve', () => {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
3-
import Calendar from './Calendar.js';
4-
import { rangeShape } from './DayCell';
5-
import { findNextRangeIndex, generateStyles } from '../utils.js';
3+
import Calendar from '../Calendar';
4+
import { rangeShape } from '../DayCell';
5+
import { findNextRangeIndex, generateStyles } from '../../utils';
66
import { isBefore, differenceInCalendarDays, addDays, min, isWithinInterval, max } from 'date-fns';
77
import classnames from 'classnames';
8-
import coreStyles from '../styles';
8+
import coreStyles from '../../styles';
99

1010
class DateRange extends Component {
1111
constructor(props, context) {

src/components/DateRange/index.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import Calendar from '../Calendar';
4+
import { rangeShape } from '../DayCell';
5+
import { findNextRangeIndex, generateStyles } from '../../utils';
6+
import { isBefore, differenceInCalendarDays, addDays, min, isWithinInterval, max } from 'date-fns';
7+
import classnames from 'classnames';
8+
import coreStyles from '../../styles';
9+
10+
class DateRange extends Component {
11+
constructor(props, context) {
12+
super(props, context);
13+
this.setSelection = this.setSelection.bind(this);
14+
this.handleRangeFocusChange = this.handleRangeFocusChange.bind(this);
15+
this.updatePreview = this.updatePreview.bind(this);
16+
this.calcNewSelection = this.calcNewSelection.bind(this);
17+
this.state = {
18+
focusedRange: props.initialFocusedRange || [findNextRangeIndex(props.ranges), 0],
19+
preview: null,
20+
};
21+
this.styles = generateStyles([coreStyles, props.classNames]);
22+
}
23+
calcNewSelection(value, isSingleValue = true) {
24+
const focusedRange = this.props.focusedRange || this.state.focusedRange;
25+
const { ranges, onChange, maxDate, moveRangeOnFirstSelection, disabledDates } = this.props;
26+
const focusedRangeIndex = focusedRange[0];
27+
const selectedRange = ranges[focusedRangeIndex];
28+
if (!selectedRange || !onChange) return {};
29+
30+
let { startDate, endDate } = selectedRange;
31+
if (!endDate) endDate = new Date(startDate);
32+
let nextFocusRange;
33+
if (!isSingleValue) {
34+
startDate = value.startDate;
35+
endDate = value.endDate;
36+
} else if (focusedRange[1] === 0) {
37+
// startDate selection
38+
const dayOffset = differenceInCalendarDays(endDate, startDate);
39+
startDate = value;
40+
endDate = moveRangeOnFirstSelection ? addDays(value, dayOffset) : value;
41+
if (maxDate) endDate = min([endDate, maxDate]);
42+
nextFocusRange = [focusedRange[0], 1];
43+
} else {
44+
endDate = value;
45+
}
46+
47+
// reverse dates if startDate before endDate
48+
let isStartDateSelected = focusedRange[1] === 0;
49+
if (isBefore(endDate, startDate)) {
50+
isStartDateSelected = !isStartDateSelected;
51+
[startDate, endDate] = [endDate, startDate];
52+
}
53+
54+
const inValidDatesWithinRange = disabledDates.filter(disabledDate =>
55+
isWithinInterval(disabledDate, {
56+
start: startDate,
57+
end: endDate,
58+
})
59+
);
60+
61+
if (inValidDatesWithinRange.length > 0) {
62+
if (isStartDateSelected) {
63+
startDate = addDays(max(inValidDatesWithinRange), 1);
64+
} else {
65+
endDate = addDays(min(inValidDatesWithinRange), -1);
66+
}
67+
}
68+
69+
if (!nextFocusRange) {
70+
const nextFocusRangeIndex = findNextRangeIndex(this.props.ranges, focusedRange[0]);
71+
nextFocusRange = [nextFocusRangeIndex, 0];
72+
}
73+
return {
74+
wasValid: !(inValidDatesWithinRange.length > 0),
75+
range: { startDate, endDate },
76+
nextFocusRange: nextFocusRange,
77+
};
78+
}
79+
setSelection(value, isSingleValue) {
80+
const { onChange, ranges, onRangeFocusChange } = this.props;
81+
const focusedRange = this.props.focusedRange || this.state.focusedRange;
82+
const focusedRangeIndex = focusedRange[0];
83+
const selectedRange = ranges[focusedRangeIndex];
84+
if (!selectedRange) return;
85+
const newSelection = this.calcNewSelection(value, isSingleValue);
86+
onChange({
87+
[selectedRange.key || `range${focusedRangeIndex + 1}`]: {
88+
...selectedRange,
89+
...newSelection.range,
90+
},
91+
});
92+
this.setState({
93+
focusedRange: newSelection.nextFocusRange,
94+
preview: null,
95+
});
96+
onRangeFocusChange && onRangeFocusChange(newSelection.nextFocusRange);
97+
}
98+
handleRangeFocusChange(focusedRange) {
99+
this.setState({ focusedRange });
100+
this.props.onRangeFocusChange && this.props.onRangeFocusChange(focusedRange);
101+
}
102+
updatePreview(val) {
103+
if (!val) {
104+
this.setState({ preview: null });
105+
return;
106+
}
107+
const { rangeColors, ranges } = this.props;
108+
const focusedRange = this.props.focusedRange || this.state.focusedRange;
109+
const color = ranges[focusedRange[0]].color || rangeColors[focusedRange[0]] || color;
110+
this.setState({ preview: { ...val.range, color } });
111+
}
112+
render() {
113+
return (
114+
<Calendar
115+
focusedRange={this.state.focusedRange}
116+
onRangeFocusChange={this.handleRangeFocusChange}
117+
preview={this.state.preview}
118+
onPreviewChange={value => {
119+
this.updatePreview(value ? this.calcNewSelection(value) : null);
120+
}}
121+
{...this.props}
122+
displayMode="dateRange"
123+
className={classnames(this.styles.dateRangeWrapper, this.props.className)}
124+
onChange={this.setSelection}
125+
updateRange={val => this.setSelection(val, false)}
126+
ref={target => {
127+
this.calendar = target;
128+
}}
129+
/>
130+
);
131+
}
132+
}
133+
134+
DateRange.defaultProps = {
135+
classNames: {},
136+
ranges: [],
137+
moveRangeOnFirstSelection: false,
138+
rangeColors: ['#3d91ff', '#3ecf8e', '#fed14c'],
139+
disabledDates: [],
140+
};
141+
142+
DateRange.propTypes = {
143+
...Calendar.propTypes,
144+
onChange: PropTypes.func,
145+
onRangeFocusChange: PropTypes.func,
146+
className: PropTypes.string,
147+
ranges: PropTypes.arrayOf(rangeShape),
148+
moveRangeOnFirstSelection: PropTypes.bool,
149+
};
150+
151+
export default DateRange;

0 commit comments

Comments
 (0)