2017-05-03 03:04:16 +03:00
|
|
|
import React from 'react';
|
2017-04-21 21:05:35 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-04-25 18:37:51 +03:00
|
|
|
import { length } from 'stringz';
|
2016-08-31 17:15:12 +03:00
|
|
|
|
2017-06-23 20:36:54 +03:00
|
|
|
export default class CharacterCounter extends React.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
|
|
|
}
|