Skip to content

Commit 0186790

Browse files
Michael Besurelevithomason
authored andcommitted
fix(Accordion) fix wrong indexes when using Accordion.Title and Accordion.Content (#832)
* fix(Accordion) fix wrong indexes when using Accordion.Title and Accordion.Content * test(Accordion): add activeIndex test, move onClick test
1 parent d3fea1a commit 0186790

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

src/modules/Accordion/Accordion.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,28 @@ export default class Accordion extends Component {
101101
renderChildren = () => {
102102
const { children } = this.props
103103
const { activeIndex } = this.state
104+
let titleIndex = 0
105+
let contentIndex = 0
104106

105-
return Children.map(children, (child, i) => {
107+
return Children.map(children, (child) => {
106108
const isTitle = child.type === AccordionTitle
107109
const isContent = child.type === AccordionContent
108110

109111
if (isTitle) {
110-
const isActive = _.has(child, 'props.active') ? child.props.active : activeIndex === i
112+
const currentIndex = titleIndex
113+
const isActive = _.has(child, 'props.active') ? child.props.active : activeIndex === currentIndex
111114
const onClick = (e) => {
112-
this.handleTitleClick(e, i)
113-
if (child.props.onClick) child.props.onClick(e, i)
115+
this.handleTitleClick(e, currentIndex)
116+
if (child.props.onClick) child.props.onClick(e, currentIndex)
114117
}
118+
titleIndex++
115119
return cloneElement(child, { ...child.props, active: isActive, onClick })
116120
}
117121

118122
if (isContent) {
119-
// content must be the a sibling too title
120-
// it is active if the active title index that precedes it is active
121-
const isActive = _.has(child, 'props.active') ? child.props.active : activeIndex === i - 1
123+
const currentIndex = contentIndex
124+
const isActive = _.has(child, 'props.active') ? child.props.active : activeIndex === currentIndex
125+
contentIndex++
122126
return cloneElement(child, { ...child.props, active: isActive })
123127
}
124128

test/specs/modules/Accordion/Accordion-test.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,29 @@ describe('Accordion', () => {
7777
wrapper
7878
.should.have.state('activeIndex', -1)
7979
})
80+
it('sets the correct pair of title/content active', () => {
81+
const wrapper = shallow(
82+
<Accordion>
83+
<Accordion.Title />
84+
<Accordion.Content />
85+
<Accordion.Title />
86+
<Accordion.Content />
87+
<Accordion.Title />
88+
<Accordion.Content />
89+
</Accordion>
90+
)
91+
wrapper.setProps({ activeIndex: 0 })
92+
wrapper.childAt(0).should.have.prop('active', true)
93+
wrapper.childAt(1).should.have.prop('active', true)
94+
95+
wrapper.setProps({ activeIndex: 1 })
96+
wrapper.childAt(2).should.have.prop('active', true)
97+
wrapper.childAt(3).should.have.prop('active', true)
98+
99+
wrapper.setProps({ activeIndex: 2 })
100+
wrapper.childAt(4).should.have.prop('active', true)
101+
wrapper.childAt(5).should.have.prop('active', true)
102+
})
80103
})
81104

82105
describe('defaultActiveIndex', () => {
@@ -89,6 +112,7 @@ describe('Accordion', () => {
89112
describe('onTitleClick', () => {
90113
it('is called with (event, index)', () => {
91114
const spy = sandbox.spy()
115+
const event = { foo: 'bar' }
92116
const titles = mount(
93117
<Accordion onTitleClick={spy}>
94118
<Accordion.Title />
@@ -97,11 +121,11 @@ describe('Accordion', () => {
97121
)
98122
.find('AccordionTitle')
99123

100-
titles.at(0).simulate('click')
101-
spy.should.have.been.calledWithMatch({}, 0)
124+
titles.at(0).simulate('click', event)
125+
spy.should.have.been.calledWithMatch(event, 0)
102126

103-
titles.at(1).simulate('click')
104-
spy.should.have.been.calledWithMatch({}, 1)
127+
titles.at(1).simulate('click', event)
128+
spy.should.have.been.calledWithMatch(event, 1)
105129
})
106130
})
107131

@@ -184,6 +208,7 @@ describe('Accordion', () => {
184208

185209
it('is called with (event, index) on AccordionTitle click', () => {
186210
const spy = sandbox.spy()
211+
const event = { foo: 'bar' }
187212
const panels = [{
188213
onClick: spy,
189214
title: 'First panel',
@@ -192,15 +217,22 @@ describe('Accordion', () => {
192217
onClick: spy,
193218
title: 'Second panel',
194219
content: 'second panel content',
220+
}, {
221+
onClick: spy,
222+
title: 'Third panel',
223+
content: 'third panel content',
195224
}]
196225
const titles = mount(<Accordion panels={panels} />)
197226
.find('AccordionTitle')
198227

199-
titles.at(0).simulate('click')
200-
spy.should.have.been.calledWithMatch({}, 0)
228+
titles.at(0).simulate('click', event)
229+
spy.should.have.been.calledWithMatch(event, 0)
230+
231+
titles.at(1).simulate('click', event)
232+
spy.should.have.been.calledWithMatch(event, 1)
201233

202-
titles.at(1).simulate('click')
203-
spy.should.have.been.calledWithMatch({}, 1)
234+
titles.at(2).simulate('click', event)
235+
spy.should.have.been.calledWithMatch(event, 2)
204236
})
205237
})
206238
})

0 commit comments

Comments
 (0)