Skip to content

fix(cascader): [cascader,select] add the changeCompat attribute to the component #2934

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

Merged
merged 2 commits into from
Feb 20, 2025
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
19 changes: 16 additions & 3 deletions packages/renderless/src/cascader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ export const computePresentContent =
}
}

/** 用户在修改值,不必触发change */
export const watchValue =
({ api, state }: { api: ICascaderApi; state: ICascaderState }) =>
(value) => {
if (!isEqual(value, state.checkedValue as any)) {
state.userChangeValue = true
state.checkedValue = value
setTimeout(api.computePresentContent)
}
Expand All @@ -97,7 +99,8 @@ export const watchCheckedValue =
dispatch,
api,
emit,
state
state,
props
}: {
api: ICascaderApi
emit: ICascadeRenderlessParamUtils['emit']
Expand All @@ -122,8 +125,18 @@ export const watchCheckedValue =
])
})

emit('update:modelValue', value)
emit('change', value)
// 用户触发的modelValue变化时: props.changeCompat=true, 则emit, 否则忽略
if (state.userChangeValue) {
if (props.changeCompat) {
emit('update:modelValue', value)
emit('change', value)
state.userChangeValue = false
}
} else {
// 非用户触发modelValue, 表明是用户操作click, delete时的动作。
emit('update:modelValue', value)
emit('change', value)
}

setTimeout(api.updateStyle)
}
Expand Down
7 changes: 4 additions & 3 deletions packages/renderless/src/cascader/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ const initState = ({
isHidden: false,
tooltipVisible: false,
tooltipContent: '',
tagTypeWhenMultiple: designConfig?.tagTypeWhenMultiple || ''
tagTypeWhenMultiple: designConfig?.tagTypeWhenMultiple || '',
userChangeValue: false
})

return state as ICascaderState
Expand Down Expand Up @@ -188,7 +189,7 @@ const initApi = ({
watchValue: watchValue({ api, state }),
computePresentTags: computePresentTags({ api, props, state }),
computePresentContent: computePresentContent({ api, state }),
watchCheckedValue: watchCheckedValue({ constants, dispatch, api, nextTick, emit, state }),
watchCheckedValue: watchCheckedValue({ constants, dispatch, api, nextTick, emit, state, props }),
toggleDropDownVisible: toggleDropDownVisible({ emit, vm, state, updatePopper }),
selfMounted: selfMounted({ api, parent, props, vm, state }),
handleBeforeUpdate: handleBeforeUpdate({ props, api, state }),
Expand Down Expand Up @@ -217,7 +218,7 @@ const initWatch = ({
}) => {
watch(() => state.disabled, api.computePresentContent)

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

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

Expand Down
4 changes: 4 additions & 0 deletions packages/vue/src/cascader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export const cascaderProps = {
blank: {
type: Boolean,
default: false
},
changeCompat: {
type: Boolean,
default: false
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/vue/src/cascader/src/mobile-first.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { IconChevronRight } from '@opentiny/vue-icon'

import CascaderMobile from '@opentiny/vue-cascader-mobile'
import PcFirst from './pc-first.vue'

export default defineComponent({
props: [
...props,
Expand Down Expand Up @@ -91,7 +92,8 @@ export default defineComponent({
'expand-change',
'active-item-change',
'remove-tag',
'created'
'created',
'changeCompat'
],
setup(props, ctx) {
const bp = useBreakpoint().current
Expand Down
3 changes: 2 additions & 1 deletion packages/vue/src/cascader/src/pc.vue
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ export default defineComponent({
'label',
'tip',
'hoverExpand',
'blank'
'blank',
'changeCompat'
Comment on lines +237 to +238
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Maintain consistency with mobile-first component.

The mobile-first component includes 'changeCompat' in its emits array, but it's missing here. Consider adding it to maintain consistency across components.

Add the event to emits:

   emits: [
     'update:modelValue',
     'change',
     'visible-change',
     'focus',
     'blur',
     'expand-change',
     'active-item-change',
     'remove-tag',
-    'created'
+    'created',
+    'changeCompat'
   ],
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'blank',
'changeCompat'
emits: [
'update:modelValue',
'change',
'visible-change',
'focus',
'blur',
'expand-change',
'active-item-change',
'remove-tag',
'created',
'changeCompat'
],

],
emits: [
'update:modelValue',
Expand Down
1 change: 1 addition & 0 deletions packages/vue/src/option/src/pc.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
highlightClass
]"
:title="state.currentLabel || ''"
>
<span v-if="state.selectMultiple" class="tiny-option__checkbox-wrap tiny-select-dropdown__item-checkbox">
<component :is="`icon-${state.selectCls}`" :class="`tiny-svg-size ${state.selectCls}`" />
Expand Down
Loading