verdnatura-chat/app/containers/UIKit/DatePicker.tsx

108 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2020-02-11 14:01:35 +00:00
import React, { useState } from 'react';
import { StyleSheet, Text, unstable_batchedUpdates, 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: 1,
borderRadius: 4,
2020-02-11 14:01:35 +00:00
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) => {
if (date || timestamp) {
const newDate = date || new Date(timestamp);
unstable_batchedUpdates(() => {
onChangeDate(newDate);
if (isAndroid) {
onShow(false);
}
});
action({ value: moment(newDate).format('YYYY-MM-DD') });
2020-02-11 14:01:35 +00:00
}
};
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={isAndroid ? 'default' : 'inline'}
value={currentDate}
onChange={onChange}
textColor={themes[theme].titleText}
/>
2020-02-11 14:01:35 +00:00
) : null;
return (
<>
{button}
{content}
</>
);
};