2020-02-11 14:01:35 +00:00
|
|
|
import React, { useState } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
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';
|
|
|
|
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2020-02-11 14:01:35 +00:00
|
|
|
import ActivityIndicator from '../ActivityIndicator';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-03-29 20:06:50 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2020-02-11 14:01:35 +00:00
|
|
|
import { BUTTON_HIT_SLOP } from '../message/utils';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../List';
|
2022-03-16 19:07:49 +00:00
|
|
|
import { IOption, IOptions, IOverflow } from './interfaces';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
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)}
|
2022-08-08 21:02:08 +00:00
|
|
|
style={styles.option}
|
|
|
|
>
|
2020-02-11 14:01:35 +00:00
|
|
|
<Text>{parser.text(text)}</Text>
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
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}
|
2020-10-30 13:59:44 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2020-02-11 14:01:35 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
const touchable: { [key: string]: Touchable | null } = {};
|
2020-02-11 14:01:35 +00:00
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
export const Overflow = ({ element, loading, action, parser }: IOverflow) => {
|
|
|
|
const { theme } = useTheme();
|
2022-03-16 19:07:49 +00:00
|
|
|
const options = element?.options || [];
|
|
|
|
const blockId = element?.blockId || '';
|
2020-02-11 14:01:35 +00:00
|
|
|
const [show, onShow] = useState(false);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const onOptionPress = ({ value }: any) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
onShow(false);
|
|
|
|
action({ value });
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Touchable
|
2022-03-29 20:06:50 +00:00
|
|
|
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}
|
2022-08-08 21:02:08 +00:00
|
|
|
style={styles.menu}
|
|
|
|
>
|
2021-09-13 20:41:05 +00:00
|
|
|
{!loading ? (
|
|
|
|
<CustomIcon size={18} name='kebab' color={themes[theme].bodyText} />
|
|
|
|
) : (
|
2022-03-18 02:37:10 +00:00
|
|
|
<ActivityIndicator style={styles.loading} />
|
2021-09-13 20:41:05 +00:00
|
|
|
)}
|
2020-02-11 14:01:35 +00:00
|
|
|
</Touchable>
|
|
|
|
<Popover
|
|
|
|
isVisible={show}
|
2022-03-29 20:06:50 +00:00
|
|
|
// fromView exists in Popover Component
|
2021-09-13 20:41:05 +00:00
|
|
|
/* @ts-ignore*/
|
2020-02-11 14:01:35 +00:00
|
|
|
fromView={touchable[blockId]}
|
2022-08-08 21:02:08 +00:00
|
|
|
onRequestClose={() => onShow(false)}
|
|
|
|
>
|
2020-02-11 14:01:35 +00:00
|
|
|
<Options options={options} onOptionPress={onOptionPress} parser={parser} theme={theme} />
|
|
|
|
</Popover>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|