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

107 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-02-11 14:01:35 +00:00
import React, { useState } from 'react';
import { FlatList, StyleSheet, Text } from 'react-native';
2020-02-11 14:01:35 +00:00
import Popover from 'react-native-popover-view';
import Touchable from 'react-native-platform-touchable';
import { CustomIcon } from '../../lib/Icons';
import ActivityIndicator from '../ActivityIndicator';
import { themes } from '../../constants/colors';
import { BUTTON_HIT_SLOP } from '../message/utils';
import * as List from '../List';
2020-02-11 14:01:35 +00:00
interface IOption {
option: {
text: string;
value: string;
};
onOptionPress: Function;
parser: any;
theme: string;
}
interface IOptions {
options: [];
onOptionPress: Function;
parser: object;
theme: string;
}
interface IOverflow {
element: any;
action: Function;
loading: boolean;
parser: object;
theme: string;
context: any;
}
const keyExtractor = (item: any) => item.value;
2020-02-11 14:01:35 +00:00
const styles = StyleSheet.create({
menu: {
justifyContent: 'center'
},
option: {
padding: 8,
minHeight: 32
},
loading: {
padding: 0
}
});
const Option = ({ option: { text, value }, onOptionPress, parser, theme }: IOption) => (
2020-02-11 14:01:35 +00:00
<Touchable
onPress={() => onOptionPress({ value })}
background={Touchable.Ripple(themes[theme].bannerBackground)}
style={styles.option}>
2020-02-11 14:01:35 +00:00
<Text>{parser.text(text)}</Text>
</Touchable>
);
const Options = ({ options, onOptionPress, parser, theme }: IOptions) => (
2020-02-11 14:01:35 +00:00
<FlatList
data={options}
renderItem={({ item }) => <Option option={item} onOptionPress={onOptionPress} parser={parser} theme={theme} />}
keyExtractor={keyExtractor}
ItemSeparatorComponent={List.Separator}
2020-02-11 14:01:35 +00:00
/>
);
const touchable = {};
export const Overflow = ({ element, loading, action, parser, theme }: IOverflow) => {
2020-02-11 14:01:35 +00:00
const { options, blockId } = element;
const [show, onShow] = useState(false);
const onOptionPress = ({ value }: any) => {
2020-02-11 14:01:35 +00:00
onShow(false);
action({ value });
};
return (
<>
<Touchable
/* @ts-ignore*/
ref={ref => (touchable[blockId] = ref)}
2020-02-11 14:01:35 +00:00
background={Touchable.Ripple(themes[theme].bannerBackground)}
onPress={() => onShow(!show)}
hitSlop={BUTTON_HIT_SLOP}
style={styles.menu}>
{!loading ? (
<CustomIcon size={18} name='kebab' color={themes[theme].bodyText} />
) : (
<ActivityIndicator style={styles.loading} theme={theme} />
)}
2020-02-11 14:01:35 +00:00
</Touchable>
<Popover
isVisible={show}
/* @ts-ignore*/
2020-02-11 14:01:35 +00:00
fromView={touchable[blockId]}
onRequestClose={() => onShow(false)}>
2020-02-11 14:01:35 +00:00
<Options options={options} onOptionPress={onOptionPress} parser={parser} theme={theme} />
</Popover>
</>
);
};