Skip to content

Commit 976d597

Browse files
Merge pull request #5858 from qburst/issue-5701/onChangeRaw-date-meta
✨ Add `selectionMeta` param to the onChangeRaw event call - to give additional details about the selected date
2 parents 95b4c0f + 4f3fba9 commit 976d597

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
() => {
22
const [selectedDate, setSelectedDate] = useState(null);
3-
const handleChangeRaw = (value) => {
3+
const handleChangeRaw = (value, selectedDateMeta) => {
4+
console.log(
5+
selectedDateMeta
6+
? `Selected Date Meta: ${JSON.stringify(selectedDateMeta)}`
7+
: "No Selection Meta is available",
8+
);
9+
410
if (value === "tomorrow") {
511
setSelectedDate(addDays(new Date(), 1));
612
}
@@ -10,7 +16,9 @@
1016
selected={selectedDate}
1117
onChange={(date) => setSelectedDate(date)}
1218
placeholderText='Enter "tomorrow"'
13-
onChangeRaw={(event) => handleChangeRaw(event.target.value)}
19+
onChangeRaw={(event, selectedDateMeta) =>
20+
handleChangeRaw(event.target.value, selectedDateMeta)
21+
}
1422
/>
1523
);
1624
};

src/index.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ export type DatePickerProps = OmitUnion<
194194
rangeSeparator?: string;
195195
onChangeRaw?: (
196196
event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>,
197+
selectionMeta?: {
198+
date: Date;
199+
formattedDate: string;
200+
},
197201
) => void;
198202
onSelect?: (
199203
date: Date | null,
@@ -717,7 +721,9 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
717721
) => {
718722
if (this.props.readOnly) return;
719723

720-
const { selectsRange, startDate, endDate, swapRange } = this.props;
724+
const { selectsRange, startDate, endDate, locale, swapRange } = this.props;
725+
const dateFormat =
726+
this.props.dateFormat ?? DatePicker.defaultProps.dateFormat;
721727
const isDateSelectionComplete =
722728
!selectsRange ||
723729
(startDate && !endDate && (swapRange || !isDateBefore(date, startDate)));
@@ -732,7 +738,11 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
732738
this.sendFocusBackToInput();
733739
}
734740
if (this.props.onChangeRaw) {
735-
this.props.onChangeRaw(event);
741+
const formattedDate = safeDateFormat(date, {
742+
dateFormat,
743+
locale,
744+
});
745+
this.props.onChangeRaw(event, { date, formattedDate });
736746
}
737747
this.setSelected(date, event, false, monthSelectedIn);
738748
if (this.props.showDateSelect) {

src/test/datepicker_test.test.tsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,4 +4819,68 @@ describe("DatePicker", () => {
48194819
});
48204820
});
48214821
});
4822+
4823+
describe("onChangeRaw - selectionMeta", () => {
4824+
it("should include selectionMeta as a second param to the onChangeRaw when user selected a date to provide extra meta about the selected date element", () => {
4825+
const selectedDate = newDate("2025-11-05");
4826+
const onChangeRawSpy = jest.fn();
4827+
const { container } = render(
4828+
<DatePicker
4829+
dateFormat="MM/dd/yyyy"
4830+
selected={selectedDate}
4831+
onChangeRaw={onChangeRawSpy}
4832+
/>,
4833+
);
4834+
expect(onChangeRawSpy).not.toHaveBeenCalled();
4835+
4836+
const input = safeQuerySelector(container, "input");
4837+
fireEvent.focus(input);
4838+
4839+
const day = safeQuerySelector(container, ".react-datepicker__day--002");
4840+
fireEvent.click(day);
4841+
4842+
expect(onChangeRawSpy).toHaveBeenCalledTimes(1);
4843+
const params = onChangeRawSpy.mock.calls[0];
4844+
expect(params.length).toBe(2);
4845+
4846+
const eventObject = params[0];
4847+
expect(typeof eventObject).toBe("object");
4848+
4849+
const selectionMeta = params[1];
4850+
expect(typeof selectionMeta).toBe("object");
4851+
expect(selectionMeta).toHaveProperty("date");
4852+
expect(selectionMeta).toHaveProperty("formattedDate");
4853+
expect(selectionMeta.formattedDate).toBe("11/02/2025");
4854+
expect(selectionMeta.date).toBeInstanceOf(Date);
4855+
});
4856+
4857+
it("should not include selectionMeta as a second param to the onChangeRaw when user updated date using date input", () => {
4858+
const selectedDate = newDate("2025-11-05");
4859+
const onChangeRawSpy = jest.fn();
4860+
const { container } = render(
4861+
<DatePicker
4862+
dateFormat="MM/dd/yyyy"
4863+
selected={selectedDate}
4864+
onChangeRaw={onChangeRawSpy}
4865+
/>,
4866+
);
4867+
expect(onChangeRawSpy).not.toHaveBeenCalled();
4868+
4869+
const input = safeQuerySelector(container, "input");
4870+
const inputValue = "11/02/2025";
4871+
fireEvent.change(input, {
4872+
target: {
4873+
value: inputValue,
4874+
},
4875+
});
4876+
expect(onChangeRawSpy).toHaveBeenCalledTimes(1);
4877+
4878+
const params = onChangeRawSpy.mock.calls[0];
4879+
expect(params.length).toBe(1);
4880+
4881+
const eventObject = params[0];
4882+
expect(typeof eventObject).toBe("object");
4883+
expect(eventObject.target?.value).toBe(inputValue);
4884+
});
4885+
});
48224886
});

0 commit comments

Comments
 (0)