-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Add dispatchEvent to fragment instances #32813
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
Merged
Merged
Changes from all commits
Commits
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
157 changes: 157 additions & 0 deletions
157
fixtures/dom/src/components/fixtures/fragment-refs/EventDispatchCase.js
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,157 @@ | ||
import TestCase from '../../TestCase'; | ||
import Fixture from '../../Fixture'; | ||
|
||
const React = window.React; | ||
const {Fragment, useRef, useState} = React; | ||
|
||
function WrapperComponent(props) { | ||
return props.children; | ||
} | ||
|
||
const initialState = { | ||
child: false, | ||
parent: false, | ||
grandparent: false, | ||
}; | ||
|
||
export default function EventListenerCase() { | ||
const fragmentRef = useRef(null); | ||
const [clickedState, setClickedState] = useState({...initialState}); | ||
const [fragmentEventFired, setFragmentEventFired] = useState(false); | ||
const [bubblesState, setBubblesState] = useState(true); | ||
|
||
function setClick(id) { | ||
setClickedState(prev => ({...prev, [id]: true})); | ||
} | ||
|
||
function fragmentClickHandler(e) { | ||
setFragmentEventFired(true); | ||
} | ||
|
||
return ( | ||
<TestCase title="Event Dispatch"> | ||
<TestCase.Steps> | ||
<li> | ||
Each box has regular click handlers, you can click each one to observe | ||
the status changing through standard bubbling. | ||
</li> | ||
<li>Clear the clicked state</li> | ||
<li> | ||
Click the "Dispatch click event" button to dispatch a click event on | ||
the Fragment. The event will be dispatched on the Fragment's parent, | ||
so the child will not change state. | ||
</li> | ||
<li> | ||
Click the "Add event listener" button to add a click event listener on | ||
the Fragment. This registers a handler that will turn the child blue | ||
on click. | ||
</li> | ||
<li> | ||
Now click the "Dispatch click event" button again. You can see that it | ||
will fire the Fragment's event handler in addition to bubbling the | ||
click from the parent. | ||
</li> | ||
<li> | ||
If you turn off bubbling, only the Fragment's event handler will be | ||
called. | ||
</li> | ||
</TestCase.Steps> | ||
|
||
<TestCase.ExpectedResult> | ||
<p> | ||
Dispatching an event on a Fragment will forward the dispatch to its | ||
parent for the standard case. You can observe when dispatching that | ||
the parent handler is called in additional to bubbling from there. A | ||
delay is added to make the bubbling more clear.{' '} | ||
</p> | ||
<p> | ||
When there have been event handlers added to the Fragment, the | ||
Fragment's event handler will be called in addition to bubbling from | ||
the parent. Without bubbling, only the Fragment's event handler will | ||
be called. | ||
</p> | ||
</TestCase.ExpectedResult> | ||
|
||
<Fixture> | ||
<Fixture.Controls> | ||
<select | ||
value={bubblesState ? 'true' : 'false'} | ||
onChange={e => { | ||
setBubblesState(e.target.value === 'true'); | ||
}}> | ||
<option value="true">Bubbles: true</option> | ||
<option value="false">Bubbles: false</option> | ||
</select> | ||
<button | ||
onClick={() => { | ||
fragmentRef.current.dispatchEvent( | ||
new MouseEvent('click', {bubbles: bubblesState}) | ||
); | ||
}}> | ||
Dispatch click event | ||
</button> | ||
<button | ||
onClick={() => { | ||
setClickedState({...initialState}); | ||
setFragmentEventFired(false); | ||
}}> | ||
Reset clicked state | ||
</button> | ||
<button | ||
onClick={() => { | ||
fragmentRef.current.addEventListener( | ||
'click', | ||
fragmentClickHandler | ||
); | ||
}}> | ||
Add event listener | ||
</button> | ||
<button | ||
onClick={() => { | ||
fragmentRef.current.removeEventListener( | ||
'click', | ||
fragmentClickHandler | ||
); | ||
}}> | ||
Remove event listener | ||
</button> | ||
</Fixture.Controls> | ||
<div | ||
id="grandparent" | ||
onClick={e => { | ||
setTimeout(() => { | ||
setClick('grandparent'); | ||
}, 200); | ||
}} | ||
className="card"> | ||
Fragment grandparent - clicked:{' '} | ||
{clickedState.grandparent ? 'true' : 'false'} | ||
<div | ||
id="parent" | ||
onClick={e => { | ||
setTimeout(() => { | ||
setClick('parent'); | ||
}, 100); | ||
}} | ||
className="card"> | ||
Fragment parent - clicked: {clickedState.parent ? 'true' : 'false'} | ||
<Fragment ref={fragmentRef}> | ||
<div | ||
style={{ | ||
backgroundColor: fragmentEventFired ? 'lightblue' : 'inherit', | ||
}} | ||
id="child" | ||
className="card" | ||
onClick={e => { | ||
setClick('child'); | ||
}}> | ||
Fragment child - clicked:{' '} | ||
{clickedState.child ? 'true' : 'false'} | ||
</div> | ||
</Fragment> | ||
</div> | ||
</div> | ||
</Fixture> | ||
</TestCase> | ||
); | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check if this
event.bubbles
is true because if it's false, it should only fire on the fragment's own listeners and no the parent node. This can be solved by the same technique as self listeners.