From 6494c8a7ffa17f9c0693e682aebe8ad9ff4a4b7c Mon Sep 17 00:00:00 2001 From: handongxun Date: Wed, 23 Feb 2022 19:22:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(nvue3):=20slider=20=E6=94=AF=E6=8C=81=20for?= =?UTF-8?q?m=EF=BC=8C=E8=AE=BE=E7=BD=AE=20min/max=20=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uni-components/src/nvue/slider/index.tsx | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/uni-components/src/nvue/slider/index.tsx b/packages/uni-components/src/nvue/slider/index.tsx index 9a4f41101a..ba111bb7e0 100644 --- a/packages/uni-components/src/nvue/slider/index.tsx +++ b/packages/uni-components/src/nvue/slider/index.tsx @@ -5,6 +5,8 @@ import { computed, watch, onMounted, + onUnmounted, + inject, reactive, ExtractPropTypes, } from 'vue' @@ -15,6 +17,7 @@ import { } from '../../helpers/useNVueEvent' import { getComponentSize } from '../helpers' import { NVueComponentStyles, createNVueTextVNode } from '../utils' +import { uniFormKey, UniFormCtx } from '../../components/form' import { sliderProps } from '../../components/slider' const slierStyles: NVueComponentStyles = [ @@ -121,6 +124,7 @@ export default defineComponent({ const state = useSliderState(props) const listeners = useSliderListeners(props, state, trigger) + useSliderInject(props, state) watch( () => props.value, @@ -187,7 +191,7 @@ function useSliderState(props: SliderProps) { const _getValueWidth = () => { const max = Number(props.max) const min = Number(props.min) - return ((sliderValue.value - min) / max - min) * sliderWidth.value + return ((sliderValue.value - min) / (max - min)) * sliderWidth.value } const state = reactive({ @@ -274,3 +278,28 @@ function useSliderListeners( return listeners } + +function useSliderInject(props: SliderProps, state: SliderState) { + const uniForm = inject(uniFormKey, false as unknown as UniFormCtx) + + const formField = { + submit: () => { + const data: [string, any] = ['', null] + if (props.name) { + data[0] = props.name + data[1] = state.sliderValue + } + return data + }, + reset: () => { + state.sliderValue = Number(props.value) + }, + } + + if (!!uniForm) { + uniForm.addField(formField) + onUnmounted(() => { + uniForm.removeField(formField) + }) + } +} -- GitLab