Rocket.Chat.ReactNative/app/containers/UIKit/DatePicker.tsx

97 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-02-11 14:01:35 +00:00
import React, { useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import DateTimePicker, { Event } from '@react-native-community/datetimepicker';
2020-02-11 14:01:35 +00:00
import Touchable from 'react-native-platform-touchable';
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';
import { themes } from '../../lib/constants';
2020-02-11 14:01:35 +00:00
import sharedStyles from '../../views/Styles';
import { CustomIcon } from '../CustomIcon';
import { isAndroid } from '../../lib/methods/helpers';
import { useTheme } from '../../theme';
2020-02-11 14:01:35 +00:00
import ActivityIndicator from '../ActivityIndicator';
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
}
});
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);
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));
// 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);
}
};
let button = placeholder ? <Button title={textParser([placeholder])} onPress={() => onShow(!show)} loading={loading} /> : null;
2020-02-11 14:01:35 +00:00
if (context === BlockContext.FORM) {
2020-02-11 14:01:35 +00:00
button = (
<Touchable
onPress={() => onShow(!show)}
style={{ backgroundColor: themes[theme].backgroundColor }}
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 }]}>
<Text style={[styles.inputText, { color: error ? themes[theme].dangerColor : themes[theme].titleText }]}>
2020-02-11 14:01:35 +00:00
{currentDate.toLocaleDateString(language)}
</Text>
{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 ? (
<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}
</>
);
};