From c292f98af7131d4f0c0adfe0477de6d46c784d15 Mon Sep 17 00:00:00 2001 From: fattahmuhyiddeen Date: Tue, 15 May 2018 14:10:51 +0800 Subject: [PATCH] Update CornerLabel.js --- CornerLabel.js | 163 ++++++++++++++++++++++++------------------------- 1 file changed, 81 insertions(+), 82 deletions(-) diff --git a/CornerLabel.js b/CornerLabel.js index f1adbde..df02931 100644 --- a/CornerLabel.js +++ b/CornerLabel.js @@ -5,95 +5,94 @@ * Copyright (c) 2016 react-native-component */ -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 ( - - - {this._renderChildren()} - - - ) - } - - _renderChildren () { - return React.Children.map(this.props.children, (child) => { - if (!React.isValidElement(child)) { - return {child} - } - return child - }) - } + render() { + return ( + + + {this._renderChildren()} + + + ); + } -} \ No newline at end of file + _renderChildren() { + return React.Children.map(this.props.children, (child) => { + if (!React.isValidElement(child)) { + return {child}; + } + return child; + }); + } +}