Skip to content

Commit

Permalink
Fix prev() and next() selectors when children() is overwritten
Browse files Browse the repository at this point in the history
Overwriting children() is recommended in the documentation to isolate a
UI component, but this breaks any layouting done within the component
using prev()/next(). This fixes that.
  • Loading branch information
tbuschto committed Mar 20, 2018
1 parent e14f0c4 commit 82eee3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/tabris/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ function resolveArray(array, widget) {

function toProxyId(ref, widget) {
if (ref === 'prev()') {
let children = getParent(widget).children();
let children = getParent(widget)._children();
let index = children.indexOf(widget);
if (index > 0) {
return types.proxy.encode(children[index - 1]) || 0;
}
return 0;
}
if (ref === 'next()') {
let children = getParent(widget).children();
let children = getParent(widget)._children();
let index = children.indexOf(widget);
if (index + 1 < children.length) {
return types.proxy.encode(children[index + 1]) || 0;
Expand Down
17 changes: 17 additions & 0 deletions test/tabris/Layout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ClientStub from './ClientStub';
import {expect, mockTabris, stub, spy, restore} from '../test';
import Layout from '../../src/tabris/Layout';
import Widget from '../../src/tabris/Widget';
import WidgetCollection from '../../src/tabris/WidgetCollection';

describe('Layout', function() {

Expand Down Expand Up @@ -124,6 +125,14 @@ describe('Layout', function() {
expect(resolve(input, other)).to.eql(expected);
});

it("translates 'prev()' when parent children() is overwritten", function() {
parent.children = () => new WidgetCollection([]);
let input = {baseline: 'prev()', left: ['prev()', 42]};
let expected = {baseline: widget.cid, left: [widget.cid, 42]};

expect(resolve(input, other)).to.eql(expected);
});

it("translates 'prev()' selector to 0 on first widget", function() {
let input = {baseline: 'prev()', left: ['prev()', 42]};
let expected = {baseline: 0, left: [0, 42]};
Expand All @@ -138,6 +147,14 @@ describe('Layout', function() {
expect(resolve(input, widget)).to.eql(expected);
});

it("translates 'next()' selector to id when parent children() is overwritten", function() {
parent.children = () => new WidgetCollection([]);
let input = {baseline: 'next()', left: ['next()', 42]};
let expected = {baseline: other.cid, left: [other.cid, 42]};

expect(resolve(input, widget)).to.eql(expected);
});

it("translates 'next()' selector to 0 on last widget", function() {
let input = {baseline: 'next()', left: ['next()', 42]};
let expected = {baseline: 0, left: [0, 42]};
Expand Down

0 comments on commit 82eee3f

Please sign in to comment.