Skip to content

Commit d4357e6

Browse files
author
Austin Green
authored
fix(datepicker): correct minValue and maxValue displays and allow same date selection within Range (#415)
1 parent ec66679 commit d4357e6

File tree

6 files changed

+29
-21
lines changed

6 files changed

+29
-21
lines changed

packages/datepickers/examples/range.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
const { Field, Label, Input, Message } = require('@zendeskgarden/react-forms/src');
33
const { addDays, compareAsc } = require('date-fns');
44

5+
const StyledCol = styled(Col)`
6+
&& {
7+
max-width: 300px;
8+
}
9+
`;
10+
511
initialState = {
612
startValue: new Date(),
713
endValue: addDays(new Date(), 16)
@@ -16,15 +22,15 @@ const isInvalid = () => compareAsc(state.startValue, state.endValue) === 1;
1622
>
1723
<Grid>
1824
<Row>
19-
<Col md>
25+
<StyledCol md>
2026
<Field>
2127
<Label>Start</Label>
2228
<DatepickerRange.Start>
2329
<Input />
2430
</DatepickerRange.Start>
2531
</Field>
26-
</Col>
27-
<Col md>
32+
</StyledCol>
33+
<StyledCol md>
2834
<Field>
2935
<Label>End</Label>
3036
<DatepickerRange.End>
@@ -34,7 +40,7 @@ const isInvalid = () => compareAsc(state.startValue, state.endValue) === 1;
3440
<Message validation="error">End date must occur after the Start date</Message>
3541
)}
3642
</Field>
37-
</Col>
43+
</StyledCol>
3844
</Row>
3945
<Row>
4046
<Col>

packages/datepickers/src/Datepicker/Datepicker.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('Datepicker', () => {
7878
for (let x = 0; x < days.length; x++) {
7979
const element = days[x];
8080

81-
if (x < 6) {
81+
if (x <= 6) {
8282
expect(element).toHaveAttribute('data-test-disabled', 'true');
8383
} else if (x > 11) {
8484
expect(element).toHaveAttribute('data-test-disabled', 'true');

packages/datepickers/src/Datepicker/components/Calendar.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import isSameDay from 'date-fns/isSameDay';
1717
import isSameMonth from 'date-fns/isSameMonth';
1818
import isBefore from 'date-fns/isBefore';
1919
import isAfter from 'date-fns/isAfter';
20-
import subDays from 'date-fns/subDays';
2120
import {
2221
StyledDatepicker,
2322
StyledCalendar,
@@ -99,11 +98,11 @@ const Calendar: React.FunctionComponent<ICalendarProps> = ({
9998
let isDisabled = false;
10099

101100
if (minValue !== undefined) {
102-
isDisabled = isBefore(date, subDays(minValue, 1));
101+
isDisabled = isBefore(date, minValue) && !isSameDay(date, minValue);
103102
}
104103

105104
if (maxValue !== undefined) {
106-
isDisabled = isDisabled || isAfter(date, maxValue);
105+
isDisabled = isDisabled || (isAfter(date, maxValue) && !isSameDay(date, maxValue));
107106
}
108107

109108
return (

packages/datepickers/src/DatepickerRange/DatepickerRange.spec.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ describe('DatepickerRange', () => {
188188
startValue={DEFAULT_START_VALUE}
189189
endValue={DEFAULT_END_VALUE}
190190
minValue={subDays(DEFAULT_START_VALUE, 2)}
191-
maxValue={addDays(DEFAULT_END_VALUE, 2)}
191+
maxValue={addDays(DEFAULT_END_VALUE, 1)}
192192
/>
193193
);
194194

@@ -200,9 +200,9 @@ describe('DatepickerRange', () => {
200200

201201
if (x < 5) {
202202
expect(element).not.toHaveAttribute('data-test-disabled');
203-
} else if (x === 5) {
203+
} else if (x < 7) {
204204
expect(element).toHaveAttribute('data-test-disabled', 'true');
205-
} else if (x > 5 && x < 33) {
205+
} else if (x >= 7 && x <= 32) {
206206
expect(element).toHaveAttribute('data-test-disabled', 'false');
207207
} else {
208208
expect(element).not.toHaveAttribute('data-test-disabled');
@@ -216,9 +216,9 @@ describe('DatepickerRange', () => {
216216

217217
if (x < 5) {
218218
expect(element).not.toHaveAttribute('data-test-disabled');
219-
} else if (x >= 5 && x < 12) {
219+
} else if (x >= 5 && x < 11) {
220220
expect(element).toHaveAttribute('data-test-disabled', 'false');
221-
} else if (x >= 12 && x < 36) {
221+
} else if (x >= 11 && x < 36) {
222222
expect(element).toHaveAttribute('data-test-disabled', 'true');
223223
} else {
224224
expect(element).not.toHaveAttribute('data-test-disabled');

packages/datepickers/src/DatepickerRange/components/Month.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ const Month: React.FunctionComponent<{
152152
let isDisabled = false;
153153

154154
if (minValue !== undefined) {
155-
isDisabled = isBefore(date, subDays(minValue, 1));
155+
isDisabled = isBefore(date, minValue) && !isSameDay(date, minValue);
156156
}
157157

158158
if (maxValue !== undefined) {
159-
isDisabled = isDisabled || isAfter(date, maxValue);
159+
isDisabled = isDisabled || (isAfter(date, maxValue) && !isSameDay(date, maxValue));
160160
}
161161

162162
let isHighlighted = false;
@@ -180,10 +180,7 @@ const Month: React.FunctionComponent<{
180180
false;
181181

182182
let isInvalidDateRange =
183-
(endValue &&
184-
startValue &&
185-
(compareAsc(endValue, startValue) === -1 || compareAsc(endValue, startValue) === 0)) ||
186-
false;
183+
(endValue && startValue && compareAsc(endValue, startValue) === -1) || false;
187184

188185
if (minValue) {
189186
if (startValue) {

packages/datepickers/src/DatepickerRange/utils/datepicker-range-reducer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,19 @@ export const datepickerRangeReducer = ({
239239
}
240240
case 'CLICK_DATE':
241241
if (state.isStartFocused) {
242-
if (endValue !== undefined && isBefore(action.value, endValue)) {
242+
if (
243+
endValue !== undefined &&
244+
(isBefore(action.value, endValue) || isSameDay(action.value, endValue))
245+
) {
243246
onChange && onChange({ startValue: action.value, endValue });
244247
} else {
245248
onChange && onChange({ startValue: action.value, endValue: undefined });
246249
}
247250
} else if (state.isEndFocused) {
248-
if (startValue !== undefined && isAfter(action.value, startValue)) {
251+
if (
252+
startValue !== undefined &&
253+
(isAfter(action.value, startValue) || isSameDay(action.value, startValue))
254+
) {
249255
onChange && onChange({ startValue, endValue: action.value });
250256
} else {
251257
onChange && onChange({ startValue: action.value, endValue: undefined });

0 commit comments

Comments
 (0)