diff --git a/docs-site/src/components/Examples/index.js b/docs-site/src/components/Examples/index.js index 0f3606fd6..052ce886c 100644 --- a/docs-site/src/components/Examples/index.js +++ b/docs-site/src/components/Examples/index.js @@ -102,6 +102,7 @@ import ExternalForm from "../../examples/externalForm"; import CalendarIcon from "../../examples/calendarIcon"; import CalendarIconExternal from "../../examples/calendarIconExternal"; import CalendarIconSvgIcon from "../../examples/calendarIconSvgIcon"; +import ToggleCalendarOnIconClick from "../../examples/toggleCalendarOnIconClick"; import "./style.scss"; import "react-datepicker/dist/react-datepicker.css"; @@ -129,6 +130,10 @@ export default class exampleComponents extends React.Component { title: "Calendar Icon using External Lib", component: CalendarIconExternal, }, + { + title: "Toggle Calendar open status on click of the calendar icon", + component: ToggleCalendarOnIconClick, + }, { title: "Calendar container", component: CalendarContainer, diff --git a/docs-site/src/examples/toggleCalendarOnIconClick.js b/docs-site/src/examples/toggleCalendarOnIconClick.js new file mode 100644 index 000000000..aea108c75 --- /dev/null +++ b/docs-site/src/examples/toggleCalendarOnIconClick.js @@ -0,0 +1,11 @@ +() => { + const [selectedDate, setSelectedDate] = useState(new Date()); + return ( + setSelectedDate(date)} + /> + ); +}; diff --git a/src/calendar_icon.jsx b/src/calendar_icon.jsx index 20961ce27..b214958a0 100644 --- a/src/calendar_icon.jsx +++ b/src/calendar_icon.jsx @@ -1,12 +1,21 @@ import React from "react"; import PropTypes from "prop-types"; -const CalendarIcon = ({ icon, className = "" }) => { +const CalendarIcon = ({ icon, className = "", onClick }) => { const defaultClass = "react-datepicker__calendar-icon"; 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); + } + }, }); } @@ -15,6 +24,7 @@ const CalendarIcon = ({ icon, className = "" }) => {