Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update support for react native >= 0.54 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 81 additions & 82 deletions CornerLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,94 @@
* Copyright (c) 2016 react-native-component <[email protected]>
*/

import React, {
PropTypes,
Component,
} from 'react'
import {
StyleSheet,
View,
Text,
} from 'react-native'
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { StyleSheet, View, Text } from 'react-native';

const styles = StyleSheet.create({
container: {
position: 'absolute',
//transform: [{rotate: '45deg'}],
justifyContent: 'flex-end',
},
label: {
justifyContent: 'center',
alignItems: 'center',
},
text: {
color: '#fff',
// fontFamily: '.HelveticaNeueInterface-MediumP4',
fontSize: 12,
},
})
container: {
position: 'absolute',
// transform: [{rotate: '45deg'}],
justifyContent: 'flex-end',
},
label: {
justifyContent: 'center',
alignItems: 'center',
},
text: {
color: '#fff',
// fontFamily: '.HelveticaNeueInterface-MediumP4',
fontSize: 12,
},
});

export default class CornerLabel extends Component {
static defaultProps = {
alignment: 'left',
};

static defaultProps = {
alignment: 'left',
}

static propTypes = {
style: View.propTypes.style,
textStyle: Text.propTypes.style,
cornerRadius: PropTypes.number.isRequired,
alignment: PropTypes.oneOf([
'left',
'right',
])
}

// 构造
constructor (props) {
super(props)
// 初始状态
this.state = {}
static propTypes = {
style: PropTypes.object,
textStyle: PropTypes.object,
cornerRadius: PropTypes.number.isRequired,
alignment: PropTypes.oneOf(['left', 'right']),
};

this._labelHeight = Math.sqrt(Math.pow(props.cornerRadius, 2) / 2)
this._labelWidth = this._labelHeight * 2
let originOffset = Math.sqrt(Math.pow(this._labelHeight / 2, 2) / 2)
let labelHorizontalPosition = -this._labelWidth / 2 + originOffset
let labelVerticalPosition = - this._labelHeight / 2 + originOffset
if(props.alignment == 'left') {
this._labelPosition = {left : labelHorizontalPosition, top: labelVerticalPosition}
this._labelTransform = {transform: [{rotate: '-45deg'}]}
}
else {
this._labelPosition = {right : labelHorizontalPosition, top: labelVerticalPosition}
this._labelTransform = {transform: [{rotate: '45deg'}]}
}
// 构造
constructor(props) {
super(props);
// 初始状态
this.state = {};

this._labelHeight = Math.sqrt(Math.pow(props.cornerRadius, 2) / 2);
this._labelWidth = this._labelHeight * 2;
const originOffset = Math.sqrt(Math.pow(this._labelHeight / 2, 2) / 2);
const labelHorizontalPosition = -this._labelWidth / 2 + originOffset;
const labelVerticalPosition = -this._labelHeight / 2 + originOffset;
if (props.alignment == 'left') {
this._labelPosition = {
left: labelHorizontalPosition,
top: labelVerticalPosition,
};
this._labelTransform = { transform: [{ rotate: '-45deg' }] };
} else {
this._labelPosition = {
right: labelHorizontalPosition,
top: labelVerticalPosition,
};
this._labelTransform = { transform: [{ rotate: '45deg' }] };
}
}

render () {
return (
<View style={[styles.container,
this._labelPosition,
this._labelTransform,
{width: this._labelWidth, height: this._labelHeight, },
]}>
<View style={[styles.label,
{height: this._labelHeight},
this.props.style,
]}>
{this._renderChildren()}
</View>
</View>
)
}

_renderChildren () {
return React.Children.map(this.props.children, (child) => {
if (!React.isValidElement(child)) {
return <Text style={[styles.text, this.props.textStyle]}>{child}</Text>
}
return child
})
}
render() {
return (
<View
style={[
styles.container,
this._labelPosition,
this._labelTransform,
{ width: this._labelWidth, height: this._labelHeight },
]}
>
<View
style={[
styles.label,
{ height: this._labelHeight },
this.props.style,
]}
>
{this._renderChildren()}
</View>
</View>
);
}

}
_renderChildren() {
return React.Children.map(this.props.children, (child) => {
if (!React.isValidElement(child)) {
return <Text style={[styles.text, this.props.textStyle]}>{child}</Text>;
}
return child;
});
}
}