Feat / Reusable SearchField + Fixed Messages align bottom flex with scroll (first message starts at bottom) (#151)

* feat(phone/ui): Make SearchField a reusable component

* Fixed conversation scrolling and align bottom

* feat(phone/ui): Autoscroll fix

* feat(phone/ui): Autoscroll fix remove unused deps

* feat(phone/ui): Autoscroll add needed dep for new messages
This commit is contained in:
J Francisco Rader authored and GitHub committed 2021-03-06 16:53:48 -05:00
1 parent 1659fa1654
commit e56ae415dc
13 files changed
+162 -196

No files matched your search

@@ -1,83 +1,22 @@
import React from 'react'; import React from 'react';
import { InputBase } from '@material-ui/core'; import { Box } from '@material-ui/core';
import SearchIcon from '@material-ui/icons/Search';
import { createStyles, Theme, makeStyles } from '@material-ui/core/styles';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useFilteredContacts } from '../../hooks/useFilteredContacts'; import { useFilteredContacts } from '../../hooks/useFilteredContacts';
import { SearchField } from '../../../../ui/components/SearchField';
const useStyles = makeStyles((theme: Theme) =>
createStyles({
bg: {
backgroundColor: theme.palette.background.paper,
display: 'flex',
height: 60,
justifyContent: 'center',
alignItems: 'center',
},
textFieldInput: {
fontSize: 20,
},
inputRoot: {
fontSize: 18,
border: '1px solid',
borderColor: theme.palette.divider,
},
inputInput: {
padding: theme.spacing(1, 4, 1, 4),
transition: theme.transitions.create('width'),
[theme.breakpoints.up('sm')]: {
width: '13ch',
'&:focus': {
width: '20ch',
},
},
},
search: {
position: 'relative',
borderRadius: theme.shape.borderRadius,
marginLeft: 0,
width: '100%',
[theme.breakpoints.up('sm')]: {
marginLeft: theme.spacing(1),
width: 'auto',
},
},
searchIcon: {
padding: theme.spacing(0, 0.5),
height: '100%',
position: 'absolute',
pointerEvents: 'none',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
}),
);
export const SearchContacts = () => { export const SearchContacts = () => {
const { setFilteredContacts } = useFilteredContacts(); const { setFilteredContacts, filteredContacts } = useFilteredContacts();
const classes = useStyles();
const { t } = useTranslation(); const { t } = useTranslation();
return ( return (
<div className={classes.bg}> <Box>
<div className={classes.search}> <SearchField
<div className={classes.searchIcon}> onChange={(e) => setFilteredContacts(e.target.value)}
<SearchIcon /> placeholder={t('APPS_CONTACT_PLACEHOLDER_SEARCH_CONTACTS')}
</div> value={filteredContacts}
<InputBase />
fullWidth </Box>
onChange={(e) => setFilteredContacts(e.target.value)}
placeholder={t('APPS_CONTACT_PLACEHOLDER_SEARCH_CONTACTS')}
classes={{
root: classes.inputRoot,
input: classes.inputInput,
}}
inputProps={{ 'aria-label': 'search', autoFocus: true }}
/>
</div>
</div>
); );
}; };
@@ -32,10 +32,7 @@ export const MessagesApp = () => {
</div> </div>
</Route> </Route>
<Route exact path="/messages"> <Route exact path="/messages">
<> <MessagesList />
<MessagesList />
<NewMessageGroupButton onClick={() => history.push('/messages/new')} />
</>
</Route> </Route>
</Switch> </Switch>
<Switch> <Switch>
@@ -44,6 +41,9 @@ export const MessagesApp = () => {
</Route> </Route>
</Switch> </Switch>
</AppContent> </AppContent>
<Route exact path="/messages">
<NewMessageGroupButton onClick={() => history.push('/messages/new')} />
</Route>
</AppWrapper> </AppWrapper>
</MessagesThemeProvider> </MessagesThemeProvider>
); );
@@ -12,7 +12,7 @@ interface IProps {
} }
const useStyles = makeStyles({ const useStyles = makeStyles({
root: { position: 'absolute', bottom: 0, width: '100%' }, root: { width: '100%' },
}); });
const MessageInput = ({ messageGroupId, onAddImageClick }: IProps) => { const MessageInput = ({ messageGroupId, onAddImageClick }: IProps) => {
@@ -6,7 +6,7 @@ import { Fab } from '@material-ui/core';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
position: 'absolute', position: 'absolute',
bottom: theme.spacing(2), bottom: theme.spacing(5),
right: theme.spacing(3), right: theme.spacing(3),
}, },
})); }));
@@ -1,37 +0,0 @@
import React from 'react';
import { useTranslation } from 'react-i18next';
import { InputBase, Paper } from '@material-ui/core';
import SearchIcon from '@material-ui/icons/Search';
import useStyles from './list.styles';
interface IProps {
value: string;
handleChange: (event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => void;
}
const MessageSearch = ({ value, handleChange }: IProps) => {
const { t } = useTranslation();
const classes = useStyles();
return (
<Paper elevation={24} variant="outlined" className={classes.bg}>
<div className={classes.search}>
<div className={classes.searchIcon}>
<SearchIcon />
</div>
<InputBase
value={value}
onChange={handleChange}
placeholder={t('APPS_MESSAGES_SEARCH_PLACEHOLDER')}
classes={{
root: classes.inputRoot,
input: classes.inputInput,
}}
inputProps={{ 'aria-label': 'search' }}
/>
</div>
</Paper>
);
};
export default MessageSearch;
@@ -1,17 +1,19 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { List } from '@material-ui/core'; import { Box, List } from '@material-ui/core';
import { MessageGroup } from '../../../../common/typings/messages'; import { MessageGroup } from '../../../../common/typings/messages';
import Nui from '../../../../os/nui-events/utils/Nui'; import Nui from '../../../../os/nui-events/utils/Nui';
import useMessages from '../../hooks/useMessages'; import useMessages from '../../hooks/useMessages';
import MessageSearch from './MessageSearch';
import MessageGroupItem from './MessageGroupItem'; import MessageGroupItem from './MessageGroupItem';
import useStyles from './list.styles'; import useStyles from './list.styles';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { goToConversation } from '../../utils/goToConversation'; import { goToConversation } from '../../utils/goToConversation';
import { SearchField } from '../../../../ui/components/SearchField';
import { useTranslation } from 'react-i18next';
const MessagesList = (): any => { const MessagesList = (): any => {
const classes = useStyles(); const classes = useStyles();
const history = useHistory(); const history = useHistory();
const { t } = useTranslation();
const { messageGroups, createMessageGroupResult, clearMessageGroupResult } = useMessages(); const { messageGroups, createMessageGroupResult, clearMessageGroupResult } = useMessages();
@@ -46,18 +48,28 @@ const MessagesList = (): any => {
}; };
return ( return (
<> <Box display="flex" flexDirection="column">
<MessageSearch value={searchValue} handleChange={(e) => setSearchValue(e.target.value)} /> <Box>
<List className={classes.root}> <SearchField
{filteredGroups.map((messageGroup) => ( value={searchValue}
<MessageGroupItem onChange={(e) => setSearchValue(e.target.value)}
key={messageGroup.groupId} placeholder={t('APPS_MESSAGES_SEARCH_PLACEHOLDER')}
messageGroup={messageGroup} />
handleClick={handleClick} </Box>
/> <Box display="flex" flexDirection="column">
))} <Box className={classes.root}>
</List> <List>
</> {filteredGroups.map((messageGroup) => (
<MessageGroupItem
key={messageGroup.groupId}
messageGroup={messageGroup}
handleClick={handleClick}
/>
))}
</List>
</Box>
</Box>
</Box>
); );
}; };
@@ -1,61 +1,10 @@
import { makeStyles, fade } from '@material-ui/core'; import { makeStyles } from '@material-ui/core';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
height: '100%', height: '580px',
overflowY: 'auto', overflowY: 'auto',
}, },
bg: {
display: 'flex',
height: 60,
justifyContent: 'center',
alignItems: 'center',
},
textFieldInput: {
fontSize: 20,
},
inputRoot: {
fontWeight: 400,
fontSize: 18,
border: '1px solid',
borderColor: theme.palette.divider,
},
inputInput: {
padding: theme.spacing(1, 4, 1, 4),
// vertical padding + font size from searchIcon
transition: theme.transitions.create('width'),
width: '100%',
[theme.breakpoints.up('sm')]: {
width: '13ch',
'&:focus': {
width: '24ch',
},
},
},
search: {
position: 'relative',
borderRadius: theme.shape.borderRadius,
backgroundColor: fade(theme.palette.common.white, 0.25),
'&:hover': {
backgroundColor: fade(theme.palette.common.white, 0.25),
},
marginLeft: 0,
width: '100%',
[theme.breakpoints.up('sm')]: {
marginLeft: theme.spacing(1),
width: 'auto',
},
},
searchIcon: {
padding: theme.spacing(0, 0.5),
height: '100%',
position: 'absolute',
pointerEvents: 'none',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
})); }));
export default useStyles; export default useStyles;
@@ -30,15 +30,29 @@ const Conversation = ({ activeMessageGroup, messages, onClickDisplay }: IProps)
isOpen={imageModalOpen} isOpen={imageModalOpen}
messageGroupId={activeMessageGroup.groupId} messageGroupId={activeMessageGroup.groupId}
/> />
<Box id={CONVERSATION_ELEMENT_ID} height="85%" py={6} className={classes.overflowAutoY}> <Box
{messages.map((message) => ( id={CONVERSATION_ELEMENT_ID}
<MessageBubble height="85%"
onClickDisplay={onClickDisplay} pt={6}
key={message.id} style={{ flex: 1, display: 'flex', overflowY: 'auto' }}
message={message} >
isGroupChat={activeMessageGroup?.isGroupChat} <Box
/> style={{
))} display: 'flex',
flexDirection: 'column',
minHeight: 'min-content',
width: '100%',
}}
>
{messages.map((message) => (
<MessageBubble
onClickDisplay={onClickDisplay}
key={message.id}
message={message}
isGroupChat={activeMessageGroup?.isGroupChat}
/>
))}
</Box>
</Box> </Box>
<MessageInput <MessageInput
/* I should do some groupDiplay here */ /* I should do some groupDiplay here */
@@ -3,6 +3,11 @@ import React from 'react';
import { Message } from '../../../../common/typings/messages'; import { Message } from '../../../../common/typings/messages';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: {
'&:first-child': {
marginTop: 'auto',
},
},
imageMessage: { imageMessage: {
width: '80%', width: '80%',
maxWidth: '80%', maxWidth: '80%',
@@ -55,7 +60,7 @@ export const MessageBubble = ({
}) => { }) => {
const classes = useStyles(); const classes = useStyles();
return ( return (
<div> <div className={classes.root}>
<Paper className={message.isMine ? classes.mySms : classes.sms} variant="outlined"> <Paper className={message.isMine ? classes.mySms : classes.sms} variant="outlined">
<Box className={classes.message}> <Box className={classes.message}>
{isImage(message.message) ? ( {isImage(message.message) ? (
@@ -82,13 +82,13 @@ export const MessageModal = () => {
}, [groupId, setActiveMessageGroup]); }, [groupId, setActiveMessageGroup]);
useEffect(() => { useEffect(() => {
if (messages) { if (isLoaded && messages) {
const element = document.getElementById(CONVERSATION_ELEMENT_ID); const element = document.getElementById(CONVERSATION_ELEMENT_ID);
if (element) { if (element) {
element.scrollTop = element.scrollHeight; element.scrollTop = element.scrollHeight;
} }
} }
}, [messages]); }, [isLoaded, messages]);
// sends all unread messages // sends all unread messages
useEffect(() => { useEffect(() => {
@@ -26,10 +26,9 @@ const useStyles = makeStyles({
bottom: '0', bottom: '0',
}, },
overflowAutoY: { overflowAutoY: {
flex: 6, height: '550px',
overflowY: 'auto', overflowY: 'auto',
overflowX: 'hidden', overflowX: 'hidden',
justifyContent: 'end',
}, },
groupdisplay: { groupdisplay: {
width: '300px', width: '300px',
+1 -1
View File
@@ -91,7 +91,7 @@
"APPS_CONTACT_UPDATE_FAILED": "Failed to update contact!", "APPS_CONTACT_UPDATE_FAILED": "Failed to update contact!",
"APPS_CONTACT_DELETE_SUCCESS": "Successfully deleted contact!", "APPS_CONTACT_DELETE_SUCCESS": "Successfully deleted contact!",
"APPS_CONTACT_DELETE_FAILED": "Failed to delete contact!", "APPS_CONTACT_DELETE_FAILED": "Failed to delete contact!",
"APPS_CONTACT_PLACEHOLDER_SEARCH_CONTACTS": "Search contacts", "APPS_CONTACT_PLACEHOLDER_SEARCH_CONTACTS": "Search contacts...",
"APPS_CONTACT_MODAL_PLACEHOLDER_NAME": "Name", "APPS_CONTACT_MODAL_PLACEHOLDER_NAME": "Name",
"APPS_CONTACT_MODAL_PLACEHOLDER_NUMBER": "Number", "APPS_CONTACT_MODAL_PLACEHOLDER_NUMBER": "Number",
"APPS_CONTACT_MODAL_PLACEHOLDER_AVATAR": "Avatar", "APPS_CONTACT_MODAL_PLACEHOLDER_AVATAR": "Avatar",
+85
View File
@@ -0,0 +1,85 @@
import React from 'react';
import { emphasize, InputBase, makeStyles, Paper } from '@material-ui/core';
import SearchIcon from '@material-ui/icons/Search';
const useStyles = makeStyles((theme) => ({
bg: {
display: 'flex',
height: '60px',
justifyContent: 'center',
alignItems: 'center',
},
textFieldInput: {
fontSize: 20,
},
inputRoot: {
fontWeight: 400,
fontSize: 18,
border: '1px solid',
borderColor: theme.palette.divider,
},
inputInput: {
padding: theme.spacing(1, 4, 1, 4),
// vertical padding + font size from searchIcon
transition: theme.transitions.create(['width', 'border']),
width: '16ch',
'&:focus': {
width: '24ch',
borderLeft: `2px solid ${theme.palette.primary.main} `,
},
},
search: {
position: 'relative',
borderRadius: theme.shape.borderRadius,
backgroundColor: emphasize(theme.palette.background.paper, 0.21),
'&:hover': {
backgroundColor: emphasize(theme.palette.background.paper, 0.28),
},
marginLeft: theme.spacing(1),
width: 'auto',
},
searchIcon: {
padding: theme.spacing(0, 0.7),
height: '100%',
position: 'absolute',
pointerEvents: 'none',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
}));
interface IProps {
onChange: (event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => void;
value: string;
placeholder?: string;
}
const DEFAULT_PROPS = {
onChange: () => {},
value: '',
placeholder: 'Search...',
};
export const SearchField = ({ value, onChange, placeholder }: IProps = DEFAULT_PROPS) => {
const classes = useStyles();
return (
<Paper elevation={24} variant="outlined" className={classes.bg}>
<div className={classes.search}>
<div className={classes.searchIcon}>
<SearchIcon />
</div>
<InputBase
value={value}
onChange={onChange}
placeholder={placeholder}
classes={{
root: classes.inputRoot,
input: classes.inputInput,
}}
inputProps={{ 'aria-label': 'search' }}
/>
</div>
</Paper>
);
};