-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[NEW] Collapsible Message #3879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8745779
create new collapsible component
dnlsilva 4df0d7e
create collapsible tests and update snapshot
dnlsilva ed1ce8b
fix quote :)
dnlsilva 4285524
update snapshot
dnlsilva eba4ae4
add support to color
dnlsilva d26153c
add collapsed prop
dnlsilva 9af417c
fix some styles
dnlsilva 10742d1
fix tests
dnlsilva 2b41bc1
wip
dnlsilva bf9eefd
clean
dnlsilva 03a8966
add CollapsibleQuote story
dnlsilva 0db8ef2
better style
dnlsilva 345bad1
update snapshots
dnlsilva c437317
add better tests
dnlsilva 9c4f9bc
remove testID
dnlsilva 0b2132e
update storyshot
dnlsilva 91eeff3
Merge branch 'develop' into new-collapsible
dnlsilva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
app/containers/message/Components/CollapsibleQuote.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { fireEvent, render, within } from '@testing-library/react-native'; | ||
|
dnlsilva marked this conversation as resolved.
|
||
| import React from 'react'; | ||
|
|
||
| import MessageContext from '../Context'; | ||
| import CollapsibleQuote from './CollapsibleQuote'; | ||
|
|
||
| // For some reason a general mock didn't work, I have to do a search | ||
| jest.mock('react-native-mmkv-storage', () => ({ | ||
| Loader: jest.fn().mockImplementation(() => ({ | ||
| setProcessingMode: jest.fn().mockImplementation(() => ({ | ||
| withEncryption: jest.fn().mockImplementation(() => ({ | ||
| initialize: jest.fn() | ||
| })) | ||
| })) | ||
| })), | ||
| create: jest.fn(), | ||
| MODES: { MULTI_PROCESS: '' } | ||
| })); | ||
|
|
||
| const attachment = { | ||
| ts: '1970-01-01T00:00:00.000Z', | ||
| title: 'Engineering (9 today)', | ||
| fields: [ | ||
| { | ||
| title: 'Out Today:\n', | ||
| value: | ||
| 'Ricardo Mellu, 1 day, until Fri Mar 11\nLoma, 1 day, until Fri Mar 11\nAnitta, 3 hours\nDiego Carlitos, 19 days, until Fri Mar 11\nGabriel Vasconcelos, 5 days, until Fri Mar 11\nJorge Leite, 1 day, until Fri Mar 11\nKevin Aleman, 1 day, until Fri Mar 11\nPierre, 1 day, until Fri Mar 11\nTiago Evangelista Pinto, 1 day, until Fri Mar 11' | ||
| } | ||
| ], | ||
| attachments: [] | ||
| }; | ||
|
|
||
| const mockFn = jest.fn(); | ||
|
|
||
| const Render = () => ( | ||
| <MessageContext.Provider | ||
| value={{ | ||
| onLongPress: () => {}, | ||
| user: { username: 'Marcos' } | ||
| }}> | ||
| <CollapsibleQuote key={0} index={0} attachment={attachment} getCustomEmoji={mockFn} timeFormat='LT' theme='light' /> | ||
| </MessageContext.Provider> | ||
| ); | ||
|
|
||
| describe('CollapsibleQuote', () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid using |
||
| test('rendered', async () => { | ||
| const { findByTestId } = render(<Render />); | ||
| const collapsibleQuoteTouchable = await findByTestId('collapsibleQuoteTouchable'); | ||
| expect(collapsibleQuoteTouchable).toBeTruthy(); | ||
| }); | ||
|
|
||
| test('title exists and is correct', async () => { | ||
| const { findByTestId } = render(<Render />); | ||
| const collapsibleQuoteTitle = await findByTestId('collapsibleQuoteTitle'); | ||
| expect(collapsibleQuoteTitle).toBeTruthy(); | ||
| expect(collapsibleQuoteTitle.props.children).toEqual(attachment.title); | ||
| }); | ||
|
|
||
| test('fields render title correctly', async () => { | ||
| const collapsibleQuote = render(<Render />); | ||
| const collapsibleQuoteTouchable = await collapsibleQuote.findByTestId('collapsibleQuoteTouchable'); | ||
| // open | ||
| fireEvent.press(collapsibleQuoteTouchable); | ||
| const open = within(collapsibleQuoteTouchable); | ||
| const fieldTitleOpen = await open.findByTestId('collapsibleQuoteTouchableFieldTitle'); | ||
| expect(fieldTitleOpen).toBeTruthy(); | ||
| expect(fieldTitleOpen.props.children).toEqual(attachment.fields[0].title); | ||
| // close | ||
| fireEvent.press(collapsibleQuoteTouchable); | ||
| collapsibleQuote.rerender(<Render />); | ||
| const close = within(collapsibleQuoteTouchable); | ||
| const fieldTitleClosed = close.queryByTestId('collapsibleQuoteTouchableFieldTitle'); | ||
| expect(fieldTitleClosed).toBeNull(); | ||
| }); | ||
|
|
||
| test('fields render fields correctly', async () => { | ||
| const collapsibleQuote = render(<Render />); | ||
| const collapsibleQuoteTouchable = await collapsibleQuote.findByTestId('collapsibleQuoteTouchable'); | ||
| // open | ||
| fireEvent.press(collapsibleQuoteTouchable); | ||
| const open = within(collapsibleQuoteTouchable); | ||
| const fieldValueOpen = open.getByLabelText(attachment.fields[0].value.split('\n')[0]); | ||
| expect(fieldValueOpen).toBeTruthy(); | ||
| // close | ||
| fireEvent.press(collapsibleQuoteTouchable); | ||
| collapsibleQuote.rerender(<Render />); | ||
| const close = within(collapsibleQuoteTouchable); | ||
| const fieldValueClosed = close.queryByTestId(attachment.fields[0].value.split('\n')[0]); | ||
| expect(fieldValueClosed).toBeNull(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import { transparentize } from 'color2k'; | ||
| import { dequal } from 'dequal'; | ||
| import React, { useContext, useState } from 'react'; | ||
| import { StyleSheet, Text, View } from 'react-native'; | ||
|
|
||
| import { themes } from '../../../constants/colors'; | ||
| import { IAttachment } from '../../../definitions/IAttachment'; | ||
| import { TGetCustomEmoji } from '../../../definitions/IEmoji'; | ||
| import { CustomIcon } from '../../../lib/Icons'; | ||
| import sharedStyles from '../../../views/Styles'; | ||
| import Markdown from '../../markdown'; | ||
| import MessageContext from '../Context'; | ||
| import Touchable from '../Touchable'; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| button: { | ||
|
dnlsilva marked this conversation as resolved.
|
||
| flex: 1, | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| marginTop: 6, | ||
| alignSelf: 'flex-start', | ||
| borderWidth: 1, | ||
| borderRadius: 4 | ||
| }, | ||
| attachmentContainer: { | ||
| flex: 1, | ||
| borderRadius: 4, | ||
| flexDirection: 'column', | ||
| padding: 8 | ||
| }, | ||
| authorContainer: { | ||
| flex: 1, | ||
| flexDirection: 'row', | ||
| alignItems: 'center' | ||
| }, | ||
| fieldsContainer: { | ||
| flex: 1, | ||
| flexWrap: 'wrap', | ||
| flexDirection: 'row' | ||
| }, | ||
| fieldContainer: { | ||
| flexDirection: 'column', | ||
| padding: 10 | ||
| }, | ||
| fieldTitle: { | ||
| fontSize: 15, | ||
| ...sharedStyles.textBold | ||
| }, | ||
| marginTop: { | ||
| marginTop: 4 | ||
| }, | ||
| marginBottom: { | ||
| marginBottom: 4 | ||
| }, | ||
| title: { | ||
| flex: 1, | ||
| fontSize: 16, | ||
| ...sharedStyles.textMedium | ||
| }, | ||
| touchableContainer: { flex: 1, flexDirection: 'row' }, | ||
|
dnlsilva marked this conversation as resolved.
Outdated
|
||
| markdownFontSize: { fontSize: 15 }, | ||
| iconContainer: { width: 20, height: 20, right: 8, top: 6, justifyContent: 'center', alignItems: 'center' } | ||
| }); | ||
|
|
||
| interface IMessageFields { | ||
| attachment: IAttachment; | ||
| theme: string; | ||
| getCustomEmoji: TGetCustomEmoji; | ||
| } | ||
|
|
||
| interface IMessageReply { | ||
| attachment: IAttachment; | ||
| timeFormat?: string; | ||
| index: number; | ||
| theme: string; | ||
| getCustomEmoji: TGetCustomEmoji; | ||
| } | ||
|
|
||
| const Fields = React.memo( | ||
| ({ attachment, theme, getCustomEmoji }: IMessageFields) => { | ||
| if (!attachment.fields) { | ||
| return null; | ||
| } | ||
|
|
||
| const { baseUrl, user } = useContext(MessageContext); | ||
| return ( | ||
| <View style={styles.fieldsContainer}> | ||
| {attachment.fields.map(field => ( | ||
| <View key={field.title} style={[styles.fieldContainer, { width: field.short ? '50%' : '100%' }]}> | ||
| <Text testID='collapsibleQuoteTouchableFieldTitle' style={[styles.fieldTitle, { color: themes[theme].bodyText }]}> | ||
| {field.title} | ||
| </Text> | ||
| <Markdown | ||
| msg={field?.value || ''} | ||
| baseUrl={baseUrl} | ||
| username={user.username} | ||
| getCustomEmoji={getCustomEmoji} | ||
| theme={theme} | ||
| style={[styles.markdownFontSize]} | ||
| /> | ||
| </View> | ||
| ))} | ||
| </View> | ||
| ); | ||
| }, | ||
| (prevProps, nextProps) => | ||
| dequal(prevProps.attachment.fields, nextProps.attachment.fields) && prevProps.theme === nextProps.theme | ||
| ); | ||
|
|
||
| const CollapsibleQuote = React.memo( | ||
| ({ attachment, index, getCustomEmoji, theme }: IMessageReply) => { | ||
|
dnlsilva marked this conversation as resolved.
Outdated
|
||
| const [open, setOpen] = useState(false); | ||
| if (!attachment) { | ||
| return null; | ||
| } | ||
|
|
||
| const onPress = () => { | ||
| setOpen(!open); | ||
| }; | ||
|
|
||
| let { | ||
| borderColor, | ||
| chatComponentBackground: backgroundColor, | ||
| collapsibleQuoteBorder, | ||
| collapsibleChevron, | ||
| headerTintColor | ||
| } = themes[theme]; | ||
|
|
||
| try { | ||
| if (attachment.color) { | ||
| backgroundColor = transparentize(attachment.color, 0.8); | ||
| borderColor = attachment.color; | ||
| collapsibleQuoteBorder = attachment.color; | ||
| collapsibleChevron = attachment.color; | ||
| headerTintColor = headerTintColor; | ||
| } | ||
| } catch (e) { | ||
| // fallback to default | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <Touchable | ||
|
dnlsilva marked this conversation as resolved.
|
||
| testID='collapsibleQuoteTouchable' | ||
| onPress={onPress} | ||
| style={[ | ||
| styles.button, | ||
| index > 0 && styles.marginTop, | ||
| attachment.description && styles.marginBottom, | ||
| { | ||
| backgroundColor, | ||
| borderLeftColor: collapsibleQuoteBorder, | ||
| borderTopColor: borderColor, | ||
| borderRightColor: borderColor, | ||
| borderBottomColor: borderColor, | ||
| borderLeftWidth: 2 | ||
| } | ||
| ]} | ||
| background={Touchable.Ripple(themes[theme].bannerBackground)}> | ||
| <View style={styles.touchableContainer}> | ||
| <View style={styles.attachmentContainer}> | ||
| <View style={styles.authorContainer}> | ||
| <Text testID='collapsibleQuoteTitle' style={[styles.title, { color: headerTintColor }]}> | ||
| {attachment.title} | ||
| </Text> | ||
| </View> | ||
| {open && <Fields attachment={attachment} getCustomEmoji={getCustomEmoji} theme={theme} />} | ||
| </View> | ||
| <View style={styles.iconContainer}> | ||
| <CustomIcon name={open ? 'chevron-up' : 'chevron-down'} size={22} color={collapsibleChevron} /> | ||
| </View> | ||
| </View> | ||
| </Touchable> | ||
| </> | ||
| ); | ||
| }, | ||
| (prevProps, nextProps) => dequal(prevProps.attachment, nextProps.attachment) && prevProps.theme === nextProps.theme | ||
| ); | ||
|
|
||
| CollapsibleQuote.displayName = 'CollapsibleQuote'; | ||
| Fields.displayName = 'CollapsibleQuoteFields'; | ||
|
|
||
| export default CollapsibleQuote; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.