about summary refs log tree commit diff
path: root/app/assets/javascripts/components/features/compose/components/character_counter.jsx
blob: 31d82d4d2fa3df7cbbd29a251d7e9e8616e7eb03 (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
import PropTypes from 'prop-types';

class CharacterCounter extends React.PureComponent {

  checkRemainingText (diff) {
    if (diff <= 0) {
      return <span className='character-counter character-counter--over'>{diff}</span>;
    }
    return <span className='character-counter'>{diff}</span>;
  }

  render () {
    const diff = this.props.max - this.props.text.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "_").length;

    return this.checkRemainingText(diff);
  }

}

CharacterCounter.propTypes = {
  text: PropTypes.string.isRequired,
  max: PropTypes.number.isRequired
}

export default CharacterCounter;