import PropTypes from 'prop-types' import React, { ReactNode } from 'react' import { StyleSheet, Text, TouchableOpacity, View, StyleProp, ViewStyle, TextStyle, Keyboard, } from 'react-native' import Color from './Color' import { StylePropType } from './utils' import { useChatContext } from './GiftedChatContext' import { useCallbackOne } from 'use-memo-one' export interface ActionsProps { options?: { [key: string]: any } optionTintColor?: string icon?: () => ReactNode wrapperStyle?: StyleProp iconTextStyle?: StyleProp containerStyle?: StyleProp onPressActionButton?(): void hideKeyboardOnActionPress?: boolean } export function Actions ({ options = {}, optionTintColor = Color.optionTintColor, icon, wrapperStyle, iconTextStyle, onPressActionButton, containerStyle, hideKeyboardOnActionPress=true }: ActionsProps) { const { actionSheet } = useChatContext() const onActionsPress = useCallbackOne(() => { hideKeyboardOnActionPress && Keyboard.dismiss() const optionKeys = Object.keys(options) const cancelButtonIndex = optionKeys.indexOf('Cancel') actionSheet().showActionSheetWithOptions( { options: optionKeys, cancelButtonIndex, tintColor: optionTintColor, }, (buttonIndex: number) => { const key = optionKeys[buttonIndex] if (key) options[key]() } ) }, []) const renderIcon = useCallbackOne(() => { if (icon) return icon() return ( {'+'} ) }, []) return ( {renderIcon()} ) } Actions.propTypes = { onSend: PropTypes.func, options: PropTypes.object, optionTintColor: PropTypes.string, icon: PropTypes.func, onPressActionButton: PropTypes.func, wrapperStyle: StylePropType, containerStyle: StylePropType, } const styles = StyleSheet.create({ container: { width: 26, height: 26, marginLeft: 10, marginBottom: 10, }, wrapper: { borderRadius: 13, borderColor: Color.defaultColor, borderWidth: 2, flex: 1, alignItems: 'center', justifyContent: 'center', }, iconText: { color: Color.defaultColor, fontWeight: 'bold', fontSize: 16, lineHeight: 16, backgroundColor: Color.backgroundTransparent, textAlign: 'center', }, })