2023-10-27 16:31:38 +08:00
|
|
|
<template>
|
|
|
|
<div class="relative" :class="fieldClass">
|
|
|
|
<label v-if="showLabel" :class="labelClass" :for="id">{{ label }}</label>
|
|
|
|
<div :class="inputClass">
|
|
|
|
<input ref="input"
|
2023-12-12 17:19:06 +08:00
|
|
|
:type="type"
|
2023-10-27 16:31:38 +08:00
|
|
|
:id="id"
|
|
|
|
:name="name"
|
2023-11-20 20:25:20 +08:00
|
|
|
:value="inputValue"
|
2023-10-27 16:31:38 +08:00
|
|
|
:class="`${error ? 'error' : ''}`"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:required="required"
|
|
|
|
@input="updateValue"
|
|
|
|
/>
|
|
|
|
<div
|
|
|
|
class="mt-2 text-sn-delete-red whitespace-nowrap truncate text-xs font-normal absolute bottom-[-1rem] w-full"
|
|
|
|
:title="error"
|
|
|
|
:class="{ visible: error, invisible: !error}"
|
|
|
|
>
|
|
|
|
{{ error }}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2024-01-04 23:34:36 +08:00
|
|
|
export default {
|
|
|
|
name: 'Input',
|
|
|
|
props: {
|
|
|
|
id: { type: String, required: false },
|
|
|
|
fieldClass: { type: String, default: '' },
|
|
|
|
inputClass: { type: String, default: '' },
|
|
|
|
labelClass: { type: String, default: '' },
|
|
|
|
type: { type: String, default: 'text' },
|
|
|
|
name: { type: String, required: true },
|
|
|
|
value: { type: [String, Number], required: false },
|
|
|
|
decimals: { type: [Number, String], default: 0 },
|
|
|
|
placeholder: { type: String, default: '' },
|
|
|
|
required: { type: Boolean, default: false },
|
|
|
|
showLabel: { type: Boolean, default: false },
|
|
|
|
label: { type: String, required: false },
|
|
|
|
autoFocus: { type: Boolean, default: false },
|
|
|
|
error: { type: String, required: false }
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
inputValue() {
|
|
|
|
if (this.type === 'text') return this.value;
|
2023-11-20 20:25:20 +08:00
|
|
|
|
2024-01-04 23:34:36 +08:00
|
|
|
return isNaN(this.value) ? '' : this.value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
updateValue($event) {
|
|
|
|
switch (this.type) {
|
|
|
|
case 'text':
|
|
|
|
this.$emit('update', $event.target.value);
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
const newValue = this.formatDecimalValue($event.target.value);
|
|
|
|
this.$refs.input.value = newValue;
|
|
|
|
if (!isNaN(newValue)) this.$emit('update', newValue);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2023-10-27 16:31:38 +08:00
|
|
|
}
|
|
|
|
},
|
2024-01-04 23:34:36 +08:00
|
|
|
formatDecimalValue(value) {
|
|
|
|
const decimalValue = value.replace(/[^-0-9.]/g, '');
|
|
|
|
if (this.decimals === '0') {
|
|
|
|
return decimalValue.split('.')[0];
|
|
|
|
}
|
|
|
|
return decimalValue.match(new RegExp(`^-?\\d*(\\.\\d{0,${this.decimals}})?`))[0];
|
2023-10-27 16:31:38 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-04 23:34:36 +08:00
|
|
|
};
|
2023-10-27 16:31:38 +08:00
|
|
|
</script>
|