diff --git a/components/accordion/__tests__/accordion.browser-test.jsx b/components/accordion/__tests__/accordion.browser-test.jsx index 3a50ef12ab..bac5759776 100644 --- a/components/accordion/__tests__/accordion.browser-test.jsx +++ b/components/accordion/__tests__/accordion.browser-test.jsx @@ -269,6 +269,23 @@ describe('Accordion', function describeFunction() { lastAccordionButton.getDOMNode() === document.activeElement ).to.equal(true); }); + + it('does not throw on arrow up with only one panel', () => { + wrapper = mount(, { + attachTo: mountNode, + }); + const accordionButtons = wrapper.find( + 'button.slds-accordion__summary-action' + ); + + expect(() => + accordionButtons.at(0).simulate('keyDown', { + key: 'ArrowUp', + keyCode: 38, + which: 38, + }) + ).to.not.throw(); + }); }); describe('Open panel', () => { diff --git a/components/accordion/index.jsx b/components/accordion/index.jsx index 2c239ee717..eba55dc8d7 100644 --- a/components/accordion/index.jsx +++ b/components/accordion/index.jsx @@ -73,9 +73,11 @@ class Accordion extends Component { ); } + const childCount = React.Children.count(this.props.children); + if (e.key === 'ArrowDown') { e.preventDefault(); - if (buttonIndex < this.props.children.length - 1) { + if (buttonIndex < childCount - 1) { this.setState({ currButtonIndex: buttonIndex + 1, }); @@ -89,7 +91,7 @@ class Accordion extends Component { currButtonIndex: buttonIndex - 1, }); } else { - this.setState({ currButtonIndex: this.props.children.length - 1 }); + this.setState({ currButtonIndex: childCount - 1 }); } } }