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