Skip to content

Commit

Permalink
fix(Dropdown):make active dropdown item scroll into view on open (#1465)
Browse files Browse the repository at this point in the history
* fix:make active dropdown item visible into view(#1388)

* fix:check this.ref, menu, item in scrollSelectedItemIntoView method

* test(Dropdown): scroll item into view on open
  • Loading branch information
mchirkin authored and levithomason committed Mar 16, 2017
1 parent c1d5a67 commit 67c737f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ export default class Dropdown extends Component {
document.addEventListener('click', this.closeOnDocumentClick)
document.removeEventListener('keydown', this.openOnArrow)
document.removeEventListener('keydown', this.openOnSpace)
this.scrollSelectedItemIntoView()
} else if (prevState.open && !this.state.open) {
debug('dropdown closed')
this.handleClose()
Expand Down Expand Up @@ -939,8 +940,11 @@ export default class Dropdown extends Component {

scrollSelectedItemIntoView = () => {
debug('scrollSelectedItemIntoView()')
if (!this.ref) return
const menu = this.ref.querySelector('.menu.visible')
if (!menu) return
const item = menu.querySelector('.item.selected')
if (!item) return
debug(`menu: ${menu}`)
debug(`item: ${item}`)
const isOutOfUpperView = item.offsetTop < menu.scrollTop
Expand All @@ -962,6 +966,7 @@ export default class Dropdown extends Component {
if (onOpen) onOpen(e, this.props)

this.trySetState({ open: true })
this.scrollSelectedItemIntoView()
}

close = (e) => {
Expand Down
14 changes: 14 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,20 @@ describe('Dropdown', () => {
.setProps({ open: true })
dropdownMenuIsOpen()
})
it('calls scrollSelectedItemIntoView when changed from false to true', () => {
wrapperMount(<Dropdown options={options} selection open={false} />)

const instance = wrapper.instance()
sandbox.spy(instance, 'scrollSelectedItemIntoView')

instance.scrollSelectedItemIntoView
.should.not.have.been.called()

wrapper.setProps({ open: true })

instance.scrollSelectedItemIntoView
.should.have.been.calledOnce()
})
})

describe('multiple', () => {
Expand Down

0 comments on commit 67c737f

Please sign in to comment.