react-native-daterange
Version:
[](https://www.npmjs.com/package/react-native-daterange) [](https://www.np
203 lines (197 loc) • 6.22 kB
JavaScript
import React, { Component } from 'react';
import { View, TouchableHighlight, Modal, Text } from 'react-native';
import DateRange from './DateRange';
import moment from 'moment';
import normalize from './normalizeText';
const styles = {
placeholderText: {
color: '#c9c9c9',
fontSize: normalize(20),
},
contentInput: {
alignItems: 'center',
justifyContent: 'center',
},
contentText: {
fontSize: normalize(18),
},
stylish: {
height: 40,
borderColor: '#bdbdbd',
borderWidth: 2,
borderRadius: 8,
},
};
export default class ComposePicker extends Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false,
allowPointerEvents: true,
showContent: false,
selected: '',
startDate: null,
endDate: null,
date: new Date(),
focus: 'startDate',
currentDate: moment(),
};
}
isDateBlocked = date => {
if (this.props.blockBefore) {
return date.isBefore(moment(), 'day');
} else if (this.props.blockAfter) {
return date.isAfter(moment(), 'day');
}
return false;
};
onDatesChange = event => {
const { startDate, endDate, focusedInput, currentDate } = event;
if (currentDate) {
this.setState({ currentDate });
return;
}
this.setState({ ...this.state, focus: focusedInput }, () => {
this.setState({ ...this.state, startDate, endDate });
});
};
setModalVisible = visible => {
this.setState({ modalVisible: visible });
};
onConfirm = () => {
const returnFormat = this.props.returnFormat || 'YYYY/MM/DD';
const outFormat = this.props.outFormat || 'LL';
if (!this.props.mode || this.props.mode === 'single') {
this.setState({
showContent: true,
selected: this.state.currentDate.format(outFormat),
});
this.setModalVisible(false);
if (typeof this.props.onConfirm === 'function') {
this.props.onConfirm({
currentDate: this.state.currentDate.format(returnFormat),
});
}
return;
}
if (this.state.startDate && this.state.endDate) {
const start = this.state.startDate.format(outFormat);
const end = this.state.endDate.format(outFormat);
this.setState({ showContent: true, selected: `${start} → ${end}` });
this.setModalVisible(false);
if (typeof this.props.onConfirm === 'function') {
this.props.onConfirm({
startDate: this.state.startDate.format(returnFormat),
endDate: this.state.endDate.format(returnFormat),
});
}
} else {
alert('please select correct date');
}
};
getTitleElement() {
const { placeholder, customStyles = {}, allowFontScaling } = this.props;
const { showContent, selected } = this.state;
if (!showContent && placeholder) {
return (
<Text
allowFontScaling={allowFontScaling}
style={[styles.placeholderText, customStyles.placeholderText]}
>
{placeholder}
</Text>
);
}
return (
<Text
allowFontScaling={allowFontScaling}
style={[styles.contentText, customStyles.contentText]}
>
{selected}
</Text>
);
}
render() {
const { customStyles = {} } = this.props;
let style = styles.stylish;
style = this.props.centerAlign ? { ...style } : style;
style = { ...style, ...this.props.style };
return (
<TouchableHighlight
underlayColor={'transparent'}
onPress={() => {
this.setModalVisible(true);
}}
style={[
{ width: '100%', height: '100%', justifyContent: 'center' },
style,
]}
>
<View>
<View>
<View style={[customStyles.contentInput, styles.contentInput]}>
{this.getTitleElement()}
</View>
</View>
<Modal
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => this.setModalVisible(false)}
>
<View stlye={{ flex: 1, flexDirection: 'column' }}>
<View style={{ height: '90%' }}>
<DateRange
headFormat={this.props.headFormat}
customStyles={customStyles}
markText={this.props.markText}
onDatesChange={this.onDatesChange}
isDateBlocked={this.isDateBlocked}
startDate={this.state.startDate}
endDate={this.state.endDate}
startDateText={this.props.startDateText}
endDateText={this.props.endDateText}
focusedInput={this.state.focus}
selectedBgColor={this.props.selectedBgColor || undefined}
selectedTextColor={this.props.selectedTextColor || undefined}
mode={this.props.mode || 'single'}
currentDate={this.state.currentDate}
/>
</View>
<View
style={{
width: '100%',
height: '10%',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
}}
>
{this.props.customButton ? (
this.props.customButton
) : (
<TouchableHighlight
underlayColor={'transparent'}
onPress={this.onConfirm}
style={[
{ width: '80%', marginHorizontal: '3%' },
this.props.ButtonStyle,
]}
>
<Text
style={[{ fontSize: 20 }, this.props.ButtonTextStyle]}
>
{this.props.ButtonText
? this.props.ButtonText
: 'Set Date'}
</Text>
</TouchableHighlight>
)}
</View>
</View>
</Modal>
</View>
</TouchableHighlight>
);
}
}