diff --git a/packages/components/slider/src/index.vue b/packages/components/slider/src/index.vue index 26511d672b..625bf8d4b9 100644 --- a/packages/components/slider/src/index.vue +++ b/packages/components/slider/src/index.vue @@ -12,7 +12,7 @@
{ } ) - watch( - () => initData.firstValue, - (val) => { - if (props.range) { - _emit([minValue.value, maxValue.value]) - } else { - _emit(val) - } - } - ) - - watch( - () => initData.secondValue, - () => { - if (props.range) { - _emit([minValue.value, maxValue.value]) - } - } - ) - watch( () => props.modelValue, (val, oldVal) => { diff --git a/packages/components/slider/src/useSlide.ts b/packages/components/slider/src/useSlide.ts index 3b47ec43e2..e6ff045f16 100644 --- a/packages/components/slider/src/useSlide.ts +++ b/packages/components/slider/src/useSlide.ts @@ -1,5 +1,9 @@ import { computed, inject, nextTick, ref, shallowRef } from 'vue' -import { CHANGE_EVENT } from '@element-plus/utils/constants' +import { + CHANGE_EVENT, + UPDATE_MODEL_EVENT, + INPUT_EVENT, +} from '@element-plus/utils/constants' import { elFormKey, elFormItemKey } from '@element-plus/tokens' import type { CSSProperties } from 'vue' import type { ButtonRefs, ISliderInitData, ISliderProps } from './slider.type' @@ -101,6 +105,24 @@ export const useSlide = ( buttonRefs[buttonRefName].value.setPosition(percent) } + const setFirstValue = (firstValue: number) => { + initData.firstValue = firstValue + _emit(props.range ? [minValue.value, maxValue.value] : firstValue) + } + + const setSecondValue = (secondValue: number) => { + initData.secondValue = secondValue + + if (props.range) { + _emit([minValue.value, maxValue.value]) + } + } + + const _emit = (val: number | number[]) => { + emit(UPDATE_MODEL_EVENT, val) + emit(INPUT_EVENT, val) + } + const emitChange = async () => { await nextTick() emit( @@ -140,5 +162,7 @@ export const useSlide = ( setPosition, emitChange, onSliderClick, + setFirstValue, + setSecondValue, } }