Skip to content

Commit

Permalink
fix: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ellinge committed Mar 28, 2019
1 parent bd1b120 commit 366d639
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
16 changes: 10 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,21 @@ class DropdownTreeSelect extends Component {
}

onKeyboardKeyDown = e => {
if (!this.props.enableKeyboardNavigation) { return }

const { enableKeyboardNavigation, keepTreeOnSearch } = this.props
if (!enableKeyboardNavigation) { return }

const { showDropdown, tags } = this.state
const { showDropdown, tags, searchModeOn } = this.state
const tm = this.treeManager

if (!showDropdown && keyboardNavigation.isValidKey(e.key, false)) {
if (!showDropdown && (keyboardNavigation.isValidKey(e.key, false) || /\w/i.test(e.key))) {
// Triggers open of dropdown and retriggers event
this.handleClick(() => this.onKeyboardKeyDown(e.persist()))
e.persist()
this.handleClick(null, () => this.onKeyboardKeyDown(e))
if (/\w/i.test(e.key)) return
} else if (showDropdown && keyboardNavigation.isValidKey(e.key, true)) {
const tree = this.state.searchModeOn ? tm.matchTree : tm.tree
if (tm.handleNavigationKey(tree, e.key)) {
const tree = searchModeOn ? tm.matchTree : tm.tree
if (tm.handleNavigationKey(tree, e.key, searchModeOn && !keepTreeOnSearch)) {
this.setState({ tags: tm.getTags() })
}
} else if (showDropdown && ['Escape', 'Tab'].indexOf(e.key) > -1) {
Expand Down
5 changes: 3 additions & 2 deletions src/tree-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,13 @@ class TreeManager {
})
}

handleNavigationKey(tree, key) {
handleNavigationKey(tree, key, flattenedTree) {
const prevFocus = this.currentFocus && this.getNodeById(this.currentFocus)
const action = keyboardNavigation.getAction(prevFocus, key)

if (FocusActionNames.has(action)) {
const newFocus = keyboardNavigation.getNextFocus(tree, prevFocus, action, id => this.getNodeById(id))
const newFocus = keyboardNavigation.getNextFocus(
tree, prevFocus, action, id => this.getNodeById(id), flattenedTree)
if (newFocus) {
newFocus._focused = true
this.currentFocus = newFocus._id
Expand Down
28 changes: 19 additions & 9 deletions src/utils/keyboardNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const isFocusFirstEvent = (key, currentFocus) =>
const isFocusLastEvent = (key, currentFocus) =>
[Keys.End, Keys.PageDown].indexOf(key) > -1 || (!currentFocus && key === Keys.Up)

const isReverseTraverseAction = action =>
[NavActions.FocusPrevious, NavActions.FocusLast].indexOf(action) > -1

const isEdgeTraverseAction = action =>
[NavActions.FocusFirst, NavActions.FocusLast].indexOf(action) > -1

const getLeftNavAction = (currentFocus, key) => {
if (!currentFocus || key !== Keys.Left) return NavActions.None

Expand Down Expand Up @@ -99,30 +105,34 @@ const getParentFocus = (prevFocus, getNodeById) =>


const getRelativeFocus = (sortedNodes, prevFocus, action) => {
if ([NavActions.FocusFirst, NavActions.FocusLast].indexOf(action) > -1) {
if (sortedNodes.length === 0) return prevFocus

if (isEdgeTraverseAction(action)) {
return sortedNodes[0]
}
if ([NavActions.FocusPrevious, NavActions.FocusNext].indexOf(action) > -1) {
const currentIndex = sortedNodes.indexOf(prevFocus)
if (currentIndex < 0 || (currentIndex + 1 === sortedNodes.length)) {
const nextIndex = sortedNodes.indexOf(prevFocus) + 1
if (nextIndex % sortedNodes.length === 0) {
return sortedNodes[0]
}
return sortedNodes[currentIndex + 1]
return sortedNodes[nextIndex]
}

return prevFocus
}

const getNextFocus = (tree, prevFocus, action, getNodeById) => {
const getNextFocus = (tree, prevFocus, action, getNodeById, flattenedTree) => {
if (action === NavActions.FocusParent) {
return getParentFocus(prevFocus, getNodeById)
} else if (!FocusActionNames.has(action)) {
}
if (!FocusActionNames.has(action)) {
return prevFocus
}

const isReverseOrder = [NavActions.FocusPrevious, NavActions.FocusLast].indexOf(action) > -1
const nodes = nodeVisitor.getVisibleNodes(tree, getNodeById, isReverseOrder)
if (nodes.length === 0) return prevFocus
let nodes = nodeVisitor.getVisibleNodes(tree, getNodeById, flattenedTree)
if (isReverseTraverseAction(action)) {
nodes = nodes.reverse()
}

return getRelativeFocus(nodes, prevFocus, action)
}
Expand Down
8 changes: 3 additions & 5 deletions src/utils/nodeVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ const getNodesMatching = (tree, nodePredicate) => {
return nodes
}

const getVisibleNodes = (tree, getItemById, reverse = false) => {
const nodes = getNodesMatching(tree, (node, key, visited) => {
if (node._children && node._children.length && node.expanded !== true) {
const getVisibleNodes = (tree, getItemById, flattenedTree) =>
getNodesMatching(tree, (node, key, visited) => {
if (!flattenedTree && node._children && node._children.length && node.expanded !== true) {
markSubTreeVisited(node, visited, getItemById)
}
return !node.hide && !node.disabled && !node.readOnly
})
return reverse ? nodes.reverse() : nodes
}

const nodeVisitor = {
getNodesMatching,
Expand Down

0 comments on commit 366d639

Please sign in to comment.