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