-
Notifications
You must be signed in to change notification settings - Fork 3k
/
index.ios.js
128 lines (117 loc) · 4.38 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React from 'react';
import {Button, View} from 'react-native';
import RNDatePicker from '@react-native-community/datetimepicker';
import moment from 'moment';
import ExpensiTextInput from '../ExpensiTextInput';
import withLocalize, {withLocalizePropTypes} from '../withLocalize';
import Popover from '../Popover';
import CONST from '../../CONST';
import styles from '../../styles/styles';
import themeColors from '../../styles/themes/default';
import {propTypes, defaultProps} from './datepickerPropTypes';
const datepickerPropTypes = {
...propTypes,
...withLocalizePropTypes,
};
class Datepicker extends React.Component {
constructor(props) {
super(props);
this.state = {
isPickerVisible: false,
selectedDate: props.value ? moment(props.value).toDate() : new Date(),
};
this.showPicker = this.showPicker.bind(this);
this.reset = this.reset.bind(this);
this.selectDate = this.selectDate.bind(this);
this.updateLocalDate = this.updateLocalDate.bind(this);
}
/**
* @param {Event} event
*/
showPicker(event) {
this.initialValue = this.state.selectedDate;
this.setState({isPickerVisible: true});
event.preventDefault();
}
/**
* Reset the date spinner to the initial value
*/
reset() {
this.setState({selectedDate: this.initialValue});
}
/**
* Accept the current spinner changes, close the spinner and propagate the change
* to the parent component (props.onChange)
*/
selectDate() {
this.setState({isPickerVisible: false});
this.props.onChange(this.state.selectedDate);
}
/**
* @param {Event} event
* @param {Date} selectedDate
*/
updateLocalDate(event, selectedDate) {
this.setState({selectedDate});
}
render() {
const dateAsText = this.props.value ? moment(this.props.value).format(CONST.DATE.MOMENT_FORMAT_STRING) : '';
return (
<>
<ExpensiTextInput
label={this.props.label}
value={dateAsText}
placeholder={this.props.placeholder}
hasError={this.props.hasError}
errorText={this.props.errorText}
containerStyles={this.props.containerStyles}
translateX={this.props.translateX}
onPress={this.showPicker}
editable={false}
disabled={this.props.disabled}
/>
<Popover
isVisible={this.state.isPickerVisible}
onClose={this.selectDate}
>
<View style={[
styles.flexRow,
styles.justifyContentBetween,
styles.borderBottom,
styles.pb1,
styles.ph4,
]}
>
<Button
title={this.props.translate('common.reset')}
color={themeColors.textError}
onPress={this.reset}
/>
<Button
title={this.props.translate('common.done')}
color={themeColors.link}
onPress={this.selectDate}
/>
</View>
<RNDatePicker
value={this.state.selectedDate}
mode="date"
display="spinner"
onChange={this.updateLocalDate}
locale={this.props.preferredLocale}
maximumDate={this.props.maximumDate}
/>
</Popover>
</>
);
}
}
Datepicker.propTypes = datepickerPropTypes;
Datepicker.defaultProps = defaultProps;
/**
* We're applying localization here because we present a modal (with buttons) ourselves
* Furthermore we're passing the locale down so that the modal and the date spinner are in the same
* locale. Otherwise the spinner would be present in the system locale and it would be weird if it happens
* that the modal buttons are in one locale (app) while the (spinner) month names are another (system)
*/
export default withLocalize(Datepicker);