11import * as React from 'react'
2- import { Field } from 'react-final-form'
2+ import { Field , FieldRenderProps } from 'react-final-form'
33import { ExternallyChangedProps } from './types'
44
55interface Props {
@@ -18,13 +18,18 @@ const ExternallyChangedState: React.FC<Props> = ({ children, input, meta }) => {
1818 const hasInitializedRef = React . useRef ( false )
1919 const initialRenderPhaseRef = React . useRef ( true )
2020
21+ // The dependency array is intentionally omitted here because this effect
22+ // is designed to run on every render. It tracks value changes and field activity
23+ // to determine if changes were made externally (not by user interaction).
2124 React . useLayoutEffect ( ( ) => {
25+ // Initialize tracking on first run
2226 if ( ! hasInitializedRef . current ) {
2327 hasInitializedRef . current = true
2428 previousValueRef . current = input . value
2529 return
2630 }
2731
32+ // Handle value changes
2833 if ( input . value !== previousValueRef . current ) {
2934 // Only consider it externally changed if:
3035 // 1. The field is not active AND
@@ -35,16 +40,19 @@ const ExternallyChangedState: React.FC<Props> = ({ children, input, meta }) => {
3540 if ( initialRenderPhaseRef . current ) {
3641 initialRenderPhaseRef . current = false
3742 // If this is the first change and field is not active, it's likely initialization
43+ // Skip setting externally changed to avoid false positives during form setup
3844 if ( ! meta . active ) {
3945 previousValueRef . current = input . value
4046 return
4147 }
4248 }
4349
50+ // Update externally changed state and track the new value
4451 setExternallyChanged ( wasExternallyChanged )
4552 previousValueRef . current = input . value
4653 } else if ( meta . active && externallyChanged ) {
47- // Reset externally changed when field becomes active
54+ // Reset externally changed when field becomes active after external change
55+ // This allows users to "acknowledge" external changes by focusing the field
4856 setExternallyChanged ( false )
4957 }
5058 } )
@@ -60,7 +68,7 @@ const ExternallyChanged: React.FC<ExternallyChangedProps> = ({
6068 name,
6169 subscription : { active : true , value : true } ,
6270 allowNull : true ,
63- render : ( props : any ) =>
71+ render : ( props : FieldRenderProps < any > ) =>
6472 React . createElement ( ExternallyChangedState , { ...props , children } )
6573 } )
6674
0 commit comments