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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ describe('ReactDOMSelect', () => {
</select>,
);
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'`value` prop on `select` should not be null. ' +
'Consider using an empty string to clear the component or `undefined` ' +
Expand All @@ -557,6 +558,34 @@ describe('ReactDOMSelect', () => {
}
});

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

ReactTestUtils.renderIntoDocument(
watadarkstar marked this conversation as resolved.
Show resolved Hide resolved
<select>
<option selected={true} />
<option selected={true} />
</select>,
);
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
}

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

it('should warn if value is null and multiple is true', () => {
spyOnDev(console, 'error');
ReactTestUtils.renderIntoDocument(
Expand Down
15 changes: 10 additions & 5 deletions 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 @@ -36,11 +38,14 @@ function flattenChildren(children) {
export function validateProps(element: Element, props: Object) {
// TODO (yungsters): Remove support for `selected` in <option>.
if (__DEV__) {
warning(
props.selected == null,
'Use the `defaultValue` or `value` props on <select> instead of ' +
'setting `selected` on <option>.',
);
if (!didWarnSelectedSetOnOption) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually I think this is wrong. This means we only check once, not warn once.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fixed in abe0faf.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woah good catch!

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

Expand Down