fix(Picker): should move to first option when all options are disabled (#7504)

This commit is contained in:
neverland 2020-11-05 16:01:07 +08:00 committed by GitHub
parent 287a4acea0
commit f07d8e6709
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 5 deletions

View File

@ -89,11 +89,13 @@ export default createComponent({
const { children } = cursor;
let defaultIndex = cursor.defaultIndex ?? +this.defaultIndex;
while (
children[defaultIndex].disabled &&
defaultIndex < children.length - 1
) {
defaultIndex++;
while (children[defaultIndex] && children[defaultIndex].disabled) {
if (defaultIndex < children.length - 1) {
defaultIndex++;
} else {
defaultIndex = 0;
break;
}
}
formatted.push({

View File

@ -6,6 +6,37 @@ exports[`disabled in cascade 1`] = `
</li>
`;
exports[`should move to first option when all options are disabled 1`] = `
<div class="van-picker">
<!---->
<div class="van-picker__columns" style="height: 264px;">
<div class="van-picker-column">
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 110px, 0); transition-duration: 0ms; transition-property: none;">
<li role="button" tabindex="-1" class="van-picker-column__item van-picker-column__item--disabled van-picker-column__item--selected" style="height: 44px;">
<div class="van-ellipsis">A1</div>
</li>
<li role="button" tabindex="-1" class="van-picker-column__item van-picker-column__item--disabled" style="height: 44px;">
<div class="van-ellipsis">A2</div>
</li>
</ul>
</div>
<div class="van-picker-column">
<ul class="van-picker-column__wrapper" style="transform: translate3d(0, 110px, 0); transition-duration: 0ms; transition-property: none;">
<li role="button" tabindex="-1" class="van-picker-column__item van-picker-column__item--disabled van-picker-column__item--selected" style="height: 44px;">
<div class="van-ellipsis">B1</div>
</li>
<li role="button" tabindex="-1" class="van-picker-column__item van-picker-column__item--disabled" style="height: 44px;">
<div class="van-ellipsis">B2</div>
</li>
</ul>
</div>
<div class="van-picker__mask" style="background-size: 100% 110px;"></div>
<div class="van-hairline-unset--top-bottom van-picker__frame" style="height: 44px;"></div>
</div>
<!---->
</div>
`;
exports[`should move to next option when default option is disabled 1`] = `
<div class="van-picker">
<!---->

View File

@ -145,3 +145,30 @@ test('should move to next option when default option is disabled', () => {
expect(wrapper).toMatchSnapshot();
});
test('should move to first option when all options are disabled', () => {
const wrapper = mount(Picker, {
propsData: {
columns: [
{
text: 'A1',
disabled: true,
children: [
{ text: 'B1', disabled: true },
{ text: 'B2', disabled: true },
],
},
{
text: 'A2',
disabled: true,
children: [
{ text: 'B3', disabled: true },
{ text: 'B4', disabled: true },
],
},
],
},
});
expect(wrapper).toMatchSnapshot();
});