Skip to content
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

Differentiate null and undefined in Custom Elements - removing sets to undefined #28716

Merged
merged 1 commit into from
Apr 2, 2024
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
10 changes: 5 additions & 5 deletions packages/react-dom-bindings/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ export function setInitialProperties(
continue;
}
const propValue = props[propKey];
if (propValue == null) {
if (propValue === undefined) {
continue;
}
setPropOnCustomElement(
Expand All @@ -1310,7 +1310,7 @@ export function setInitialProperties(
propKey,
propValue,
props,
null,
undefined,
);
}
return;
Expand Down Expand Up @@ -1741,14 +1741,14 @@ export function updateProperties(
const lastProp = lastProps[propKey];
if (
lastProps.hasOwnProperty(propKey) &&
lastProp != null &&
lastProp !== undefined &&
!nextProps.hasOwnProperty(propKey)
) {
setPropOnCustomElement(
domElement,
tag,
propKey,
null,
undefined,
nextProps,
lastProp,
);
Expand All @@ -1760,7 +1760,7 @@ export function updateProperties(
if (
nextProps.hasOwnProperty(propKey) &&
nextProp !== lastProp &&
(nextProp != null || lastProp != null)
(nextProp !== undefined || lastProp !== undefined)
) {
setPropOnCustomElement(
domElement,
Expand Down
73 changes: 71 additions & 2 deletions packages/react-dom/src/__tests__/DOMPropertyOperations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ describe('DOMPropertyOperations', () => {
});
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(2);
expect(customelement.oncustomevent).toBe(null);
expect(customelement.oncustomevent).toBe(undefined);
expect(customelement.getAttribute('oncustomevent')).toBe(null);
});

Expand Down Expand Up @@ -1290,12 +1290,33 @@ describe('DOMPropertyOperations', () => {
await act(() => {
root.render(<my-custom-element />);
});
expect(customElement.foo).toBe(null);
expect(customElement.foo).toBe(undefined);
await act(() => {
root.render(<my-custom-element foo={myFunction} />);
});
expect(customElement.foo).toBe(myFunction);
});

it('switching between null and undefined should update a property', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<my-custom-element foo={undefined} />);
});
const customElement = container.querySelector('my-custom-element');
customElement.foo = undefined;

await act(() => {
root.render(<my-custom-element foo={null} />);
});
expect(customElement.foo).toBe(null);

await act(() => {
root.render(<my-custom-element foo={undefined} />);
});
expect(customElement.foo).toBe(undefined);
});
});

describe('deleteValueForProperty', () => {
Expand Down Expand Up @@ -1349,5 +1370,53 @@ describe('DOMPropertyOperations', () => {
});
expect(container.firstChild.getAttribute('size')).toBe('5px');
});

it('custom elements should remove by setting undefined to restore defaults', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<my-custom-element />);
});
const customElement = container.querySelector('my-custom-element');

// Non-setter but existing property to active the `in` heuristic
customElement.raw = 1;

// Install a setter to activate the `in` heuristic
Object.defineProperty(customElement, 'object', {
set: function (value = null) {
this._object = value;
},
get: function () {
return this._object;
},
});

Object.defineProperty(customElement, 'string', {
set: function (value = '') {
this._string = value;
},
get: function () {
return this._string;
},
});

const obj = {};
await act(() => {
root.render(<my-custom-element raw={2} object={obj} string="hi" />);
});
expect(customElement.raw).toBe(2);
expect(customElement.object).toBe(obj);
expect(customElement.string).toBe('hi');

// Removing the properties should reset to defaults by passing undefined
await act(() => {
root.render(<my-custom-element />);
});
expect(customElement.raw).toBe(undefined);
expect(customElement.object).toBe(null);
expect(customElement.string).toBe('');
});
});
});