Skip to content

Commit

Permalink
chore: add useModelMigration composable
Browse files Browse the repository at this point in the history
Signed-off-by: Grigorii K. Shartsev <[email protected]>
  • Loading branch information
ShGKme committed Oct 29, 2024
1 parent 777d206 commit 1333165
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/composables/useModelMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { getCurrentInstance, computed } from 'vue'

/**
* Create model proxy to new v9 model (modelValue + update:modelValue) with a fallback to old model
* @param {string} oldModelName - Name of model prop in nextcloud-vue v8
* @param {string} oldModelEvent - Event name of model event in nextcloud-vue v8
* @return {import('vue').WritableComputedRef} - model proxy
*/
export function useModelMigration(oldModelName, oldModelEvent) {
const vm = getCurrentInstance()!.proxy

const model = computed({
get() {
if (vm.$props[oldModelName] !== undefined) {
return vm.$props[oldModelName]
}
return vm.$props.modelValue
},

set(value) {
vm.$emit('update:modelValue', value)
vm.$emit(oldModelEvent, value)
}
})

return model
}

0 comments on commit 1333165

Please sign in to comment.