Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Search|Dropdown): Improve arrow navigation #701

Merged
merged 2 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,10 @@ export default class Dropdown extends Component {
const isOutOfUpperView = item.offsetTop < menu.scrollTop
const isOutOfLowerView = (item.offsetTop + item.clientHeight) > menu.scrollTop + menu.clientHeight

if (isOutOfUpperView || isOutOfLowerView) {
if (isOutOfUpperView) {
menu.scrollTop = item.offsetTop
} else if (isOutOfLowerView) {
menu.scrollTop = item.offsetTop + item.clientHeight - menu.clientHeight
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/modules/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,10 @@ export default class Search extends Component {
const isOutOfUpperView = item.offsetTop < menu.scrollTop
const isOutOfLowerView = (item.offsetTop + item.clientHeight) > menu.scrollTop + menu.clientHeight

if (isOutOfUpperView || isOutOfLowerView) {
if (isOutOfUpperView) {
menu.scrollTop = item.offsetTop
} else if (isOutOfLowerView) {
menu.scrollTop = item.offsetTop + item.clientHeight - menu.clientHeight
}
}

Expand Down
10 changes: 8 additions & 2 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ describe('Dropdown Component', () => {
dropdownMenuIsOpen()
const menu = document.querySelector('.ui.dropdown .menu.visible')

// Limit the menu's height and set an overflow so it's scrollable
menu.style.height = '100px'
menu.style.overflow = 'auto'

//
// Scrolls to bottom
//
Expand Down Expand Up @@ -467,8 +471,10 @@ describe('Dropdown Component', () => {
.find('.selected')
.should.contain.text(opts[0].text)

// menu should be completely scrolled to the bottom
const isMenuScrolledToTop = menu.scrollTop === 0
// Note: For some reason the first item's offsetTop is not 0 so we need
// to find the item's offsetTop and ensure it's at the top.
const selectedItem = document.querySelector('.ui.dropdown .menu.visible .item.selected')
const isMenuScrolledToTop = menu.scrollTop === selectedItem.offsetTop
isMenuScrolledToTop.should.be.true(
'When the first item in the list was selected, DropdownMenu did not scroll to top.'
)
Expand Down
12 changes: 9 additions & 3 deletions test/specs/modules/Search/Search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let wrapper
// ----------------------------------------
// Wrapper
// ----------------------------------------
// we need to unmount the dropdown after every test to ensure all event listeners are cleaned up
// we need to unmount the search after every test to ensure all event listeners are cleaned up
// wrap the render methods to update a global wrapper that is unmounted after each test
const wrapperMount = (node, opts) => {
attachTo = document.createElement('div')
Expand Down Expand Up @@ -188,6 +188,10 @@ describe('Search Component', () => {
searchResultsIsOpen()
const menu = document.querySelector('.ui.search .results.visible')

// Limit the menu's height and set an overflow so it's scrollable
menu.style.height = '100px'
menu.style.overflow = 'auto'

//
// Scrolls to bottom
//
Expand Down Expand Up @@ -223,8 +227,10 @@ describe('Search Component', () => {
.find('.result.active')
.should.contain.text(opts[0].title)

// menu should be completely scrolled to the bottom
const isMenuScrolledToTop = menu.scrollTop === 0
// Note: For some reason the first item's offsetTop is not 0 so we need
// to find the item's offsetTop and ensure it's at the top.
const selectedItem = document.querySelector('.ui.search .results.visible .result.active')
const isMenuScrolledToTop = menu.scrollTop === selectedItem.offsetTop
isMenuScrolledToTop.should.be.true(
'When the first item in the list was selected, SearchResults did not scroll to top.'
)
Expand Down