Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(channel-web): convos view arrow navigation
- Loading branch information
Showing
4 changed files
with
149 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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,20 @@ | ||
import React from 'react' | ||
|
||
export default ({ height, width }) => ( | ||
<i> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
version="1.1" | ||
x="0px" | ||
y="0px" | ||
width={width || 20} | ||
height={height || 20} | ||
viewBox="0 0 357 357" | ||
space="preserve" | ||
> | ||
<g id="add"> | ||
<path d="M357,204H204v153h-51V204H0v-51h153V0h51v153h153V204z" /> | ||
</g> | ||
</svg> | ||
</i> | ||
) |
97 changes: 97 additions & 0 deletions
97
modules/channel-web/src/views/lite/side/ConversationList.jsx
This file contains 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,97 @@ | ||
import React from 'react' | ||
import style from './style.scss' | ||
|
||
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now' | ||
import Add from '../icons/Add' | ||
|
||
const ConversationListItem = ({ conversation, onClick, hasFocus }) => { | ||
const title = conversation.title || conversation.message_author || 'Untitled Conversation' | ||
const date = distanceInWordsToNow(new Date(conversation.message_sent_on || conversation.created_on)) | ||
const message = conversation.message_text || '...' | ||
|
||
return ( | ||
<div className={`bp-item ${style.item} ${hasFocus && style.focus}`} onClick={onClick}> | ||
<div className={style.right}> | ||
<div className={'bp-title ' + style.title}> | ||
<div className={style.name}>{title}</div> | ||
<div className={style.date}> | ||
<span>{date}</span> | ||
</div> | ||
</div> | ||
<div className={'bp-preview ' + style.text}>{message}</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
class ConversationList extends React.Component { | ||
state = { | ||
focusIdx: null | ||
} | ||
|
||
changeFocus = step => { | ||
let focusIdx = this.state.focusIdx || 0 | ||
focusIdx += step | ||
|
||
if (focusIdx > this.props.conversations.length) { | ||
focusIdx = 0 | ||
} else if (focusIdx < 0) { | ||
focusIdx = this.props.conversations.length | ||
} | ||
|
||
this.setState({ focusIdx }) | ||
} | ||
|
||
componentDidMount() { | ||
this.main.focus() | ||
} | ||
|
||
componentDidUpdate(_, prevState) { | ||
if (this.state.focusIdx === this.props.conversations.length) { | ||
this.btn.focus() | ||
} else if (prevState.focusIdx === this.props.conversations.length) { | ||
this.main.focus() | ||
} | ||
} | ||
|
||
handleKeyDown = e => { | ||
if (e.key === 'ArrowDown' || e.key === 'ArrowLeft') { | ||
this.changeFocus(1) | ||
} else if (e.key == 'ArrowUp' || e.key == 'ArrowRight') { | ||
this.changeFocus(-1) | ||
} else if (e.key == 'Enter' && this.state.focusIdx && this.state.focusIdx < this.props.conversations.length) { | ||
const convoId = this.props.conversations[this.state.focusIdx].id | ||
this.props.onConversationClicked(convoId) | ||
} | ||
} | ||
|
||
render() { | ||
const { conversations, createConversation, onConversationClicked } = this.props | ||
return ( | ||
<div | ||
tabindex="0" | ||
ref={el => (this.main = el)} | ||
className={`bp-list-convo ${style.list}`} | ||
onKeyDown={this.handleKeyDown} | ||
> | ||
{conversations.map((convo, idx) => ( | ||
<ConversationListItem | ||
key={convo.id} | ||
hasFocus={this.state.focusIdx == idx} | ||
conversation={convo} | ||
onClick={onConversationClicked.bind(this, convo.id)} | ||
/> | ||
))} | ||
<button | ||
ref={el => (this.btn = el)} | ||
className={'bp-new-convo-btn ' + style.addConvoBtn} | ||
onClick={createConversation} | ||
> | ||
<Add width={15} height={15} /> | ||
</button> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default ConversationList |
This file contains 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 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