Skip to content

Commit e244e6a

Browse files
Merge pull request #85 from developit/fix_select_value
Fix select value not marking option as selected
2 parents 6c72036 + d447826 commit e244e6a

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);
3434

3535

3636
/** The default export is an alias of `render()`. */
37-
function renderToString(vnode, context, opts, inner, isSvgMode) {
37+
function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
3838
if (vnode==null || typeof vnode==='boolean') {
3939
return '';
4040
}
@@ -65,7 +65,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
6565
getChildren(children, vnode.props.children);
6666

6767
for (let i = 0; i < children.length; i++) {
68-
rendered += renderToString(children[i], context, opts, opts.shallowHighOrder!==false, isSvgMode);
68+
rendered += renderToString(children[i], context, opts, opts.shallowHighOrder!==false, isSvgMode, selectValue);
6969
}
7070
return rendered;
7171
}
@@ -97,7 +97,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
9797
context = assign(assign({}, context), c.getChildContext());
9898
}
9999

100-
return renderToString(rendered, context, opts, opts.shallowHighOrder!==false);
100+
return renderToString(rendered, context, opts, opts.shallowHighOrder!==false, isSvgMode, selectValue);
101101
}
102102
}
103103

@@ -149,6 +149,16 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
149149
continue;
150150
}
151151
}
152+
153+
if (name==='value') {
154+
if (nodeName==='select') {
155+
selectValue = v;
156+
continue;
157+
}
158+
else if (nodeName==='option' && selectValue==v) {
159+
s += ` selected`;
160+
}
161+
}
152162
s += ` ${name}="${encodeEntities(v)}"`;
153163
}
154164
}
@@ -183,7 +193,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
183193
let child = children[i];
184194
if (child!=null && child!==false) {
185195
let childSvgMode = nodeName==='svg' ? true : nodeName==='foreignObject' ? false : isSvgMode,
186-
ret = renderToString(child, context, opts, true, childSvgMode);
196+
ret = renderToString(child, context, opts, true, childSvgMode, selectValue);
187197
if (pretty && !hasLarge && isLargeString(ret)) hasLarge = true;
188198
if (ret) pieces.push(ret);
189199
}

test/render.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,4 +730,52 @@ describe('render', () => {
730730
expect(called).to.equal(false);
731731
});
732732
});
733+
734+
it('should render select value on option', () => {
735+
let res = render(
736+
<select value="B">
737+
<option value="A">A</option>
738+
<option value="B">B</option>
739+
</select>
740+
);
741+
expect(res).to.equal('<select><option value="A">A</option><option selected value="B">B</option></select>');
742+
});
743+
744+
it('should render select value on option with a Fragment', () => {
745+
let res = render(
746+
<select value="B">
747+
<Fragment>
748+
<option value="A">A</option>
749+
<option value="B">B</option>
750+
</Fragment>
751+
</select>
752+
);
753+
expect(res).to.equal('<select><option value="A">A</option><option selected value="B">B</option></select>');
754+
});
755+
756+
it('should render select value on option through a component', () => {
757+
function Foo() {
758+
return (
759+
<optgroup label="foo">
760+
<option value="A">A</option>
761+
<option value="B">B</option>
762+
</optgroup>
763+
);
764+
}
765+
let res = render(
766+
<select value="B">
767+
<Foo />
768+
</select>
769+
);
770+
expect(res).to.equal('<select><optgroup label="foo"><option value="A">A</option><option selected value="B">B</option></optgroup></select>');
771+
});
772+
773+
it('should render select value with loose equality', () => {
774+
let res = render(
775+
<select value={2}>
776+
<option value="2">2</option>
777+
</select>
778+
);
779+
expect(res).to.equal('<select><option selected value="2">2</option></select>');
780+
});
733781
});

0 commit comments

Comments
 (0)