@@ -219,19 +219,25 @@ public MaterialAutoComplete(SuggestOracle suggestions) {
219219 setup (suggestions );
220220 }
221221
222+ private HandlerRegistration listHandler , itemBoxKeyDownHandler , itemBoxBlurHandler , itemBoxClickHandler ;
223+
222224 @ Override
223225 protected void onLoad () {
224226 super .onLoad ();
225227
226- registerHandler (list .addDomHandler (event -> suggestBox .showSuggestionList (), ClickEvent .getType ()));
228+ loadHandlers ();
229+ }
230+
231+ protected void loadHandlers () {
232+ listHandler = list .addDomHandler (event -> suggestBox .showSuggestionList (), ClickEvent .getType ());
227233
228- registerHandler ( itemBox .addBlurHandler (blurEvent -> {
234+ itemBoxBlurHandler = itemBox .addBlurHandler (blurEvent -> {
229235 if (getValue ().size () > 0 ) {
230236 label .addStyleName (CssName .ACTIVE );
231237 }
232- })) ;
238+ });
233239
234- registerHandler ( itemBox .addKeyDownHandler (event -> {
240+ itemBoxKeyDownHandler = itemBox .addKeyDownHandler (event -> {
235241 boolean changed = false ;
236242
237243 switch (event .getNativeKeyCode ()) {
@@ -278,34 +284,52 @@ protected void onLoad() {
278284 itemBox .setFocus (true );
279285 break ;
280286 }
287+ });
281288
282- if (changed ) {
283- ValueChangeEvent .fire (MaterialAutoComplete .this , getValue ());
284- }
285- }));
289+ itemBoxClickHandler = itemBox .addClickHandler (event -> suggestBox .showSuggestionList ());
290+ }
286291
287- registerHandler (itemBox .addClickHandler (event -> suggestBox .showSuggestionList ()));
292+ @ Override
293+ protected void onUnload () {
294+ super .onUnload ();
288295
289- registerHandler ( suggestBox . addSelectionHandler ( selectionEvent -> {
290- Suggestion selectedItem = selectionEvent . getSelectedItem ();
291- itemBox . setValue ( "" );
292- if ( addItem ( selectedItem ) ) {
293- ValueChangeEvent . fire ( MaterialAutoComplete . this , getValue () );
294- }
295- itemBox . setFocus ( true );
296- }) );
296+ unloadHandlers ();
297+ }
298+
299+ protected void unloadHandlers ( ) {
300+ removeHandler ( listHandler );
301+ removeHandler ( itemBoxBlurHandler );
302+ removeHandler ( itemBoxKeyDownHandler );
303+ removeHandler ( itemBoxClickHandler );
297304 }
298305
299306 /**
300307 * Generate and build the List Items to be set on Auto Complete box.
301308 */
302309 protected void setup (SuggestOracle suggestions ) {
310+
311+ if (itemBoxKeyDownHandler != null ) {
312+ itemBoxKeyDownHandler .removeHandler ();
313+ }
314+
303315 list .setStyleName (AddinsCssName .MULTIVALUESUGGESTBOX_LIST );
304316 this .suggestions = suggestions ;
305317 final ListItem item = new ListItem ();
306318
307319 item .setStyleName (AddinsCssName .MULTIVALUESUGGESTBOX_INPUT_TOKEN );
320+
308321 suggestBox = new SuggestBox (suggestions , itemBox );
322+ suggestBox .addSelectionHandler (selectionEvent -> {
323+ Suggestion selectedItem = selectionEvent .getSelectedItem ();
324+ itemBox .setValue ("" );
325+ if (addItem (selectedItem )) {
326+ ValueChangeEvent .fire (MaterialAutoComplete .this , getValue ());
327+ }
328+ itemBox .setFocus (true );
329+ });
330+
331+ loadHandlers ();
332+
309333 setLimit (this .limit );
310334 String autocompleteId = DOM .createUniqueId ();
311335 itemBox .getElement ().setId (autocompleteId );
@@ -316,7 +340,7 @@ protected void setup(SuggestOracle suggestions) {
316340
317341 panel .add (list );
318342 panel .getElement ().setAttribute ("onclick" ,
319- "document.getElementById('" + autocompleteId + "').focus()" );
343+ "document.getElementById('" + autocompleteId + "').focus()" );
320344 panel .add (errorLabel );
321345 suggestBox .setFocus (true );
322346 }
@@ -526,6 +550,7 @@ public void setLimit(int limit) {
526550 * Set the number of suggestions to be displayed to the user. This differs from
527551 * setLimit() which set both the suggestions displayed AND the limit of values
528552 * allowed within the autocomplete.
553+ *
529554 * @param limit
530555 */
531556 public void setAutoSuggestLimit (int limit ) {
@@ -545,9 +570,8 @@ public void setPlaceholder(String placeholder) {
545570 }
546571
547572 /**
548- * @see gwt.material.design.client.ui.MaterialValueBox#setLabel(String)
549- *
550573 * @param label
574+ * @see gwt.material.design.client.ui.MaterialValueBox#setLabel(String)
551575 */
552576 public void setLabel (String label ) {
553577 this .label .setText (label );
0 commit comments