1717 :onkeypress =" `return ${allowSearch};`"
1818 :placeholder =" placeholder"
1919 :disabled =" disabled"
20- :readonly =" !searchable"
20+ :readonly =" !searchable || !active "
2121 :support-link-url =" supportLinkUrl || linkUrl"
2222 :support-link =" supportLink || linkText"
2323 :floating-label =" floatingLabel || mobile"
@@ -325,6 +325,10 @@ const selectOptions = useTemplateRef('select-options');
325325const liRefs = ref ({});
326326const { emitClick , emitFocus , emitBlur , emitKeydown } = nativeEmits (emits);
327327const searchString = ref (' ' );
328+ const isTyping = ref (false );
329+ const internalInputValue = ref (' ' );
330+
331+ let isSelectingItem = false ;
328332
329333/* COMPUTED */
330334const resolveChevronTop = computed (() => {
@@ -362,7 +366,17 @@ const showAddOption = computed(() => {
362366 && ! localOptions .value .some (option => option[props .optionsField ]? .toLowerCase () === searchString? .value .toLowerCase ());
363367});
364368
365- const computedModel = computed (() => localValue .value [props .optionsField ]);
369+ const computedModel = computed ({
370+ get () {
371+ return isTyping .value ? internalInputValue .value : localValue .value ? .[props .optionsField ];
372+ },
373+ set (val ) {
374+ if (isSelectingItem) return ;
375+
376+ isTyping .value = true ;
377+ internalInputValue .value = val;
378+ }
379+ });
366380
367381// NOTE: Essa computada vai ser removida junto com a descontinuação da prop width na V4
368382const computedFluid = computed (() => {
@@ -385,16 +399,27 @@ watch(() => props.options, (newValue, oldValue) => {
385399
386400watch (model, (newValue , oldValue ) => {
387401 if (newValue !== oldValue && newValue !== localValue .value ) {
402+ isSelectingItem = true ;
388403 if (newValue instanceof Object ) {
389404 localValue .value = newValue;
390405 } else {
391406 localValue .value = {id: newValue, value: newValue }
392407 }
393408 }
409+
410+ nextTick (() => {
411+ isSelectingItem = false ;
412+ });
394413}, { immediate: true });
395414
396415watch (localValue, (currentValue ) => {
397- if (currentValue === model .value ) return ;
416+ if (JSON .stringify (currentValue) === JSON .stringify (model .value )) return ;
417+
418+ const isValueEmpty = ! currentValue || (typeof currentValue === ' object' && Object .keys (currentValue).length === 0 );
419+ if (isValueEmpty) {
420+ model .value = props .returnValue ? ' ' : {};
421+ return ;
422+ }
398423
399424 const compatibleOptions = localOptions .value .filter (
400425 (option ) => JSON .stringify (option) === JSON .stringify (currentValue),
@@ -424,6 +449,10 @@ onMounted(() => {
424449
425450/* FUNCTIONS */
426451function filterOptions (value ) {
452+ if (! isTyping .value ) {
453+ return ;
454+ }
455+
427456 if (props .searchable && props .addable ) {
428457 searchString .value = value;
429458 }
@@ -472,25 +501,35 @@ function activeSelection() {
472501function activateSelectionOnEnter () {
473502 if (props .disabled ) return ;
474503
504+ if (localOptions .value .length === 0 && ! (props .searchable && props .addable )) {
505+ active .value = false ;
506+ isTyping .value = false ;
507+ select .value .blur ();
508+ return ;
509+ }
510+
475511 active .value = ! active .value ;
512+ isSelectingItem = true ;
476513
477514 resetActiveSelection ();
478515
479516 if (typeof localOptions .value [currentPos .value ] === ' undefined' ) {
480517 handleAddOption ();
481518
482- nextTick (() => {
483- localValue .value = props .searchable && props .addable
484- ? localValue .value
485- : cloneDeep (localOptions .value [0 ]);
486- });
487-
519+ localValue .value = props .searchable && props .addable
520+ ? localValue .value
521+ : localOptions .value .length ? cloneDeep (localOptions .value [0 ]) : {};
488522 } else {
489523 localValue .value = cloneDeep (localOptions .value [currentPos .value ]);
490524 }
491525
492526 searchString .value = ' ' ;
527+ isTyping .value = false ;
493528 select .value .blur ();
529+
530+ nextTick (() => {
531+ isSelectingItem = false ;
532+ });
494533}
495534
496535function activateSelectionOnClick () {
@@ -511,6 +550,12 @@ function activateSelectionOnClick() {
511550}
512551
513552function hide () {
553+ isSelectingItem = true ;
554+
555+ if (! searchString .value && ! props .addable ) {
556+ model .value = null ;
557+ }
558+
514559 if (! searchString .value ) {
515560 localValue .value = localOptions .value .some (item => item[props .optionsField ]? .toLowerCase () === get (localValue .value , props .optionsField )? .toLowerCase ())
516561 ? localValue .value
@@ -521,14 +566,24 @@ function hide() {
521566 localOptions .value = pristineOptions .value ;
522567 searchString .value = ' ' ;
523568 active .value = false ;
569+ isTyping .value = false ;
570+ isSelectingItem = false ;
524571 });
525572
526573 emitBlur ();
527574}
528575
529576function selectItem () {
577+ isSelectingItem = false ;
530578 searchString .value = ' ' ;
531579 localValue .value = cloneDeep (localOptions .value [currentPos .value ]);
580+ active .value = false ;
581+ isTyping .value = false ;
582+ select .value .blur ();
583+
584+ nextTick (() => {
585+ isSelectingItem = false ;
586+ });
532587}
533588
534589function getLiInDOM (position ) {
0 commit comments