Skip to content

Commit 692762c

Browse files
committed
Add support for onInvalidChange prop
1 parent decba0f commit 692762c

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Displays an input field complete with custom inputs, native input, and a calenda
111111
| onCalendarOpen | Function called when the calendar opens. | n/a | `() => alert('Calendar opened')` |
112112
| onChange | Function called when the user picks a valid date. If any of the fields were excluded using custom `format`, `new Date(y, 0, 1, 0, 0, 0)`, where `y` is the current year, is going to serve as a "base". | n/a | `(value) => alert('New date is: ', value)` |
113113
| onFocus | Function called when the focuses an input. | n/a | `(event) => alert('Focused input: ', event.target.name)` |
114+
| onInvalidChange | Function called when the user picks an invalid date. | n/a | `() => alert('Invalid date')` |
114115
| openCalendarOnFocus | Whether to open the calendar on input focus. Note: It's recommended to use shouldOpenCalendar function instead. | `true` | `false` |
115116
| portalContainer | Element to render the calendar in using portal. | n/a | `document.getElementById('my-div')` |
116117
| rangeDivider | Divider between date inputs. | `"–"` | `" to "` |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"make-event-props": "^1.4.2",
4444
"prop-types": "^15.6.0",
4545
"react-calendar": "^4.2.1",
46-
"react-date-picker": "^10.0.1",
46+
"react-date-picker": "^10.2.0",
4747
"react-fit": "^1.5.1"
4848
},
4949
"devDependencies": {

src/DateRangePicker.spec.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,23 @@ describe('DateRangePicker', () => {
622622
expect(onChange).toHaveBeenCalledWith([new Date(2023, 0, 1), null]);
623623
});
624624

625+
it('calls onInvalidChange callback when changing value to an invalid one', () => {
626+
const value = new Date(2023, 0, 31);
627+
const onInvalidChange = vi.fn();
628+
629+
const { container } = render(
630+
<DateRangePicker onInvalidChange={onInvalidChange} value={value} />,
631+
);
632+
633+
const dayInput = container.querySelector('input[name="day"]') as HTMLInputElement;
634+
635+
act(() => {
636+
fireEvent.change(dayInput, { target: { value: '32' } });
637+
});
638+
639+
expect(onInvalidChange).toHaveBeenCalled();
640+
});
641+
625642
it('clears the value when clicking on a button', () => {
626643
const onChange = vi.fn();
627644

src/DateRangePicker.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export type DateRangePickerProps = {
8686
onCalendarOpen?: () => void;
8787
onChange?: (value: Value) => void;
8888
onFocus?: (event: React.FocusEvent<HTMLDivElement>) => void;
89+
onInvalidChange?: () => void;
8990
openCalendarOnFocus?: boolean;
9091
portalContainer?: HTMLElement | null;
9192
rangeDivider?: React.ReactNode;
@@ -128,6 +129,7 @@ export default function DateRangePicker(props: DateRangePickerProps) {
128129
onCalendarOpen,
129130
onChange: onChangeProps,
130131
onFocus: onFocusProps,
132+
onInvalidChange,
131133
openCalendarOnFocus = true,
132134
rangeDivider = '–',
133135
required,
@@ -326,6 +328,7 @@ export default function DateRangePicker(props: DateRangePickerProps) {
326328
maxDate,
327329
maxDetail,
328330
minDate,
331+
onInvalidChange,
329332
required,
330333
showLeadingZeros,
331334
};

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ __metadata:
808808
prop-types: ^15.6.0
809809
react: ^18.2.0
810810
react-calendar: ^4.2.1
811-
react-date-picker: ^10.0.1
811+
react-date-picker: ^10.2.0
812812
react-dom: ^18.2.0
813813
react-fit: ^1.5.1
814814
rimraf: ^3.0.0
@@ -4047,9 +4047,9 @@ __metadata:
40474047
languageName: node
40484048
linkType: hard
40494049

4050-
"react-date-picker@npm:^10.0.1":
4051-
version: 10.0.1
4052-
resolution: "react-date-picker@npm:10.0.1"
4050+
"react-date-picker@npm:^10.2.0":
4051+
version: 10.2.0
4052+
resolution: "react-date-picker@npm:10.2.0"
40534053
dependencies:
40544054
"@wojtekmaj/date-utils": ^1.1.3
40554055
clsx: ^1.2.1
@@ -4062,7 +4062,7 @@ __metadata:
40624062
peerDependencies:
40634063
react: ^16.8.0 || ^17.0.0 || ^18.0.0
40644064
react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0
4065-
checksum: eb2fdc9b8d843ec49a87e9776367c8952f4fd941d52dae069551c13f9752498fa71daa0f204aae82e759586e2c51d2b4ef48b1b915ca2f1ddb7be73fc4ea20fa
4065+
checksum: 0aaf5b7bbab59113aec542e3b807d33335a9758739797fa9bf54433f3754b8e24a36b1a53814f344a1841df64888fa0cfdc6423155533aa64ff34d0d03c6d6bd
40664066
languageName: node
40674067
linkType: hard
40684068

0 commit comments

Comments
 (0)