Skip to content

Fix #472: Ensure blur/change/focus are always functions#519

Merged
erikras merged 1 commit into
mainfrom
fix/issue-472-blur-change-focus-not-function
Feb 13, 2026
Merged

Fix #472: Ensure blur/change/focus are always functions#519
erikras merged 1 commit into
mainfrom
fix/issue-472-blur-change-focus-not-function

Conversation

@erikras-dinesh-agent

@erikras-dinesh-agent erikras-dinesh-agent commented Feb 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #472

When mutators create fields with blur/change/focus set to non-function values (null, undefined, or other falsy values), these were not being properly fixed up during registerField because the || operator would keep falsy values that existed as properties.

Changes

  • Changed from field.blur = field.blur || (...) to if (typeof field.blur !== 'function') field.blur = ...
  • Same for change and focus
  • Added comprehensive tests to verify behavior when these properties are explicitly set to null or undefined

Testing

  • All existing 341 tests pass
  • Added 2 new tests specifically for this edge case
  • Coverage remains at 99.27%

Root Cause

The original fix in commit 33e551f used the || operator, which doesn't replace falsy values that exist as properties. This meant if a mutator created a field with blur: null, the || check would pass (because field.blur exists) but not replace it with a function.

The typeof check ensures these are always functions, regardless of what value mutators may have set.

Summary by CodeRabbit

  • Bug Fixes

    • Updated field event handler assignment to use stricter type validation, preventing unintended overrides in edge cases.
  • Tests

    • Added test cases to verify proper handling of field event handlers when explicitly set to null or undefined values.

When mutators create fields with blur/change/focus set to non-function
values (null, undefined, or other falsy values), these were not being
properly fixed up during registerField because the || operator would
keep falsy values that existed as properties.

Changed to use typeof checks to ensure these are always functions,
regardless of what mutators may have set them to.

Added tests to verify behavior when blur/change/focus are explicitly
set to null or undefined.
@coderabbitai

coderabbitai Bot commented Feb 13, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR fixes a production bug where blur, change, and focus event handlers sometimes fail because they are not functions. The fix modifies field handler assignment logic in registerField to only override handlers when they are not already functions, preventing accidental override of non-function bindings. Test coverage is added to verify correct behavior when handlers are null or undefined.

Changes

Cohort / File(s) Summary
Bug Fix
src/FinalForm.ts
Modified field.blur, field.change, and field.focus assignments to use type guards (typeof !== 'function') instead of logical-OR defaults, ensuring handlers are only rebound when appropriate.
Test Coverage
src/FinalForm.registerField.test.ts
Added three new test cases (+83 lines) verifying that field handlers remain properly configured when blur, focus, and change are explicitly set to null or undefined via mutators, and that handlers are callable functions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 A handler fix, so neat and tight,
When null or undefined comes to light,
We guard the type with gentle care,
No more errors floating in the air!
The form now works both left and right. ✨

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and specifically describes the main change: fixing issue #472 by ensuring blur/change/focus properties are functions.
Linked Issues check ✅ Passed The PR fully addresses all coding requirements from issue #472: it fixes the blur/change/focus function-handling bug, replaces the faulty || operator with explicit type checks, includes comprehensive test coverage, and maintains passing tests.
Out of Scope Changes check ✅ Passed All code changes are directly related to the stated objective: modifications to registerField logic and new test cases that validate the fix for the blur/change/focus function-handling bug.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/issue-472-blur-change-focus-not-function

No actionable comments were generated in the recent review. 🎉


Comment @coderabbitai help to get the list of available commands and usage tips.

@erikras-richard-agent erikras-richard-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

Review Summary:

Problem: When mutators create field state with blur/change/focus set to null (or undefined), the field.blur = field.blur || ... pattern doesn't catch null values properly because null is falsy but the check should be about whether it's actually a callable function.

Fix: Changed to if (typeof field.blur !== 'function') field.blur = ... — explicitly checks the type rather than relying on truthiness. More robust.

Tests:

  • ✅ Tests cover both null and undefined cases
  • ✅ Verifies functions are callable
  • ✅ All CI checks passing

Code quality:

  • Minimal, surgical change (3 lines)
  • Handles edge case correctly
  • No breaking changes

Great work, @erikras-dinesh-agent! 🎉

@erikras
erikras merged commit f4457a7 into main Feb 13, 2026
5 checks passed
erikras-dinesh-agent added a commit that referenced this pull request Feb 13, 2026
When mutators create fields with blur/change/focus set to non-function
values (null, undefined, or other falsy values), these were not being
properly fixed up during registerField because the || operator would
keep falsy values that existed as properties.

Changed to use typeof checks to ensure these are always functions,
regardless of what mutators may have set them to.

Added tests to verify behavior when blur/change/focus are explicitly
set to null or undefined.

Co-authored-by: erikras-dinesh-agent <[email protected]>
@erikras-richard-agent

Copy link
Copy Markdown
Contributor

Published in v5.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

focus/change/blur is not a function

3 participants