mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-18 11:36:36 +00:00
fix(date-picker): load rangePlugin dynamically
This commit is contained in:
parent
48d4c0ba37
commit
77771712f9
3 changed files with 46 additions and 41 deletions
|
@ -128,43 +128,43 @@
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function initCalendar() {
|
||||||
|
calendar = await createCalendar({
|
||||||
|
options: {
|
||||||
|
appendTo: datePickerRef,
|
||||||
|
dateFormat,
|
||||||
|
defaultDate: $inputValue,
|
||||||
|
locale,
|
||||||
|
maxDate,
|
||||||
|
minDate,
|
||||||
|
mode: $mode,
|
||||||
|
},
|
||||||
|
base: inputRef,
|
||||||
|
input: inputRefTo,
|
||||||
|
dispatch: (event) => {
|
||||||
|
const detail = { selectedDates: calendar.selectedDates };
|
||||||
|
|
||||||
|
if ($range) {
|
||||||
|
const from = inputRef.value;
|
||||||
|
const to = inputRefTo.value;
|
||||||
|
|
||||||
|
detail.dateStr = {
|
||||||
|
from: inputRef.value,
|
||||||
|
to: inputRefTo.value,
|
||||||
|
};
|
||||||
|
|
||||||
|
valueFrom = from;
|
||||||
|
valueTo = to;
|
||||||
|
} else {
|
||||||
|
detail.dateStr = inputRef.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return dispatch(event, detail);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
afterUpdate(() => {
|
afterUpdate(() => {
|
||||||
if ($hasCalendar && !calendar) {
|
|
||||||
calendar = createCalendar({
|
|
||||||
options: {
|
|
||||||
appendTo: datePickerRef,
|
|
||||||
dateFormat,
|
|
||||||
defaultDate: $inputValue,
|
|
||||||
locale,
|
|
||||||
maxDate,
|
|
||||||
minDate,
|
|
||||||
mode: $mode,
|
|
||||||
},
|
|
||||||
base: inputRef,
|
|
||||||
input: inputRefTo,
|
|
||||||
dispatch: (event) => {
|
|
||||||
const detail = { selectedDates: calendar.selectedDates };
|
|
||||||
|
|
||||||
if ($range) {
|
|
||||||
const from = inputRef.value;
|
|
||||||
const to = inputRefTo.value;
|
|
||||||
|
|
||||||
detail.dateStr = {
|
|
||||||
from: inputRef.value,
|
|
||||||
to: inputRefTo.value,
|
|
||||||
};
|
|
||||||
|
|
||||||
valueFrom = from;
|
|
||||||
valueTo = to;
|
|
||||||
} else {
|
|
||||||
detail.dateStr = inputRef.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return dispatch(event, detail);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -72,9 +72,7 @@
|
||||||
|
|
||||||
add({ id, labelText });
|
add({ id, labelText });
|
||||||
|
|
||||||
onMount(() => {
|
$: if (ref) declareRef({ id, ref });
|
||||||
declareRef({ id, ref });
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|
|
@ -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,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue