diff --git a/lib/widget.js b/lib/widget.js index 73bb851..f7673e1 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -3936,11 +3936,7 @@ function List(options) { options.items.forEach(this.add.bind(this)); } - if (this.children.length) { - // Will throw if this.parent is not set! - // Probably not good to have in a constructor. - // this.select(0); - } + this.select(0); if (options.mouse) { this.screen._listenMouse(this); @@ -4109,6 +4105,10 @@ List.prototype.appendItem = function(item) { this.items.push(item); this.append(item); + if (this.items.length === 1) { + this.select(0); + } + if (this.mouse) { item.on('click', function(data) { if (self.items[self.selected] === item) { @@ -4131,6 +4131,9 @@ List.prototype.removeItem = function(child) { child = this.items.splice(i, 1)[0]; this.ritems.splice(i, 1); this.remove(child); + if (i === this.selected) { + this.select(i - 1); + } } }; @@ -4163,7 +4166,12 @@ List.prototype.setItems = function(items) { }; List.prototype.select = function(index) { - if (!this.items.length) return; + if (!this.items.length) { + this.selected = 0; + this.value = ''; + this.scrollTo(0); + return; + } if (typeof index === 'object') { index = this.items.indexOf(index);