|
| 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 | +} |
0 commit comments