index.vue
3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<script setup lang="ts">
import { InputGroup, Select, Input } from 'ant-design-vue';
import { computed, ref, unref } from 'vue';
import { InputTypeEnum } from './config';
import { isNullOrUnDef } from '/@/utils/is';
const props = withDefaults(
defineProps<{
type?: InputTypeEnum;
value?: number | string;
hexWithPrefix?: boolean;
hexUpperCase?: boolean;
disabled?: boolean;
maxValue?: number;
showScaleSelect?: boolean;
}>(),
{
type: InputTypeEnum.HEX,
hexWithPrefix: false,
hexUpperCase: true,
maxValue: Number.MAX_SAFE_INTEGER,
showScaleSelect: true,
}
);
const emits = defineEmits(['update:value', 'change', 'hexChange']);
const typOptions = Object.values(InputTypeEnum).map((value) => ({ label: value, value }));
const inputType = ref(props.type);
const validateDECReg = /^[\d]+$/;
const validateHEXReg = /^(0x)?[\da-fA-F]+$/;
const getMaxValue = computed(() => {
const { maxValue } = props;
return maxValue;
});
const getDecToHex = (value: number | string) => {
const { hexUpperCase, hexWithPrefix } = props;
let hex = Number(value).toString(16);
hex = hexUpperCase ? hex.toUpperCase() : hex;
return `${hexWithPrefix ? '0x' : ''}${hex}`;
};
const getHexToDec = (value: number | string) => {
return parseInt(value, 16);
};
const getDECValue = (value: number | string) => {
return parseInt(value, unref(inputType) === InputTypeEnum.HEX ? 16 : 10);
};
const getFormatValue = (value: number | string) => {
if (unref(inputType) === InputTypeEnum.HEX) {
const { hexUpperCase, hexWithPrefix } = props;
if (hexUpperCase) value = value.toString().toUpperCase();
if (hexWithPrefix) value = `0x${value}`;
return value;
}
return value;
};
const getInputValue = computed({
get() {
const { type, value } = props;
if (isNullOrUnDef(value)) return;
if (type === unref(inputType)) return value;
if (type === InputTypeEnum.HEX && unref(inputType) === InputTypeEnum.DEC) {
return getHexToDec(value);
} else {
return getDecToHex(value);
}
},
set(value: string) {
const { type } = props;
if (unref(inputType) === InputTypeEnum.HEX) value = value.replace('0x', '');
if (!value) {
emits('update:value', undefined);
return;
}
if (unref(inputType) === InputTypeEnum.HEX && !validateHEXReg.test(value)) return;
if (unref(inputType) === InputTypeEnum.DEC && !validateDECReg.test(value)) return;
if (getDECValue(value) > unref(getMaxValue)) return;
if (type === unref(inputType)) {
emits('update:value', getFormatValue(value));
return;
}
if (type === InputTypeEnum.HEX && unref(inputType) === InputTypeEnum.DEC) {
emits('update:value', getDecToHex(value));
} else {
emits('update:value', getHexToDec(value));
}
},
});
const getNegationValue = computed(() => {
const { hexWithPrefix } = props;
if (isNullOrUnDef(unref(getInputValue))) return 0;
return unref(inputType) === InputTypeEnum.HEX
? getHexToDec(unref(getInputValue)!)
: `${hexWithPrefix ? '0x' : ''}${Number(unref(getInputValue)).toString(16).toUpperCase()}`;
});
const handleInputTypeChange = (e) => {
emits('hexChange', e);
};
</script>
<template>
<InputGroup class="!flex w-full h-full justify-center items-center" compact>
<Select
v-if="showScaleSelect"
v-model:value="inputType"
class="min-w-20"
:options="typOptions"
:disabled="disabled"
@change="handleInputTypeChange"
/>
<Input v-bind="$attrs" v-model:value="getInputValue" class="!w-full" :disabled="disabled" />
<div class="min-w-14 h-full px-2 flex-grow flex-shrink-0 text-center leading-8 bg-gray-200">
{{ getNegationValue }}
</div>
</InputGroup>
</template>