DefaultCurrencyInput.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <q-input
  3. ref="inputRef"
  4. v-model="formattedValue"
  5. outlined
  6. :error-message="errorMessage"
  7. :error="!!errorMessage"
  8. :disable="disable"
  9. :class="disable ? 'no-pointer-events' : ''"
  10. :label="newLabel"
  11. :readonly="readonly"
  12. >
  13. </q-input>
  14. </template>
  15. <script setup>
  16. import { useCurrencyInput } from "vue-currency-input";
  17. import { computed, watch } from "vue";
  18. import { useI18n } from "vue-i18n";
  19. const model = defineModel();
  20. const { options, disable, readonly, label } = defineProps({
  21. options: {
  22. type: Object,
  23. default: () => ({
  24. locale: "pt-BR",
  25. currency: "BRL",
  26. currencyDisplay: "symbol",
  27. hideCurrencySymbolOnFocus: false,
  28. hideGroupingSeparatorOnFocus: false,
  29. hideNegligibleDecimalDigitsOnFocus: false,
  30. autoDecimalDigits: true,
  31. useGrouping: true,
  32. accountingSign: false,
  33. }),
  34. },
  35. disable: {
  36. type: Boolean,
  37. default: false,
  38. },
  39. readonly: {
  40. type: Boolean,
  41. default: false,
  42. },
  43. label: {
  44. type: String,
  45. default: () => useI18n().t("common.terms.currency"),
  46. },
  47. });
  48. const { inputRef, formattedValue, numberValue, setValue } =
  49. useCurrencyInput(options);
  50. const errorMessage = computed(() =>
  51. numberValue.value < 0
  52. ? useI18n().t("validation.rules.value_smaller_than_zero")
  53. : undefined,
  54. );
  55. const newLabel = computed(() => (label ? label : void 0));
  56. watch(
  57. () => model.value,
  58. (value) => {
  59. setValue(value);
  60. },
  61. );
  62. </script>