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

Deduplication of warn when selected is set on <option> #11821

Merged
merged 5 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,25 @@ describe('ReactDOMSelect', () => {
}
});

it('should warn if selected is set on <option>', () => {
spyOnDev(console, 'error');

ReactTestUtils.renderIntoDocument(
<select>
<option selected={true} />
<option selected={true} />
</select>,
);
if (__DEV__) {
expect(console.error.calls.argsFor(0)[0]).toContain(
'Use the `defaultValue` or `value` props on <select> instead of ' +
'setting `selected` on <option>.',
);
// Test deduplication
expect(console.error.calls.count()).toBe(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this assertion first. If it's 0, the previous line would throw with a weird error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
});

it('should warn if value is null and multiple is true', () => {
spyOnDev(console, 'error');
ReactTestUtils.renderIntoDocument(
Expand Down
5 changes: 4 additions & 1 deletion packages/react-dom/src/client/ReactDOMFiberOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import React from 'react';
import warning from 'fbjs/lib/warning';

let didWarnSelectedSetOnOption = false;

function flattenChildren(children) {
let content = '';

Expand All @@ -35,12 +37,13 @@ function flattenChildren(children) {

export function validateProps(element: Element, props: Object) {
// TODO (yungsters): Remove support for `selected` in <option>.
if (__DEV__) {
if (__DEV__ && !didWarnSelectedSetOnOption) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally try to avoid __DEV__ && ... because it's not always obvious what's static and what's dynamic.
Could you please just use nested ifs?

warning(
props.selected == null,
'Use the `defaultValue` or `value` props on <select> instead of ' +
'setting `selected` on <option>.',
);
didWarnSelectedSetOnOption = true;
}
}

Expand Down