Skip to content

Commit 8afbca1

Browse files
committed
Revert "Remove initOption special case (facebook#26595)"
This reverts commit 343a45f.
1 parent 1c90108 commit 8afbca1

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

packages/react-dom-bindings/src/client/ReactDOMComponent.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
updateInput,
3333
restoreControlledInputState,
3434
} from './ReactDOMInput';
35-
import {validateOptionProps} from './ReactDOMOption';
35+
import {initOption, validateOptionProps} from './ReactDOMOption';
3636
import {
3737
validateSelectProps,
3838
initSelect,
@@ -995,6 +995,7 @@ export function setInitialProperties(
995995
}
996996
}
997997
}
998+
initOption(domElement, props);
998999
return;
9991000
}
10001001
case 'dialog': {

packages/react-dom-bindings/src/client/ReactDOMOption.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import {Children} from 'react';
11+
import {getToStringValue, toString} from './ToStringValue';
1112

1213
let didWarnSelectedSetOnOption = false;
1314
let didWarnInvalidChild = false;
@@ -58,3 +59,10 @@ export function validateOptionProps(element: Element, props: Object) {
5859
}
5960
}
6061
}
62+
63+
export function initOption(element: Element, props: Object) {
64+
// value="" should make a value attribute (#6219)
65+
if (props.value != null) {
66+
element.setAttribute('value', toString(getToStringValue(props.value)));
67+
}
68+
}

packages/react-dom/src/__tests__/ReactDOMSelect-test.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99

1010
'use strict';
1111

12-
// Fix JSDOM. setAttribute is supposed to throw on things that can't be implicitly toStringed.
13-
const setAttribute = Element.prototype.setAttribute;
14-
Element.prototype.setAttribute = function (name, value) {
15-
// eslint-disable-next-line react-internal/safe-string-coercion
16-
return setAttribute.call(this, name, '' + value);
17-
};
18-
1912
describe('ReactDOMSelect', () => {
2013
let React;
2114
let ReactDOM;
@@ -856,7 +849,7 @@ describe('ReactDOMSelect', () => {
856849
});
857850

858851
describe('When given a Symbol value', () => {
859-
it('treats initial Symbol value as missing', () => {
852+
it('treats initial Symbol value as an empty string', () => {
860853
let node;
861854

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

872-
expect(node.value).toBe('A Symbol!');
865+
expect(node.value).toBe('');
873866
});
874867

875-
it('treats updated Symbol value as missing', () => {
868+
it('treats updated Symbol value as an empty string', () => {
876869
let node;
877870

878871
expect(() => {
@@ -895,7 +888,7 @@ describe('ReactDOMSelect', () => {
895888
</select>,
896889
);
897890

898-
expect(node.value).toBe('A Symbol!');
891+
expect(node.value).toBe('');
899892
});
900893

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

914-
expect(node.value).toBe('A Symbol!');
907+
expect(node.value).toBe('');
915908
});
916909

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

940-
expect(node.value).toBe('A Symbol!');
933+
expect(node.value).toBe('');
941934
});
942935
});
943936

944937
describe('When given a function value', () => {
945-
it('treats initial function value as missing', () => {
938+
it('treats initial function value as an empty string', () => {
946939
let node;
947940

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

958-
expect(node.value).toBe('A function!');
951+
expect(node.value).toBe('');
959952
});
960953

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

974-
expect(node.value).toBe('A function!');
967+
expect(node.value).toBe('');
975968
});
976969

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

1000-
expect(node.value).toBe('A function!');
993+
expect(node.value).toBe('');
1001994
});
1002995

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

1026-
expect(node.value).toBe('A function!');
1019+
expect(node.value).toBe('');
10271020
});
10281021
});
10291022

0 commit comments

Comments
 (0)