flowbite icon indicating copy to clipboard operation
flowbite copied to clipboard

how to add flowbite datepicker in angular application

Open sunysunysuny opened this issue 2 years ago • 13 comments

datepicker

I am following the steps as suggest in documentation

npm i @themesberg/tailwind-datepicker --save

As I am importing it into my component file it displaying below error

import Datepicker from '@themesberg/tailwind-datepicker/Datepicker';

Cannot find module '@themesberg/tailwind-datepicker/Datepicker' or its corresponding type declarations.

Let me know where I am making mistakes

Thanks

sunysunysuny avatar Mar 24 '22 13:03 sunysunysuny

Hi @sunysunysuny Have you try to import the Datepicker from th CDN? You can find the link here

But it looks like Flowbite doesn't support types 🤔

stefanovolpatti avatar May 05 '22 08:05 stefanovolpatti

datepicker

I am following the steps as suggest in documentation

npm i @themesberg/tailwind-datepicker --save

As I am importing it into my component file it displaying below error

import Datepicker from '@themesberg/tailwind-datepicker/Datepicker';

Cannot find module '@themesberg/tailwind-datepicker/Datepicker' or its corresponding type declarations.

Let me know where I am making mistakes

Thanks

@sunysunysuny Could you solve this or find any workaround? If yes, give me the info, please.

yunushuyut avatar Apr 05 '23 10:04 yunushuyut

I've same problem. I'm using vanilla flowbite

BariqDharmawan avatar May 09 '23 08:05 BariqDharmawan

nothing yet ?

leomunizq avatar May 09 '23 08:05 leomunizq

Using the datepicker from the Flowbite docs https://flowbite.com/docs/plugins/datepicker/#javascript I got the same issue, the import Datepicker from 'flowbite-datepicker/Datepicker'; was not working.

So what I did (I don't think it's the perfect way to do it, but it works):

Add this to the scripts section of angular.json file:

node_modules/flowbite-datepicker/dist/js/datepicker-full.min.js

image

Then to use it in my component I declared a var named Datepicker:

declare var Datepicker: any;

Then use it simply like in the docs.

image

EDIT

I faced another problem, when the input value changed (using the date picker, the user choose a date) the form control is not getting the change event, so the value is not updated on it. So I added an event listener (javascript way) and added some logic:

private initDatePickerElement(element: any): void {
    new Datepicker(element, this.datePickerConfig);
    element.addEventListener('changeDate', (e: any) => {
      const value = e.target.value;
      const formControlName = e.target.getAttribute('formControlNamePath');
      const formControl = this.form.get(formControlName);
      formControl?.setValue(value);
      formControl?.markAsDirty();
    });
  }

Explanation:

  1. Flowbite Datepicker initialization
  2. Add an event listener on changeDate event
  3. Getting the formControl dynamically based on the formControlName attribute of the field that it's value changed
  4. Setting the value on this formControl
  5. Mark the formControl as touched (dirty), this is optional (I have to do it because I have some logic on my html)

heloufir avatar Aug 05 '23 19:08 heloufir

Amazing, your solution works! Thank you so much!

nieuwie avatar Jan 02 '24 18:01 nieuwie

@heloufir works fine for "Datepicker" but does NOT work for "DateRangePicker"

declare var Datepicker: any;
declare var DateRangePicker: any;
export class DateRangeSelectComponent implements AfterViewInit {
  @ViewChild('dateField') date!: ElementRef;

  ngAfterViewInit(): void {
    new Datepicker(this.date.nativeElement, {}); // Works fine
    new DateRangePicker(this.date.nativeElement, {}); // Not works!
}

pachoGit avatar Jan 11 '24 20:01 pachoGit

@heloufir @pachoGit Would you be so kind and provide a minimal repro using stackblitz please? I can't make it work.

alator21 avatar Mar 11 '24 17:03 alator21

@pachoGit for DateRangePicker the nativeElement must be a container of two inputs otherwise it doesn't work

cirinoe avatar Mar 14 '24 18:03 cirinoe

@heloufir works fine for "Datepicker" but does NOT work for "DateRangePicker"

declare var Datepicker: any;
declare var DateRangePicker: any;
export class DateRangeSelectComponent implements AfterViewInit {
  @ViewChild('dateField') date!: ElementRef;

  ngAfterViewInit(): void {
    new Datepicker(this.date.nativeElement, {}); // Works fine
    new DateRangePicker(this.date.nativeElement, {}); // Not works!
}

i did follow this but i got an error 'ReferenceError: Datepicker is not defined'

kidskidspacana avatar Mar 31 '24 10:03 kidskidspacana

@heloufir works fine for "Datepicker" but does NOT work for "DateRangePicker"

declare var Datepicker: any;
declare var DateRangePicker: any;
export class DateRangeSelectComponent implements AfterViewInit {
  @ViewChild('dateField') date!: ElementRef;

  ngAfterViewInit(): void {
    new Datepicker(this.date.nativeElement, {}); // Works fine
    new DateRangePicker(this.date.nativeElement, {}); // Not works!
}

i did follow this but i got an error 'ReferenceError: Datepicker is not defined'

Hi,

Just got the same issue on a new project, and resolved it following these steps:

  • install Datepicker dependency: npm install flowbite-datepicker --save
  • Add javascript file to you angular.json file:
"scripts": [
  "...",
  "node_modules/flowbite-datepicker/dist/js/datepicker.js"
]
  • add the Datepicker reference into your component:
declare var Datepicker: any;
  • then use the Datepicker object:
new Datepicker(element, {});

heloufir avatar Apr 02 '24 01:04 heloufir

Using the datepicker from the Flowbite docs https://flowbite.com/docs/plugins/datepicker/#javascript I got the same issue, the import Datepicker from 'flowbite-datepicker/Datepicker'; was not working.

So what I did (I don't think it's the perfect way to do it, but it works):

Add this to the scripts section of angular.json file:

node_modules/flowbite-datepicker/dist/js/datepicker-full.min.js

image Then to use it in my component I declared a var named `Datepicker`:

declare var Datepicker: any;

Then use it simply like in the docs.

image ### EDIT I faced another problem, when the input value changed (using the date picker, the user choose a date) the form control is not getting the change event, so the value is not updated on it. So I added an event listener (javascript way) and added some logic:
private initDatePickerElement(element: any): void {
    new Datepicker(element, this.datePickerConfig);
    element.addEventListener('changeDate', (e: any) => {
      const value = e.target.value;
      const formControlName = e.target.getAttribute('formControlNamePath');
      const formControl = this.form.get(formControlName);
      formControl?.setValue(value);
      formControl?.markAsDirty();
    });
  }

Explanation:

  1. Flowbite Datepicker initialization
  2. Add an event listener on changeDate event
  3. Getting the formControl dynamically based on the formControlName attribute of the field that it's value changed
  4. Setting the value on this formControl
  5. Mark the formControl as touched (dirty), this is optional (I have to do it because I have some logic on my html)

Nice fix. Thanks !!

JuankAnacona avatar Apr 18 '24 20:04 JuankAnacona