2020-03-27 13:59:38 -07:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-09-22 14:05:17 -07:00
|
|
|
import { injectIntl, defineMessages } from 'react-intl';
|
2020-03-27 13:59:38 -07:00
|
|
|
import { length } from 'stringz';
|
2021-09-22 14:05:17 -07:00
|
|
|
import ProgressCircle from 'soapbox/components/progress_circle';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-09-22 14:05:17 -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
|
|
|
|
2021-09-22 14:05:17 -07:00
|
|
|
/**
|
|
|
|
* Renders a character counter
|
|
|
|
* @param {string} props.text - text to use to measure
|
|
|
|
* @param {number} props.max - max text allowed
|
|
|
|
*/
|
2021-10-07 13:45:41 -07:00
|
|
|
class VisualCharacterCounter extends React.PureComponent {
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-09-22 14:05:17 -07:00
|
|
|
render() {
|
|
|
|
const { intl, text, max } = this.props;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-09-22 14:05:17 -07:00
|
|
|
const textLength = length(text);
|
|
|
|
const progress = textLength / max;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-09-22 14:05:17 -07:00
|
|
|
return (
|
2021-10-07 13:45:41 -07:00
|
|
|
<div className='visual-character-counter'>
|
|
|
|
<ProgressCircle
|
|
|
|
title={intl.formatMessage(messages.title, { chars: textLength, maxChars: max })}
|
|
|
|
progress={progress}
|
|
|
|
radius={10}
|
|
|
|
stroke={3}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-09-22 14:05:17 -07:00
|
|
|
);
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-09-22 14:05:17 -07:00
|
|
|
|
2021-10-07 13:45:41 -07:00
|
|
|
VisualCharacterCounter.propTypes = {
|
2021-09-22 14:05:17 -07:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
max: PropTypes.number.isRequired,
|
|
|
|
};
|
|
|
|
|
2021-10-07 13:45:41 -07:00
|
|
|
export default injectIntl(VisualCharacterCounter);
|