From f07d8e67092821efca185e0ce1fb1edae860a3af Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 5 Nov 2020 16:01:07 +0800 Subject: [PATCH] fix(Picker): should move to first option when all options are disabled (#7504) --- src/picker/index.js | 12 ++++--- .../test/__snapshots__/cascade.spec.js.snap | 31 +++++++++++++++++++ src/picker/test/cascade.spec.js | 27 ++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/picker/index.js b/src/picker/index.js index 5b5a379b1..689c6323c 100644 --- a/src/picker/index.js +++ b/src/picker/index.js @@ -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({ diff --git a/src/picker/test/__snapshots__/cascade.spec.js.snap b/src/picker/test/__snapshots__/cascade.spec.js.snap index 09e3c4952..df19f4545 100644 --- a/src/picker/test/__snapshots__/cascade.spec.js.snap +++ b/src/picker/test/__snapshots__/cascade.spec.js.snap @@ -6,6 +6,37 @@ exports[`disabled in cascade 1`] = ` `; +exports[`should move to first option when all options are disabled 1`] = ` +
+ +
+
+
    +
  • +
    A1
    +
  • +
  • +
    A2
    +
  • +
+
+
+
    +
  • +
    B1
    +
  • +
  • +
    B2
    +
  • +
+
+
+
+
+ +
+`; + exports[`should move to next option when default option is disabled 1`] = `
diff --git a/src/picker/test/cascade.spec.js b/src/picker/test/cascade.spec.js index 7fce043eb..90d599878 100644 --- a/src/picker/test/cascade.spec.js +++ b/src/picker/test/cascade.spec.js @@ -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(); +});