简体   繁体   English

反应日期选择器 | 单击图标时打开日历,但单击任意位置时关闭

[英]react-datepicker | Open calendar when clicking icon but closing when click anywhere

Currently I'm trying to, when clicking the icon, be able to open the calendar (which I can do) but I would like to close the calendar when clicking anywhere else of the window.目前,我正在尝试在单击图标时打开日历(我可以这样做),但我想在单击 window 的其他任何位置时关闭日历。

I'm not entirely sure on how to do this and would love to have some help我不完全确定如何做到这一点,并希望得到一些帮助

Here's my component这是我的组件

在此处输入图像描述

const DatePicker: FC<Props> = ({
  label,
  icon,
  date,
  onChange,
  minDate,
  maxDate,
  tabIndex,
}) => {
  const dateObj = useMemo(() => (date ? date.toDate() : null), [date])
  const minDateObj = useMemo(() => (minDate ? minDate.toDate() : null), [
    minDate,
  ])
  const maxDateObj = useMemo(() => (maxDate ? maxDate.toDate() : null), [
    maxDate,
  ])

  const [calendarIsOpen, setCalendarIsOpen] = useState(false)

  return (
    <div className={css.host}>
      <div className={css.label}>{label}</div>
      <div className={css.wrapper}>
        <label>
          <button
            className={css.calendarButton}
            onClick={() => setCalendarIsOpen(!calendarIsOpen)}
          >
            {icon}
          </button>
        </label>
        <ReactDatePicker
          selected={dateObj}
          className={css.input}
          calendarClassName={css.calendar}
          showTimeSelect
          dateFormat="dd/MM/yyyy h:mm:ss aa"
          onChange={(newDate: Date) => {
            if (newDate) {
              const momentDate = moment(newDate)
              onChange(momentDate)
            }
          }}
          startDate={minDateObj}
          endDate={maxDateObj}
          minDate={minDateObj}
          maxDate={maxDateObj}
          showPopperArrow={false}
          popperModifiers={{
            offset: {
              enabled: true,
              offset: '-28px, 4px',
            },
          }}
          renderCustomHeader={customHeader}
          open={calendarIsOpen}
          tabIndex={tabIndex}
        />
      </div>
    </div>
  )
}

export default DatePicker

Solution:解决方案:

This should work.这应该有效。

/**
 * Hook that alerts clicks outside of the passed ref
 */
function useOutsideAlerter(ref, onOutSideClick) {
  /**
   * Alert if clicked on outside of element
   */
  function handleClickOutside(event) {
    if (ref.current && !ref.current.contains(event.target)) {
      onOutSideClick();
    }
  }

  useEffect(() => {
    // Bind the event listener
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      // Unbind the event listener on clean up
      document.removeEventListener("mousedown", handleClickOutside);
    };
  });
}

export default function DatePicker({ onOutSideClick,...props }) {
  const wrapperRef = useRef(null);
  useOutsideAlerter(wrapperRef, onOutSideClick);
   /// your code
  return (
   // your code
  );
}

and use call it like <DatePicker onOutsideClick={()=>{// hide this date picker component.}}/>并使用<DatePicker onOutsideClick={()=>{// hide this date picker component.}}/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应日期选择器 | 日历未在选择日期时关闭 - React-datepicker | Calendar not closing on selection of date 要单击图像图标,reaction-datepicker的输入框不起作用 - To click on image-icon, of input box of react-datepicker are not working 单击日历图标时需要打开日期选择器弹出窗口,而不是单击输入字段 - Need to open the datepicker popup on click of the calendar icon instead of clicking on the input field react-datepicker选择句柄选择时的对象范围 - react-datepicker select object range when handle select 单击屏幕上的任何其他位置时,菜单未关闭 - Menu not closing when click anywhere else on screen 有没有办法可以防止在点击日期选择器之外的已知容器时关闭kendo datepicker日历弹出窗口? - Is there a way I can prevent a kendo datepicker calendar pop-up from closing when clicking on a known container which is outside of the datepicker? 在浏览器上单击任意位置时,防止daterangepicker关闭 - Prevent daterangepicker from closing when clicking anywhere on browser 当我在日期选择器输入中粘贴新日期时出现日期问题(react-datepicker) - Problem with date (react-datepicker) when i paste new date in datepicker input 如何将自定义图标用于react-datepicker组件 - How to use custom icon for react-datepicker component 如何在React JS中单击按钮或图标单击时打开日期选择器 - how to open datepicker on button click or icon click in react js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM