mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(Picker): picker columnsfieldnames responsiveness (#10562)
* fix(Picker): picker columnsfieldnames responsiveness * fix(Picker): picker columnsfieldnames responsiveness by computed Co-authored-by: wenyaoyan <wenyaoyan@tomatogames.com>
This commit is contained in:
parent
3b0965520b
commit
040ffbb8ca
@ -90,19 +90,14 @@ export default defineComponent({
|
|||||||
const hasOptions = ref(false);
|
const hasOptions = ref(false);
|
||||||
const formattedColumns = ref<PickerObjectColumn[]>([]);
|
const formattedColumns = ref<PickerObjectColumn[]>([]);
|
||||||
|
|
||||||
const {
|
const columnsFieldNames = computed(() => {
|
||||||
text: textKey,
|
const { columnsFieldNames } = props;
|
||||||
values: valuesKey,
|
return {
|
||||||
children: childrenKey,
|
text: columnsFieldNames?.text || props.valueKey || 'text',
|
||||||
} = extend(
|
values: columnsFieldNames?.values || 'values',
|
||||||
{
|
children: columnsFieldNames?.children || 'children',
|
||||||
// compatible with valueKey prop
|
};
|
||||||
text: props.valueKey || 'text',
|
});
|
||||||
values: 'values',
|
|
||||||
children: 'children',
|
|
||||||
},
|
|
||||||
props.columnsFieldNames
|
|
||||||
);
|
|
||||||
|
|
||||||
const { children, linkChildren } = useChildren(PICKER_KEY);
|
const { children, linkChildren } = useChildren(PICKER_KEY);
|
||||||
|
|
||||||
@ -113,10 +108,10 @@ export default defineComponent({
|
|||||||
const dataType = computed(() => {
|
const dataType = computed(() => {
|
||||||
const firstColumn = props.columns[0];
|
const firstColumn = props.columns[0];
|
||||||
if (typeof firstColumn === 'object') {
|
if (typeof firstColumn === 'object') {
|
||||||
if (childrenKey in firstColumn) {
|
if (columnsFieldNames.value.children in firstColumn) {
|
||||||
return 'cascade';
|
return 'cascade';
|
||||||
}
|
}
|
||||||
if (valuesKey in firstColumn) {
|
if (columnsFieldNames.value.values in firstColumn) {
|
||||||
return 'object';
|
return 'object';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -127,11 +122,11 @@ export default defineComponent({
|
|||||||
const formatted: PickerObjectColumn[] = [];
|
const formatted: PickerObjectColumn[] = [];
|
||||||
|
|
||||||
let cursor: PickerObjectColumn = {
|
let cursor: PickerObjectColumn = {
|
||||||
[childrenKey]: props.columns,
|
[columnsFieldNames.value.children]: props.columns,
|
||||||
};
|
};
|
||||||
|
|
||||||
while (cursor && cursor[childrenKey]) {
|
while (cursor && cursor[columnsFieldNames.value.children]) {
|
||||||
const children = cursor[childrenKey];
|
const children = cursor[columnsFieldNames.value.children];
|
||||||
let defaultIndex = cursor.defaultIndex ?? +props.defaultIndex;
|
let defaultIndex = cursor.defaultIndex ?? +props.defaultIndex;
|
||||||
|
|
||||||
while (children[defaultIndex] && children[defaultIndex].disabled) {
|
while (children[defaultIndex] && children[defaultIndex].disabled) {
|
||||||
@ -144,7 +139,8 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
|
|
||||||
formatted.push({
|
formatted.push({
|
||||||
[valuesKey]: cursor[childrenKey],
|
[columnsFieldNames.value.values]:
|
||||||
|
cursor[columnsFieldNames.value.children],
|
||||||
className: cursor.className,
|
className: cursor.className,
|
||||||
defaultIndex,
|
defaultIndex,
|
||||||
});
|
});
|
||||||
@ -159,7 +155,9 @@ export default defineComponent({
|
|||||||
const { columns } = props;
|
const { columns } = props;
|
||||||
|
|
||||||
if (dataType.value === 'plain') {
|
if (dataType.value === 'plain') {
|
||||||
formattedColumns.value = [{ [valuesKey]: columns }];
|
formattedColumns.value = [
|
||||||
|
{ [columnsFieldNames.value.values]: columns },
|
||||||
|
];
|
||||||
} else if (dataType.value === 'cascade') {
|
} else if (dataType.value === 'cascade') {
|
||||||
formatCascade();
|
formatCascade();
|
||||||
} else {
|
} else {
|
||||||
@ -167,7 +165,9 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
|
|
||||||
hasOptions.value = formattedColumns.value.some(
|
hasOptions.value = formattedColumns.value.some(
|
||||||
(item) => item[valuesKey] && item[valuesKey].length !== 0
|
(item) =>
|
||||||
|
item[columnsFieldNames.value.values] &&
|
||||||
|
item[columnsFieldNames.value.values].length !== 0
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -185,18 +185,19 @@ export default defineComponent({
|
|||||||
|
|
||||||
const onCascadeChange = (columnIndex: number) => {
|
const onCascadeChange = (columnIndex: number) => {
|
||||||
let cursor: PickerObjectColumn = {
|
let cursor: PickerObjectColumn = {
|
||||||
[childrenKey]: props.columns,
|
[columnsFieldNames.value.children]: props.columns,
|
||||||
};
|
};
|
||||||
const indexes = getIndexes();
|
const indexes = getIndexes();
|
||||||
|
|
||||||
for (let i = 0; i <= columnIndex; i++) {
|
for (let i = 0; i <= columnIndex; i++) {
|
||||||
cursor = cursor[childrenKey][indexes[i]];
|
cursor = cursor[columnsFieldNames.value.children][indexes[i]];
|
||||||
}
|
}
|
||||||
|
|
||||||
while (cursor && cursor[childrenKey]) {
|
while (cursor && cursor[columnsFieldNames.value.children]) {
|
||||||
columnIndex++;
|
columnIndex++;
|
||||||
setColumnValues(columnIndex, cursor[childrenKey]);
|
setColumnValues(columnIndex, cursor[columnsFieldNames.value.children]);
|
||||||
cursor = cursor[childrenKey][cursor.defaultIndex || 0];
|
cursor =
|
||||||
|
cursor[columnsFieldNames.value.children][cursor.defaultIndex || 0];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -345,14 +346,14 @@ export default defineComponent({
|
|||||||
formattedColumns.value.map((item, columnIndex) => (
|
formattedColumns.value.map((item, columnIndex) => (
|
||||||
<Column
|
<Column
|
||||||
v-slots={{ option: slots.option }}
|
v-slots={{ option: slots.option }}
|
||||||
textKey={textKey}
|
textKey={columnsFieldNames.value.text}
|
||||||
readonly={props.readonly}
|
readonly={props.readonly}
|
||||||
allowHtml={props.allowHtml}
|
allowHtml={props.allowHtml}
|
||||||
className={item.className}
|
className={item.className}
|
||||||
itemHeight={itemHeight.value}
|
itemHeight={itemHeight.value}
|
||||||
defaultIndex={item.defaultIndex ?? +props.defaultIndex}
|
defaultIndex={item.defaultIndex ?? +props.defaultIndex}
|
||||||
swipeDuration={props.swipeDuration}
|
swipeDuration={props.swipeDuration}
|
||||||
initialOptions={item[valuesKey]}
|
initialOptions={item[columnsFieldNames.value.values]}
|
||||||
visibleItemCount={props.visibleItemCount}
|
visibleItemCount={props.visibleItemCount}
|
||||||
onChange={() => onChange(columnIndex)}
|
onChange={() => onChange(columnIndex)}
|
||||||
/>
|
/>
|
||||||
|
@ -28,7 +28,6 @@ test('simple columns confirm & cancel event', () => {
|
|||||||
expect(wrapper.emitted('cancel')![0]).toEqual(['1990', 0]);
|
expect(wrapper.emitted('cancel')![0]).toEqual(['1990', 0]);
|
||||||
wrapper.unmount();
|
wrapper.unmount();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('multiple columns confirm & cancel event', () => {
|
test('multiple columns confirm & cancel event', () => {
|
||||||
const wrapper = mount(Picker, {
|
const wrapper = mount(Picker, {
|
||||||
props: {
|
props: {
|
||||||
@ -359,3 +358,28 @@ test('should not render mask and frame when options is empty', async () => {
|
|||||||
expect(wrapper.find('.van-picker__mask').exists()).toBeTruthy();
|
expect(wrapper.find('.van-picker__mask').exists()).toBeTruthy();
|
||||||
expect(wrapper.find('.van-picker__frame').exists()).toBeTruthy();
|
expect(wrapper.find('.van-picker__frame').exists()).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('columns-field-names responsiveness', async () => {
|
||||||
|
const columnsOne = [
|
||||||
|
{ type: 1, name: 'Ios' },
|
||||||
|
{ type: 2, name: 'Android' },
|
||||||
|
];
|
||||||
|
const columnsTwo = [
|
||||||
|
{ type: 1, serverName: 'server1' },
|
||||||
|
{ type: 2, serverName: 'server2' },
|
||||||
|
];
|
||||||
|
const wrapper = mount(Picker, {
|
||||||
|
props: {
|
||||||
|
columns: columnsOne,
|
||||||
|
columnsFieldNames: {
|
||||||
|
text: 'name',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(wrapper.findAll('.van-ellipsis')[0].text()).toEqual('Ios');
|
||||||
|
await wrapper.setProps({
|
||||||
|
columns: columnsTwo,
|
||||||
|
columnsFieldNames: { text: 'serverName' },
|
||||||
|
});
|
||||||
|
expect(wrapper.findAll('.van-ellipsis')[0].text()).toEqual('server1');
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user