fix(DropdownMenu): fix recursive update when passing object literal to title-class (#12614)

This commit is contained in:
inottn 2024-02-03 12:54:58 +08:00 committed by GitHub
parent da08e699eb
commit a5a9a6c5db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import {
ref,
computed,
defineComponent,
toRaw,
type InjectionKey,
type CSSProperties,
type ExtractPropTypes,
@ -17,7 +18,6 @@ import {
makeNumericProp,
createNamespace,
HAPTICS_FEEDBACK,
type ComponentInstance,
} from '../utils';
// Composables
@ -33,6 +33,7 @@ import {
// Types
import type { DropdownMenuProvide, DropdownMenuDirection } from './types';
import type { DropdownItemInstance } from '../dropdown-item';
const [name, bem] = createNamespace('dropdown-menu');
@ -63,7 +64,10 @@ export default defineComponent({
const barRef = ref<HTMLElement>();
const offset = ref(0);
const { children, linkChildren } = useChildren(DROPDOWN_KEY);
const { children, linkChildren } = useChildren<
DropdownItemInstance,
unknown
>(DROPDOWN_KEY);
const scrollParent = useScrollParent(root);
const opened = computed(() =>
@ -121,9 +125,9 @@ export default defineComponent({
});
};
const renderTitle = (item: ComponentInstance, index: number) => {
const renderTitle = (item: DropdownItemInstance, index: number) => {
const { showPopup } = item.state;
const { disabled, titleClass } = item;
const { disabled, titleClass } = toRaw(item.$props);
return (
<div

View File

@ -367,3 +367,28 @@ test('auto-locate prop', async () => {
vi.doUnmock('../../utils/dom');
});
test('title-class prop', async () => {
const titleClass = ref({ custom: true });
const wrapper = mount({
setup() {
return () => (
<DropdownMenu>
<DropdownItem titleClass={{ custom: true }} />
<DropdownItem titleClass={titleClass.value} />
</DropdownMenu>
);
},
});
await later();
const titles = wrapper.findAll('.van-dropdown-menu__title');
// using object literal should work
expect(titles[0].classes()).toContain('custom');
expect(titles[1].classes()).toContain('custom');
titleClass.value.custom = false;
await later();
expect(titles[1].classes()).not.toContain('custom');
});