Skip to content

Commit 1f59cde

Browse files
committed
refactor: made actions code more DRY
1 parent 82ce1cc commit 1f59cde

File tree

2 files changed

+15
-37
lines changed

2 files changed

+15
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-flatpickr-plus",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Flatpickr is a lightweight and powerful datetime picker. Svelte Flatpickr Plus is a wrapper for Flatpickr with some extra features.",
55
"homepage": "https://github.com/kodaicoder/svelte-flatpickr-plus",
66
"bugs": {

src/lib/actions.svelte.js

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -289,28 +289,20 @@ function attachFlatpickr(node, opts, plugins = opts.noCalendar ? [] : [yearDropd
289289
return fp;
290290
}
291291

292-
/** @type {import('svelte/action').Action<HTMLInputElement,FlatpickrOptions,EventKey>} */
293-
const datePicker = (node, options) => {
294-
options = { ...defaultOptions, ...options };
295-
const opts = modifyHooks(options);
296-
const instance = attachFlatpickr(node, opts);
297-
298-
$effect(() => {
299-
if (opts.defaultDate) {
300-
const event = new Event('input');
301-
node.dispatchEvent(event);
292+
/** @type {import('svelte/action').Action} */
293+
export default function (node, options = defaultOptions) {
294+
if (options.isMonthPicker) {
295+
options = {
296+
...defaultMonthOptions, ...options
302297
}
303-
return () => {
304-
instance.destroy();
305-
instance._input?.form?.removeEventListener('reset', (ev) => resetFlatpickr(ev, instance, opts));
306-
};
307-
});
308-
}
298+
} else {
299+
options = {
300+
...defaultOptions, ...options
301+
}
302+
}
309303

310-
/** @type {import('svelte/action').Action<HTMLInputElement,FlatpickrOptions,EventKey>} */
311-
const monthPicker = (node, options) => {
312-
options = { ...defaultOptions, ...options };
313304
const opts = modifyHooks(options);
305+
314306
const monthPlugins =
315307
[
316308
monthSelectPlugin({
@@ -320,31 +312,17 @@ const monthPicker = (node, options) => {
320312
}),
321313
yearDropdownPlugin(),
322314
]
323-
const instance = attachFlatpickr(node, opts, monthPlugins);
315+
316+
const instance = attachFlatpickr(node, opts, options.isMonthPicker ? monthPlugins : []);
317+
324318
$effect(() => {
325319
if (opts.defaultDate) {
326320
const event = new Event('input');
327321
node.dispatchEvent(event);
328322
}
329-
330323
return () => {
331324
instance.destroy();
332325
instance._input?.form?.removeEventListener('reset', (ev) => resetFlatpickr(ev, instance, opts));
333326
};
334327
});
335-
}
336-
337-
/** @type {import('svelte/action').Action<HTMLInputElement,FlatpickrOptions,EventKey>} */
338-
export default function (node, options = defaultOptions) {
339-
if (options.isMonthPicker) {
340-
options = {
341-
...defaultMonthOptions, ...options
342-
}
343-
monthPicker(node, options);
344-
} else {
345-
options = {
346-
...defaultOptions, ...options
347-
}
348-
datePicker(node, options);
349-
}
350328
}

0 commit comments

Comments
 (0)