2020-02-11 14:01:35 +00:00
|
|
|
import React, { useState } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { StyleSheet, Text, View } from 'react-native';
|
2022-03-29 20:06:50 +00:00
|
|
|
import DateTimePicker, { Event } from '@react-native-community/datetimepicker';
|
2020-02-11 14:01:35 +00:00
|
|
|
import Touchable from 'react-native-platform-touchable';
|
|
|
|
import { BLOCK_CONTEXT } from '@rocket.chat/ui-kit';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
import Button from '../Button';
|
|
|
|
import { textParser } from './utils';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2020-02-11 14:01:35 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2020-02-11 14:01:35 +00:00
|
|
|
import { isAndroid } from '../../utils/deviceInfo';
|
2022-03-29 20:06:50 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2020-02-11 14:01:35 +00:00
|
|
|
import ActivityIndicator from '../ActivityIndicator';
|
2022-03-16 19:07:49 +00:00
|
|
|
import { IDatePicker } from './interfaces';
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
input: {
|
|
|
|
height: 48,
|
|
|
|
paddingLeft: 16,
|
|
|
|
borderWidth: StyleSheet.hairlineWidth,
|
|
|
|
borderRadius: 2,
|
|
|
|
alignItems: 'center',
|
|
|
|
flexDirection: 'row'
|
|
|
|
},
|
|
|
|
inputText: {
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
fontSize: 14
|
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
right: 16,
|
|
|
|
position: 'absolute'
|
|
|
|
},
|
|
|
|
loading: {
|
|
|
|
padding: 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
export const DatePicker = ({ element, language, action, context, loading, value, error }: IDatePicker) => {
|
|
|
|
const { theme } = useTheme();
|
2020-02-11 14:01:35 +00:00
|
|
|
const [show, onShow] = useState(false);
|
2022-03-16 19:07:49 +00:00
|
|
|
const initial_date = element?.initial_date;
|
|
|
|
const placeholder = element?.placeholder;
|
|
|
|
|
2020-02-11 14:01:35 +00:00
|
|
|
const [currentDate, onChangeDate] = useState(new Date(initial_date || value));
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
// timestamp as number exists in Event
|
|
|
|
// @ts-ignore
|
|
|
|
const onChange = ({ nativeEvent: { timestamp } }: Event, date?: Date) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
const newDate = date || new Date(timestamp);
|
|
|
|
onChangeDate(newDate);
|
|
|
|
action({ value: moment(newDate).format('YYYY-MM-DD') });
|
|
|
|
if (isAndroid) {
|
|
|
|
onShow(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
let button = placeholder ? (
|
|
|
|
<Button title={textParser([placeholder])} onPress={() => onShow(!show)} loading={loading} theme={theme} />
|
|
|
|
) : null;
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
if (context === BLOCK_CONTEXT.FORM) {
|
|
|
|
button = (
|
|
|
|
<Touchable
|
|
|
|
onPress={() => onShow(!show)}
|
|
|
|
style={{ backgroundColor: themes[theme].backgroundColor }}
|
2021-09-13 20:41:05 +00:00
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}>
|
2020-02-11 14:01:35 +00:00
|
|
|
<View style={[styles.input, { borderColor: error ? themes[theme].dangerColor : themes[theme].separatorColor }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Text style={[styles.inputText, { color: error ? themes[theme].dangerColor : themes[theme].titleText }]}>
|
2020-02-11 14:01:35 +00:00
|
|
|
{currentDate.toLocaleDateString(language)}
|
|
|
|
</Text>
|
2021-09-13 20:41:05 +00:00
|
|
|
{loading ? (
|
|
|
|
<ActivityIndicator style={[styles.loading, styles.icon]} />
|
|
|
|
) : (
|
|
|
|
<CustomIcon
|
|
|
|
name='calendar'
|
|
|
|
size={20}
|
|
|
|
color={error ? themes[theme].dangerColor : themes[theme].auxiliaryText}
|
|
|
|
style={styles.icon}
|
|
|
|
/>
|
|
|
|
)}
|
2020-02-11 14:01:35 +00:00
|
|
|
</View>
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = show ? (
|
2021-09-13 20:41:05 +00:00
|
|
|
<DateTimePicker mode='date' display='default' value={currentDate} onChange={onChange} textColor={themes[theme].titleText} />
|
2020-02-11 14:01:35 +00:00
|
|
|
) : null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{button}
|
|
|
|
{content}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|