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