From 210fcd54d61ee220da819823ad9609b183db146f Mon Sep 17 00:00:00 2001 From: Balaji Sridharan Date: Mon, 18 Dec 2023 14:41:47 +0530 Subject: [PATCH] feature: Enable the onClick event also on the custom calendar icon component Attach the onClick listener on the custom calendar icon component without overriding it's existing onClick listener and call both the event handlers based on their existence Closes #4091 --- src/calendar_icon.jsx | 9 +++++++++ test/calendar_icon.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/calendar_icon.jsx b/src/calendar_icon.jsx index 8583c8367..b214958a0 100644 --- a/src/calendar_icon.jsx +++ b/src/calendar_icon.jsx @@ -7,6 +7,15 @@ const CalendarIcon = ({ icon, className = "", onClick }) => { if (React.isValidElement(icon)) { return React.cloneElement(icon, { className: `${icon.props.className || ""} ${defaultClass} ${className}`, + onClick: (e) => { + if (typeof icon.props.onClick === "function") { + icon.props.onClick(e); + } + + if (typeof onClick === "function") { + onClick(e); + } + }, }); } diff --git a/test/calendar_icon.test.js b/test/calendar_icon.test.js index b144ecc3d..151febbe8 100644 --- a/test/calendar_icon.test.js +++ b/test/calendar_icon.test.js @@ -61,4 +61,29 @@ describe("CalendarIcon", () => { expect(onClickMock).toHaveBeenCalledTimes(1); }); + + it("should fire onClick event on the click of custom icon component when provided", () => { + const onClickCustomIcon = jest.fn(); + + const { container } = render( + + } + onClick={onClickMock} + />, + ); + + const icon = container.querySelector("svg.react-datepicker__calendar-icon"); + fireEvent.click(icon); + + expect(onClickMock).toHaveBeenCalledTimes(1); + expect(onClickCustomIcon).toHaveBeenCalledTimes(1); + }); });