vue-currency-input icon indicating copy to clipboard operation
vue-currency-input copied to clipboard

No emits on vue 2.7.10 & nuxt

Open BorisKamp opened this issue 2 years ago • 5 comments

Vue Currency Input version

3.0.5

Vue version

2.7.10

What browser are you using?

Chrome

What operating system are you using?

OSX

Reproduction link

https://example.com

Describe your issue

Hi! I've setup according to the guide, I created a CurrencyInput.vue component with the following content:

<template>
  <input ref="inputRef" type="text" class="input__input" />
</template>

<script>
import { useCurrencyInput } from 'vue-currency-input'

export default {
  name: 'CurrencyInput',

  props: {
    value: {
      type: Number,
      default: 0,
    },
    options: {
      type: Object,
      default: () => {
        return {
          currency: 'EUR',
        }
      },
    },
  },

  setup(props) {
    const { inputRef } = useCurrencyInput(props.options)

    return { inputRef }
  },
}
</script>

I then use it in my other component: <currency-input ref="inp" id="input-price" :v-model="price" placeholder=" " @change="priceChanged" />

But @change is never called, so no emits. What am I doing wrong here? It clearly states the following in the guide:

By default, the number value is automatically emitted on each input

BorisKamp avatar Jul 18 '23 16:07 BorisKamp

See https://dm4t2.github.io/vue-currency-input/guide.html#lazy-value-binding for using a lazy value binding.

dm4t2 avatar Jul 18 '23 21:07 dm4t2

See https://dm4t2.github.io/vue-currency-input/guide.html#lazy-value-binding for using a lazy value binding.

Thank you for replying, but that does not work either. When I use the following code: <currency-input ref="inp" id="input-price" :value="price" placeholder=" " @change="value = $event" /> nothing is passed along/emitted. price is initially 10, but it is not passed to the input. When the input is rendered, it's empty. And nothing is emitted. Im using Vue2 and Nuxt, see specs above.

BorisKamp avatar Jul 19 '23 05:07 BorisKamp

Can you share a minimal reproduction? Thanks!

dm4t2 avatar Jul 19 '23 09:07 dm4t2

Just encountered the same. Using these:

nuxt: 2.17.3
vue: 2.7.16
vue-currency-input: 3.1.0

Here is minimal reproduction: https://codesandbox.io/p/devbox/nuxt-2-17-vue-currency-input-not-emitting-rrwg2h?file=%2Fcomponents%2FCurrencyInput.vue%3A2%2C34

Any workarounds? @dm4t2 @BorisKamp Thanks in advance!

canthis avatar Mar 06 '24 13:03 canthis

The useCurrencyInput composable seems to be broken with Vue 2.7 & Nuxt. Besides that auto emit doesn't work, the numberValue ref is also not reactive and thus can be not used with a watcher to handle emits manually.

As a workaround you can use intl-number-input:

<template>
  <input
    ref="inputRef"
    type="text"
  >
</template>

<script>
import { defineComponent, onMounted, ref } from 'vue'
import { NumberInput } from 'intl-number-input'

export default defineComponent({
  name: 'NumberInput',
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  emits: ['input'],
  setup (props, { emit }) {
    let numberInput
    const inputRef = ref(null)
    onMounted(() => {
      if (inputRef.value) {
        numberInput = new NumberInput({
          el: inputRef.value,
          options: {
            formatStyle: 'currency',
            currency: 'USD'
          },
          onInput: (value) => {
            emit('input', value.number)
          }
        })
        numberInput.setValue(props.value)
      }
    })

    return { inputRef }
  }
})
</script>

dm4t2 avatar Mar 18 '24 21:03 dm4t2