2017-04-21 20:05:35 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2016-08-31 16:15:12 +02:00
|
|
|
|
2017-04-21 20:05:35 +02:00
|
|
|
class CharacterCounter extends React.PureComponent {
|
2016-08-31 16:15:12 +02:00
|
|
|
|
2017-04-17 10:34:33 +02:00
|
|
|
checkRemainingText (diff) {
|
|
|
|
if (diff <= 0) {
|
2017-04-23 04:26:55 +02:00
|
|
|
return <span className='character-counter character-counter--over'>{diff}</span>;
|
2017-04-17 10:34:33 +02:00
|
|
|
}
|
2017-04-23 04:26:55 +02:00
|
|
|
return <span className='character-counter'>{diff}</span>;
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
2017-04-17 10:34:33 +02:00
|
|
|
|
2016-08-25 19:52:55 +02:00
|
|
|
render () {
|
2017-02-27 13:32:41 +01:00
|
|
|
const diff = this.props.max - this.props.text.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "_").length;
|
2016-09-25 15:26:56 +02:00
|
|
|
|
2017-04-17 10:34:33 +02:00
|
|
|
return this.checkRemainingText(diff);
|
2016-08-25 19:52:55 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CharacterCounter.propTypes = {
|
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
max: PropTypes.number.isRequired
|
|
|
|
}
|
2016-08-25 19:52:55 +02:00
|
|
|
|
|
|
|
export default CharacterCounter;
|