adrienpoly / stimulus-flatpickr

A modest, yet powerful wrapper of Flatpickr 📆 for Stimulus
MIT License
414 stars 30 forks source link

Best/any way to do trigger Flatpickr from another Stimulus controller? #81

Open k2director opened 3 years ago

k2director commented 3 years ago

First of all, thanks again for Stimulus-Flatpickr, it's been working great for setting due dates on Invoices in my app! I have a Due Date field which displays the due date of that invoice in a plain English format ("August 30, 2021") and when the user clicks on that, they can pick a new due date with the Flatpickr pop-up. Thanks for making that easy.

Now, I wanted to create a Due Date Shortcut drop-down/select menu, with options like "Today", "In Seven Days", "in 14 days", etc., so a user doesn't have to pick a date manually. So I've created a Select UI drop-down element with those items, and a new Stimulus controller that reads the select whenever it occurs, but I'm wondering about the best way to proceed from here. Ultimately, I want to read the Invoice's Issued date (also a Flatpickr field), then add the number of days selected in the shortcut (for example, 14 days) to that Issued date, and then update the Due Date Flatpcikr field to reflect the new due date in plain English ("Sept 14, 2021").

Any general advice on going about that? In some rough experiments, I'm able to put a Stimulus target on my Due Date flatpickr field, and can use my Shortcut Stimulus controller to change the numeric date value of the field (like "2022-01-01"), but I can't figure out how to change the plain English text that appears in the field (like "August 30, 2021"). But that might not still get me where I need to go, because I still need a way, in my shortcut Javascript controller, to add the user's shortcut choice to the Issue date of the invoice, and then insert that new date into the Due Date field ("2022-01-01"), and update its plain English text ("Jan 1, 2022").

Any ideas appreciated, thanks much!