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';
|
2020-02-11 14:01:35 +00:00
|
|
|
import DateTimePicker from '@react-native-community/datetimepicker';
|
|
|
|
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';
|
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import sharedStyles from '../../views/Styles';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import { isAndroid } from '../../utils/deviceInfo';
|
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
export const DatePicker = ({ element, language, action, context, theme, loading, value, error }: IDatePicker) => {
|
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));
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const onChange = ({ nativeEvent: { timestamp } }: any, date: any) => {
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
let button = <Button title={textParser([placeholder])} onPress={() => onShow(!show)} loading={loading} theme={theme} />;
|
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}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|