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: Focus first match on search #370

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,25 @@ class DropdownTreeSelect extends Component {
}

onInputChange = value => {
const { allNodesHidden, tree } = this.treeManager.filterTree(
const { allNodesHidden, tree, matches } = this.treeManager.filterTree(
value,
this.props.keepTreeOnSearch,
this.props.keepChildrenOnSearch
)
const searchModeOn = value.length > 0

const { currentFocus } = this.state
if (!allNodesHidden && !matches.includes(currentFocus)) {
const currentFocusNode = currentFocus && this.treeManager.getNodeById(currentFocus)
const firstMatchNode = this.treeManager.getNodeById(matches[0])
keyboardNavigation.adjustFocusedProps(currentFocusNode, firstMatchNode)
}

this.setState({
tree,
searchModeOn,
allNodesHidden,
currentFocus: !allNodesHidden && !matches.includes(currentFocus) ? matches[0] : currentFocus,
})
}

Expand Down
30 changes: 28 additions & 2 deletions src/index.keyboardNav.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,44 @@ test('can collapse on keyboardNavigation', t => {
t.false(wrapper.find('#c3').exists())
})

test('should set focus to first match on search', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} showDropdown="always" />)
wrapper.instance().onInputChange('bb')
triggerOnKeyboardKeyDown(wrapper, ['b'])
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 1')
})

test('remembers current focus on search clear', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
wrapper.instance().onInputChange('bb')
triggerOnKeyboardKeyDown(wrapper, ['b'])
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 1')
triggerOnKeyboardKeyDown(wrapper, ['Backspace', 'Backspace', 'Backspace'])
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 1')
})

test('should set focus on subsequent searches', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
wrapper.instance().onInputChange('bb')
triggerOnKeyboardKeyDown(wrapper, ['b'])
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 1')
wrapper.instance().onInputChange('aa')
triggerOnKeyboardKeyDown(wrapper, ['a'])
t.deepEqual(wrapper.find('li.focused').text(), 'aaa 1')
})

test('can navigate searchresult on keyboardNavigation', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} showDropdown="initial" />)
wrapper.instance().onInputChange('bb')
triggerOnKeyboardKeyDown(wrapper, ['b', 'ArrowDown', 'ArrowDown', 'ArrowDown'])
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 1')
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 2')
})

test('can navigate with keepTreOnSearch on keyboardNavigation', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} keepTreeOnSearch />)
wrapper.instance().onInputChange('bb')
triggerOnKeyboardKeyDown(wrapper, ['b', 'ArrowDown', 'ArrowDown', 'ArrowDown'])
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 1')
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 2')
t.true(wrapper.find('#c1').exists())
})

Expand Down
2 changes: 1 addition & 1 deletion src/tree-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class TreeManager {
// this is the least intrusive way of fixing #190
this.matchTree = matchTree

return { allNodesHidden, tree: matchTree }
return { allNodesHidden, tree: matchTree, matches }
}

restoreNodes() {
Expand Down