-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: Don't reset form fields if reset event is cancelled. #7603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nanto
wants to merge
5
commits into
adobe:main
Choose a base branch
from
nanto:cancelled-form-reset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
623de5a
fix: Don't reset form fields if reset event is cancelled.
nanto 5e25da6
Merge branch 'main' into cancelled-form-reset
snowystinger 45e92cf
tests, handle bubble preventdefault and stoppropagation
snowystinger 0d17cc6
fix: Allow multiple form elements with useFormReset
nanto ed83dff
Merge branch 'main' into cancelled-form-reset
snowystinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright 2025 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import {fireEvent, render} from '@react-spectrum/test-utils-internal'; | ||
import React, {useRef} from 'react'; | ||
import {useFormReset} from '../'; | ||
|
||
describe('useFormReset', () => { | ||
it('should call onReset on reset', () => { | ||
const onReset = jest.fn(); | ||
const Form = () => { | ||
const ref = useRef<HTMLInputElement>(null); | ||
useFormReset(ref, '', onReset); | ||
return ( | ||
<form> | ||
<input ref={ref} type="text" /> | ||
<button type="reset">Reset</button> | ||
</form> | ||
); | ||
}; | ||
const {getByRole} = render(<Form />); | ||
const button = getByRole('button'); | ||
fireEvent.click(button); | ||
expect(onReset).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onReset on reset even if event is stopped', () => { | ||
const onReset = jest.fn(); | ||
const Form = () => { | ||
const ref = useRef<HTMLInputElement>(null); | ||
useFormReset(ref, '', onReset); | ||
return ( | ||
<form onReset={(e) => e.stopPropagation()}> | ||
<input ref={ref} type="text" /> | ||
<button type="reset">Reset</button> | ||
</form> | ||
); | ||
}; | ||
const {getByRole} = render(<Form />); | ||
const button = getByRole('button'); | ||
fireEvent.click(button); | ||
expect(onReset).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call every onReset on reset', () => { | ||
const onReset1 = jest.fn(); | ||
const onReset2 = jest.fn(); | ||
const Form = () => { | ||
const ref1 = useRef<HTMLInputElement>(null); | ||
useFormReset(ref1, '', onReset1); | ||
const ref2 = useRef<HTMLInputElement>(null); | ||
useFormReset(ref2, '', onReset2); | ||
return ( | ||
<form> | ||
<input ref={ref1} type="text" /> | ||
<input ref={ref2} type="text" /> | ||
<button type="reset">Reset</button> | ||
</form> | ||
); | ||
}; | ||
const {getByRole} = render(<Form />); | ||
const button = getByRole('button'); | ||
fireEvent.click(button); | ||
expect(onReset1).toHaveBeenCalled(); | ||
expect(onReset2).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call onReset if reset is cancelled', async () => { | ||
const onReset = jest.fn(); | ||
const Form = () => { | ||
const ref = useRef<HTMLInputElement>(null); | ||
useFormReset(ref, '', onReset); | ||
return ( | ||
<form onReset={(e) => e.preventDefault()}> | ||
<input ref={ref} type="text" /> | ||
<button type="reset">Reset</button> | ||
</form> | ||
); | ||
}; | ||
const {getByRole} = render(<Form />); | ||
const button = getByRole('button'); | ||
fireEvent.click(button); | ||
expect(onReset).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call onReset if reset is cancelled in capture phase', async () => { | ||
const onReset = jest.fn(); | ||
const Form = () => { | ||
const ref = useRef<HTMLInputElement>(null); | ||
useFormReset(ref, '', onReset); | ||
return ( | ||
<form onResetCapture={(e) => e.preventDefault()}> | ||
<input ref={ref} type="text" /> | ||
<button type="reset">Reset</button> | ||
</form> | ||
); | ||
}; | ||
const {getByRole} = render(<Form />); | ||
const button = getByRole('button'); | ||
fireEvent.click(button); | ||
expect(onReset).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call any onReset if reset is cancelled', () => { | ||
const onReset1 = jest.fn(); | ||
const onReset2 = jest.fn(); | ||
const Form = () => { | ||
const ref1 = useRef<HTMLInputElement>(null); | ||
useFormReset(ref1, '', onReset1); | ||
const ref2 = useRef<HTMLInputElement>(null); | ||
useFormReset(ref2, '', onReset2); | ||
return ( | ||
<form onReset={(e) => e.preventDefault()}> | ||
<input ref={ref1} type="text" /> | ||
<input ref={ref2} type="text" /> | ||
<button type="reset">Reset</button> | ||
</form> | ||
); | ||
}; | ||
const {getByRole} = render(<Form />); | ||
const button = getByRole('button'); | ||
fireEvent.click(button); | ||
expect(onReset1).not.toHaveBeenCalled(); | ||
expect(onReset2).not.toHaveBeenCalled(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.