|
| 1 | +/** |
| 2 | + * Created by TinySymphony on 2017-05-11. |
| 3 | + */ |
| 4 | + |
| 5 | +import React, {PropTypes, Component} from 'react'; |
| 6 | +import { |
| 7 | + View, |
| 8 | + Text, |
| 9 | + StyleSheet, |
| 10 | + TouchableHighlight |
| 11 | +} from 'react-native'; |
| 12 | +import Moment from 'moment'; |
| 13 | +import styles from './style'; |
| 14 | + |
| 15 | +export default class Day extends Component { |
| 16 | + static propTypes = { |
| 17 | + onChoose: PropTypes.func |
| 18 | + } |
| 19 | + constructor (props) { |
| 20 | + super(props); |
| 21 | + this._chooseDay = this._chooseDay.bind(this); |
| 22 | + this._statusCheck = this._statusCheck.bind(this); |
| 23 | + this._statusCheck(); |
| 24 | + } |
| 25 | + _chooseDay () { |
| 26 | + this.props.onChoose && this.props.onChoose(this.props.date); |
| 27 | + } |
| 28 | + _statusCheck (props) { |
| 29 | + const { |
| 30 | + startDate, |
| 31 | + endDate, |
| 32 | + today, |
| 33 | + date = null, |
| 34 | + minDate, |
| 35 | + maxDate, |
| 36 | + empty |
| 37 | + } = props || this.props; |
| 38 | + this.isToday = today.isSame(date, 'd'); |
| 39 | + this.isValid = date && |
| 40 | + (date >= minDate || date.isSame(minDate, 'd')) && |
| 41 | + (date <= maxDate || date.isSame(maxDate, 'd')); |
| 42 | + this.isMid = date > startDate && date < endDate || |
| 43 | + (!date && empty >= startDate && empty <= endDate); |
| 44 | + this.isStart = date && date.isSame(startDate, 'd'); |
| 45 | + this.isStartPart = this.isStart && endDate; |
| 46 | + this.isEnd = date && date.isSame(endDate, 'd'); |
| 47 | + this.isFocus = this.isMid || this.isStart || this.isEnd; |
| 48 | + return this.isFocus; |
| 49 | + } |
| 50 | + shouldComponentUpdate (nextProps) { |
| 51 | + let prevStatus = this.isFocus; |
| 52 | + let nextStatus = this._statusCheck(nextProps); |
| 53 | + if (prevStatus || nextStatus) return true; |
| 54 | + return false; |
| 55 | + } |
| 56 | + render () { |
| 57 | + const { |
| 58 | + date, |
| 59 | + color |
| 60 | + } = this.props; |
| 61 | + let text = date ? date.date() : ''; |
| 62 | + let mainColor = {color: color.mainColor}; |
| 63 | + let subColor = {color: color.subColor}; |
| 64 | + let mainBack = {backgroundColor: color.mainColor}; |
| 65 | + let subBack = {backgroundColor: color.subColor}; |
| 66 | + return ( |
| 67 | + <View |
| 68 | + style={[ |
| 69 | + styles.dayContainer, |
| 70 | + this.isMid && subBack, |
| 71 | + this.isStartPart && styles.startContainer, |
| 72 | + this.isEnd && styles.endContainer, |
| 73 | + (this.isStartPart || this.isEnd) && subBack |
| 74 | + ]}> |
| 75 | + {this.isValid ? |
| 76 | + <TouchableHighlight |
| 77 | + style={[styles.day, this.isToday && styles.today, this.isFocus && subBack]} |
| 78 | + underlayColor="rgba(255, 255, 255, 0.35)" |
| 79 | + onPress={this._chooseDay}> |
| 80 | + <Text style={[styles.dayText, subColor, this.isFocus && mainColor]}>{text}</Text> |
| 81 | + </TouchableHighlight> : |
| 82 | + <View style={[styles.day, this.isToday && styles.today]}> |
| 83 | + <Text style={styles.dayTextDisabled}>{text}</Text> |
| 84 | + </View> |
| 85 | + } |
| 86 | + </View> |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments