2022-03-21 11:09:01 -07:00
|
|
|
import classNames from 'classnames';
|
2021-10-07 13:45:41 -07:00
|
|
|
import PropTypes from 'prop-types';
|
2022-01-10 14:17:52 -08:00
|
|
|
import React from 'react';
|
2021-10-07 13:45:41 -07:00
|
|
|
import { length } from 'stringz';
|
|
|
|
|
|
|
|
export default class TextCharacterCounter extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
max: PropTypes.number.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
checkRemainingText(diff) {
|
2022-03-21 11:09:01 -07:00
|
|
|
return (
|
|
|
|
<span
|
|
|
|
className={classNames('text-sm font-semibold', {
|
|
|
|
'text-gray-400': diff >= 0,
|
|
|
|
'text-danger-600': diff < 0,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{diff}
|
|
|
|
</span>
|
|
|
|
);
|
2021-10-07 13:45:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const diff = this.props.max - length(this.props.text);
|
|
|
|
return this.checkRemainingText(diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|