Skip to content

Commit 82b7769

Browse files
Fix select value not marking option as selected
1 parent 6c72036 commit 82b7769

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
149149
continue;
150150
}
151151
}
152+
153+
if (name==='value') {
154+
if (nodeName==='select') {
155+
context = assign({}, context);
156+
context.__preactSelect__ = v;
157+
continue;
158+
}
159+
else if (context.__preactSelect__!==undefined && nodeName==='option' && context.__preactSelect__===v) {
160+
s += ` selected`;
161+
}
162+
}
152163
s += ` ${name}="${encodeEntities(v)}"`;
153164
}
154165
}

test/render.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,4 +730,42 @@ 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 not leak select value into parent context', () => {
745+
let fn = spy();
746+
function Bar(_, context) {
747+
fn(context);
748+
return null;
749+
}
750+
751+
class Foo extends Component {
752+
getChildContext() {
753+
return { foo: 'bar' };
754+
}
755+
756+
render() {
757+
return (
758+
<div>
759+
<select value="A">
760+
<option value="A">A</option>
761+
</select>
762+
<Bar />
763+
</div>
764+
);
765+
}
766+
}
767+
768+
render(<Foo />);
769+
expect(fn.args[0][0]).to.deep.equal({ foo: 'bar' });
770+
});
733771
});

0 commit comments

Comments
 (0)