@@ -296,20 +296,32 @@ export default {
296296 all_option: {' text' : ' Select All' , ' value' : ' all' },
297297 };
298298 },
299- watch: {
299+ watch: {
300300 value () {
301301 if (Array .isArray (this .value )) {
302302 this .selectedItems = [... this .value ];
303303 }
304304 if (! this .multiple && this .searchable ) {
305305 const selected = this .items .find ((i ) => i .value == this .value );
306- this .searchInput = selected ? selected .text : this .value ;
306+ this .selected = selected || null ;
307+ if (this .add_option ){
308+ this .searchInput = " " ;
309+ }else {
310+ this .searchInput = selected ? selected .text : this .value ;
311+ }
312+ } else if (! this .multiple ) {
313+ const selected = this .items .find ((i ) => i .value == this .value );
314+ this .selected = selected || null ;
307315 }
308- this .setAllOptions ()
316+ this .setAllOptions ();
309317 },
310318 items: {
311319 handler : function () {
312- this .setAllOptions ()
320+ if (! this .multiple ) {
321+ const selected = this .items .find ((i ) => i .value == this .value );
322+ this .selected = selected || null ;
323+ }
324+ this .setAllOptions ();
313325 }
314326 }
315327 },
@@ -461,12 +473,30 @@ export default {
461473 }
462474 },
463475 addOption () {
464- let value = this .searchInput ;
465- this .searchInput = ' ' ;
466- this .$emit (" addOption" , value);
467- this .eventEmit ({}, " searchInputChange" );
468- this .calculateViewport ();
469- },
476+ let value = this .searchInput && this .searchInput .trim ();
477+ if (! value) return ;
478+ // Create new item
479+ const newItem = {
480+ text: value,
481+ value: value
482+ };
483+
484+ // Add to items
485+ this .$emit (" addOption" , newItem);
486+ // Select the new item
487+ if (this .multiple ) {
488+ this .selectedItems .push (newItem .value );
489+ this .$emit (" input" , this .selectedItems );
490+ this .$emit (" change" , this .selectedItems );
491+ } else {
492+ this .selected = newItem;
493+ this .$emit (" input" , newItem .value );
494+ this .$emit (" change" , newItem .value );
495+ this .searchInput = this .add_option ? ' ' : newItem .text ;
496+ }
497+ this .eventEmit ({}, " searchInputChange" );
498+ this .calculateViewport ();
499+ },
470500 setCheckedItem () {
471501 this .$emit (" input" , this .selectedItems ); // v-model implementation
472502 this .$emit (" change" , this .selectedItems );
@@ -479,7 +509,17 @@ export default {
479509 text: this .searchInput ,
480510 };
481511 if (! this .searchInput ) {
482- this .setAllOptions ()
512+ this .setAllOptions ();
513+ // Remove selected option data when search input is cleared
514+ if (this .multiple ) {
515+ this .selectedItems = [];
516+ this .$emit (" input" , []);
517+ this .$emit (" change" , []);
518+ } else {
519+ this .selected = null ;
520+ this .$emit (" input" , " " );
521+ this .$emit (" change" , " " );
522+ }
483523 }
484524 this .eventEmit (obj, " searchInputChange" );
485525 this .calculateViewport ();
0 commit comments