Skip to content

Commit

Permalink
fix: form 调用 switch getValue取值不正确
Browse files Browse the repository at this point in the history
  • Loading branch information
lareinayanyu committed Nov 5, 2024
1 parent 08b5a1b commit b409624
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* ✔ color
*/
import { Switch, SwitchProps, ViewStyle, NativeSyntheticEvent } from 'react-native'
import { useRef, useEffect, forwardRef, JSX, useState, useContext } from 'react'
import { useRef, useEffect, forwardRef, JSX, useState, useContext, useCallback } from 'react'
import { warn } from '@mpxjs/utils'
import useNodesRef, { HandlerRef } from './useNodesRef' // 引入辅助函数
import useInnerProps, { getCustomEvent } from './getInnerListeners'
Expand Down Expand Up @@ -51,6 +51,8 @@ const _Switch = forwardRef<HandlerRef<Switch, _SwitchProps>, _SwitchProps>((prop

const changeHandler = bindchange || catchchange

const checkedValueRef = useRef(checked)

let formValuesMap: Map<string, FormFieldValue> | undefined

const formContext = useContext(FormContext)
Expand All @@ -76,6 +78,10 @@ const _Switch = forwardRef<HandlerRef<Switch, _SwitchProps>, _SwitchProps>((prop
setIsChecked(checked)
}, [checked])

useEffect(() => {
checkedValueRef.current = isChecked
}, [isChecked])

const nodeRef = useRef(null)
useNodesRef<Switch, _SwitchProps>(props, ref, nodeRef)

Expand All @@ -95,13 +101,13 @@ const _Switch = forwardRef<HandlerRef<Switch, _SwitchProps>, _SwitchProps>((prop
}
}

const resetValue = () => {
const resetValue = useCallback(() => {
setIsChecked(false)
}
}, [])

const getValue = () => {
return isChecked
}
const getValue = useCallback(() => {
return checkedValueRef.current
}, [])

useEffect(() => {
if (formValuesMap) {
Expand All @@ -116,7 +122,7 @@ const _Switch = forwardRef<HandlerRef<Switch, _SwitchProps>, _SwitchProps>((prop
formValuesMap.delete(props.name)
}
}
}, [])
}, [])

const innerProps = useInnerProps(props, {
ref: nodeRef,
Expand Down

0 comments on commit b409624

Please sign in to comment.