Skip to content

Commit 8766c14

Browse files
authored
Merge pull request #5 from dimaportenko/issues-2
fix: issue-2 and add tests
2 parents 9f169b0 + 6300989 commit 8766c14

File tree

4 files changed

+110
-10
lines changed

4 files changed

+110
-10
lines changed

example/src/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ const styles = StyleSheet.create({
119119
shadowRadius: 4.65,
120120
},
121121
shadow6: {
122-
shadowColor: '#000',
123122
shadowOffset: {
124123
width: 2,
125124
height: 4,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dimaportenko/react-native-shadow-view",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "React Native library with Android native view which supports same shadows styles as iOS ",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/__tests__/index.test.tsx

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,88 @@
1-
it.todo('write a test');
1+
import { getShadowPropsFromStyle } from '../';
2+
3+
describe('getShadowPropsFromStyle function test', () => {
4+
const emptyProps = {
5+
shadowColor: undefined,
6+
shadowOffset: undefined,
7+
shadowOpacity: undefined,
8+
shadowRadius: undefined,
9+
};
10+
const pinkColorInDecemal = 4294951115;
11+
const blackColorInDecemal = 4278190080;
12+
13+
test('empty styles return empty object', () => {
14+
const result = getShadowPropsFromStyle({});
15+
expect(result).toStrictEqual(emptyProps);
16+
});
17+
18+
test('non shadow properties returns empty object', () => {
19+
const result = getShadowPropsFromStyle({
20+
width: 100,
21+
backgroundColor: 'blue',
22+
});
23+
expect(result).toStrictEqual(emptyProps);
24+
});
25+
26+
test('shadow styles return shadow properties', () => {
27+
const result = getShadowPropsFromStyle({
28+
shadowColor: 'pink',
29+
shadowOffset: {
30+
width: -4,
31+
height: 4,
32+
},
33+
shadowOpacity: 1,
34+
shadowRadius: 4.65,
35+
});
36+
expect(result).toStrictEqual({
37+
shadowColor: pinkColorInDecemal,
38+
shadowOffset: {
39+
width: -4,
40+
height: 4,
41+
},
42+
shadowOpacity: 1,
43+
shadowRadius: 4.65,
44+
});
45+
});
46+
47+
test('return black shadow property if not set but shadowOffset is presented', () => {
48+
const result = getShadowPropsFromStyle({
49+
shadowOffset: {
50+
width: -4,
51+
height: 4,
52+
},
53+
});
54+
expect(result).toStrictEqual({
55+
shadowColor: blackColorInDecemal,
56+
shadowOffset: {
57+
width: -4,
58+
height: 4,
59+
},
60+
shadowOpacity: undefined,
61+
shadowRadius: undefined,
62+
});
63+
});
64+
65+
test('return black shadow property if not set but shadowOffset is presented', () => {
66+
const result = getShadowPropsFromStyle({
67+
shadowOpacity: 1,
68+
});
69+
expect(result).toStrictEqual({
70+
shadowColor: blackColorInDecemal,
71+
shadowOffset: undefined,
72+
shadowOpacity: 1,
73+
shadowRadius: undefined,
74+
});
75+
});
76+
77+
test('return black shadow property if not set but shadowRadius is presented', () => {
78+
const result = getShadowPropsFromStyle({
79+
shadowRadius: 4,
80+
});
81+
expect(result).toStrictEqual({
82+
shadowColor: blackColorInDecemal,
83+
shadowOffset: undefined,
84+
shadowOpacity: undefined,
85+
shadowRadius: 4,
86+
});
87+
});
88+
});

src/index.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
StyleSheet,
77
ViewProps,
88
View,
9+
ViewStyle,
910
} from 'react-native';
1011
import ShadowComponent from './nativeComponent';
1112

@@ -30,6 +31,24 @@ export type ShadowProps =
3031

3132
type Props = ViewProps;
3233

34+
export const getShadowPropsFromStyle = (flattenStyles: ViewStyle) => {
35+
if (
36+
(flattenStyles.shadowOffset ||
37+
flattenStyles.shadowOpacity ||
38+
flattenStyles.shadowRadius) &&
39+
!flattenStyles.shadowColor
40+
) {
41+
flattenStyles.shadowColor = '#000000';
42+
}
43+
const shadowStyle: ShadowProps = {
44+
shadowColor: processColor(flattenStyles.shadowColor),
45+
shadowOffset: flattenStyles.shadowOffset,
46+
shadowOpacity: flattenStyles.shadowOpacity,
47+
shadowRadius: flattenStyles.shadowRadius,
48+
};
49+
return shadowStyle;
50+
};
51+
3352
export const ShadowView: FC<Props> = ({ children, style, ...otherProps }) => {
3453
if (Platform.OS !== 'android') {
3554
return (
@@ -38,13 +57,8 @@ export const ShadowView: FC<Props> = ({ children, style, ...otherProps }) => {
3857
</View>
3958
);
4059
}
41-
const flattenStyles = StyleSheet.flatten(style);
42-
const shadowStyle: ShadowProps = {
43-
shadowColor: processColor(flattenStyles.shadowColor),
44-
shadowOffset: flattenStyles.shadowOffset,
45-
shadowOpacity: flattenStyles.shadowOpacity,
46-
shadowRadius: flattenStyles.shadowRadius,
47-
};
60+
const flattenStyles = StyleSheet.flatten(style ?? {});
61+
const shadowStyle: ShadowProps = getShadowPropsFromStyle(flattenStyles);
4862

4963
if (
5064
flattenStyles.borderRadius ||

0 commit comments

Comments
 (0)