rzkhosroshahi / zaman

It's a lightweight React component for creating a Jalali/Georgian datepicker
https://rzkhosroshahi.github.io/zaman-landing
MIT License
246 stars 46 forks source link

Close DatePicker Modal, by a button or after selecting date #113

Open HaminJuri opened 11 months ago

HaminJuri commented 11 months ago

Hey, as it's on the title: How do you close DatePicker Modal by using a button or after the user selects its desired date? package.json: "...", "react": "18.2.0", "next": "13.4.12", "zaman": "^2.0.7"

sepehrsamavati commented 10 months ago

I couldn't close it either, so I did it by clicking its input, which involves DOM manipulation and is not considered best practice:

// in Component.tsx
const toggleModal = useCallback(() => {
    const dateInput: HTMLInputElement | null = wrapperRef.current?.querySelector(`.${dateInputClassName}`) ?? null;
    dateInput?.click();
}, []);

wrapperRef: reference to the parent of <DatePicker /> component
dateInputClassName: class name used in <DatePicker inputClass={dateInputClassName} />

This can be called in onChange callback:

<DatePicker
    inputClass={dateInputClassName}
    onChange={() => toggleModal()}
/>
rzkhosroshahi commented 10 months ago

There are two ways you can achieve it. The calendar will be closed when the user clicks outside the date picker, however, you can also pass the show prop if you want to close the date picker manually.

mahbobJan commented 10 months ago

There are two ways you can achieve it. The calendar will be closed when the user clicks outside the date picker, however, you can also pass the show prop if you want to close the date picker manually.

show prop not work

mmsadati commented 8 months ago

There are two ways you can achieve it. The calendar will be closed when the user clicks outside the date picker, however, you can also pass the show prop if you want to close the date picker manually.

show prop not work

I have the same issue When is this bug going to be fixed?

MHMighani commented 7 months ago

H @rzkhosroshahi I'm working on this issue. I want to know that when the show prop is passed, do you want the calendar window still closes when the user clicks outside or nothing happens and it is fully controlled by the show prop?

moeinrahimi commented 5 months ago

after reading this issue seems my issue is duplicated. https://github.com/rzkhosroshahi/zaman/issues/140

rzkhosroshahi commented 5 months ago

I might change the show prop to the defaultShow. it won't control the inside component state. In other words, it gonna just set the default behavior of showing date-picker, and my component its not going to listen to the prop.

MahdiFarzami commented 1 month ago

i have same issue and handle it with this code

function simulateClick(x, y) { const event = new MouseEvent('click', { view: window, bubbles: true, cancelable: true, clientX: x, clientY: y });

const target = document.elementFromPoint(x, y);
if (target) {
  target.dispatchEvent(event);
}

}

its not best way but there is no ohter way.