2017-05-03 02:04:16 +02:00
|
|
|
import React from 'react';
|
2017-04-21 20:05:35 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2017-04-25 17:37:51 +02:00
|
|
|
import { length } from 'stringz';
|
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-05-12 14:44:10 +02:00
|
|
|
static propTypes = {
|
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
max: PropTypes.number.isRequired
|
|
|
|
};
|
|
|
|
|
2017-04-17 10:34:33 +02:00
|
|
|
checkRemainingText (diff) {
|
2017-04-30 14:58:33 +02:00
|
|
|
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-04-25 17:37:51 +02:00
|
|
|
const diff = this.props.max - length(this.props.text);
|
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
|
|
|
}
|
|
|
|
|
2016-08-25 19:52:55 +02:00
|
|
|
export default CharacterCounter;
|