Skip to content

Commit

Permalink
feat: expose datepicker instance
Browse files Browse the repository at this point in the history
  • Loading branch information
blasdfaa committed Jun 24, 2024
1 parent 79f3e77 commit dd66443
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/vue-air-datepicker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
},
"dependencies": {
"@vueuse/core": "^10.9.0",
"air-datepicker": "3.5.3"
"air-datepicker": "3.5.3",
"vue-demi": ">=0.14.8"
},
"devDependencies": {
"typescript": "^5.4.5",
Expand Down
15 changes: 9 additions & 6 deletions packages/vue-air-datepicker/src/use-datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,34 @@ import type { MaybeRefOrGetter } from '@vueuse/core'
import { tryOnMounted, tryOnUnmounted, unrefElement } from '@vueuse/core'
import type { AirDatepickerOptions } from 'air-datepicker'
import AirDatepicker from 'air-datepicker'
import { ref } from 'vue-demi'

type UserDatepickerOptions = AirDatepickerOptions<HTMLElement>

export function useDatepicker(
el: MaybeRefOrGetter<HTMLElement | null | undefined> | string,
options: UserDatepickerOptions = {},
) {
let datepicker: AirDatepicker<HTMLElement> | undefined
const datepicker = ref<AirDatepicker<HTMLElement>>()

function init() {
if (datepicker?.isDestroyed)
if (datepicker.value?.isDestroyed)
return

const target = (typeof el === 'string' ? el : unrefElement(el))
if (!target || datepicker !== undefined)
if (!target || datepicker.value !== undefined)
return

datepicker = new AirDatepicker(target, options)
datepicker.value = new AirDatepicker(target, options)
}

function destroy() {
datepicker?.destroy()
datepicker = undefined
datepicker.value?.destroy()
datepicker.value = undefined
}

tryOnMounted(init)
tryOnUnmounted(destroy)

return datepicker
}
11 changes: 7 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd66443

Please sign in to comment.