2015-04-14 05:10:00 +00:00
|
|
|
/**
|
|
|
|
* @providesModule LinearGradient
|
|
|
|
* @flow
|
|
|
|
*/
|
2016-06-13 11:22:34 +00:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-12-07 07:15:02 +00:00
|
|
|
import { processColor, requireNativeComponent, PointPropType, View } from 'react-native';
|
|
|
|
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.'
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: point[0],
|
|
|
|
y: point[1]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return point;
|
|
|
|
};
|
2015-09-29 16:43:22 +00:00
|
|
|
|
2016-11-08 23:05:34 +00:00
|
|
|
type PropsType = {
|
2016-12-07 07:15:02 +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);
|
|
|
|
|
2016-06-13 11:22:34 +00:00
|
|
|
export default class LinearGradient extends Component {
|
|
|
|
static propTypes = {
|
2016-12-07 07:15:02 +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.'
|
|
|
|
)
|
|
|
|
]),
|
2016-06-13 11:22:34 +00:00
|
|
|
colors: PropTypes.arrayOf(PropTypes.string).isRequired,
|
|
|
|
locations: PropTypes.arrayOf(PropTypes.number),
|
|
|
|
...View.propTypes,
|
|
|
|
};
|
2016-11-08 23:05:34 +00:00
|
|
|
props: PropsType;
|
|
|
|
gradientRef: any;
|
2016-06-12 20:21:55 +00:00
|
|
|
|
2016-11-08 23:05:34 +00:00
|
|
|
setNativeProps(props: PropsType) {
|
|
|
|
this.gradientRef.setNativeProps(props);
|
2016-10-25 17:59:39 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 11:22:34 +00:00
|
|
|
render() {
|
|
|
|
const {
|
2016-12-07 07:15:02 +00:00
|
|
|
start,
|
|
|
|
end,
|
2016-06-13 11:22:34 +00:00
|
|
|
colors,
|
|
|
|
locations,
|
|
|
|
...otherProps
|
|
|
|
} = this.props;
|
|
|
|
if ((colors && locations) && (colors.length !== locations.length)) {
|
|
|
|
console.warn('LinearGradient colors and locations props should be arrays of the same length');
|
|
|
|
}
|
2016-05-25 09:56:09 +00:00
|
|
|
|
2016-06-13 11:22:34 +00:00
|
|
|
return (
|
|
|
|
<NativeLinearGradient
|
2016-11-08 23:05:34 +00:00
|
|
|
ref={(component) => { this.gradientRef = component; }}
|
2016-06-13 11:22:34 +00:00
|
|
|
{...otherProps}
|
2016-12-07 07:15:02 +00:00
|
|
|
start={convertPoint('start', start)}
|
|
|
|
end={convertPoint('end', end)}
|
2016-06-13 11:22:34 +00:00
|
|
|
colors={colors.map(processColor)}
|
|
|
|
locations={locations ? locations.slice(0, colors.length) : null}
|
|
|
|
/>
|
|
|
|
);
|
2015-09-29 16:43:22 +00:00
|
|
|
}
|
2016-06-06 14:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const NativeLinearGradient = requireNativeComponent('BVLinearGradient', null);
|