Skip to content

Commit c47a9f4

Browse files
chore: Update AnimatedStyle __getValue to return an array of objects
1 parent 4dbf649 commit c47a9f4

File tree

2 files changed

+128
-59
lines changed

2 files changed

+128
-59
lines changed

Libraries/Animated/__tests__/Animated-test.js

Lines changed: 126 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,29 @@ describe('Animated tests', () => {
6262
);
6363

6464
expect(node.__getValue()).toEqual({
65-
style: {
66-
backgroundColor: 'red',
67-
opacity: 0,
68-
transform: [{translate: [100, 100]}, {translateX: 100}, {scale: 0}],
69-
shadowOffset: {
70-
width: 0,
71-
height: 0,
65+
style: [
66+
{
67+
backgroundColor: 'red',
68+
opacity: anim,
69+
shadowOffset: {
70+
width: anim,
71+
height: anim,
72+
},
73+
transform: [
74+
{translate: [translateAnim, translateAnim]},
75+
{translateX: translateAnim},
76+
{scale: anim},
77+
],
7278
},
73-
},
79+
{
80+
opacity: 0,
81+
transform: [{translate: [100, 100]}, {translateX: 100}, {scale: 0}],
82+
shadowOffset: {
83+
width: 0,
84+
height: 0,
85+
},
86+
},
87+
],
7488
});
7589

7690
expect(anim.__getChildren().length).toBe(0);
@@ -84,15 +98,33 @@ describe('Animated tests', () => {
8498
expect(callback).toBeCalled();
8599

86100
expect(node.__getValue()).toEqual({
87-
style: {
88-
backgroundColor: 'red',
89-
opacity: 0.5,
90-
transform: [{translate: [150, 150]}, {translateX: 150}, {scale: 0.5}],
91-
shadowOffset: {
92-
width: 0.5,
93-
height: 0.5,
101+
style: [
102+
{
103+
backgroundColor: 'red',
104+
opacity: anim,
105+
shadowOffset: {
106+
width: anim,
107+
height: anim,
108+
},
109+
transform: [
110+
{translate: [translateAnim, translateAnim]},
111+
{translateX: translateAnim},
112+
{scale: anim},
113+
],
94114
},
95-
},
115+
{
116+
opacity: 0.5,
117+
transform: [
118+
{translate: [150, 150]},
119+
{translateX: 150},
120+
{scale: 0.5},
121+
],
122+
shadowOffset: {
123+
width: 0.5,
124+
height: 0.5,
125+
},
126+
},
127+
],
96128
});
97129

98130
node.__detach();
@@ -173,15 +205,15 @@ describe('Animated tests', () => {
173205
<Animated.View style={{opacity}} />,
174206
);
175207

176-
expect(testRenderer.toJSON().props.style.opacity).toEqual(0);
208+
expect(testRenderer.toJSON().props.style[1].opacity).toEqual(0);
177209

178210
Animated.timing(opacity, {
179211
toValue: 1,
180212
duration: 0,
181213
useNativeDriver: false,
182214
}).start();
183215

184-
expect(testRenderer.toJSON().props.style.opacity).toEqual(1);
216+
expect(testRenderer.toJSON().props.style[1].opacity).toEqual(1);
185217
});
186218

187219
it('warns if `useNativeDriver` is missing', () => {
@@ -804,30 +836,40 @@ describe('Animated tests', () => {
804836
describe('Animated Vectors', () => {
805837
it('should animate vectors', () => {
806838
const vec = new Animated.ValueXY();
839+
const vecLayout = vec.getLayout();
840+
const opacity = vec.x.interpolate({
841+
inputRange: [0, 42],
842+
outputRange: [0.2, 0.8],
843+
});
807844

808845
const callback = jest.fn();
809846

810847
const node = new AnimatedProps(
811848
{
812849
style: {
813-
opacity: vec.x.interpolate({
814-
inputRange: [0, 42],
815-
outputRange: [0.2, 0.8],
816-
}),
850+
opacity,
817851
transform: vec.getTranslateTransform(),
818-
...vec.getLayout(),
852+
...vecLayout,
819853
},
820854
},
821855
callback,
822856
);
823857

824858
expect(node.__getValue()).toEqual({
825-
style: {
826-
opacity: 0.2,
827-
transform: [{translateX: 0}, {translateY: 0}],
828-
left: 0,
829-
top: 0,
830-
},
859+
style: [
860+
{
861+
top: vecLayout.top,
862+
left: vecLayout.left,
863+
opacity,
864+
transform: vec.getTranslateTransform(),
865+
},
866+
{
867+
opacity: 0.2,
868+
transform: [{translateX: 0}, {translateY: 0}],
869+
left: 0,
870+
top: 0,
871+
},
872+
],
831873
});
832874

833875
node.__attach();
@@ -839,12 +881,20 @@ describe('Animated tests', () => {
839881
expect(callback.mock.calls.length).toBe(2); // once each for x, y
840882

841883
expect(node.__getValue()).toEqual({
842-
style: {
843-
opacity: 0.8,
844-
transform: [{translateX: 42}, {translateY: 1492}],
845-
left: 42,
846-
top: 1492,
847-
},
884+
style: [
885+
{
886+
top: vecLayout.top,
887+
left: vecLayout.left,
888+
opacity,
889+
transform: vec.getTranslateTransform(),
890+
},
891+
{
892+
opacity: 0.8,
893+
transform: [{translateX: 42}, {translateY: 1492}],
894+
left: 42,
895+
top: 1492,
896+
},
897+
],
848898
});
849899

850900
node.__detach();
@@ -937,13 +987,22 @@ describe('Animated tests', () => {
937987
expect(listener.mock.calls.length).toBe(2);
938988
expect(listener).toBeCalledWith({value: 137});
939989
expect(view.__getValue()).toEqual({
940-
style: {
941-
transform: [
942-
{
943-
translateX: 137,
944-
},
945-
],
946-
},
990+
style: [
991+
{
992+
transform: [
993+
{
994+
translateX: value4,
995+
},
996+
],
997+
},
998+
{
999+
transform: [
1000+
{
1001+
translateX: 137,
1002+
},
1003+
],
1004+
},
1005+
],
9471006
});
9481007
value4.removeListener(id);
9491008
value1.setValue(40);
@@ -1011,17 +1070,18 @@ describe('Animated tests', () => {
10111070

10121071
it('should animate colors', () => {
10131072
const color = new Animated.Color({r: 255, g: 0, b: 0, a: 1.0});
1073+
const scale = color.a.interpolate({
1074+
inputRange: [0, 1],
1075+
outputRange: [1, 2],
1076+
});
10141077
const callback = jest.fn();
10151078
const node = new AnimatedProps(
10161079
{
10171080
style: {
10181081
backgroundColor: color,
10191082
transform: [
10201083
{
1021-
scale: color.a.interpolate({
1022-
inputRange: [0, 1],
1023-
outputRange: [1, 2],
1024-
}),
1084+
scale,
10251085
},
10261086
],
10271087
},
@@ -1030,10 +1090,16 @@ describe('Animated tests', () => {
10301090
);
10311091

10321092
expect(node.__getValue()).toEqual({
1033-
style: {
1034-
backgroundColor: 'rgba(255, 0, 0, 1)',
1035-
transform: [{scale: 2}],
1036-
},
1093+
style: [
1094+
{
1095+
backgroundColor: color,
1096+
transform: [{scale}],
1097+
},
1098+
{
1099+
backgroundColor: 'rgba(255, 0, 0, 1)',
1100+
transform: [{scale: 2}],
1101+
},
1102+
],
10371103
});
10381104

10391105
node.__attach();
@@ -1042,10 +1108,16 @@ describe('Animated tests', () => {
10421108
color.setValue({r: 11, g: 22, b: 33, a: 0.5});
10431109
expect(callback.mock.calls.length).toBe(4);
10441110
expect(node.__getValue()).toEqual({
1045-
style: {
1046-
backgroundColor: 'rgba(11, 22, 33, 0.5)',
1047-
transform: [{scale: 1.5}],
1048-
},
1111+
style: [
1112+
{
1113+
backgroundColor: color,
1114+
transform: [{scale}],
1115+
},
1116+
{
1117+
backgroundColor: 'rgba(11, 22, 33, 0.5)',
1118+
transform: [{scale: 1.5}],
1119+
},
1120+
],
10491121
});
10501122

10511123
node.__detach();

Libraries/Animated/nodes/AnimatedStyle.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ export default class AnimatedStyle extends AnimatedWithChildren {
6161
return updatedStyle;
6262
}
6363

64-
__getValue(): Object {
65-
return flattenStyle([
66-
this._inputStyle,
67-
this._walkStyleAndGetValues(this._style),
68-
]);
64+
__getValue(): Array<Object> {
65+
return [this._inputStyle, this._walkStyleAndGetValues(this._style)];
6966
}
7067

7168
// Recursively get animated values for nested styles (like iOS's shadowOffset)

0 commit comments

Comments
 (0)