fix(date-picker): load rangePlugin dynamically

This commit is contained in:
Eric Y Liu 2021-07-14 12:14:07 -07:00
commit 77771712f9
3 changed files with 46 additions and 41 deletions

View file

@ -128,9 +128,8 @@
}, },
}); });
afterUpdate(() => { async function initCalendar() {
if ($hasCalendar && !calendar) { calendar = await createCalendar({
calendar = createCalendar({
options: { options: {
appendTo: datePickerRef, appendTo: datePickerRef,
dateFormat, dateFormat,
@ -165,6 +164,7 @@
}); });
} }
afterUpdate(() => {
if (calendar) { if (calendar) {
if ($range) { if ($range) {
calendar.setDate([$inputValueFrom, $inputValueTo]); calendar.setDate([$inputValueFrom, $inputValueTo]);
@ -189,6 +189,7 @@
$: valueFrom = $inputValueFrom; $: valueFrom = $inputValueFrom;
$: inputValueTo.set(valueTo); $: inputValueTo.set(valueTo);
$: valueTo = $inputValueTo; $: valueTo = $inputValueTo;
$: if ($hasCalendar && !calendar && inputRef) initCalendar();
</script> </script>
<svelte:body <svelte:body

View file

@ -72,9 +72,7 @@
add({ id, labelText }); add({ id, labelText });
onMount(() => { $: if (ref) declareRef({ id, ref });
declareRef({ id, ref });
});
</script> </script>
<div <div

View file

@ -1,5 +1,4 @@
import flatpickr from "flatpickr"; import flatpickr from "flatpickr";
import rangePlugin from "flatpickr/dist/plugins/rangePlugin";
let l10n; let l10n;
@ -48,7 +47,7 @@ function updateMonthNode(instance) {
} }
} }
function createCalendar({ options, base, input, dispatch }) { async function createCalendar({ options, base, input, dispatch }) {
let locale = options.locale; let locale = options.locale;
if (options.locale === "en" && l10n && l10n.en) { if (options.locale === "en" && l10n && l10n.en) {
@ -61,6 +60,13 @@ function createCalendar({ options, base, input, dispatch }) {
locale = l10n.en; locale = l10n.en;
} }
let rangePlugin;
if (options.mode === "range") {
const importee = await import("flatpickr/dist/esm/plugins/rangePlugin");
rangePlugin = importee.default;
}
return new flatpickr(base, { return new flatpickr(base, {
...options, ...options,
allowInput: true, allowInput: true,