-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.android.js
119 lines (109 loc) · 3.53 KB
/
index.android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// @flow
import React, { Component } from "react";
import PropTypes from "prop-types";
import {
processColor,
requireNativeComponent,
PointPropType,
StyleSheet,
View,
ViewPropTypes
} from "react-native";
import type { ViewProps } from "react-native/Libraries/Components/View/ViewPropTypes";
const deprecatedPropType = require("react-native/Libraries/Utilities/deprecatedPropType.js");
const convertPoint = (name, point) => {
if (Array.isArray(point)) {
console.warn(
`LinearGradient '${name}' property shoule be an object with fields 'x' and 'y', ` +
"Array type is deprecated."
);
}
if (point !== null && typeof point === "object") {
return [point.x, point.y];
}
return point;
};
type PropsType = {
startPoint?: Array<number> | { x: number, y: number },
endPoint?: Array<number> | { x: number, y: number },
colors: Array<string>,
locations?: Array<number>
} & ViewProps;
export default class LinearGradient extends Component {
static propTypes = {
startPoint: PropTypes.oneOfType([
PointPropType,
deprecatedPropType(
PropTypes.arrayOf(PropTypes.number),
"Use point object with {x, y} instead."
)
]),
endPoint: PropTypes.oneOfType([
PointPropType,
deprecatedPropType(
PropTypes.arrayOf(PropTypes.number),
"Use point object with {x, y} instead."
)
]),
colors: PropTypes.arrayOf(PropTypes.string).isRequired,
locations: PropTypes.arrayOf(PropTypes.number),
...ViewPropTypes
};
props: PropsType;
gradientRef: any;
setNativeProps(props: PropsType) {
this.gradientRef.setNativeProps(props);
}
render() {
const {
children,
colors,
endPoint,
locations,
startPoint,
style,
...otherProps
} = this.props;
if (colors && locations && colors.length !== locations.length) {
console.warn(
"LinearGradient colors and locations props should be arrays of the same length"
);
}
// inherit container borderRadius until this issue is resolved:
// https://github.com/facebook/react-native/issues/3198
const flatStyle = StyleSheet.flatten(style) || {};
const borderRadius = flatStyle.borderRadius || 0;
// this is the format taken by:
// http://developer.android.com/reference/android/graphics/Path.html#addRoundRect(android.graphics.RectF, float[], android.graphics.Path.Direction)
const borderRadiiPerCorner = [
flatStyle.borderTopLeftRadius || borderRadius,
flatStyle.borderTopLeftRadius || borderRadius,
flatStyle.borderTopRightRadius || borderRadius,
flatStyle.borderTopRightRadius || borderRadius,
flatStyle.borderBottomRightRadius || borderRadius,
flatStyle.borderBottomRightRadius || borderRadius,
flatStyle.borderBottomLeftRadius || borderRadius,
flatStyle.borderBottomLeftRadius || borderRadius
];
return (
<View
ref={component => {
this.gradientRef = component;
}}
{...otherProps}
style={style}
>
<NativeLinearGradient
style={{ position: "absolute", top: 0, left: 0, bottom: 0, right: 0 }}
colors={colors.map(processColor)}
startPoint={convertPoint("startPoint", startPoint)}
endPoint={convertPoint("endPoint", endPoint)}
locations={locations ? locations.slice(0, colors.length) : null}
borderRadii={borderRadiiPerCorner}
/>
{children}
</View>
);
}
}
const NativeLinearGradient = requireNativeComponent("BVLinearGradient", null);