Skip to content

Commit 542ea41

Browse files
Pass select value as arg instead of via context
1 parent 82b7769 commit 542ea41

File tree

2 files changed

+4
-33
lines changed

2 files changed

+4
-33
lines changed

src/index.js

Lines changed: 4 additions & 5 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
}
@@ -152,11 +152,10 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
152152

153153
if (name==='value') {
154154
if (nodeName==='select') {
155-
context = assign({}, context);
156-
context.__preactSelect__ = v;
155+
selectValue = v;
157156
continue;
158157
}
159-
else if (context.__preactSelect__!==undefined && nodeName==='option' && context.__preactSelect__===v) {
158+
else if (nodeName==='option' && selectValue===v) {
160159
s += ` selected`;
161160
}
162161
}
@@ -194,7 +193,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
194193
let child = children[i];
195194
if (child!=null && child!==false) {
196195
let childSvgMode = nodeName==='svg' ? true : nodeName==='foreignObject' ? false : isSvgMode,
197-
ret = renderToString(child, context, opts, true, childSvgMode);
196+
ret = renderToString(child, context, opts, true, childSvgMode, selectValue);
198197
if (pretty && !hasLarge && isLargeString(ret)) hasLarge = true;
199198
if (ret) pieces.push(ret);
200199
}

test/render.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -740,32 +740,4 @@ describe('render', () => {
740740
);
741741
expect(res).to.equal('<select><option value="A">A</option><option selected value="B">B</option></select>');
742742
});
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-
});
771743
});

0 commit comments

Comments
 (0)