2023-05-23 04:52:27 -04:00
|
|
|
import { PureComponent } from 'react';
|
2017-04-21 14:05:35 -04:00
|
|
|
import PropTypes from 'prop-types';
|
2017-04-25 11:37:51 -04:00
|
|
|
import { length } from 'stringz';
|
2016-08-31 10:15:12 -04:00
|
|
|
|
2023-05-23 04:52:27 -04:00
|
|
|
export default class CharacterCounter extends PureComponent {
|
2016-08-31 10:15:12 -04:00
|
|
|
|
2017-05-12 08:44:10 -04:00
|
|
|
static propTypes = {
|
|
|
|
text: PropTypes.string.isRequired,
|
2017-05-20 11:31:47 -04:00
|
|
|
max: PropTypes.number.isRequired,
|
2017-05-12 08:44:10 -04:00
|
|
|
};
|
|
|
|
|
2017-04-17 04:34:33 -04:00
|
|
|
checkRemainingText (diff) {
|
2017-04-30 08:58:33 -04:00
|
|
|
if (diff < 0) {
|
2017-04-22 22:26:55 -04:00
|
|
|
return <span className='character-counter character-counter--over'>{diff}</span>;
|
2017-04-17 04:34:33 -04:00
|
|
|
}
|
2017-07-28 18:06:29 -04:00
|
|
|
|
2017-04-22 22:26:55 -04:00
|
|
|
return <span className='character-counter'>{diff}</span>;
|
2017-04-21 14:05:35 -04:00
|
|
|
}
|
2017-04-17 04:34:33 -04:00
|
|
|
|
2016-08-25 13:52:55 -04:00
|
|
|
render () {
|
2017-04-25 11:37:51 -04:00
|
|
|
const diff = this.props.max - length(this.props.text);
|
2017-04-17 04:34:33 -04:00
|
|
|
return this.checkRemainingText(diff);
|
2016-08-25 13:52:55 -04:00
|
|
|
}
|
|
|
|
|
2017-04-21 14:05:35 -04:00
|
|
|
}
|