Skip to content

Clearing the input when an item has been selected #99

@robjbrain

Description

@robjbrain

I'm submitting a ...

  • bug report
  • feature request
  • support request

What is the current behavior?

When an item is selected the old input stays there.

What is the expected behavior?

There should be a simple way to clear the old input after an item has been selected

How are you importing Vue-simple-suggest?

  • ESNext (original code, single-file .vue component, css included) (import VueSimpleSuggest from 'vue-simple-suggest/lib')
  • ES6 (import VueSimpleSuggest from 'vue-simple-suggest')
  • ES7 and above (import VueSimpleSuggest from 'vue-simple-suggest/dist/es7')
  • Bundled version (import VueSimpleSuggest from 'vue-simple-suggest')
  • CommonJS (const VueSimpleSuggest = require('vue-simple-suggest'))
  • UMD Component (<script type="text/javascript" src="https://unpkg.com/vue-simple-suggest"></script>)

What is the motivation / use case for changing the behavior?

I'm not sure if this is a support request or a feature request, it depends on whether or not it's actually possible. It seems like a simple enough request, but a little dive into the code suggests it might not be possible.

When an item is selected i'd like to clear the text from the input field.

The following doesn't work:

<vue-simple-suggest ... ref="input" @select="onSelect" ...></vue-simple-suggest>

onSelect (suggestion) {
    this.$refs.input.text = ''
    this.someActionWithMySelection(suggestion)
    // presumably this fails because the full text from the selection is set as the input field after i've cleared it?
},
<vue-simple-suggest ... v-model="suggested_item" @select="onSelect" ...></vue-simple-suggest>

onSelect (suggestion) {
    this.suggested_item = ''
    this.someActionWithMySelection(suggestion)
},

From what I can tell it's literally impossible because of this little snippet of code:

    autocompleteText (text) {
      this.$emit('input', text)
      this.inputElement.value = text
      this.text = text
    },
    select (item) {
      this.hovered = null
      this.selected = item

      this.$emit('select', item)

      this.autocompleteText(this.displayProperty(item))
    },

Since my onSelect method will always be called when this.$emit('select', item) happens anything I do will always be overwritten by the next line where it auto completes this.autocompleteText(this.displayProperty(item))

  1. Is there a way around this to clear the input after a selection?

One thing I have considered is clearing on focus and blurring after selection but this.$refs.input.blur() throws an error and this.$refs.input.$el.blur() doesn't seem to actually blur.

  1. If there is literally no way to do this at present, can it please be added as a feature request for V2? Not necessarily as default behaviour, but as a possibility.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginsignificantThe bug is insignificant and does not have any major consequensesminorThis is a thing to include in the next minor releaseresolvedThis issue is resolved in the latest version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions