Skip to content

Commit ff8e580

Browse files
authored
fix: ensure the new date is always between max and min dates (#986)
1 parent 81ab049 commit ff8e580

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

example/src/DatePickerExample.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ function DatePickerExample({ theme }) {
113113
label="Date with minimum and maximum"
114114
placeholder="Select a date..."
115115
type="solid"
116-
minimumDate={new Date("April 10, 2023")}
117-
maximumDate={new Date("May 10, 2023")}
116+
minimumDate={new Date("2023-04-10")}
117+
maximumDate={new Date("2023-05-10")}
118118
/>
119119
</Section>
120120
<Section title="Styled">

packages/core/src/components/DatePicker/DatePicker.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
paddingStyleNames,
3535
positionStyleNames,
3636
} from "../../utilities";
37+
import { parseDate } from "./parseDate";
3738

3839
const AnimatedText = Animated.createAnimatedComponent(Text);
3940

@@ -399,6 +400,29 @@ const DatePicker: React.FC<React.PropsWithChildren<Props>> = ({
399400
type === "solid" ? { marginHorizontal: 12 } : {},
400401
];
401402

403+
React.useEffect(() => {
404+
const currentDate = parseDate(value);
405+
406+
if (!currentDate) return;
407+
408+
const minDate = parseDate(minimumDate);
409+
const maxDate = parseDate(maximumDate);
410+
411+
let newDate = currentDate;
412+
413+
if (minDate && currentDate < minDate) {
414+
newDate = minDate;
415+
}
416+
if (maxDate && currentDate > maxDate) {
417+
newDate = maxDate;
418+
}
419+
420+
if (newDate !== currentDate) {
421+
setValue(newDate);
422+
onDateChange(newDate);
423+
}
424+
}, [value, minimumDate, maximumDate, onDateChange]);
425+
402426
const Picker = (
403427
<DateTimePicker
404428
value={getValidDate()}
@@ -619,20 +643,4 @@ const styles = StyleSheet.create({
619643
pickerContainer: { flexDirection: "column", width: "100%", zIndex: 100 },
620644
});
621645

622-
function parseDate(date?: string | Date) {
623-
if (typeof date === "string") {
624-
const parsed = Date.parse(date);
625-
if (!isNaN(parsed)) {
626-
return new Date(parsed);
627-
}
628-
console.warn(
629-
"Invalid date string:",
630-
`'${date}'.`,
631-
"See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format"
632-
);
633-
return undefined;
634-
}
635-
return date;
636-
}
637-
638646
export default withTheme(DatePicker);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function parseDate(date?: string | Date) {
2+
if (typeof date === "string") {
3+
const parsed = Date.parse(date);
4+
if (!isNaN(parsed)) {
5+
return new Date(parsed);
6+
}
7+
console.warn(
8+
"Invalid date string:",
9+
`'${date}'.`,
10+
"See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format"
11+
);
12+
return undefined;
13+
}
14+
return date;
15+
}

0 commit comments

Comments
 (0)