-
Notifications
You must be signed in to change notification settings - Fork 1
/
channel.jsx
61 lines (53 loc) · 1.72 KB
/
channel.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from 'react'
import suggestBox from 'suggest-box'
import t from 'patchwork-translations'
function getChannelSuggestions (channels) {
return channels.map(ch => {
return {
cls: 'user',
title: '#'+ch.name,
value: ch.name
}
})
}
const disallowedCharRegex = /[#\s]/g
function sanitize (str) {
return str.replace(disallowedCharRegex, '')
}
export default class ComposerChannel extends React.Component {
static propTypes = {
channels: React.PropTypes.array.isRequired,
onChange: React.PropTypes.func.isRequired,
isReadOnly: React.PropTypes.bool,
value: React.PropTypes.string
}
componentDidMount() {
this.setupSuggest()
}
componentDidUpdate() {
this.setupSuggest()
}
setupSuggest() {
// setup the suggest-box
const input = this.refs && this.refs.input
if (!input || input.isSetup)
return
input.isSetup = true
suggestBox(input, { any: getChannelSuggestions(this.props.channels) }, { cls: 'msg-recipients' })
input.addEventListener('suggestselect', this.onSuggestSelect.bind(this))
}
onChange(e) {
this.props.onChange(sanitize(e.target.value))
}
onSuggestSelect(e) {
this.props.onChange(sanitize(e.detail.value))
}
render() {
if (this.props.isReadOnly) {
return <div><i className="fa fa-hashtag" /> {t('channel')}: {this.props.value||<span style={{color:'#aaa'}}>{t('composer.channelNone')}</span>}</div>
}
return <div className="flex flex-fill recps-inputs">
<span><i className="fa fa-hashtag" /> {t('channel')}:</span> <input className="flex-fill" ref="input" type="text" placeholder={t('composer.ChannelPlaceholder')} value={this.props.value} onChange={this.onChange.bind(this)} />
</div>
}
}