-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.ios.js
140 lines (129 loc) · 4.97 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
129
130
131
132
133
134
135
136
137
138
139
140
import React from 'react';
// eslint-disable-next-line no-restricted-imports
import {Button, View, Keyboard} from 'react-native';
import RNDatePicker from '@react-native-community/datetimepicker';
import moment from 'moment';
import _ from 'underscore';
import TextInput from '../TextInput';
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 || props.defaultValue ? moment(props.value || props.defaultValue).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) {
Keyboard.dismiss();
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.onInputChange)
*/
selectDate() {
this.setState({isPickerVisible: false});
this.props.onInputChange(this.state.selectedDate);
}
/**
* @param {Event} event
* @param {Date} selectedDate
*/
updateLocalDate(event, selectedDate) {
this.setState({selectedDate});
}
render() {
const dateAsText = this.props.value || this.props.defaultValue ? moment(this.props.value || this.props.defaultValue).format(CONST.DATE.MOMENT_FORMAT_STRING) : '';
return (
<>
<TextInput
label={this.props.label}
value={dateAsText}
placeholder={this.props.placeholder}
errorText={this.props.errorText}
containerStyles={this.props.containerStyles}
textInputContainerStyles={this.state.isPickerVisible ? [styles.borderColorFocus] : []}
onPress={this.showPicker}
editable={false}
disabled={this.props.disabled}
onBlur={this.props.onBlur}
ref={(el) => {
if (!_.isFunction(this.props.innerRef)) {
return;
}
this.props.innerRef(el);
}}
/>
<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"
themeVariant="dark"
onChange={this.updateLocalDate}
locale={this.props.preferredLocale}
/>
</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(React.forwardRef((props, ref) => (
/* eslint-disable-next-line react/jsx-props-no-spreading */
<DatePicker {...props} innerRef={ref} />
)));