Giter VIP home page Giter VIP logo

rails_stimulus_flatpickr's Introduction

Rails Showcase of Stimulus-Flatpickr wrapper

This project is in support of the article I published on dev.to https://dev.to/adrienpoly/introducing-stimulus-flatpickr-wrapper--5c23

This example shows how to use stimulus-flatpickr wrapper in a Rails app to have an advanced date picking solution with:

  • localization of the datepicker ๐ŸŒ
  • localization of the date formats ๐ŸŒ
  • availabilities in the date picker ๐Ÿ“…
  • Fully boosted with Turbolinks ๐Ÿš€

rails stimulus flatpickr demo

What does it takes to make this app

The date picker field

<%= form_with model: appointment do |f| %>
  <%= f.text_field :start_at,
      placeholder: t(".select"),
      data: {
        controller: "flatpickr",
        flatpickr_alt_format: t("date.formats.long"),
        flatpickr_alt_input: true,
        flatpickr_default_date: appointment.start_at,
        flatpickr_locale: locale,
        flatpickr_show_months: 2,
        flatpickr_min_date: Time.zone.now,
        flatpickr_max_date: Time.zone.now + 60.days,
        flatpickr_disable: Appointment.up_comings(60).pluck(:start_at) - [appointment.start_at],
      } %>
<% end %>

and the stimulus controller

import Flatpickr from "stimulus-flatpickr";
import Rails from "rails-ujs";

// import a theme (could be in your main CSS entry too...)
import "flatpickr/dist/themes/dark.css";

// import the translation files and create a translation mapping
import { French } from "flatpickr/dist/l10n/fr.js";
import { english } from "flatpickr/dist/l10n/default.js";

// create a new Stimulus controller by extending stimulus-flatpickr wrapper controller
export default class extends Flatpickr {
  locales = {
    fr: French,
    en: english
  };

  connect() {
    //define locale and global flatpickr settings for all datepickers
    this.config = {
      locale: this.locale,
      altInput: true,
      showMonths: 2
    };

    super.connect();
  }

  // automatically submit form when a date is selected
  change(selectedDates, dateStr, instance) {
    const form = this.element.closest("form");
    Rails.fire(form, "submit");
  }

  get locale() {
    if (this.locales && this.data.has("locale")) {
      return this.locales[this.data.get("locale")];
    } else {
      return "";
    }
  }
}

rails_stimulus_flatpickr's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rails_stimulus_flatpickr's Issues

Can't get flatpickr to work in production/staging environment.

Probably not your fault, but you seem to understand this stuff better that I do and maybe you can point me in the right direction. I don't do a lot of java/coffee script, enough to get by. I tried to modernize an app and more or less started from scratch with rails 6, web packer, no jQuery. Also had to ditch ZurbFoundation since it required jquery and soon found out that mixing frameworks caused problems (used w3.css instead). Got most things working after finding stimulus. I even got Flatpickr working without stimulus - in development, just using the data-behavior approach.

I was ready to show someone and set up a staging server. Everything worked except for flatpickr. I get a console log msg that it fired, but any date fields do not show the calendar. The field in not enterable, there are no javascript error. I then used your approach of putting putting it under stimulus, and got the same results. This is deployed with Capistrano and even though webpacker says staging environment will default to production, I added that to see if it had any affect.

My stimulus control is a stripped down version on one of your examples. Webpacker has been a tedious journey, you can do things different way and there is no real documentation or good examples on getting it all working. Here is the controller and again, work fine in development.

import Flatpickr from "stimulus-flatpickr";
console.log("entering picker")

import { Application } from 'stimulus'

import { definitionsFromContext } from 'stimulus/webpack-helpers'
const application = Application.start()
const context = require.context('../controllers', true, /\.js$/)
application.load(definitionsFromContext(context))

// Manually register Flatpickr as a stimulus controller
application.register('flatpickr', Flatpickr)

// import a theme (could be in your main CSS entry too...)
import "flatpickr/dist/flatpickr.css";

export default class extends Flatpickr {

  initialize() {
     //global options
    console.log("got a picker")
    this.config = {
      altInput: true,
      altFormat: "F j, Y",
      dateFormat: "Y-m-d"
    }
    super.connect();
  }
}

I guess it could be my old enemy TurboLinks - I usually had to disable it with Foundation. With no errors, I really don't know where to look.

Trying this out with Rails 7, Hotwire and Stimulus, busted

I am trying a basic example of stimulus-flatpickr

But when I try my esbuild of the packages and try and run the sample code, it stops at this:

node_modules/stimulus-flatpickr/dist/index.m.js:1:27:
13:59:49 js.1 | 1 โ”‚ import { Controller } from 'stimulus';

Any idea how to fix that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.