Skip to content

Commit

Permalink
Merge pull request #654 from shoutem/release/4.6.6
Browse files Browse the repository at this point in the history
Release/4.6.6
  • Loading branch information
tomislav-arambasic authored Dec 9, 2021
2 parents 3d19bcd + f69df71 commit 4c5db56
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 20 deletions.
94 changes: 76 additions & 18 deletions components/DateTimePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,42 @@ import { View } from './View';

const isIos = Platform.OS === 'ios';

export const DATEPICKER_MODES = {
DATE: 'date',
DATETIME: 'datetime',
TIME: 'time',
};

const DISPLAY_MODES = {
[DATEPICKER_MODES.DATE]: 'inline',
[DATEPICKER_MODES.DATETIME]: 'inline',
[DATEPICKER_MODES.TIME]: 'spinner',
};

function getDisplayMode(mode) {
if (!isIos || !Object.keys(DISPLAY_MODES).includes(mode)) {
return 'default';
}

return DISPLAY_MODES[mode];
}

class DateTimePicker extends PureComponent {
constructor(props) {
super(props);

autoBindReact(this);

const { mode, value } = props;

this.state = {
display: getDisplayMode(mode),
showPicker: false,
value: props.value,
showTimePicker: false,
value,
};
}

componentDidUpdate() {
if (!isIos) {
const { value } = this.props;

this.setState({ value });
}
}

handleValueChanged(event, value) {
if (isIos) {
return this.setState({ value });
Expand All @@ -42,10 +58,33 @@ class DateTimePicker extends PureComponent {
return this.handleHidePicker();
}

const { mode, onValueChanged } = this.props;

const showTimePicker = mode === DATEPICKER_MODES.DATETIME;

this.setState({ value, showPicker: false, showTimePicker });
return !showTimePicker && onValueChanged(value);
}

handleTimeValueChanged(event, selectedValue) {
if (event.type === 'dismissed') {
return this.handleHidePicker();
}

const { onValueChanged } = this.props;
const { value } = this.state;

this.setState({ showPicker: false });
return onValueChanged(value);
// Time picker value will override preselected date
// Use current date from state and only set hours & minutes
const currentDate = new Date(value);
const selectedDate = new Date(selectedValue);
const hour = selectedDate.getHours();
const minutes = selectedDate.getMinutes();

currentDate.setHours(hour, minutes, 0);

this.setState({ value: currentDate, showTimePicker: false });
return onValueChanged(currentDate);
}

handleShowPicker() {
Expand All @@ -66,14 +105,15 @@ class DateTimePicker extends PureComponent {

render() {
const {
cancelButtonText,
confirmButtonText,
is24Hour,
mode,
textValue,
style,
...otherProps
} = this.props;
const { showPicker, value } = this.state;
const { display, showPicker, showTimePicker, value } = this.state;

return (
<View styleName="horizontal">
Expand All @@ -82,7 +122,7 @@ class DateTimePicker extends PureComponent {
style={style.textContainer}
onPress={this.handleShowPicker}
>
<Text> {textValue}</Text>
<Text>{textValue}</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={this.handleShowPicker}
Expand All @@ -91,7 +131,7 @@ class DateTimePicker extends PureComponent {
>
<Icon name="drop-down" style={style.icon} />
</TouchableOpacity>
{isIos && (
{isIos && showPicker && (
<Modal
backdropOpacity={0.5}
isVisible={showPicker}
Expand All @@ -103,7 +143,7 @@ class DateTimePicker extends PureComponent {
>
<View styleName="md-gutter" style={style.modalContainer}>
<RNCDateTimePicker
display="spinner"
display={display}
is24Hour={is24Hour}
mode={mode}
onChange={this.handleValueChanged}
Expand All @@ -114,8 +154,14 @@ class DateTimePicker extends PureComponent {
/>
<View
style={style.modalButtonContainer}
styleName="md-gutter-top"
styleName="horizontal md-gutter-top"
>
<Button
style={style.modalButton}
onPress={this.handleHidePicker}
>
<Text>{cancelButtonText}</Text>
</Button>
<Button
style={style.modalButton}
onPress={this.handleConfirmPress}
Expand All @@ -128,29 +174,41 @@ class DateTimePicker extends PureComponent {
)}
{!isIos && showPicker && (
<RNCDateTimePicker
display="default"
display={display}
is24Hour={is24Hour}
mode={mode}
onChange={this.handleValueChanged}
value={value}
{...otherProps}
/>
)}
{!isIos && !showPicker && showTimePicker && (
<RNCDateTimePicker
display={display}
is24Hour={is24Hour}
mode="time"
onChange={this.handleTimeValueChanged}
value={value}
{...otherProps}
/>
)}
</View>
);
}
}

DateTimePicker.propTypes = {
cancelButtonText: PropTypes.string,
confirmButtonText: PropTypes.string,
is24Hour: PropTypes.bool,
mode: PropTypes.string,
mode: PropTypes.oneOf(Object.values(DATEPICKER_MODES)),
onValueChanged: PropTypes.func,
textValue: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
};

DateTimePicker.defaultProps = {
cancelButtonText: 'Cancel',
confirmButtonText: 'Confirm',
is24Hour: false,
mode: 'datetime',
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shoutem/ui",
"version": "4.6.5",
"version": "4.6.6",
"description": "Styleable set of components for React Native applications",
"scripts": {
"lint": "eslint .",
Expand Down
2 changes: 2 additions & 0 deletions theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ export default (variables = defaultThemeVariables) => ({
},

'shoutem.ui.Screen': {
[INCLUDE]: ['commonVariants', 'guttersPadding'],

'.full-screen': {
marginTop: -(NAVIGATION_BAR_HEIGHT + StyleSheet.hairlineWidth),
},
Expand Down

0 comments on commit 4c5db56

Please sign in to comment.