Skip to content

Commit 7fa656d

Browse files
Cheng Louzpao
authored andcommitted
Don't let new keys on style from transferPropsTo override old ones
Previous behavior: `transferPropsTo(<div style={{color: 'red'}} />)` would get `color` overriden if we transfer in a `style={{color: 'blue'}}`. This is inconsistent with how other props are transfered. This simply reverses the order of arguments. closes facebook#1435
1 parent 3d605da commit 7fa656d

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/core/ReactPropTransferer.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ function createTransferStrategy(mergeStrategy) {
4040
};
4141
}
4242

43+
var transferStrategyMerge = createTransferStrategy(function(a, b) {
44+
// `merge` overrides the first object's (`props[key]` above) keys using the
45+
// second object's (`value`) keys. An object's style's existing `propA` would
46+
// get overridden. Flip the order here.
47+
return merge(b, a);
48+
});
49+
4350
/**
4451
* Transfer strategies dictate how props are transferred by `transferPropsTo`.
4552
* NOTE: if you add any more exceptions to this list you should be sure to
@@ -65,7 +72,7 @@ var TransferStrategies = {
6572
/**
6673
* Transfer the `style` prop (which is an object) by merging them.
6774
*/
68-
style: createTransferStrategy(merge)
75+
style: transferStrategyMerge
6976
};
7077

7178
/**

src/core/__tests__/ReactPropTransferer-test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('ReactPropTransferer', function() {
3737
return this.transferPropsTo(
3838
<input
3939
className="textinput"
40-
style={{display: 'block'}}
40+
style={{display: 'block', color: 'green'}}
4141
type="text"
4242
value=""
4343
/>
@@ -55,7 +55,7 @@ describe('ReactPropTransferer', function() {
5555
.toBeComponentOfType(React.DOM.input)
5656
.scalarPropsEqual({
5757
className: 'textinput',
58-
style: {display: 'block'},
58+
style: {display: 'block', color: 'green'},
5959
type: 'text',
6060
value: ''
6161
});
@@ -75,7 +75,7 @@ describe('ReactPropTransferer', function() {
7575
var instance =
7676
<TestComponent
7777
className="hidden_elem"
78-
style={{width: '100%'}}
78+
style={{width: '100%', display: 'none'}}
7979
/>;
8080
instance = ReactTestUtils.renderIntoDocument(instance);
8181

@@ -85,6 +85,7 @@ describe('ReactPropTransferer', function() {
8585
.scalarPropsEqual({
8686
className: 'textinput hidden_elem',
8787
style: {
88+
color: 'green',
8889
display: 'block',
8990
width: '100%'
9091
}

0 commit comments

Comments
 (0)