Skip to content

Commit

Permalink
fix: fix(cascader): 修复变更选项触发两次onchange事件和v-model绑定ref()没有响应式问题
Browse files Browse the repository at this point in the history
fix(cascader): 修复变更选项触发两次onchange事件和v-model绑定ref()没有响应式问题
  • Loading branch information
bitjian committed Sep 18, 2023
1 parent fccc3c2 commit 1671db0
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/shared/useVModel/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ref, Ref, getCurrentInstance, ComponentInternalInstance } from 'vue';
import kebabCase from 'lodash/kebabCase';

export type ChangeHandler<T> = (value: T, ...args: any[]) => void;

Expand All @@ -11,20 +10,12 @@ export function useVModel<T>(
propName = 'value',
// emit 和 eventName 用于支持 v-model 和 xxx.sync 语法糖
): [Ref<T>, ChangeHandler<T>] {
const { emit, vnode } = getCurrentInstance() as ComponentInternalInstance;
const { emit } = getCurrentInstance() as ComponentInternalInstance;
const internalValue = ref<T>() as Ref<T>;
internalValue.value = defaultValue;

const vProps = vnode.props || {};
const isVM =
Object.prototype.hasOwnProperty.call(vProps, 'modelValue') ||
Object.prototype.hasOwnProperty.call(vProps, 'model-value');
const isVMP =
Object.prototype.hasOwnProperty.call(vProps, propName) ||
Object.prototype.hasOwnProperty.call(vProps, kebabCase(propName));

// 受控模式 v-model:propName
if (isVMP) {
if (typeof value.value !== 'undefined') {
return [
value,
(newValue, ...args) => {
Expand All @@ -35,7 +26,7 @@ export function useVModel<T>(
}

// 受控模式:modelValue v-model
if (isVM) {
if (typeof modelValue.value !== 'undefined') {
return [
modelValue,
(newValue, ...args) => {
Expand Down

0 comments on commit 1671db0

Please sign in to comment.