Skip to content

Commit 4eda9d7

Browse files
committed
fix popper onUpdate api usage
1 parent bcf06d4 commit 4eda9d7

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

src/portal-popper.jsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class PortalPopper extends Component {
4444

4545
return (
4646
<Portal
47-
ref='popper'
4847
className={`${className} ${prefix}-${placement}`}
48+
ref='portal'
4949
style={this._getPopperStyle()}
5050
>
5151
<span>{title}</span>
@@ -63,24 +63,17 @@ class PortalPopper extends Component {
6363
}
6464

6565
componentDidMount () {
66-
this.popper = new this.props.Popper(this.props.getTargetNode(), this.refs.popper.domNode, {
66+
this.popper = new this.props.Popper(this.props.getTargetNode(), this.refs.portal.domNode, {
6767
content: this.props.title,
6868
placement: this.props.placement,
6969
modifiers: {
7070
applyStyle: { enabled: true },
7171
arrow: { element: this.refs.arrow },
7272
},
73+
onCreate: this._updateData,
74+
onUpdate: this._updateData,
7375
})
7476

75-
this.popper.onUpdate = (data) => {
76-
if (this.isUnmounted) return
77-
78-
const newState = {}
79-
if (data.offsets.arrow) newState.arrowProps = data.offsets.arrow
80-
if (data.offsets.popper) newState.popperProps = data.offsets.popper
81-
this.setState(newState)
82-
}
83-
8477
this.popper.scheduleUpdate()
8578
}
8679

@@ -90,6 +83,16 @@ class PortalPopper extends Component {
9083
}
9184
}
9285

86+
_updateData = (data) => {
87+
if (this.isUnmounted) return
88+
89+
const newState = {}
90+
if (data.offsets.arrow) newState.arrowProps = data.offsets.arrow
91+
if (data.offsets.popper) newState.popperProps = data.offsets.popper
92+
if (data.flipped != null) newState.flipped = data.flipped
93+
this.setState(newState)
94+
}
95+
9396
_getPopperStyle () {
9497
const left = Math.round(this.state.popperProps.left)
9598
const top = Math.round(this.state.popperProps.top)

src/portal-popper.spec.jsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const getProps = (props) => {
1616
}
1717

1818
const popperInstanceStub = () => ({
19-
onUpdate: sinon.spy(),
19+
onCreate: sinon.stub(),
20+
onUpdate: sinon.stub(),
2021
scheduleUpdate: sinon.spy(),
2122
destroy: sinon.spy(),
2223
})
@@ -91,58 +92,65 @@ describe('<PortalPopper />', () => {
9192
describe('Popper#onUpate', () => {
9293
it('updates the arrow props if specified', () => {
9394
const popperInstance = popperInstanceStub()
94-
const component = mount(<PortalPopper {...getProps({ Popper: popperStub(popperInstance) })} />)
95-
popperInstance.onUpdate({ offsets: { arrow: { left: 5, top: 10 } } })
95+
const Popper = popperStub(popperInstance)
96+
const component = mount(<PortalPopper {...getProps({ Popper })} />)
97+
Popper.firstCall.args[2].onUpdate({ offsets: { arrow: { left: 5, top: 10 } } })
9698
expect(component.ref('arrow').prop('style').left).to.equal(5)
9799
expect(component.ref('arrow').prop('style').top).to.equal(10)
98100
})
99101

100102
it('does not update the arrow props if not specified', () => {
101103
const popperInstance = popperInstanceStub()
102-
const component = mount(<PortalPopper {...getProps({ Popper: popperStub(popperInstance) })} />)
103-
popperInstance.onUpdate({ offsets: {} })
104+
const Popper = popperStub(popperInstance)
105+
const component = mount(<PortalPopper {...getProps({ Popper })} />)
106+
Popper.firstCall.args[2].onUpdate({ offsets: {} })
104107
expect(component.ref('arrow').prop('style').left).to.equal(0)
105108
expect(component.ref('arrow').prop('style').top).to.equal(0)
106109
})
107110

108111
it('only updates the arrow props that are specified', () => {
109112
const popperInstance = popperInstanceStub()
110-
const component = mount(<PortalPopper {...getProps({ Popper: popperStub(popperInstance) })} />)
111-
popperInstance.onUpdate({ offsets: { arrow: { top: 20 } } })
113+
const Popper = popperStub(popperInstance)
114+
const component = mount(<PortalPopper {...getProps({ Popper })} />)
115+
Popper.firstCall.args[2].onUpdate({ offsets: { arrow: { top: 20 } } })
112116
expect(component.ref('arrow').prop('style').left).to.equal(null)
113117
expect(component.ref('arrow').prop('style').top).to.equal(20)
114118
})
115119

116120
it('rounds the arrow props', () => {
117121
const popperInstance = popperInstanceStub()
118-
const component = mount(<PortalPopper {...getProps({ Popper: popperStub(popperInstance) })} />)
119-
popperInstance.onUpdate({ offsets: { arrow: { left: 7.2, top: 20.8 } } })
122+
const Popper = popperStub(popperInstance)
123+
const component = mount(<PortalPopper {...getProps({ Popper })} />)
124+
Popper.firstCall.args[2].onUpdate({ offsets: { arrow: { left: 7.2, top: 20.8 } } })
120125
expect(component.ref('arrow').prop('style').left).to.equal(7)
121126
expect(component.ref('arrow').prop('style').top).to.equal(21)
122127
})
123128

124129
it('updates the popper props if specified', () => {
125130
const popperInstance = popperInstanceStub()
126-
const component = mount(<PortalPopper {...getProps({ Popper: popperStub(popperInstance) })} />)
127-
popperInstance.onUpdate({ offsets: { popper: { position: 'relative', left: 2, top: 4 } } })
131+
const Popper = popperStub(popperInstance)
132+
const component = mount(<PortalPopper {...getProps({ Popper })} />)
133+
Popper.firstCall.args[2].onUpdate({ offsets: { popper: { position: 'relative', left: 2, top: 4 } } })
128134
expect(component.find(Portal).prop('style').position).to.equal('relative')
129135
expect(component.find(Portal).prop('style').transform).to.equal('translate3d(2px, 4px, 0)')
130136
expect(component.find(Portal).prop('style').WebkitTransform).to.equal('translate3d(2px, 4px, 0)')
131137
})
132138

133139
it('does not update the popper props if not specified', () => {
134140
const popperInstance = popperInstanceStub()
135-
const component = mount(<PortalPopper {...getProps({ Popper: popperStub(popperInstance) })} />)
136-
popperInstance.onUpdate({ offsets: {} })
141+
const Popper = popperStub(popperInstance)
142+
const component = mount(<PortalPopper {...getProps({ Popper })} />)
143+
Popper.firstCall.args[2].onUpdate({ offsets: {} })
137144
expect(component.find(Portal).prop('style').position).to.equal('absolute')
138145
expect(component.find(Portal).prop('style').transform).to.equal('translate3d(0px, 0px, 0)')
139146
expect(component.find(Portal).prop('style').WebkitTransform).to.equal('translate3d(0px, 0px, 0)')
140147
})
141148

142149
it('rounds the popper props', () => {
143150
const popperInstance = popperInstanceStub()
144-
const component = mount(<PortalPopper {...getProps({ Popper: popperStub(popperInstance) })} />)
145-
popperInstance.onUpdate({ offsets: { popper: { left: 15.2, top: 2.8 } } })
151+
const Popper = popperStub(popperInstance)
152+
const component = mount(<PortalPopper {...getProps({ Popper })} />)
153+
Popper.firstCall.args[2].onUpdate({ offsets: { popper: { left: 15.2, top: 2.8 } } })
146154
expect(component.find(Portal).prop('style').transform).to.equal('translate3d(15px, 3px, 0)')
147155
expect(component.find(Portal).prop('style').WebkitTransform).to.equal('translate3d(15px, 3px, 0)')
148156
})

0 commit comments

Comments
 (0)