bigbuffet-rw/app/soapbox/features/compose/components/character_counter.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import React from 'react';
import PropTypes from 'prop-types';
import { injectIntl, defineMessages } from 'react-intl';
2020-03-27 13:59:38 -07:00
import { length } from 'stringz';
import ProgressCircle from 'soapbox/components/progress_circle';
2020-03-27 13:59:38 -07:00
const messages = defineMessages({
title: { id: 'compose.character_counter.title', defaultMessage: 'Used {chars} out of {maxChars} characters' },
});
2020-03-27 13:59:38 -07:00
/**
* Renders a character counter
* @param {string} props.text - text to use to measure
* @param {number} props.max - max text allowed
*/
class CharacterCounter extends React.PureComponent {
2020-03-27 13:59:38 -07:00
render() {
const { intl, text, max } = this.props;
2020-03-27 13:59:38 -07:00
const textLength = length(text);
const progress = textLength / max;
2020-03-27 13:59:38 -07:00
return (
<ProgressCircle
title={intl.formatMessage(messages.title, { chars: textLength, maxChars: max })}
progress={progress}
radius={10}
2021-09-26 21:30:37 -07:00
stroke={3}
/>
);
2020-03-27 13:59:38 -07:00
}
}
CharacterCounter.propTypes = {
intl: PropTypes.object.isRequired,
text: PropTypes.string.isRequired,
max: PropTypes.number.isRequired,
};
export default injectIntl(CharacterCounter);