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