Skip to content

Commit 800483c

Browse files
authored
DateTimePicker - turn to controlled component (issue #972) (#989)
1 parent fa0447c commit 800483c

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/components/dateTimePicker/index.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import _ from 'lodash';
22
import PropTypes from 'prop-types';
33
import moment from 'moment';
4-
import React from 'react';
4+
import React, {Component} from 'react';
55
import {StyleSheet} from 'react-native';
66
import RNDateTimePicker from '@react-native-community/datetimepicker';
77
import {Constants} from '../../helpers';
88
import {Colors} from '../../style';
99
import Assets from '../../assets';
10-
import {BaseComponent} from '../../commons';
10+
import {asBaseComponent} from '../../commons';
1111
import {TextField} from '../inputs';
1212
import Dialog from '../dialog';
1313
import View from '../view';
1414
import Button from '../button';
1515

16+
1617
const MODES = {
1718
DATE: 'date',
1819
TIME: 'time'
@@ -28,8 +29,9 @@ const MODES = {
2829
*/
2930
/*eslint-enable*/
3031

31-
class DateTimePicker extends BaseComponent {
32+
class DateTimePicker extends Component {
3233
static displayName = 'DateTimePicker';
34+
3335
static propTypes = {
3436
...TextField.propTypes,
3537
/**
@@ -98,17 +100,23 @@ class DateTimePicker extends BaseComponent {
98100
constructor(props) {
99101
super(props);
100102

101-
const initialValue = props.value;
102-
this.chosenDate = initialValue;
103+
this.chosenDate = props.value;
103104

104105
this.state = {
105106
showExpandableOverlay: false,
106-
value: initialValue
107+
prevValue: props.value,
108+
value: props.value
107109
};
108110
}
109111

110-
generateStyles() {
111-
this.styles = createStyles(this.props);
112+
static getDerivedStateFromProps(nextProps, prevState) {
113+
if (nextProps.value !== prevState.prevValue) {
114+
return {
115+
prevValue: prevState.value,
116+
value: nextProps.value
117+
};
118+
}
119+
return null;
112120
}
113121

114122
handleChange = (event = {}, date) => {
@@ -141,14 +149,14 @@ class DateTimePicker extends BaseComponent {
141149
// since handleChange() is not called on iOS when there is no actual change
142150
this.chosenDate = new Date();
143151
}
144-
152+
145153
_.invoke(this.props, 'onChange', this.chosenDate);
146154
this.setState({value: this.chosenDate});
147155
});
148156

149157
getStringValue = () => {
150158
const {value} = this.state;
151-
const {mode, dateFormat, timeFormat, dateFormatter, timeFormatter} = this.getThemeProps();
159+
const {mode, dateFormat, timeFormat, dateFormatter, timeFormatter} = this.props;
152160
if (value) {
153161
switch (mode) {
154162
case MODES.DATE:
@@ -168,7 +176,7 @@ class DateTimePicker extends BaseComponent {
168176
};
169177

170178
renderExpandableOverlay = () => {
171-
const {testID, dialogProps} = this.getThemeProps();
179+
const {testID, dialogProps} = this.props;
172180
const {showExpandableOverlay} = this.state;
173181

174182
return (
@@ -180,7 +188,7 @@ class DateTimePicker extends BaseComponent {
180188
bottom
181189
centerH
182190
onDismiss={this.toggleExpandableOverlay}
183-
containerStyle={this.styles.dialog}
191+
containerStyle={styles.dialog}
184192
testID={`${testID}.dialog`}
185193
supportedOrientations={['portrait', 'landscape', 'landscape-left', 'landscape-right']} // iOS only
186194
{...dialogProps}
@@ -197,7 +205,7 @@ class DateTimePicker extends BaseComponent {
197205
const {useCustomTheme} = this.props;
198206

199207
return (
200-
<View row spread bg-white paddingH-20 style={this.styles.header}>
208+
<View row spread bg-white paddingH-20 style={styles.header}>
201209
<Button
202210
link
203211
iconSource={Assets.icons.x}
@@ -235,7 +243,7 @@ class DateTimePicker extends BaseComponent {
235243
};
236244

237245
render() {
238-
const textInputProps = TextField.extractOwnProps(this.getThemeProps());
246+
const textInputProps = TextField.extractOwnProps(this.props);
239247

240248
return (
241249
<TextField
@@ -249,23 +257,19 @@ class DateTimePicker extends BaseComponent {
249257
}
250258
}
251259

252-
export default DateTimePicker;
260+
export {DateTimePicker}; // For tests
261+
export default asBaseComponent(DateTimePicker);
253262

254-
function createStyles(props) {
255-
const borderRadius = 12;
256263

257-
const styles = StyleSheet.create({
258-
header: {
259-
height: 56,
260-
borderBottomWidth: 1,
261-
borderBottomColor: Colors.dark80
262-
},
263-
dialog: {
264-
backgroundColor: Colors.white,
265-
borderTopLeftRadius: borderRadius,
266-
borderTopRightRadius: borderRadius
267-
}
268-
});
269-
270-
return styles;
271-
}
264+
const styles = StyleSheet.create({
265+
header: {
266+
height: 56,
267+
borderBottomWidth: 1,
268+
borderBottomColor: Colors.dark80
269+
},
270+
dialog: {
271+
backgroundColor: Colors.white,
272+
borderTopLeftRadius: 12,
273+
borderTopRightRadius: 12
274+
}
275+
});

0 commit comments

Comments
 (0)