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

84 lines
2.2 KiB
JavaScript
Raw Normal View History

/**
* @providesModule LinearGradient
* @flow
*/
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);
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.'
)
]),
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);
}
render() {
const {
2016-12-07 07:15:02 +00:00
start,
end,
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');
}
return (
<NativeLinearGradient
2016-11-08 23:05:34 +00:00
ref={(component) => { this.gradientRef = component; }}
{...otherProps}
2016-12-07 07:15:02 +00:00
start={convertPoint('start', start)}
end={convertPoint('end', end)}
colors={colors.map(processColor)}
locations={locations ? locations.slice(0, colors.length) : null}
/>
);
2015-09-29 16:43:22 +00:00
}
}
const NativeLinearGradient = requireNativeComponent('BVLinearGradient', null);