Skip to content

Commit 1885de7

Browse files
committed
fix #164
1 parent 06de164 commit 1885de7

4 files changed

Lines changed: 39 additions & 17 deletions

File tree

drop-down-common.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
***************************************************************************** */
1616
import { ObservableArray } from "data/observable-array";
1717
import { CoercibleProperty, Property, View } from "ui/core/view";
18+
import { addWeakEventListener, removeWeakEventListener } from "ui/core/weak-event-listener";
1819
import { ItemsSource } from "ui/list-picker";
1920
import * as types from "utils/types";
2021
import { DropDown as DropDownDefinition, ValueItem, ValueList as ValueListDefinition } from ".";
@@ -35,6 +36,7 @@ export abstract class DropDownBase extends View implements DropDownDefinition {
3536

3637
public abstract open();
3738
public abstract close();
39+
public abstract refresh();
3840

3941
public _getItemAsString(index: number) {
4042
const items = this.items;
@@ -130,6 +132,14 @@ export const itemsProperty = new Property<DropDownBase, any[] | ItemsSource>({
130132

131133
target.isItemsSourceIn = typeof getItem === "function";
132134
target.isValueListIn = typeof getDisplay === "function";
135+
136+
if (oldValue instanceof ObservableArray) {
137+
removeWeakEventListener(oldValue, ObservableArray.changeEvent, target.refresh, target);
138+
}
139+
140+
if (newValue instanceof ObservableArray) {
141+
addWeakEventListener(newValue, ObservableArray.changeEvent, target.refresh, target);
142+
}
133143
}
134144
});
135145
itemsProperty.register(DropDownBase);

drop-down.android.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,18 @@ export class DropDown extends DropDownBase {
117117
this.nativeView.onDetachedFromWindowX();
118118
}
119119

120+
public refresh() {
121+
this._updateSelectedIndexOnItemsPropertyChanged(this.items);
122+
(this.android.getAdapter() as DropDownAdapter).notifyDataSetChanged();
123+
124+
// Coerce selected index after we have set items to native view.
125+
selectedIndexProperty.coerce(this);
126+
}
127+
120128
public [selectedIndexProperty.getDefault](): number {
121129
return null;
122130
}
123131
public [selectedIndexProperty.setNative](value: number) {
124-
this._clearCache(RealizedViewType.DropDownView);
125-
126132
const actualIndex = (types.isNullOrUndefined(value) ? 0 : value + 1);
127133
this.nativeView.setSelection(actualIndex);
128134
}
@@ -195,6 +201,16 @@ export class DropDown extends DropDownBase {
195201
return this._realizedItems[realizedViewType].get(convertView);
196202
}
197203

204+
public _clearCache(realizedViewType: RealizedViewType) {
205+
const realizedItems = this._realizedItems[realizedViewType];
206+
realizedItems.forEach((view) => {
207+
if (view.parent) {
208+
view.parent._removeView(view);
209+
}
210+
});
211+
realizedItems.clear();
212+
}
213+
198214
private _propagateStylePropertyToRealizedViews(property: string, value: any, isIncludeHintIn = true) {
199215
const realizedItems = this._realizedItems;
200216
for (const item of realizedItems) {
@@ -224,16 +240,6 @@ export class DropDown extends DropDownBase {
224240
this.selectedIndex = null;
225241
}
226242
}
227-
228-
private _clearCache(realizedViewType: RealizedViewType) {
229-
const realizedItems = this._realizedItems[realizedViewType];
230-
realizedItems.forEach((view) => {
231-
if (view.parent) {
232-
view.parent._removeView(view);
233-
}
234-
});
235-
realizedItems.clear();
236-
}
237243
}
238244

239245
/* A snapshot-friendly, lazy-loaded class for TNSSpinner BEGIN */
@@ -285,6 +291,8 @@ function initializeTNSSpinner() {
285291
eventName: DropDownBase.closedEvent,
286292
object: owner
287293
});
294+
295+
owner._clearCache(RealizedViewType.DropDownView);
288296
}
289297
}
290298

drop-down.ios.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ export class DropDown extends DropDownBase {
136136
this.ios.resignFirstResponder();
137137
}
138138

139+
public refresh() {
140+
this._listPicker.reloadAllComponents();
141+
142+
// Coerce selected index after we have set items to native view.
143+
selectedIndexProperty.coerce(this);
144+
}
145+
139146
public [selectedIndexProperty.getDefault](): number {
140147
return null;
141148
}
@@ -151,10 +158,7 @@ export class DropDown extends DropDownBase {
151158
return null;
152159
}
153160
public [itemsProperty.setNative](value: any[] | ItemsSource) {
154-
this._listPicker.reloadAllComponents();
155-
156-
// Coerce selected index after we have set items to native view.
157-
selectedIndexProperty.coerce(this);
161+
this.refresh();
158162
}
159163

160164
public [hintProperty.getDefault](): string {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-drop-down",
3-
"version": "3.2.1",
3+
"version": "3.2.2",
44
"description": "A NativeScript DropDown widget.",
55
"main": "drop-down",
66
"typings": "drop-down.d.ts",

0 commit comments

Comments
 (0)