Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 75 additions & 13 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -2988,6 +2988,9 @@ type StoredEventListener = {
type: string,
listener: EventListener,
optionsOrUseCapture: void | EventListenerOptionsOrUseCapture,
// When once:true, a wrapper that removes the fragment listener after the
// first fire. Otherwise the same as listener.
attachedListener: EventListener,
};

export type FragmentInstanceType = {
Expand Down Expand Up @@ -3042,13 +3045,37 @@ FragmentInstance.prototype.addEventListener = function (
const isNewEventListener =
indexOfEventListener(listeners, type, listener, optionsOrUseCapture) === -1;
if (isNewEventListener) {
listeners.push({type, listener, optionsOrUseCapture});
const fragmentInstance = this;
let attachedListener = listener;
if (isOnceOption(optionsOrUseCapture)) {
// once is fragment-scoped: the first fire on any child removes this
// listener from the fragment and every host child.
attachedListener = function (this: EventTarget, event: Event) {
fragmentInstance.removeEventListener(
type,
listener,
optionsOrUseCapture,
);
if (typeof listener === 'function') {
listener.call(this, event);
} else {
listener.handleEvent(event);
}
};
}
const attachOptions = getAttachOptions(optionsOrUseCapture);
listeners.push({
type,
listener,
optionsOrUseCapture,
attachedListener,
});
traverseFragmentInstancesAndTextInstances(
this._fragmentFiber,
addEventListenerToChild,
type,
listener,
optionsOrUseCapture,
attachedListener,
attachOptions,
);
}
this._eventListeners = listeners;
Expand Down Expand Up @@ -3083,12 +3110,15 @@ FragmentInstance.prototype.removeEventListener = function (
if (index === -1) {
return;
}
const {attachedListener, optionsOrUseCapture: storedOptions} =
listeners[index];
const attachOptions = getAttachOptions(storedOptions);
traverseFragmentInstancesAndTextInstances(
this._fragmentFiber,
removeEventListenerFromChild,
type,
listener,
optionsOrUseCapture,
attachedListener,
attachOptions,
);
listeners.splice(index, 1);
};
Expand All @@ -3102,6 +3132,22 @@ function removeEventListenerFromChild(
instance.removeEventListener(type, listener, optionsOrUseCapture);
return false;
}
function isOnceOption(opts: ?EventListenerOptionsOrUseCapture): boolean {
return opts != null && typeof opts !== 'boolean' && opts.once === true;
}
function getAttachOptions(
opts: void | EventListenerOptionsOrUseCapture,
): void | EventListenerOptionsOrUseCapture {
// Strip once when attaching to host children; Fragment owns once semantics.
if (opts == null || typeof opts === 'boolean' || opts.once !== true) {
return opts;
}
return {
capture: opts.capture,
passive: opts.passive,
signal: opts.signal,
};
}
function normalizeListenerOptions(
opts: ?EventListenerOptionsOrUseCapture,
): string {
Expand Down Expand Up @@ -3166,16 +3212,24 @@ FragmentInstance.prototype.dispatchEvent = function (
: document.createTextNode('');
if (eventListeners) {
for (let i = 0; i < eventListeners.length; i++) {
const {type, listener, optionsOrUseCapture} = eventListeners[i];
temp.addEventListener(type, listener, optionsOrUseCapture);
const {type, attachedListener, optionsOrUseCapture} = eventListeners[i];
temp.addEventListener(
type,
attachedListener,
getAttachOptions(optionsOrUseCapture),
);
}
}
parentHostInstance.appendChild(temp);
const cancelable = temp.dispatchEvent(event);
if (eventListeners) {
for (let i = 0; i < eventListeners.length; i++) {
const {type, listener, optionsOrUseCapture} = eventListeners[i];
temp.removeEventListener(type, listener, optionsOrUseCapture);
const {type, attachedListener, optionsOrUseCapture} = eventListeners[i];
temp.removeEventListener(
type,
attachedListener,
getAttachOptions(optionsOrUseCapture),
);
}
}
parentHostInstance.removeChild(temp);
Expand Down Expand Up @@ -3730,8 +3784,12 @@ export function commitNewChildToFragmentInstance(
const eventListeners = fragmentInstance._eventListeners;
if (eventListeners !== null) {
for (let i = 0; i < eventListeners.length; i++) {
const {type, listener, optionsOrUseCapture} = eventListeners[i];
childInstance.addEventListener(type, listener, optionsOrUseCapture);
const {type, attachedListener, optionsOrUseCapture} = eventListeners[i];
childInstance.addEventListener(
type,
attachedListener,
getAttachOptions(optionsOrUseCapture),
);
}
}
// Observers and fragment handles only apply to element children.
Expand All @@ -3756,8 +3814,12 @@ export function deleteChildFromFragmentInstance(
const eventListeners = fragmentInstance._eventListeners;
if (eventListeners !== null) {
for (let i = 0; i < eventListeners.length; i++) {
const {type, listener, optionsOrUseCapture} = eventListeners[i];
childInstance.removeEventListener(type, listener, optionsOrUseCapture);
const {type, attachedListener, optionsOrUseCapture} = eventListeners[i];
childInstance.removeEventListener(
type,
attachedListener,
getAttachOptions(optionsOrUseCapture),
);
}
}
if (childInstance.nodeType === TEXT_NODE) {
Expand Down
94 changes: 94 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFragmentRefs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,100 @@ describe('FragmentRefs', () => {
expect(hasClicked).toBe(true);
});

// @gate enableFragmentRefs
it('fires a once listener only once across existing children', async () => {
const fragmentRef = React.createRef();
const childARef = React.createRef();
const childBRef = React.createRef();
const root = ReactDOMClient.createRoot(container);

await act(() => {
root.render(
<div>
<Fragment ref={fragmentRef}>
<div ref={childARef} id="a">
A
</div>
<div ref={childBRef} id="b">
B
</div>
</Fragment>
</div>,
);
});

const logs = [];
fragmentRef.current.addEventListener(
'click',
() => {
logs.push('once');
},
{once: true},
);

childARef.current.click();
expect(logs).toEqual(['once']);

logs.length = 0;
childBRef.current.click();
expect(logs).toEqual([]);
});

// @gate enableFragmentRefs
it('does not re-arm a once listener when a new child is inserted', async () => {
const fragmentRef = React.createRef();
const childARef = React.createRef();
const childBRef = React.createRef();
const root = ReactDOMClient.createRoot(container);
let showChildB;

function Component() {
const [shouldShowChildB, setShouldShowChildB] = React.useState(false);
showChildB = () => {
setShouldShowChildB(true);
};

return (
<div>
<Fragment ref={fragmentRef}>
<div ref={childARef} id="a">
A
</div>
{shouldShowChildB && (
<div ref={childBRef} id="b">
B
</div>
)}
</Fragment>
</div>
);
}

await act(() => {
root.render(<Component />);
});

const logs = [];
fragmentRef.current.addEventListener(
'click',
() => {
logs.push('once');
},
{once: true},
);

childARef.current.click();
expect(logs).toEqual(['once']);

await act(() => {
showChildB();
});

logs.length = 0;
childBRef.current.click();
expect(logs).toEqual([]);
});

// @gate enableFragmentRefs && enableFragmentRefsTextNodes
it('adds an event listener to a newly added text child', async () => {
const fragmentRef = React.createRef();
Expand Down
1 change: 0 additions & 1 deletion packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -3374,7 +3374,6 @@ function reappearLayoutEffects(
// Fallthrough
}
case HostComponent: {
// TODO: Enable HostText for RN
if (
enableFragmentRefs &&
(finishedWork.tag === HostComponent ||
Expand Down
Loading