Skip to content

Commit da5ce0f

Browse files
authored
fix: preserve component props when className.target is false (#226)
* test: add breaking test * fix: preserve left/right props on deepMergeConfig
1 parent 8f4acc5 commit da5ce0f

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Button as RNButton, type ButtonProps } from "react-native";
2+
3+
import { render } from "@testing-library/react-native";
4+
import { copyComponentProperties } from "react-native-css/components/copyComponentProperties";
5+
import { registerCSS, testID } from "react-native-css/jest";
6+
import { useCssElement } from "react-native-css/native";
7+
import type {
8+
StyledConfiguration,
9+
StyledProps,
10+
} from "react-native-css/runtime.types";
11+
12+
test("Component preserves props when mapping specifies 'target: false'", () => {
13+
registerCSS(`.sign-in { color: orange; }`);
14+
15+
const mapping: StyledConfiguration<typeof RNButton> = {
16+
className: {
17+
target: false,
18+
nativeStyleMapping: {
19+
color: "color",
20+
},
21+
},
22+
};
23+
24+
const Button = copyComponentProperties(
25+
RNButton,
26+
(props: StyledProps<ButtonProps, typeof mapping>) => {
27+
return useCssElement(RNButton, props, mapping);
28+
},
29+
);
30+
31+
const onPress = jest.fn();
32+
33+
const result = render(
34+
<Button
35+
testID={testID}
36+
className="sign-in"
37+
title="Sign In"
38+
onPress={onPress}
39+
/>,
40+
);
41+
42+
expect(result.getByText("Sign In")).toBeTruthy();
43+
44+
const renderedElement = result.getByTestId(testID);
45+
const renderedProps = renderedElement.props;
46+
47+
expect(renderedProps.testID).toBe(testID);
48+
expect(renderedProps).not.toHaveProperty("className");
49+
50+
// the <Text> element in the RN button
51+
// should apply an orange color to the element (because of the "sign-in" className)
52+
const titleElement = result.getByText("Sign In");
53+
expect(titleElement.props.style).toBeInstanceOf(Array);
54+
expect(titleElement.props.style).toHaveLength(2);
55+
expect(titleElement.props.style[1]).toEqual({ color: "#ffa500" });
56+
});

src/native/styles/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ function deepMergeConfig(
362362
result = Object.assign({}, left, right);
363363
}
364364
} else {
365-
result = { ...left };
365+
result = Object.assign({}, left, right);
366366
}
367367

368368
if (

0 commit comments

Comments
 (0)