feat: convert number slider to composition API

This commit is contained in:
IchliebedichZhu 2024-03-09 23:14:12 +00:00
parent e91acafde7
commit c23edc33b6

View File

@ -8,68 +8,72 @@
<template> <template>
<div id="number-slider"> <div id="number-slider">
<span :style="{ width: labelWidth }" class="label">{{ label }}</span> <span :style="{ width: labelWidth }" class="label">{{ label }}</span>
<el-slider v-model="innerValue" :min="minValue" :max="maxValue" :step="step" input-size="small" :show-input="showInput" :show-tooltip="false" :show-input-controls="false" @change="changeValue"> </el-slider> <el-slider
v-model="innerValue"
:min="minValue" :max="maxValue" :step="step"
input-size="small"
:show-input="showInput" :show-tooltip="false" :show-input-controls="false"
@change="changeValue"
/>
</div> </div>
</template> </template>
<script> <script lang="ts" setup>
const NAME = 'number-slider' // const NAME = 'number-slider'
import { mapGetters, mapActions } from 'vuex' import { Arrayable } from 'element-plus/es/utils';
import { watch, ref, onMounted } from 'vue';
import { mapActions } from 'vuex'
export default { type TProps = {
name: NAME, label?: string
props: { labelWidth?: string
label: { modelValue?: number
default: '', minValue?: number
}, maxValue?: number
labelWidth: { step?: number
default: '71px', showInput?: boolean
}, }
modelValue: {
default: 0, type TEmits = {
}, (event: 'update:modelValue', data: number): void
minValue: { (event: 'finish', data: Arrayable<number>): void
default: 0, }
},
maxValue: { const props = withDefaults(defineProps<TProps>(), {
default: 500, label: '',
}, labelWidth: '71px',
step: { modelValue: 0,
default: 1, minValue: 0,
}, maxValue: 500,
showInput: { step: 1,
default: true, showInput: true
}, })
}, const emit = defineEmits<TEmits>()
emits: ['update:modelValue', 'finish'],
data() { const innerValue = ref<number>(0)
return {
innerValue: 0, watch(
// first: true, () => innerValue.value,
(value) => {
if (props.modelValue !== value) {
emit('update:modelValue', value)
} }
}, }
computed: { )
...mapGetters([]),
}, watch(
watch: { () => props.modelValue,
innerValue(value) { () => {
if (this.modelValue !== value) { innerValue.value = props.modelValue
this.$emit('update:modelValue', value) }
} )
},
modelValue(val) { onMounted(() => {
this.innerValue = this.modelValue innerValue.value = props.modelValue
}, })
},
created() { function changeValue(value: Arrayable<number>) {
this.innerValue = this.modelValue emit('finish', value)
},
methods: {
...mapActions([]),
changeValue(value) {
this.$emit('finish', value)
},
},
} }
</script> </script>