Skip to content

Commit

Permalink
tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasha Kondrashov committed Mar 3, 2024
1 parent a10d684 commit 6f64485
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
12 changes: 8 additions & 4 deletions src/modules/Tab/Tab.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ export interface StrictTabProps {
* Called on tab change.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - The proposed new Tab.Pane.
* @param {object} data.activeIndex - The new proposed activeIndex.
* @param {object} data.panes - Props of the new proposed active pane.
* @param {object} props - The proposed new Tab.Pane.
* @param {object} props.panes - Props of the new proposed active pane.
* @param {number} activeIndex - The current activeIndex.
*/
onTabChange?: (event: React.MouseEvent<HTMLDivElement>, data: TabProps) => void
onTabChange?: (
event: React.MouseEvent<HTMLDivElement>,
props: TabProps,
activeIndex: number,
) => void

/**
* Array of objects describing each Menu.Item and Tab.Pane:
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Tab/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const Tab = React.forwardRef(function (props, ref) {
})

const handleItemClick = (e, { index }) => {
_.invoke(props, 'onTabChange', e, { ...props, activeIndex: index })
_.invoke(props, 'onTabChange', e, props, index)
setActiveIndex(index)
}

Expand Down
33 changes: 16 additions & 17 deletions test/specs/modules/Tab/Tab-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ describe('Tab', () => {
})

describe('onTabChange', () => {
it('is called with (e, { ...props, activeIndex }) a menu item is clicked', () => {
it('is called when a menu item is clicked', () => {
const activeIndex = 1
const spy = sandbox.spy()
const onTabChange = sandbox.spy()
const event = { fake: 'event' }
const props = { onTabChange: spy, panes }
const props = { onTabChange, panes }

mount(<Tab {...props} />)
.find('MenuItem')
Expand All @@ -167,30 +167,29 @@ describe('Tab', () => {

// Since React will have generated a key the returned tab won't match
// exactly so match on the props instead.
spy.should.have.been.calledOnce()
spy.firstCall.args[0].should.have.property('fake', 'event')
spy.firstCall.args[1].should.have.property('activeIndex', 1)
spy.firstCall.args[1].should.have.property('onTabChange', spy)
spy.firstCall.args[1].should.have.property('panes', panes)
onTabChange.should.have.been.calledOnce()
onTabChange.should.have.been.calledWithMatch(event, props, 1)
})
it('is called with the new proposed activeIndex, not the current', () => {
const spy = sandbox.spy()
const onTabChange = sandbox.spy()

const items = mount(<Tab activeIndex={-1} onTabChange={spy} panes={panes} />).find('MenuItem')
const items = mount(<Tab activeIndex={-1} onTabChange={onTabChange} panes={panes} />).find(
'MenuItem',
)

spy.should.have.callCount(0)
onTabChange.should.have.callCount(0)

items.at(0).simulate('click')
spy.should.have.callCount(1)
spy.lastCall.args[1].should.have.property('activeIndex', 0)
onTabChange.should.have.callCount(1)
onTabChange.lastCall.args[2].should.equal(0)

items.at(1).simulate('click')
spy.should.have.callCount(2)
spy.lastCall.args[1].should.have.property('activeIndex', 1)
onTabChange.should.have.callCount(2)
onTabChange.lastCall.args[2].should.equal(1)

items.at(2).simulate('click')
spy.should.have.callCount(3)
spy.lastCall.args[1].should.have.property('activeIndex', 2)
onTabChange.should.have.callCount(3)
onTabChange.lastCall.args[2].should.equal(2)
})
})

Expand Down

0 comments on commit 6f64485

Please sign in to comment.