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