Skip to content

Commit

Permalink
fix(PickerGroup): rendering correctly when using v-for (youzan#12732)
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn authored and bluesky335 committed Apr 1, 2024
1 parent 736903d commit 0779694
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/vant/src/picker-group/PickerGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import {
defineComponent,
Comment,
Fragment,
type InjectionKey,
type ExtractPropTypes,
type VNode,
ComponentPublicInstance,
nextTick,
} from 'vue';

// Utils
import {
flat,
pick,
extend,
makeArrayProp,
Expand Down Expand Up @@ -105,9 +108,20 @@ export default defineComponent({
useExpose<PickerGroupExpose>({ confirm: doConfirm });

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
Expand Down
22 changes: 22 additions & 0 deletions packages/vant/src/picker-group/test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
3 changes: 3 additions & 0 deletions packages/vant/src/utils/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), []);

0 comments on commit 0779694

Please sign in to comment.