DefaultCurrencyInput.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <DefaultInput
  3. ref="inputRef"
  4. v-model="formattedValue"
  5. v-bind="$attrs"
  6. :label="label"
  7. :rules="finalRules"
  8. :input-class="inputClass"
  9. />
  10. </template>
  11. <script setup>
  12. import { computed, watch } from "vue";
  13. import { useCurrencyInput } from "vue-currency-input";
  14. import { useInputRules } from "src/composables/useInputRules";
  15. import DefaultInput from "./DefaultInput.vue";
  16. const { inputRules } = useInputRules();
  17. const model = defineModel({ type: Number });
  18. const { options, label, rules } = defineProps({
  19. options: {
  20. type: Object,
  21. default: () => ({
  22. locale: "pt-BR",
  23. currency: "BRL",
  24. currencyDisplay: "symbol",
  25. hideCurrencySymbolOnFocus: false,
  26. hideGroupingSeparatorOnFocus: false,
  27. hideNegligibleDecimalDigitsOnFocus: false,
  28. autoDecimalDigits: true,
  29. useGrouping: true,
  30. accountingSign: false,
  31. }),
  32. },
  33. label: {
  34. type: String,
  35. default: "Valor",
  36. },
  37. errorMessage: {
  38. type: String,
  39. default: "",
  40. },
  41. inputClass: {
  42. type: String,
  43. default: "",
  44. },
  45. rules: {
  46. type: Array,
  47. default: () => [],
  48. },
  49. });
  50. const { inputRef, formattedValue, numberValue, setValue } =
  51. useCurrencyInput(options);
  52. watch(
  53. () => model.value,
  54. (newValue) => {
  55. setValue(newValue);
  56. },
  57. );
  58. watch(
  59. () => numberValue.value,
  60. (newValue) => {
  61. model.value = newValue;
  62. },
  63. );
  64. const minRule = inputRules.minValue(0);
  65. const finalRules = computed(() => [
  66. ...rules,
  67. () => minRule(numberValue.value),
  68. ]);
  69. </script>