fix(Picker): failed to update modelValue

This commit is contained in:
chenjiahan 2022-01-20 15:53:25 +08:00 committed by neverland
parent 6c64bc33c1
commit ff69fdacc7
3 changed files with 37 additions and 13 deletions

View File

@ -21,6 +21,7 @@ import {
BORDER_UNSET_TOP_BOTTOM,
} from '../utils';
import {
isValuesEqual,
getColumnsType,
findOptionByValue,
formatCascadeColumns,
@ -115,6 +116,14 @@ export default defineComponent({
fields.value,
selectedValues
);
// reset values after cascading
selectedValues.value.forEach((value, index) => {
const options = currentColumns.value[index];
if (!options.find((option) => option[fields.value.value] === value)) {
selectedValues.value[index] = options[0][fields.value.value];
}
});
}
emit('change', {
@ -265,16 +274,22 @@ export default defineComponent({
watch(
() => props.modelValue,
(value) => {
selectedValues.value = value;
}
(newValues) => {
if (!isValuesEqual(newValues, selectedValues.value)) {
selectedValues.value = newValues;
}
},
{ deep: true }
);
watch(
selectedValues,
(newValues) => {
if (!isValuesEqual(newValues, props.modelValue)) {
emit('update:modelValue', selectedValues.value);
}
},
{ deep: true }
);
watch(selectedValues, () => {
if (selectedValues.value !== props.modelValue) {
emit('update:modelValue', selectedValues.value);
}
});
useExpose<PickerExpose>({ confirm });

View File

@ -325,9 +325,9 @@ export default {
| Event | Description | Arguments |
| --- | --- | --- |
| confirm | Emitted when click confirm button | _{ selectedValues, selectedOptions }_ |
| cancel | Emitted when click cancel button | _{ selectedValues, selectedOptions }_ |
| change | Emitted when current option changed | _{ selectedValues, selectedOptions, columnIndex }_ |
| confirm | Emitted when the confirm button is clicked | _{ selectedValues, selectedOptions }_ |
| cancel | Emitted when the cancel button is clicked | _{ selectedValues, selectedOptions }_ |
| change | Emitted when current option is changed | _{ selectedValues, selectedOptions, columnIndex }_ |
### Slots

View File

@ -70,7 +70,6 @@ export function formatCascadeColumns(
if (!cursor && options.length) {
const firstValue = getFirstEnabledOption(options)[fields.value];
selectedValues.value[columnIndex] = firstValue;
cursor = findOptionByValue(options, firstValue, fields);
}
@ -86,3 +85,13 @@ export function getElementTranslateY(element: Element) {
const translateY = transform.slice(7, transform.length - 1).split(', ')[5];
return Number(translateY);
}
export function isValuesEqual(
valuesA: Array<string | number>,
valuesB: Array<string | number>
) {
return (
valuesA.length === valuesB.length &&
valuesA.every((value, index) => value === valuesB[index])
);
}