aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/MessageInput.js
blob: 5d603a4b42514bddeaaad8eec68126d0c3d30de7 (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React from 'react';
import PropTypes from 'prop-types';

import { MessageType } from './Types';

import Icon from './Icon';
import Button from './Button';

export default class MessageInput extends React.Component {
    constructor(props) {
        super(props)
        this.textarea = React.createRef();
        this.fileinput = React.createRef();
        this.state = {
            isActive: false,
            mid: this.props.data.mid,
            rid: this.props.data.rid || 0,
            to: this.props.data.to || {},
            body: this.props.text || '',
            attach: ''
        }
    }

    handleCtrlEnter = (event) => {
        if (event.ctrlKey && (event.charCode == 10 || event.charCode == 13)) {
            this.onSubmit({})
        }
    }

    onSubmit = (event) => {
        if (event.preventDefault) event.preventDefault();
        const input = this.fileinput.current;
        this.props.onSend({
            mid: this.state.mid,
            rid: this.state.rid,
            body: this.state.body,
            attach: this.state.attach ? input.files[0] : '',
            to: this.state.to
        })
        this.setState({
            body: '',
            attach: ''
        })
    }

    componentDidMount() {
        this.textarea.current.focus()
    }
    textChanged = (event) => {
        this.setState({
            body: event.target.value
        })
        const el = this.textarea.current;
        const offset = el.offsetHeight - el.clientHeight;
        const height = el.scrollHeight + offset;
        el.style.height = `${height + offset}px`;
    }
    attachmentChanged = (event) => {
        this.setState({
            attach: event.target.value
        })
    }
    openfile = () => {
        const input = this.fileinput.current;
        if (this.state.attach) {
            this.setState({
                attach: ''
            })
        } else {
            input.click()
        }
    }
    render() {
        return (
            <form className="msg-comment-target">
                <div style={commentStyle}>
                    <textarea name="body" onChange={this.textChanged} onKeyPress={this.handleCtrlEnter}
                            ref={this.textarea} style={textInputStyle} value={this.state.body}
                            rows={this.props.rows || "1"} placeholder={this.props.children} />
                    <div style={inputBarStyle}>
                        <div style={this.state.attach ? activeStyle : inactiveStyle}
                            onClick={this.openfile}>
                            <Icon name="ei-camera" size="s" />
                            <input type="file" accept="image/jpg,image/png" onClick={e => e.stopPropagation()}
                                style={{ display: 'none' }} ref={this.fileinput} value={this.state.attach}
                                onChange={this.attachmentChanged} />
                        </div>
                        <Button onClick={this.onSubmit}><Icon name="ei-envelope" size="s"/>Send</Button>
                    </div>
                </div>
            </form>
        )
    }
}

const commentStyle = {
    display: 'flex',
    flexDirection: 'column',
    width: '100%',
    marginTop: '10px'
}

const inactiveStyle = {
    cursor: 'pointer',
    color: '#888'
};
const activeStyle = {
    cursor: 'pointer',
    color: 'green'
};

const inputBarStyle = {
    display: 'flex',
    borderTop: '1px #eee solid',
    alignItems: 'center',
    justifyContent: 'space-between',
    padding: '3px'
}

const textInputStyle = {
    overflow: 'hidden',
    resize: 'none',
    display: 'block',
    boxSizing: 'border-box',
    border: 0,
    outline: 'none',
    padding: '4px',
    resize: 'none'
}

MessageInput.propTypes = {
    data: MessageType.isRequired,
    onSend: PropTypes.func.isRequired,
    rows: PropTypes.string,
    text: PropTypes.string
};