Skip to content

Commit 51e3f49

Browse files
watadarkstargaearon
authored andcommitted
Deduplication of warn when selected is set on <option> (#11821)
* Deduplication of warn selected on option - Wrote a failing test - Deduplication when selected is set on option * Ran yarn preitter * Fixed PR request - Moved dedupe test to above - Moved && case to seperate if to seperate static and dynamic things - Render'd component twice * Actually check for deduplication * Minor nits
1 parent f23dd71 commit 51e3f49

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ describe('ReactDOMSelect', () => {
540540
</select>,
541541
);
542542
if (__DEV__) {
543+
expect(console.error.calls.count()).toBe(1);
543544
expect(console.error.calls.argsFor(0)[0]).toContain(
544545
'`value` prop on `select` should not be null. ' +
545546
'Consider using an empty string to clear the component or `undefined` ' +
@@ -557,6 +558,34 @@ describe('ReactDOMSelect', () => {
557558
}
558559
});
559560

561+
it('should warn if selected is set on <option>', () => {
562+
spyOnDev(console, 'error');
563+
564+
ReactTestUtils.renderIntoDocument(
565+
<select>
566+
<option selected={true} />
567+
<option selected={true} />
568+
</select>,
569+
);
570+
if (__DEV__) {
571+
expect(console.error.calls.count()).toBe(1);
572+
}
573+
574+
ReactTestUtils.renderIntoDocument(
575+
<select>
576+
<option selected={true} />
577+
<option selected={true} />
578+
</select>,
579+
);
580+
if (__DEV__) {
581+
expect(console.error.calls.count()).toBe(1);
582+
expect(console.error.calls.argsFor(0)[0]).toContain(
583+
'Use the `defaultValue` or `value` props on <select> instead of ' +
584+
'setting `selected` on <option>.',
585+
);
586+
}
587+
});
588+
560589
it('should warn if value is null and multiple is true', () => {
561590
spyOnDev(console, 'error');
562591
ReactTestUtils.renderIntoDocument(

packages/react-dom/src/client/ReactDOMFiberOption.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import React from 'react';
1111
import warning from 'fbjs/lib/warning';
1212

13+
let didWarnSelectedSetOnOption = false;
14+
1315
function flattenChildren(children) {
1416
let content = '';
1517

@@ -36,11 +38,14 @@ function flattenChildren(children) {
3638
export function validateProps(element: Element, props: Object) {
3739
// TODO (yungsters): Remove support for `selected` in <option>.
3840
if (__DEV__) {
39-
warning(
40-
props.selected == null,
41-
'Use the `defaultValue` or `value` props on <select> instead of ' +
42-
'setting `selected` on <option>.',
43-
);
41+
if (!didWarnSelectedSetOnOption) {
42+
warning(
43+
props.selected == null,
44+
'Use the `defaultValue` or `value` props on <select> instead of ' +
45+
'setting `selected` on <option>.',
46+
);
47+
didWarnSelectedSetOnOption = true;
48+
}
4449
}
4550
}
4651

0 commit comments

Comments
 (0)