Skip to content

fix(VNumberInput): focus after click handler executed #21217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

J-Sek
Copy link
Contributor

@J-Sek J-Sek commented Apr 5, 2025

Description

VNumberInput has a "lock" on external changes when the field is focused. I helps prevent typing issues (e.g. type 5.0 and just before I type 1 to get the 5.01 any external change will cause the .0 to disappear). The annoying behavior without the lock can be reproduced with VTextField when using v-model:number=....

Currently when user is clicking a button placed in #append-inner, the click handler fires after the focus and I cannot influence this by simply adding .stop. There are 3 options I see:

  • (kinda bad)
    • artificial delay (around 100ms) for the internal lock... measured min. 70ms locally
  • (ok, unless it breaks something)
    • prevent mousedown on inner slots on VField
    • this helps @click.stop work as intended
  • (hm...)
    • drop the "enhancement" and allow external changes when focused... can be tested on the 2nd field on the playground below

fixes #21213

Markup:

<template>
  <v-app>
    <v-container>
      <v-row justify="center">
        <v-col cols="12" md="6">
          <v-number-input
            :precision="2"
            v-model="numericValue"
            label="Enter a number"
            control-variant="stacked"
            class="my-4"
          >
            <template #append-inner>
              <v-btn
                v-if="numericValue == null || numericValue < 100"
                color="primary"
                size="small"
                text="Add +50"
                @click="numericValue += 50"
              />
            </template>
          </v-number-input>

          <v-text-field
            v-model.number="numericValue"
            label="Enter a number"
            control-variant="stacked"
            :min="0"
            :max="100"
            class="my-4"
          >
            <template #append-inner>
              <v-btn
                v-if="numericValue == null || numericValue < 100"
                variant="outlined"
                size="small"
                text="Add +50"
                @click="numericValue += 50"
              />
            </template>
          </v-text-field>

          <pre>Current value: {{ numericValue }}</pre>

          <v-btn @click="changeValueExternally" class="mt-4">
            Set value to 50 externally
          </v-btn>

          <v-btn @click="setNull" class="mt-4 ml-2"> Set value to null </v-btn>
          <div class="mt-3">
            <v-btn
              v-if="!interval"
              @click="startIncrements"
              prepend-icon="mdi-play"
              >Start increments</v-btn
            >
            <v-btn v-else @click="stopIncrements" prepend-icon="mdi-stop"
              >Stop</v-btn
            >
          </div>
        </v-col>
      </v-row>
    </v-container>
  </v-app>
</template>

<script setup>
  import { ref } from 'vue'

  const numericValue = ref(10)

  const changeValueExternally = () => (numericValue.value = 50)
  const setNull = () => (numericValue.value = null)

  let interval = ref(0)
  const startIncrements = () => {
    clearInterval(interval.value)
    interval.value = setInterval(() => (numericValue.value += 5), 1000)
  }
  const stopIncrements = () => {
    clearInterval(interval.value)
    interval.value = 0
  }
</script>
<style>
  .v-input--horizontal .v-input__append {
    margin-inline-start: 0;
  }
</style>

@J-Sek J-Sek added T: bug Functionality that does not work as intended/expected C: VField C: VNumberInput labels Apr 5, 2025
@J-Sek J-Sek self-assigned this Apr 5, 2025
@J-Sek J-Sek requested review from yuwu9145 and KaelWD April 5, 2025 10:50
@jfrag
Copy link

jfrag commented May 2, 2025

@KaelWD or @yuwu9145 can you review this PR ?
I need this bugfix on my project

<div
key="prepend"
class="v-field__prepend-inner"
onMousedown={ (e: MouseEvent) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any consideration for PointerEvent?

@J-Sek J-Sek requested a review from johnleider May 23, 2025 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: VField C: VNumberInput T: bug Functionality that does not work as intended/expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug Report][3.8.0] VNumberInput append-inner max button don' update value
3 participants