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-04-22 03:05:35 +09:00
|
|
|
class CharacterCounter extends React.PureComponent {
|
2016-08-31 23:15:12 +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-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);
|
2016-09-25 22:26:56 +09:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
CharacterCounter.propTypes = {
|
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
max: PropTypes.number.isRequired
|
|
|
|
}
|
2016-08-26 02:52:55 +09:00
|
|
|
|
|
|
|
export default CharacterCounter;
|