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

insertTree: Maintain active children if requested #98

Open
wants to merge 3 commits into
base: master
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
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ export class Lrud {
* @param {boolean} [options.maintainIndex] - if true, and new tree is replacing an existing branch of the tree,
* maintain the original branches relative index; default: true
*/
insertTree (subTreeRootNodeConfig: NodeConfig, options: InsertTreeOptions = { maintainIndex: true }): void {
insertTree (subTreeRootNodeConfig: NodeConfig, options: InsertTreeOptions = { maintainIndex: true, maintainActiveChildren: false }): void {
if (!subTreeRootNodeConfig) {
return
}
Expand All @@ -1051,6 +1051,17 @@ export class Lrud {
}

this.registerTree(subTreeRootNodeConfig)

if (options.maintainActiveChildren) {
traverseNodeSubtree(subTreeRootNodeConfig, traversedNodeConfig => {
if (traversedNodeConfig.activeChild) {
this.getNode(traversedNodeConfig.id).activeChild = this.getNode(
typeof traversedNodeConfig.activeChild === 'string'
? traversedNodeConfig.activeChild
: traversedNodeConfig.activeChild.id)
}
})
}
}

/**
Expand Down
63 changes: 63 additions & 0 deletions src/insert-tree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,67 @@ describe('insertTree()', () => {

expect(() => navigation.insertTree(undefined)).not.toThrow()
})

test('should not maintain active children if maintainActiveChildren not enabled', () => {
const navigation = new Lrud()
.registerNode('root')
.registerNode('a')
.registerNode('b')

const subTree = {
id: 'c',
orientation: 'vertical',
activeChild: 'c_1',
children: [
{ id: 'c_1', isFocusable: true },
{ id: 'c_2', isFocusable: true }
]
}

navigation.insertTree(subTree)

expect(navigation.getNode('c').activeChild).not.toBeDefined()
})

test('should maintain active children if maintainActiveChildren enabled', () => {
const navigation = new Lrud()
.registerNode('root')
.registerNode('a')
.registerNode('b')

const subTree = {
id: 'c',
orientation: 'vertical',
activeChild: 'c_1',
children: [
{ id: 'c_1', isFocusable: true },
{ id: 'c_2', isFocusable: true }
]
}

navigation.insertTree(subTree, { maintainActiveChildren: true })

expect(navigation.getNode('c').activeChild.id).toEqual('c_1')
})

test('should maintain active children if Node supplied', () => {
const navigation = new Lrud()
.registerNode('root')
.registerNode('a')
.registerNode('b')

const subTree = {
id: 'c',
orientation: 'vertical',
activeChild: { id: 'c_1' },
children: [
{ id: 'c_1', isFocusable: true },
{ id: 'c_2', isFocusable: true }
]
}

navigation.insertTree(subTree, { maintainActiveChildren: true })

expect(navigation.getNode('c').activeChild.id).toEqual('c_1')
})
})
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface Node extends Tree<Node> {
export interface NodeConfig extends Tree<NodeConfig>, Omit<Node, 'id'|'parent'|'activeChild'|'children'|'overrides'|'overrideSources'> {
id?: NodeId
parent?: NodeId
activeChild?: NodeId | Node
}

export type NodesBag = { [id in NodeId]: Node }
Expand All @@ -75,6 +76,7 @@ export interface HandleKeyEventOptions {

export interface InsertTreeOptions {
maintainIndex?: boolean
maintainActiveChildren?: boolean
}

export interface UnregisterNodeOptions {
Expand Down