summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/components/Chat/MessageField.js
blob: 947bed493ba53cf3c2afe97d54dd9635397bff77 (plain)
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import 'ba-styles/vendors/emoji-picker-react/emoji-picker-react.css';

class MessageField extends Component {
  constructor(props) {
    super(props);

    this.state = {
      value: props.value || '',
    };

    this.onChange = this.onChange.bind(this);
  }

  onChange(e) {
    const { val } = this.state;
    const { onChange } = this.props;
    const value = e ? e.target.value : val;

    this.setState({ value }, () => {
      if (typeof onChange === 'function') {
        onChange(e, value);
      }
    });
  }

  onPickerkeypress(e) {
    if (e.keyCode === 27 || e.which === 27 || e.key === 'Escape' || e.code === 'Escape') {
      this.closePicker();
    }
  }

  render() {
    const {
      onChange,
      fieldType,
      ...rest
    } = this.props;

    const className = `emoji-text-field emoji-${fieldType}`;
    const { value } = this.state;
    const isInput = fieldType === 'input';
    const ref = (_field) => {
      this._field = _field;
      return this._field;
    };

    return (
      <div className={className}>
        { (isInput) && (<input {...rest} onChange={this.onChange} type="text" ref={ref} value={value} />) }
        { (!isInput) && (<textarea {...rest} onChange={this.onChange} ref={ref} value={value} />) }
      </div>
    );
  }
}

MessageField.propTypes = {
  value: PropTypes.string,
  onChange: PropTypes.func,
  fieldType: PropTypes.string.isRequired
};

MessageField.defaultProps = {
  value: '',
  onChange: () => {},
};

export default MessageField;