2015-04-14 05:10:00 +00:00
|
|
|
/**
|
|
|
|
* @providesModule LinearGradient
|
|
|
|
* @flow
|
|
|
|
*/
|
2017-06-29 12:50:59 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-05-23 13:42:38 +00:00
|
|
|
import { processColor, requireNativeComponent, PointPropType, View, ViewPropTypes } from 'react-native';
|
2016-12-07 07:15:02 +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.'
|
|
|
|
);
|
|
|
|
|
|
|
|
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),
|
2017-05-23 13:42:38 +00:00
|
|
|
...ViewPropTypes,
|
2016-06-13 11:22:34 +00:00
|
|
|
};
|
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}
|
2017-11-16 07:31:10 +00:00
|
|
|
startPoint={convertPoint('start', start)}
|
|
|
|
endPoint={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);
|