Search code examples
vue.jsvuetify.jsv-select

Vuetify v-select prevent input


How I can prevent an input event on Vuetify's v-select element to ask confirmation first.

User Story:

  1. I select an option.
  2. Confirm pops up asking "Do you want to change the value?"
  3. If I confirm, the value changes.
  4. If I deny, the value does not change.

Solution

  • Then you should use separate variable for data and input. And use change event to toggle dialog.

    Example Code:

    <v-app>
      <v-container>
        <v-select label="Standard" v-model="input" :items="items" @change="change"/>
        <v-dialog ref="dialog" v-model="dialog">
          <v-card>
            <v-card-title>Do you want to change the value?</v-card-title>
            <v-card-actions>
              <v-btn text @click="discardChange">Cancel</v-btn>
              <v-btn color="primary" text @click="acceptChange">Accept</v-btn>
            </v-card-actions>
          </v-card>
        </v-dialog>
      </v-container>
      <div>Value: {{ value }}</div>
    </v-app>
    
    export default {
      data: () => ({
        dialog: false,
        value: "Fizz",
        input: "Fizz",
        items: ["Foo", "Bar", "Fizz", "Buzz"]
      }),
      methods: {
        change() {
          this.dialog = true;
        },
    
        discardChange() {
          this.input = this.value;
          this.dialog = false;
        },
    
        acceptChange() {
          this.value = this.input;
          this.dialog = false;
        }
      }
    };
    

    CodeSandbox