Skip to content

Commit fbd89c8

Browse files
committed
Add fixture for fragment refs
1 parent b0de151 commit fbd89c8

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

fixtures/dom/src/components/Header.js

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class Header extends React.Component {
8989
<option value="/selection-events">Selection Events</option>
9090
<option value="/suspense">Suspense</option>
9191
<option value="/form-state">Form State</option>
92+
<option value="/fragment-refs">Fragment Refs</option>
9293
</select>
9394
</label>
9495
<label htmlFor="global_version">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import Fixture from '../../Fixture';
2+
import FixtureSet from '../../FixtureSet';
3+
import TestCase from '../../TestCase';
4+
5+
const React = window.React;
6+
const {Fragment, useEffect, useRef, useState} = React;
7+
8+
function WrapperComponent(props) {
9+
return props.children;
10+
}
11+
12+
function handler(e) {
13+
const text = e.currentTarget.innerText;
14+
alert('You clicked: ' + text);
15+
}
16+
17+
export default function FragmentRefsPage() {
18+
const fragmentRef = useRef(null);
19+
const [extraChildCount, setExtraChildCount] = useState(0);
20+
21+
React.useEffect(() => {
22+
fragmentRef.current.addEventListener('click', handler);
23+
24+
return () => {
25+
fragmentRef.current.removeEventListener('click', handler);
26+
};
27+
});
28+
29+
return (
30+
<FixtureSet title="Fragment Refs">
31+
<TestCase title="Event registration">
32+
<TestCase.Steps>
33+
<li>Click one of the children, observe the alert</li>
34+
<li>Add a new child, click it, observe the alert</li>
35+
<li>Remove the event listeners, click a child, observe no alert</li>
36+
<li>
37+
Add the event listeners back, click a child, observe the alert
38+
</li>
39+
</TestCase.Steps>
40+
41+
<TestCase.ExpectedResult>
42+
<p>
43+
Fragment refs can manage event listeners on the first level of host
44+
children. This page loads with an effect that sets up click event
45+
hanndlers on each child card. Clicking on a card will show an alert
46+
with the card's text.
47+
</p>
48+
<p>
49+
New child nodes will also have event listeners applied. Removed
50+
nodes will have their listeners cleaned up.
51+
</p>
52+
</TestCase.ExpectedResult>
53+
54+
<Fixture>
55+
<div className="control-box" id="control-box">
56+
<div>Target count: {extraChildCount + 3}</div>
57+
<button
58+
onClick={() => {
59+
setExtraChildCount(prev => prev + 1);
60+
}}>
61+
Add Child
62+
</button>
63+
<button
64+
onClick={() => {
65+
fragmentRef.current.addEventListener('click', handler);
66+
}}>
67+
Add click event listeners
68+
</button>
69+
<button
70+
onClick={() => {
71+
fragmentRef.current.removeEventListener('click', handler);
72+
}}>
73+
Remove click event listeners
74+
</button>
75+
<div class="card-container">
76+
<Fragment ref={fragmentRef}>
77+
<div className="card" id="child-a">
78+
Child A
79+
</div>
80+
<div className="card" id="child-b">
81+
Child B
82+
</div>
83+
<WrapperComponent>
84+
<div className="card" id="child-c">
85+
Child C
86+
</div>
87+
{Array.from({length: extraChildCount}).map((_, index) => (
88+
<div
89+
className="card"
90+
id={'extra-child-' + index}
91+
key={index}>
92+
Extra Child {index}
93+
</div>
94+
))}
95+
</WrapperComponent>
96+
</Fragment>
97+
</div>
98+
</div>
99+
</Fixture>
100+
</TestCase>
101+
</FixtureSet>
102+
);
103+
}

fixtures/dom/src/style.css

+13
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,16 @@ th {
309309
tbody tr:nth-child(even) {
310310
background: #f0f0f0;
311311
}
312+
313+
.card-container {
314+
display: flex;
315+
flex-wrap: wrap;
316+
}
317+
318+
.card {
319+
background: white;
320+
border-radius: 2px;
321+
border: 1px solid rgba(0, 0, 0, 0.24);
322+
margin: 10px;
323+
padding: 10px;
324+
}

0 commit comments

Comments
 (0)