feat: move config preparation in separate function

This commit is contained in:
Dominik Pschenitschni 2022-10-24 16:40:57 +02:00
parent 87300f0e01
commit 5772cf7483
Signed by: dpschen
GPG Key ID: B257AC0149F43A77
1 changed files with 17 additions and 11 deletions

View File

@ -84,7 +84,7 @@ const props = defineProps({
// );
// }
},
// https://chmln.github.io/flatpickr/options/
// https://flatpickr.js.org/options/
config: {
type: Object as PropType<Options>,
default: () => ({
@ -117,17 +117,10 @@ const root = ref<HTMLInputElement | null>(null)
const fp = ref<Flatpickr.Instance | null>(null)
const safeConfig = ref<Options>(cloneObject(props.config))
onMounted(() => {
function prepareConfig() {
// Don't mutate original object on parent component
const newConfig = cloneObject(props.config)
if (
fp.value || // Return early if flatpickr is already loaded
!root.value // our input needs to be mounted
) {
return
}
props.events.forEach((hook) => {
// Respect global callbacks registered via setDefault() method
const globalCallbacks = Flatpickr.defaultConfig[hook] || []
@ -155,6 +148,19 @@ onMounted(() => {
safeConfig.value = newConfig
return safeConfig.value
}
onMounted(() => {
if (
fp.value || // Return early if flatpickr is already loaded
!root.value // our input needs to be mounted
) {
return
}
prepareConfig()
/**
* Get the HTML node where flatpickr to be attached
* Bind on parent element if wrap is true
@ -225,8 +231,8 @@ watch(
newValue => {
// Prevent updates if v-model value is same as input's current value
if (!root.value || newValue === nullify(root.value.value)) return
// Make sure we have a flatpickr instanceand
// Notify flatpickr instance that there is a change in value
// Make sure we have a flatpickr instance and
// notify flatpickr instance that there is a change in value
fp.value?.setDate(newValue, true)
},
{deep: true},