1

I've got a bit of an add situation with a custom "Edit In Place" Vuetify text input. I noticed that on keyup of the enter key, there were two requests being made to my backend to update the field. I know what's causing the issue, just not sure how to fix it!

I have this text field:

<v-text-field
    :id="uniqueIdentifier"
    :ref="uniqueIdentifier"
    v-focus
    v-mask="mask"
    :class="[ 'click-to-edit', active ? 'active' : 'b-none', customClass ]"
    color="primary"
    dense
    :full-width="false"
    hide-details
    outlined
    :style="{...customStyle}"
    :type="type"
    :value="localValue"
    @blur="handleUpdateItem($event)"
    @click="activateInput"
    @keyup.enter.native="handleUpdateItem($event)" />

As you can see, I have both @blur and @keyup.enter.native events being fired to update the text field. The method to update the field looks like this:

handleUpdateItem (e) {
  this.localValue = e.target.value;
  this.active = false;
  this.$emit('handle-update-item', this.localValue);

  // if (e.type === 'keyup') { this.$refs[this.uniqueIdentifier].blur(); }
  // if (e.type === 'keyup') { this.active = false; }
}

The two last lines of that method that are commented out are my attempts, and are intended to "deactivate" the input on click of enter, however because the first line is then running .blur(), the text field runs this function a second time. The second line still has the blinking cursor in the input, so it's not really "deactivated" either.

Ultimately what I'm looking for is a way to allow the user to either press enter or click outside of the input to fire off the request (obviously without it firing twice) and deactivate the input. Does anyone have a suggestion?

1 Answer 1

5

If I understand correctly, you want to emit the local value when either the input is tabbed/clicked out of OR if the enter key is pressed but not double up on emits.

If the enter key is pressed, we need to programmically unfocus which will trigger the blur listener. If the input is unfocused (with a outside click or tab key or with the aforementioned unfocus) then we need to emit the value.

<v-text-field
  ...
  @keyup.native.enter="$refs[uniqueIdentifier].blur()"
  @blur="handleUpdateItem($event)"
/>
handleUpdateItem (e) {
  this.localValue = e.target.value;
  this.active = false;
  this.$emit('handle-update-item', this.localValue);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.