fix(PickerGroup): rendering correctly when using v-for (#12732)

This commit is contained in:
inottn 2024-03-31 16:05:59 +08:00 committed by GitHub
parent a65e9d4bd1
commit b1e746f45b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 2 deletions

View File

@ -1,12 +1,15 @@
import {
defineComponent,
Comment,
Fragment,
type InjectionKey,
type ExtractPropTypes,
type VNode,
} from 'vue';
// Utils
import {
flat,
pick,
extend,
makeArrayProp,
@ -76,9 +79,20 @@ export default defineComponent({
const onCancel = () => emit('cancel');
return () => {
const childNodes = slots
let childNodes = slots
.default?.()
?.filter((node) => node.type !== Comment);
?.filter((node) => node.type !== Comment)
.map((node) => {
if (node.type === Fragment) {
return node.children as VNode[];
}
return node;
});
if (childNodes) {
childNodes = flat(childNodes);
}
const confirmButtonText = showNextButton()
? props.nextStepText

View File

@ -143,3 +143,25 @@ test('support controlled mode to set active-tab', async () => {
await later();
expect(tabs[1]?.classes()).toContain('van-tab--active');
});
test('should render correctly with v-for', async () => {
const Comp = {
components: {
Picker,
PickerGroup,
},
template: `
<picker-group :tabs="['Tab1', 'Tab2']">
<picker v-for="_ in 2" :columns="[{ text: '1', value: '1' }]" />
</picker-group>
`,
};
const wrapper = mount(Comp);
const items = wrapper.findAll('.van-swipe-item');
items.forEach((item) => {
const picker = item.find('.van-picker');
expect(picker.exists()).toEqual(true);
});
});

View File

@ -81,3 +81,6 @@ export const isSameValue = (newValue: unknown, oldValue: unknown) =>
export const toArray = <T>(item: T | T[]): T[] =>
Array.isArray(item) ? item : [item];
export const flat = <T>(arr: Array<T | T[]>) =>
arr.reduce<T[]>((acc, val) => acc.concat(val), []);