formio.js icon indicating copy to clipboard operation
formio.js copied to clipboard

Calendar picker (Flatpickr) does not work properly in Arabic

Open dthtien opened this issue 1 year ago • 2 comments

Describe the bug Hi guys, thanks for the wonderful library. I’ve found an issue that may be a potential bug. The value in the date/time picker is not properly retained on blur when using Arabic.

Version/Branch Latest CDN: https://cdn.form.io/formiojs/formio.full.min.js

To Reproduce CodeSandbox Steps to reproduce the behavior:

  1. Create form...
  2. Add component...
<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
    />
    <link
      rel="stylesheet"
      href="https://cdn.form.io/formiojs/formio.full.min.css"
    />
    <script src="https://cdn.form.io/formiojs/formio.full.min.js"></script>
  </head>

  <body class="container px-1 pt-2">
    <div id="app"></div>

    <script src="src/index.js"></script>
    <script>
      window.addEventListener("load", () => {
        const form = {
          display: "wizard",
          components: [
            {
              label: "Date / Time",
              tableView: false,
              datePicker: {
                disableWeekends: false,
                disableWeekdays: false,
              },
              enableMinDateInput: false,
              enableMaxDateInput: false,
              validateWhenHidden: false,
              key: "dateTime",
              type: "datetime",
              input: true,
              widget: {
                type: "calendar",
                displayInTimezone: "viewer",
                locale: "ar",
                useLocaleSettings: false,
                allowInput: true,
                mode: "single",
                enableTime: true,
                noCalendar: false,
                format: "yyyy-MM-dd hh:mm a",
                hourIncrement: 1,
                minuteIncrement: 1,
                time_24hr: false,
                minDate: null,
                disableWeekends: false,
                disableWeekdays: false,
                maxDate: null,
              },
            },
          ],
        };
        Formio.builder(document.getElementById("app"), form, {
          language: "ar",
        });
      });
    </script>
  </body>
</html>
  1. Select date and time
  2. When selecting the period (AM/PM), the value is reset on blur but it will retain the value properly when hitting Return/Enter

Expected behavior It should retain the value correctly on blur.

Screenshots

https://github.com/user-attachments/assets/96f666c5-3062-4f28-a814-9863228b822f

Additional context Please help to take a look. Thank you

dthtien avatar Aug 23 '24 01:08 dthtien

I am not able to reproduce this issue on our latest version of formiojs. You may want to try using our latest version of formiojs and see if that fixes your issue. Below is the script tag for the cdn <script src="https://cdn.form.io/js/formio.embed.js"></script>

ZenMasterJacob20011 avatar Aug 28 '24 13:08 ZenMasterJacob20011

I am not able to reproduce this issue on our latest version of formiojs. You may want to try using our latest version of formiojs and see if that fixes your issue. Below is the script tag for the cdn <script src="https://cdn.form.io/js/formio.embed.js"></script>

Hi @ZenMasterJacob20011, Thanks for your reply. You can see the issue in the CodeSandbox URL that I mentioned in the issue description.

By the way, if I use the embed script you provided, <script src="https://cdn.form.io/js/formio.embed.js"></script>, the Calendar renders in English instead of Arabic. Here is the Codesandbox URL

Screenshot 2024-08-29 at 13 52 58

Code

<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
    />
    <link
      rel="stylesheet"
      href="https://cdn.form.io/formiojs/formio.full.min.css"
    />
    <script src="https://cdn.form.io/js/formio.embed.js"></script>
  </head>

  <body class="container px-1 pt-2">
    <div id="app"></div>

    <script src="src/index.js"></script>
    <script>
      window.addEventListener("load", () => {
        const form = {
          display: "wizard",
          components: [
            {
              label: "Date / Time",
              tableView: false,
              datePicker: {
                disableWeekends: false,
                disableWeekdays: false,
              },
              enableMinDateInput: false,
              enableMaxDateInput: false,
              validateWhenHidden: false,
              key: "dateTime",
              type: "datetime",
              input: true,
              widget: {
                type: "calendar",
                displayInTimezone: "viewer",
                locale: "ar",
                useLocaleSettings: false,
                allowInput: true,
                mode: "single",
                enableTime: true,
                noCalendar: false,
                format: "yyyy-MM-dd hh:mm a",
                hourIncrement: 1,
                minuteIncrement: 1,
                time_24hr: false,
                minDate: null,
                disableWeekends: false,
                disableWeekdays: false,
                maxDate: null,
              },
            },
          ],
        };
        Formio.builder(document.getElementById("app"), form, {
          language: "ar",
        });
      });
    </script>
  </body>
</html>

dthtien avatar Aug 29 '24 03:08 dthtien

Did you make sure that you imported the correct locale scripts before you loaded the renderer with this locale? Try to add the arabic local in a similar way to the docs here.

https://flatpickr.js.org/localization/

travist avatar Sep 04 '24 20:09 travist

Hi @travist, do we need to do that? As I saw in the code, it should import automatically (please correct me if I’m wrong). I also tried importing the Flatpickr localization via a script tag, but it didn’t work. CodeSandbox

dthtien avatar Sep 19 '24 23:09 dthtien

The code from "requireLibrary" only works if the locale already exists within our CDN. I would inspect the network of your form when it is rendered and if there is a network error trying to load a file that does not exist, then this means that your locale is not included. Our recommendation of loading your own locale puts the control back to you where you can explicitly provide the locales you need without having to rely on our inclusion of it in our cdn.

travist avatar Sep 20 '24 14:09 travist

Thanks! I’ll add it myself, @travist. However, isn’t the Flatpickr Arabic locale already included in the FormIO CDN? Here’s the link: https://cdn.form.io/flatpickr-formio/4.6.13-formio.3/l10n/flatpickr-ar.js

dthtien avatar Sep 23 '24 00:09 dthtien

It does seem to be, but you will need to inspect the network traffic of your browser to determine if it properly loads this file if AR is used as the language. It is possible that it is pulling from a path that is not defined.

travist avatar Sep 23 '24 14:09 travist

The issue is that when I use the CDN https://cdn.form.io/formiojs/formio.full.min.js, the calendar is rendered correctly in Arabic, but it has the problem described in the issue. However, if I use https://cdn.form.io/js/formio.embed.js, the calendar doesn’t render in Arabic, which is confusing.

dthtien avatar Sep 23 '24 22:09 dthtien

Are there any assets that get loaded (by looking at Network tab), or API's that fire when you use the "full" vs the "embed" script?

travist avatar Sep 24 '24 20:09 travist