TW-Elements icon indicating copy to clipboard operation
TW-Elements copied to clipboard

Date Picker Realtime reactive filter dates

Open manalkaff opened this issue 1 year ago • 9 comments

Im using svelte and firebase. fetching realtime data into variable. i want to use DatePicker and streaming disabled dates from database.

when the DatePicker window is open, the filter function doesnt updates when the variable does.

how do i make the DatePicker, filter dates realtime even when the DatePicker window is open.

i event try this and still doesnt work

export let disabled_dates = []
  let date_open = false;
  let myDatepicker = null

  

  $: filterFunction = (date) => {

    for (let disabled_index in disabled_dates){
      let disabled = disabled_dates[disabled_index].split("/")
      if(disabled[0] == date.getDate() && disabled[1]-1 == date.getMonth() && disabled[2] == date.getFullYear()){
        return false
      }
    }
    return true
  }

  $: refresh_dates(disabled_dates)

  function refresh_dates(disabled_dates){
    console.log("refresh_dates: ", disabled_dates)
    if(myDatepicker != null){
        if(date_open){
          myDatepicker.close()
          myDatepicker.open()
        }
    }
  }

  function load_input(){
    document.getElementById("myDatepicker").addEventListener("open.te.datepicker", event => {
      date_open = true
    })
    const myInput = new Input(document.getElementById("myDatepicker"));
    const options = {
      format: "dd/mm/yyyy",
      // confirmDateOnSelect: true,
      removeClearBtn: true,
      // removeOkBtn: true,
      // removeCancelBtn: true,
      disablePast: true,
      filter: filterFunction}
    myDatepicker = new Datepicker(
      document.getElementById("myDatepicker"),
      options
    );

manalkaff avatar Sep 10 '23 23:09 manalkaff

Hi! Have you tried using the Datepicker's update method? I think it could be useful in your case

image https://tailwind-elements.com/docs/standard/forms/datepicker/#docsTabsAPI

juujisai avatar Sep 11 '23 05:09 juujisai

@juujisai i did tried it before, and still doesnt work. i want a live update even when the calender is open

datePicker.update({
 filter: filterFunction 
})

manalkaff avatar Sep 11 '23 05:09 manalkaff

@manalkaff The only thing I can think right now is to call close and open method together with update.

    datepickerInstance.close();
    datepickerInstance.update({ filter: filterFunction });
    datepickerInstance.open();

It will make the backdrop flash a little, but it will update the component

juujisai avatar Sep 12 '23 06:09 juujisai

@juujisai same result, when the date change from the database. it did disable live and realtime. its just got bugged where when the date or ok button clicked its only gray highlight the button not selecting it.

image

manalkaff avatar Sep 12 '23 08:09 manalkaff

@manalkaff I see. Seems that the datepicker doesn't have time to close properly before opening and blocks the picker. You can try delaying the update and open methods but then the close/open animation is more visible

datepickerInstance.close();
setTimeout(() => {
  datepickerInstance.update({ filter: filterFunction });
  datepickerInstance.open();
}, 150);

juujisai avatar Sep 13 '23 05:09 juujisai

@juujisai yeah even tho there is a visible close and open animation, at least it works now. thank you so much. im still hoping there will be an update to fix this tho. like sending events to the date picker without refreshing it.

at last. thank you so much @juujisai

manalkaff avatar Sep 14 '23 00:09 manalkaff

@manalkaff Thanks for posting this kind of solutions. One question please.

Why .close() and .open() function doesn't work in my side? Thanks.

firstninja111 avatar Oct 11 '23 09:10 firstninja111

@jamesito0923 I can't be sure without any details but its probably the variable datepickerInstance isnt intialized before you calling .close() and .open()

manalkaff avatar Oct 11 '23 13:10 manalkaff

@jamesito0923 Does it throw any errors?

juujisai avatar Oct 12 '23 06:10 juujisai