DefaultCurrencyInput.vue 1.6 KB

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