Skip to content

Commit 1b406ea

Browse files
authored
fix(cascader): [cascader,select] add the changeCompat attribute to the component (#2934)
* fix(option): add title prop for options * fix(cascader): add changeCompat属性
1 parent c1080ec commit 1b406ea

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed

packages/renderless/src/cascader/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ export const computePresentContent =
8181
}
8282
}
8383

84+
/** 用户在修改值,不必触发change */
8485
export const watchValue =
8586
({ api, state }: { api: ICascaderApi; state: ICascaderState }) =>
8687
(value) => {
8788
if (!isEqual(value, state.checkedValue as any)) {
89+
state.userChangeValue = true
8890
state.checkedValue = value
8991
setTimeout(api.computePresentContent)
9092
}
@@ -97,7 +99,8 @@ export const watchCheckedValue =
9799
dispatch,
98100
api,
99101
emit,
100-
state
102+
state,
103+
props
101104
}: {
102105
api: ICascaderApi
103106
emit: ICascadeRenderlessParamUtils['emit']
@@ -122,8 +125,18 @@ export const watchCheckedValue =
122125
])
123126
})
124127

125-
emit('update:modelValue', value)
126-
emit('change', value)
128+
// 用户触发的modelValue变化时: props.changeCompat=true, 则emit, 否则忽略
129+
if (state.userChangeValue) {
130+
if (props.changeCompat) {
131+
emit('update:modelValue', value)
132+
emit('change', value)
133+
state.userChangeValue = false
134+
}
135+
} else {
136+
// 非用户触发modelValue, 表明是用户操作click, delete时的动作。
137+
emit('update:modelValue', value)
138+
emit('change', value)
139+
}
127140

128141
setTimeout(api.updateStyle)
129142
}

packages/renderless/src/cascader/vue.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ const initState = ({
133133
isHidden: false,
134134
tooltipVisible: false,
135135
tooltipContent: '',
136-
tagTypeWhenMultiple: designConfig?.tagTypeWhenMultiple || ''
136+
tagTypeWhenMultiple: designConfig?.tagTypeWhenMultiple || '',
137+
userChangeValue: false
137138
})
138139

139140
return state as ICascaderState
@@ -188,7 +189,7 @@ const initApi = ({
188189
watchValue: watchValue({ api, state }),
189190
computePresentTags: computePresentTags({ api, props, state }),
190191
computePresentContent: computePresentContent({ api, state }),
191-
watchCheckedValue: watchCheckedValue({ constants, dispatch, api, nextTick, emit, state }),
192+
watchCheckedValue: watchCheckedValue({ constants, dispatch, api, nextTick, emit, state, props }),
192193
toggleDropDownVisible: toggleDropDownVisible({ emit, vm, state, updatePopper }),
193194
selfMounted: selfMounted({ api, parent, props, vm, state }),
194195
handleBeforeUpdate: handleBeforeUpdate({ props, api, state }),
@@ -217,7 +218,7 @@ const initWatch = ({
217218
}) => {
218219
watch(() => state.disabled, api.computePresentContent)
219220

220-
watch(() => props.modelValue, api.watchValue)
221+
watch(() => props.modelValue, api.watchValue) // 用户在修改值,不必触发change
221222

222223
watch(() => state.checkedValue, api.watchCheckedValue)
223224

packages/vue/src/cascader/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export const cascaderProps = {
100100
blank: {
101101
type: Boolean,
102102
default: false
103+
},
104+
changeCompat: {
105+
type: Boolean,
106+
default: false
103107
}
104108
}
105109

packages/vue/src/cascader/src/mobile-first.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { IconChevronRight } from '@opentiny/vue-icon'
4040

4141
import CascaderMobile from '@opentiny/vue-cascader-mobile'
4242
import PcFirst from './pc-first.vue'
43+
4344
export default defineComponent({
4445
props: [
4546
...props,
@@ -91,7 +92,8 @@ export default defineComponent({
9192
'expand-change',
9293
'active-item-change',
9394
'remove-tag',
94-
'created'
95+
'created',
96+
'changeCompat'
9597
],
9698
setup(props, ctx) {
9799
const bp = useBreakpoint().current

packages/vue/src/cascader/src/pc.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ export default defineComponent({
234234
'label',
235235
'tip',
236236
'hoverExpand',
237-
'blank'
237+
'blank',
238+
'changeCompat'
238239
],
239240
emits: [
240241
'update:modelValue',

packages/vue/src/option/src/pc.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
highlightClass
1919
]"
20+
:title="state.currentLabel || ''"
2021
>
2122
<span v-if="state.selectMultiple" class="tiny-option__checkbox-wrap tiny-select-dropdown__item-checkbox">
2223
<component :is="`icon-${state.selectCls}`" :class="`tiny-svg-size ${state.selectCls}`" />

0 commit comments

Comments
 (0)