Skip to content

Conversation

@prem22k
Copy link

@prem22k prem22k commented Jan 20, 2026

Link to Jira Ticket

Fixes #632

Description

What changed?

I updated the onChange event handler in src/components/Form.tsx to handle a specific edge case with File components.
Specifically, I added a check within the onAnyEvent handler to detect if an onChange event originates from a component of type 'file'. If the modified flag is false in this case, the code now correctly forces it to true.

Why have you chosen this solution?

File components were firing onChange events with modified: false even when a file was added or removed. This prevented the form's "pristine" state from being updated correctly and caused issues with features relying on the modified flag (like "Save" button enablement).
By inspecting the event flags and component type, we ensure that file changes are treated as user modifications, consistent with other fields like text inputs.

Breaking Changes / Backwards Compatibility

None. This ensures correct state reporting for file components.

Dependencies

None.

How has this PR been tested?

  • New Regression Test: Added src/components/__tests__/FormFileChange.test.tsx which reproduces the reported issue (verifying modified was false) and confirms the fix (verifying modified is now true).
  • Existing Tests: Ran npm test to ensure no regressions in existing functionality (Form.test.tsx, usePagination.test.ts).

Checklist:

  • I have completed the above PR template
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (if applicable)
  • My changes generate no new warnings
  • My changes include tests that prove my fix is effective (or that my feature works as intended)
  • New and existing unit/integration tests pass locally with my changes
  • Any dependent changes have corresponding PRs that are listed above

Copilot AI review requested due to automatic review settings January 20, 2026 18:09
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug where file component onChange events were incorrectly reporting modified: false even when users added or removed files, preventing proper form state management and features like "Save" button enablement.

Changes:

  • Added special handling in the onChange event handler to force modified: true for file component changes
  • Added a regression test to verify file component changes correctly report modified status

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
src/components/Form.tsx Modified onChange event handler to detect file components and ensure modified flag is set to true
src/components/tests/FormFileChange.test.tsx Added new test to verify file component onChange reports modified=true

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


test('onChange should report modified=true when file is changed', async () => {
const handleChange = jest.fn((value, flags, modified) => {
// console.log('onChange called', { value, flags, modified });
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

The commented-out console.log statement should be removed. Commented-out code should not be committed as it creates clutter and can be confusing for future maintainers. If this was for debugging purposes during development, it should be removed entirely.

Suggested change
// console.log('onChange called', { value, flags, modified });

Copilot uses AI. Check for mistakes.
data: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='
}];

// Trigger change. Currently reproduces modified=false.
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

The comment "Currently reproduces modified=false" is outdated after the fix has been applied. This comment implies the bug is still present, which is misleading. It should be updated or removed to reflect the current state where the fix ensures modified=true is correctly set.

Suggested change
// Trigger change. Currently reproduces modified=false.
// Trigger change; this previously reproduced modified=false before the fix.

Copilot uses AI. Check for mistakes.
const args = handleChange.mock.calls[0];
console.log('File Change Args:', { modified: args[2] });

// This should PASS after the fix
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

The comment "This should PASS after the fix" is outdated. Since the fix is already applied in this PR, the comment should state that this test verifies the fix works correctly, not that it will pass after a future fix.

Suggested change
// This should PASS after the fix
// This test verifies that the fix correctly reports modified=true

Copilot uses AI. Check for mistakes.
export type FormSource = string | FormType;
interface FormConstructor {
new (
new(
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

This appears to be an unintentional formatting change where a space was removed between "new" and the opening parenthesis. The original format "new (" is more consistent with typical TypeScript/JavaScript style guides. This change should be reverted unless there's a specific style guide being followed that requires no space.

Suggested change
new(
new (

Copilot uses AI. Check for mistakes.
Comment on lines +142 to +144
// Fixed: Check if the change is from a file component and ensure modified is true
// See https://github.com/formio/react/issues/632
if (!modified && flags?.changed?.instance?.type === 'file') {
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

The logic only checks for the 'file' component type, but there may be other component types that have similar issues with the modified flag not being set correctly. Consider investigating whether components like 'signature' or other file-based components might have the same problem. If this is a broader issue with multiple component types, the fix should be generalized or documented why it's file-specific.

Suggested change
// Fixed: Check if the change is from a file component and ensure modified is true
// See https://github.com/formio/react/issues/632
if (!modified && flags?.changed?.instance?.type === 'file') {
// Fixed: Check if the change is from a file-like component and ensure modified is true
// See https://github.com/formio/react/issues/632
const changedInstance = flags?.changed?.instance as { type?: string } | undefined;
const fileLikeComponentTypes = ['file', 'signature'];
if (
!modified &&
changedInstance?.type &&
fileLikeComponentTypes.includes(changedInstance.type)
) {

Copilot uses AI. Check for mistakes.

await waitFor(() => expect(handleChange).toHaveBeenCalledTimes(1));
const args = handleChange.mock.calls[0];
console.log('File Change Args:', { modified: args[2] });
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

The console.log statement should be removed from production test code. Debug logging statements should not be committed in test files as they clutter the test output and provide no value in CI/CD environments.

Suggested change
console.log('File Change Args:', { modified: args[2] });

Copilot uses AI. Check for mistakes.
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.

onChange modified property remains false when file component is updated

1 participant