From b102e256634ce7973eee6695e9aa8cb46ad14e42 Mon Sep 17 00:00:00 2001 From: Trevor Smith Date: Thu, 7 Apr 2016 16:30:51 -0600 Subject: [PATCH 1/3] Correctly select options when nested inside an optgroup --- .../dom/client/wrappers/ReactDOMOption.js | 12 ++++++++++-- .../wrappers/__tests__/ReactDOMSelect-test.js | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/renderers/dom/client/wrappers/ReactDOMOption.js b/src/renderers/dom/client/wrappers/ReactDOMOption.js index 917ed0ee4a167..a7e479d7e7780 100644 --- a/src/renderers/dom/client/wrappers/ReactDOMOption.js +++ b/src/renderers/dom/client/wrappers/ReactDOMOption.js @@ -32,8 +32,16 @@ var ReactDOMOption = { // Look up whether this option is 'selected' var selectValue = null; - if (nativeParent != null && nativeParent._tag === 'select') { - selectValue = ReactDOMSelect.getSelectValueContext(nativeParent); + if (nativeParent != null) { + var selectParent = nativeParent; + + if (selectParent._tag === 'optgroup') { + selectParent = selectParent._nativeParent; + } + + if (selectParent._tag === 'select') { + selectValue = ReactDOMSelect.getSelectValueContext(selectParent); + } } // If the value is null (e.g., no specified value or after initial mount) diff --git a/src/renderers/dom/client/wrappers/__tests__/ReactDOMSelect-test.js b/src/renderers/dom/client/wrappers/__tests__/ReactDOMSelect-test.js index 97736bc99a552..406c0306a24fa 100644 --- a/src/renderers/dom/client/wrappers/__tests__/ReactDOMSelect-test.js +++ b/src/renderers/dom/client/wrappers/__tests__/ReactDOMSelect-test.js @@ -537,4 +537,22 @@ describe('ReactDOMSelect', function() { "Cannot set property 'pendingUpdate' of null" ); }); + + it('should select grandchild options nested inside an optgroup', function() { + var stub = + ; + var container = document.createElement('div'); + stub = ReactDOM.render(stub, container); + var node = ReactDOM.findDOMNode(stub); + + expect(node.options[0].selected).toBe(false); // a + expect(node.options[1].selected).toBe(true); // b + expect(node.options[2].selected).toBe(false); // c + }); }); From 8577600a61a0d2e4712f86782a43daa5ce385e84 Mon Sep 17 00:00:00 2001 From: Trevor Smith Date: Thu, 7 Apr 2016 16:42:50 -0600 Subject: [PATCH 2/3] Add a null check when checking for the native parent of an option element. --- src/renderers/dom/client/wrappers/ReactDOMOption.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderers/dom/client/wrappers/ReactDOMOption.js b/src/renderers/dom/client/wrappers/ReactDOMOption.js index a7e479d7e7780..221f8fc00433f 100644 --- a/src/renderers/dom/client/wrappers/ReactDOMOption.js +++ b/src/renderers/dom/client/wrappers/ReactDOMOption.js @@ -39,7 +39,7 @@ var ReactDOMOption = { selectParent = selectParent._nativeParent; } - if (selectParent._tag === 'select') { + if (selectParent != null && selectParent._tag === 'select') { selectValue = ReactDOMSelect.getSelectValueContext(selectParent); } } From f7181e089cf3047c69e1c7ecf313af0f88b70d8b Mon Sep 17 00:00:00 2001 From: Trevor Smith Date: Thu, 7 Apr 2016 16:43:35 -0600 Subject: [PATCH 3/3] Remove an unnecessary call to findDOMNode in the select optgroup test. --- .../dom/client/wrappers/__tests__/ReactDOMSelect-test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/renderers/dom/client/wrappers/__tests__/ReactDOMSelect-test.js b/src/renderers/dom/client/wrappers/__tests__/ReactDOMSelect-test.js index 406c0306a24fa..0a3f53028314f 100644 --- a/src/renderers/dom/client/wrappers/__tests__/ReactDOMSelect-test.js +++ b/src/renderers/dom/client/wrappers/__tests__/ReactDOMSelect-test.js @@ -548,8 +548,7 @@ describe('ReactDOMSelect', function() { ; var container = document.createElement('div'); - stub = ReactDOM.render(stub, container); - var node = ReactDOM.findDOMNode(stub); + var node = ReactDOM.render(stub, container); expect(node.options[0].selected).toBe(false); // a expect(node.options[1].selected).toBe(true); // b