Skip to content

Commit

Permalink
Revert "Remove initOption special case (facebook#26595)"
Browse files Browse the repository at this point in the history
This reverts commit 343a45f.
  • Loading branch information
kassens committed Apr 17, 2023
1 parent 1c90108 commit 8afbca1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/react-dom-bindings/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
updateInput,
restoreControlledInputState,
} from './ReactDOMInput';
import {validateOptionProps} from './ReactDOMOption';
import {initOption, validateOptionProps} from './ReactDOMOption';
import {
validateSelectProps,
initSelect,
Expand Down Expand Up @@ -995,6 +995,7 @@ export function setInitialProperties(
}
}
}
initOption(domElement, props);
return;
}
case 'dialog': {
Expand Down
8 changes: 8 additions & 0 deletions packages/react-dom-bindings/src/client/ReactDOMOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import {Children} from 'react';
import {getToStringValue, toString} from './ToStringValue';

let didWarnSelectedSetOnOption = false;
let didWarnInvalidChild = false;
Expand Down Expand Up @@ -58,3 +59,10 @@ export function validateOptionProps(element: Element, props: Object) {
}
}
}

export function initOption(element: Element, props: Object) {
// value="" should make a value attribute (#6219)
if (props.value != null) {
element.setAttribute('value', toString(getToStringValue(props.value)));
}
}
29 changes: 11 additions & 18 deletions packages/react-dom/src/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

'use strict';

// Fix JSDOM. setAttribute is supposed to throw on things that can't be implicitly toStringed.
const setAttribute = Element.prototype.setAttribute;
Element.prototype.setAttribute = function (name, value) {
// eslint-disable-next-line react-internal/safe-string-coercion
return setAttribute.call(this, name, '' + value);
};

describe('ReactDOMSelect', () => {
let React;
let ReactDOM;
Expand Down Expand Up @@ -856,7 +849,7 @@ describe('ReactDOMSelect', () => {
});

describe('When given a Symbol value', () => {
it('treats initial Symbol value as missing', () => {
it('treats initial Symbol value as an empty string', () => {
let node;

expect(() => {
Expand All @@ -869,10 +862,10 @@ describe('ReactDOMSelect', () => {
);
}).toErrorDev('Invalid value for prop `value`');

expect(node.value).toBe('A Symbol!');
expect(node.value).toBe('');
});

it('treats updated Symbol value as missing', () => {
it('treats updated Symbol value as an empty string', () => {
let node;

expect(() => {
Expand All @@ -895,7 +888,7 @@ describe('ReactDOMSelect', () => {
</select>,
);

expect(node.value).toBe('A Symbol!');
expect(node.value).toBe('');
});

it('treats initial Symbol defaultValue as an empty string', () => {
Expand All @@ -911,7 +904,7 @@ describe('ReactDOMSelect', () => {
);
}).toErrorDev('Invalid value for prop `value`');

expect(node.value).toBe('A Symbol!');
expect(node.value).toBe('');
});

it('treats updated Symbol defaultValue as an empty string', () => {
Expand All @@ -937,12 +930,12 @@ describe('ReactDOMSelect', () => {
</select>,
);

expect(node.value).toBe('A Symbol!');
expect(node.value).toBe('');
});
});

describe('When given a function value', () => {
it('treats initial function value as missing', () => {
it('treats initial function value as an empty string', () => {
let node;

expect(() => {
Expand All @@ -955,7 +948,7 @@ describe('ReactDOMSelect', () => {
);
}).toErrorDev('Invalid value for prop `value`');

expect(node.value).toBe('A function!');
expect(node.value).toBe('');
});

it('treats initial function defaultValue as an empty string', () => {
Expand All @@ -971,7 +964,7 @@ describe('ReactDOMSelect', () => {
);
}).toErrorDev('Invalid value for prop `value`');

expect(node.value).toBe('A function!');
expect(node.value).toBe('');
});

it('treats updated function value as an empty string', () => {
Expand All @@ -997,7 +990,7 @@ describe('ReactDOMSelect', () => {
</select>,
);

expect(node.value).toBe('A function!');
expect(node.value).toBe('');
});

it('treats updated function defaultValue as an empty string', () => {
Expand All @@ -1023,7 +1016,7 @@ describe('ReactDOMSelect', () => {
</select>,
);

expect(node.value).toBe('A function!');
expect(node.value).toBe('');
});
});

Expand Down

0 comments on commit 8afbca1

Please sign in to comment.