2016-08-31 17:15:12 +03:00
|
|
|
import PureRenderMixin from 'react-addons-pure-render-mixin';
|
|
|
|
|
2016-08-25 20:52:55 +03:00
|
|
|
const CharacterCounter = React.createClass({
|
2016-08-31 17:15:12 +03:00
|
|
|
|
2016-08-25 20:52:55 +03:00
|
|
|
propTypes: {
|
2016-09-25 16:26:56 +03:00
|
|
|
text: React.PropTypes.string.isRequired,
|
|
|
|
max: React.PropTypes.number.isRequired
|
2016-08-25 20:52:55 +03:00
|
|
|
},
|
|
|
|
|
2016-08-31 17:15:12 +03:00
|
|
|
mixins: [PureRenderMixin],
|
|
|
|
|
2017-04-17 11:34:33 +03:00
|
|
|
checkRemainingText (diff) {
|
|
|
|
if (diff <= 0) {
|
|
|
|
return <span style={{ fontSize: '16px', cursor: 'default', color: '#ff5050' }}>{diff}</span>;
|
|
|
|
}
|
|
|
|
return <span style={{ fontSize: '16px', cursor: 'default' }}>{diff}</span>;
|
|
|
|
},
|
|
|
|
|
2016-08-25 20:52:55 +03:00
|
|
|
render () {
|
2017-02-27 14:32:41 +02:00
|
|
|
const diff = this.props.max - this.props.text.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "_").length;
|
2016-09-25 16:26:56 +03:00
|
|
|
|
2017-04-17 11:34:33 +03:00
|
|
|
return this.checkRemainingText(diff);
|
2016-08-25 20:52:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
export default CharacterCounter;
|