Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Cascader): 修复 change 事件被触发两次和 v-model 没有响应式的问题 #1096

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/cascader/cascader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ export default defineComponent({
childrenInfo.value = e;
childrenInfo.level = level;
} else {
setCascaderValue(item[(keys as Ref<KeysType>).value?.value ?? 'value']);
props.onChange?.(
setCascaderValue(
item[(keys as Ref<KeysType>).value?.value ?? 'value'],
items.map((item, index) => toRaw(item?.[selectedIndexes[index]])),
);
Expand Down
15 changes: 11 additions & 4 deletions src/shared/useVModel/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ref, Ref, getCurrentInstance, ComponentInternalInstance } from 'vue';
import kebabCase from 'lodash/kebabCase';

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

Expand All @@ -10,12 +11,18 @@ export function useVModel<T>(
propName = 'value',
// emit 和 eventName 用于支持 v-model 和 xxx.sync 语法糖
): [Ref<T>, ChangeHandler<T>] {
const { emit } = getCurrentInstance() as ComponentInternalInstance;
const { emit, vnode } = 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 (typeof value.value !== 'undefined') {
if (isVMP || typeof value.value !== 'undefined') {
anlyyao marked this conversation as resolved.
Show resolved Hide resolved
return [
value,
(newValue, ...args) => {
Expand All @@ -26,7 +33,7 @@ export function useVModel<T>(
}

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