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';
|
2022-05-26 14:07:17 +00:00
|
|
|
import { BlockContext } from '@rocket.chat/ui-kit';
|
2020-02-11 14:01:35 +00:00
|
|
|
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';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { isAndroid } from '../../lib/methods/helpers';
|
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,
|
2022-09-22 21:16:25 +00:00
|
|
|
borderWidth: 2,
|
2020-02-11 14:01:35 +00:00
|
|
|
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-05-20 16:37:57 +00:00
|
|
|
let button = placeholder ? <Button title={textParser([placeholder])} onPress={() => onShow(!show)} loading={loading} /> : null;
|
2020-02-11 14:01:35 +00:00
|
|
|
|
2022-05-26 14:07:17 +00:00
|
|
|
if (context === BlockContext.FORM) {
|
2020-02-11 14:01:35 +00:00
|
|
|
button = (
|
|
|
|
<Touchable
|
|
|
|
onPress={() => onShow(!show)}
|
|
|
|
style={{ backgroundColor: themes[theme].backgroundColor }}
|
2022-08-08 21:02:08 +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}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|