mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-10 00:27:41 +08:00
fix(datetime): values are adjusted to be in bounds (#26125)
resolves #25894, resolves #25708
This commit is contained in:
@ -36,70 +36,6 @@ export class PickerColumnInternal implements ComponentInterface {
|
||||
* A list of options to be displayed in the picker
|
||||
*/
|
||||
@Prop() items: PickerColumnItem[] = [];
|
||||
@Watch('items')
|
||||
itemsChange(currentItems: PickerColumnItem[], previousItems: PickerColumnItem[]) {
|
||||
const { value } = this;
|
||||
|
||||
/**
|
||||
* When the items change, it is possible for the item
|
||||
* that was selected to no longer exist. In that case, we need
|
||||
* to automatically select the nearest item. If we do not,
|
||||
* then the scroll position will be reset to zero and it will
|
||||
* look like the first item was automatically selected.
|
||||
*
|
||||
* If we cannot find a closest item then we do nothing, and
|
||||
* the browser will reset the scroll position to 0.
|
||||
*/
|
||||
const findCurrentItem = currentItems.find((item) => item.value === value);
|
||||
if (!findCurrentItem) {
|
||||
/**
|
||||
* The default behavior is to assume
|
||||
* that the new set of data is similar to the old
|
||||
* set of data, just with some items filtered out.
|
||||
* We walk backwards through the data to find the
|
||||
* closest enabled picker item and select it.
|
||||
*
|
||||
* Developers can also swap the items out for an entirely
|
||||
* new set of data. In that case, the value we select
|
||||
* here likely will not make much sense. For this use case,
|
||||
* developers should update the `value` prop themselves
|
||||
* when swapping out the data.
|
||||
*/
|
||||
const findPreviousItemIndex = previousItems.findIndex((item) => item.value === value);
|
||||
if (findPreviousItemIndex === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Step through the current items backwards
|
||||
* until we find a neighbor we can select.
|
||||
* We start at the last known location of the
|
||||
* current selected item in order to
|
||||
* account for data that has been added. This
|
||||
* search prioritizes stability in that it
|
||||
* tries to keep the scroll position as close
|
||||
* to where it was before the update.
|
||||
* Before Items: ['a', 'b', 'c'], Selected Value: 'b'
|
||||
* After Items: ['a', 'dog', 'c']
|
||||
* Even though 'dog' is a different item than 'b',
|
||||
* it is the closest item we can select while
|
||||
* preserving the scroll position.
|
||||
*/
|
||||
let nearestItem;
|
||||
for (let i = findPreviousItemIndex; i >= 0; i--) {
|
||||
const item = currentItems[i];
|
||||
if (item !== undefined && item.disabled !== true) {
|
||||
nearestItem = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (nearestItem) {
|
||||
this.setValue(nearestItem.value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The selected option in the picker.
|
||||
|
||||
Reference in New Issue
Block a user