Skip to content

Commit

Permalink
feat(InputNumber): create Vue component for movie cost input
Browse files Browse the repository at this point in the history
Implement a new Vue component named InputNumber, designed for entering movie cost values.
The component includes props for value, placeholder, disabled state, maximum value,
and an append text. It also features a custom input handler that formats the input
value, ensuring it adheres to a numerical format with a single decimal point, and
prevents input exceeding the specified maximum value.
  • Loading branch information
WuChenDi committed Jul 25, 2024
1 parent 42f1d75 commit c868555
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions 05-Vue/vue-components/InputNumber/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<script>
export default {
name: 'InputNumber',
props: {
value: {
type: [String, Number],
default: null,
},
placeholder: {
type: String,
default: '',
},
disabled: {
type: Boolean,
default: false,
},
maximum: {
type: Number,
default: 999999999.99,
},
appendText: {
type: String,
default: '',
},
},
emits: ['input'],
data() {
return {
localMovieCost: this.value,
}
},
watch: {
value(newVal) {
this.localMovieCost = newVal
},
},
methods: {
handleInput(event) {
let temp = event.toString()
temp = temp.replace(//g, '.')
temp = temp.replace(/[^\d.]/g, '')
temp = temp.replace(/^\./g, '')
temp = temp.replace(/\.{2,}/g, '')
temp = temp.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
temp = temp.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
// 限制最大值
if (Number.parseFloat(temp) > this.maximum) {
temp = this.maximum.toString()
}
this.$emit('input', temp)
this.localMovieCost = temp
},
},
}
</script>

<template>
<el-input
:value="localMovieCost"
:placeholder="placeholder"
:disabled="disabled"
@input="handleInput"
>
<template slot="append">{{ appendText }}</template>
</el-input>
</template>

0 comments on commit c868555

Please sign in to comment.