|
| 1 | +<template> |
| 2 | + <div class="BaseValueInput"> |
| 3 | + <Tooltip :visible="!!errorMessage" placement="topLeft" :title="errorMessage"> |
| 4 | + <Input |
| 5 | + v-model="inputText" |
| 6 | + class="Input" |
| 7 | + :class="{ Error: !!errorMessage }" |
| 8 | + :disabled="disabled" |
| 9 | + @focus="hideMaxValue" |
| 10 | + @blur="showMaxValueIfEmpty" |
| 11 | + /> |
| 12 | + <div class="Overlay right-2" :class="{ 'opacity-50': disabled }"> |
| 13 | + <format-currency v-if="!inputValue && maxValue" :value="maxValue" /> DAI |
| 14 | + </div> |
| 15 | + </Tooltip> |
| 16 | + </div> |
| 17 | +</template> |
| 18 | + |
| 19 | +<script lang="ts"> |
| 20 | +import Vue from 'vue'; |
| 21 | +import BigNumber from 'bignumber.js'; |
| 22 | +import { Input, Tooltip } from 'ant-design-vue'; |
| 23 | +import FormatCurrency from '~/components/utils/FormatCurrency.vue'; |
| 24 | +
|
| 25 | +export default Vue.extend({ |
| 26 | + components: { FormatCurrency, Tooltip, Input }, |
| 27 | + props: { |
| 28 | + inputValue: { |
| 29 | + type: Object as Vue.PropType<BigNumber> | undefined, |
| 30 | + default: undefined, |
| 31 | + }, |
| 32 | + minValue: { |
| 33 | + type: Object as Vue.PropType<BigNumber>, |
| 34 | + default: undefined, |
| 35 | + }, |
| 36 | + maxValue: { |
| 37 | + type: Object as Vue.PropType<BigNumber>, |
| 38 | + default: undefined, |
| 39 | + }, |
| 40 | + validator: { |
| 41 | + type: Function as Vue.PropType<function>, |
| 42 | + default: () => {}, |
| 43 | + }, |
| 44 | + disabled: { |
| 45 | + type: Boolean, |
| 46 | + default: false, |
| 47 | + }, |
| 48 | + }, |
| 49 | + data() { |
| 50 | + return { |
| 51 | + inputText: '', |
| 52 | + }; |
| 53 | + }, |
| 54 | + computed: { |
| 55 | + isInputEmpty(): boolean { |
| 56 | + return this.inputText.trim() === ''; |
| 57 | + }, |
| 58 | + inputTextParsed(): BigNumber | undefined { |
| 59 | + if (this.isInputEmpty) { |
| 60 | + return undefined; |
| 61 | + } |
| 62 | + return new BigNumber(this.inputText.trim()); |
| 63 | + }, |
| 64 | + errorMessage(): string { |
| 65 | + try { |
| 66 | + if (this.validator) { |
| 67 | + this.validator(this.inputTextParsed, this.minValue, this.maxValue); |
| 68 | + } |
| 69 | + } catch (error) { |
| 70 | + return error.message; |
| 71 | + } |
| 72 | + return ''; |
| 73 | + }, |
| 74 | + }, |
| 75 | + watch: { |
| 76 | + inputText(_newInputText: string, oldInputText: string) { |
| 77 | + if (this.errorMessage || !this.inputTextParsed) { |
| 78 | + this.$emit('update:inputValue', new BigNumber(NaN)); |
| 79 | + return; |
| 80 | + } |
| 81 | + if (this.inputTextParsed.isNaN()) { |
| 82 | + this.inputText = oldInputText || ''; |
| 83 | + return; |
| 84 | + } |
| 85 | + this.$emit('update:inputValue', this.inputTextParsed); |
| 86 | + }, |
| 87 | + inputValue(newInputValue: BigNumber | undefined) { |
| 88 | + if (!newInputValue) { |
| 89 | + this.inputText = ''; |
| 90 | + return; |
| 91 | + } |
| 92 | + if (newInputValue.isEqualTo(this.minValue) && !newInputValue.isZero()) { |
| 93 | + this.inputText = newInputValue.toFixed(); |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + methods: { |
| 98 | + hideMaxValue(): void { |
| 99 | + if (!this.inputValue) { |
| 100 | + this.$emit('update:inputValue', new BigNumber(NaN)); |
| 101 | + } |
| 102 | + }, |
| 103 | + showMaxValueIfEmpty(): void { |
| 104 | + if (this.isInputEmpty) { |
| 105 | + this.$emit('update:inputValue', undefined); |
| 106 | + } |
| 107 | + }, |
| 108 | + }, |
| 109 | +}); |
| 110 | +</script> |
| 111 | + |
| 112 | +<style scoped> |
| 113 | +.BaseValueInput { |
| 114 | + @apply w-full relative dark:text-gray-300; |
| 115 | +} |
| 116 | +
|
| 117 | +.Input { |
| 118 | + @apply text-right pr-8; |
| 119 | +} |
| 120 | +
|
| 121 | +.Error { |
| 122 | + @apply border-red-500 hover:border-red-400; |
| 123 | +} |
| 124 | +
|
| 125 | +.Error:focus { |
| 126 | + @apply border-red-400; |
| 127 | +
|
| 128 | + box-shadow: 0 0 3px rgb(239, 68, 68); |
| 129 | +} |
| 130 | +
|
| 131 | +.Overlay { |
| 132 | + @apply absolute pointer-events-none h-full flex items-center; |
| 133 | +
|
| 134 | + top: 0.5px; |
| 135 | +} |
| 136 | +</style> |
0 commit comments