From e674c53acb7dc6e34dada1b3cfcd00d5379941f6 Mon Sep 17 00:00:00 2001 From: Mina Sameh Date: Wed, 10 Jul 2024 17:37:29 +0300 Subject: [PATCH] fix: avatar defaultProps warning Warning: Avatar: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead closes #2498 --- src/Avatar.tsx | 100 +++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/src/Avatar.tsx b/src/Avatar.tsx index fb0cceec0..2735170cf 100644 --- a/src/Avatar.tsx +++ b/src/Avatar.tsx @@ -1,17 +1,17 @@ import PropTypes from 'prop-types' import React, { ReactNode } from 'react' import { - StyleSheet, - View, ImageStyle, + StyleSheet, TextStyle, + View, ViewStyle, } from 'react-native' import GiftedAvatar from './GiftedAvatar' import { StylePropType, isSameUser, isSameDay } from './utils' -import { Omit, IMessage, User, LeftRightStyle } from './Models' +import { IMessage, LeftRightStyle, User } from './Models' -const styles = { +const styles: { [key: string]: any } = { left: StyleSheet.create({ container: { marginRight: 8, @@ -46,7 +46,7 @@ export interface AvatarProps { currentMessage?: TMessage previousMessage?: TMessage nextMessage?: TMessage - position: 'left' | 'right' + position: 'left' | 'right' | string // Allow string as a fallback value renderAvatarOnTop?: boolean showAvatarForEveryMessage?: boolean imageStyle?: LeftRightStyle @@ -57,20 +57,19 @@ export interface AvatarProps { onLongPressAvatar?(user: User): void } -export function Avatar( - props: AvatarProps, -) { - const { - renderAvatarOnTop, - showAvatarForEveryMessage, - containerStyle, - position, - currentMessage, - renderAvatar, - previousMessage, - nextMessage, - imageStyle, - } = props +export function Avatar({ + renderAvatarOnTop = false, + showAvatarForEveryMessage = false, + containerStyle = {} as { [key: string]: any }, + position = 'left', + currentMessage = {} as IMessage, + previousMessage = {} as IMessage, + nextMessage = {} as IMessage, + renderAvatar = (({ }) => null) as AvatarProps['renderAvatar'], + imageStyle = {} as { [key: string]: any }, + onPressAvatar = ({ }) => { }, + onLongPressAvatar = ({ }) => { }, +}) { const messageToCompare = renderAvatarOnTop ? previousMessage : nextMessage const computedStyle = renderAvatarOnTop ? 'onTop' : 'onBottom' @@ -93,41 +92,43 @@ export function Avatar( ]} > ) } const renderAvatarComponent = () => { - if (props.renderAvatar) { - const { renderAvatar, ...avatarProps } = props - return props.renderAvatar(avatarProps) + if (renderAvatar) { + return renderAvatar({ + renderAvatarOnTop, + showAvatarForEveryMessage, + containerStyle, + position, + currentMessage, + previousMessage, + nextMessage, + imageStyle, + onPressAvatar, + onLongPressAvatar, + }) } - - if (props.currentMessage) { + if (currentMessage) { return ( props.onPressAvatar?.(props.currentMessage!.user)} - onLongPress={() => - props.onLongPressAvatar?.(props.currentMessage!.user) - } + avatarStyle={[ + styles[position].image, + imageStyle && imageStyle[position], + ]} + user={currentMessage.user} + onPress={() => onPressAvatar(currentMessage.user)} + onLongPress={() => onLongPressAvatar(currentMessage.user)} /> ) } - return null } @@ -144,21 +145,6 @@ export function Avatar( ) } -Avatar.defaultProps = { - renderAvatarOnTop: false, - showAvatarForEveryMessage: false, - position: 'left', - currentMessage: { - user: null, - }, - previousMessage: {}, - nextMessage: {}, - containerStyle: {}, - imageStyle: {}, - onPressAvatar: () => {}, - onLongPressAvatar: () => {}, -} - Avatar.propTypes = { renderAvatarOnTop: PropTypes.bool, showAvatarForEveryMessage: PropTypes.bool,