react-native-linear-gradient/index.android.js

104 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2016-11-08 23:05:34 +00:00
// @flow
import React, { Component } from 'react';
import PropTypes from 'prop-types';
2017-05-23 13:42:38 +00:00
import { processColor, requireNativeComponent, PointPropType, StyleSheet, View, ViewPropTypes } from 'react-native';
2016-12-26 17:53:00 +00:00
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];
2016-12-26 17:53:00 +00:00
}
return point;
};
2015-09-30 17:06:36 +00:00
2016-11-08 23:05:34 +00:00
type PropsType = {
2016-12-26 17:53:00 +00:00
start?: Array<number> | {x: number, y: number};
end?: Array<number> | {x: number, y: number};
2016-11-08 23:05:34 +00:00
colors: Array<string>;
locations?: Array<number>;
} & typeof(View);
export default class LinearGradient extends Component {
static propTypes = {
2016-12-26 17:53:00 +00:00
start: PropTypes.oneOfType([
PointPropType,
deprecatedPropType(
PropTypes.arrayOf(PropTypes.number),
'Use point object with {x, y} instead.'
)
]),
end: 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),
2017-05-23 13:42:38 +00:00
...ViewPropTypes,
};
2016-11-08 23:05:34 +00:00
props: PropsType;
gradientRef: any;
2016-11-08 23:05:34 +00:00
setNativeProps(props: PropsType) {
this.gradientRef.setNativeProps(props);
}
render() {
const {
children,
colors,
end,
locations,
start,
style,
...otherProps
} = this.props;
2015-09-30 17:06:36 +00:00
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 (
2016-11-08 23:05:34 +00:00
<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('start', start)}
endPoint={convertPoint('end', end)}
locations={locations ? locations.slice(0, colors.length) : null}
borderRadii={borderRadiiPerCorner}
/>
{ children }
</View>
);
}
}
const NativeLinearGradient = requireNativeComponent('BVLinearGradient', null);