1

I'm trying to find a date picker for my custom web component.

I've found Pikaday, which works outside of SF, but inside SF it loads fine, but when I click on the date field the calendar flashes up and disappears.

If I alt tab out and back into chrome the calender appears and I can use it. Very strange.

I've believe I've integrated it correctly as per instructions of Pikaday site and followed the integration into SF as a static resource. Check my homework.

I've used other libraries without a problem. Can anyone share a working reference?

I wonder if it is a CSS issue, but cannot fathom it.

Code of the webcomponent is here:

Template

<template>
    <lightning-card title="HelloWorld" icon-name="custom:custom14">
        <div class="slds-m-around_medium">
        <input type="text" class="date-picker" lwc:dom="manual"></input>
    </div>
    </lightning-card>
</template>

JS class

import { LightningElement, track } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
import Pika from '@salesforce/resourceUrl/Pikaday';

export default class CreateEvent extends LightningElement {

    changeHandler(event) {
        this.greeting = event.target.value;
    }

    pikaInitialized = false;

    renderedCallback() {
        if (this.pikaInitialized) {
            return;
        }
        this.pikaInitialized = true;

        Promise.all([
            loadScript(this, Pika + '/Pikaday-master/pikaday.js'),
            loadStyle(this, Pika + '/Pikaday-master/css/site.css'),
            loadStyle(this,Pika + '/Pikaday-master/css/pikaday.css'),


        ])
            .then(() => {
                this.initializePikaday();
            })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error loading Pika',
                        message: error.message,
                        variant: 'error',
                    }),
                );
            });
    }

    initializePikaday() {
        const field = this.template.querySelector('.date-picker');

        // eslint-disable-next-line
        var picker = new Pikaday({
            field: field,
            pickWholeWeek: true,
            onSelect: function (date) {
                var sundayDate = date.getDate() - date.getDay();
                var sunday = new Date(date.setDate(sundayDate));
                var saturday = new Date(date.setDate(sundayDate + 6));
                field.value = sunday.toLocaleDateString() + ' - ' + saturday.toLocaleDateString();
            }
        });

    }
}
7
  • do you have some code samples so that we can take a look and see how we can help?
    – Ronnie
    Apr 10, 2019 at 23:19
  • Apologies. Yes, edit now to include html and JS. Pikaday library is loaded as a static resource.
    – Skynet5
    Apr 11, 2019 at 8:46
  • Have you tried moving the PikaDay into connectedCallback instead of renderedCallback? you will go through many render phases that will happen on the component, so I would think the connectedCallback should initialize it 1 time properly and not have to re-init every render.
    – Ronnie
    Apr 11, 2019 at 18:29
  • Ok. I've tried that, didn't work. Very odd. I can click on the desktop and back to chrome and the drop down appears. Thanks for the support.
    – Skynet5
    Apr 11, 2019 at 21:22
  • what browser are you doing this in? have you tried other browsers as well and see the same performance from this API?
    – Ronnie
    Apr 12, 2019 at 3:48

0

Browse other questions tagged .