2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-02-14 00:40:04 +00:00
|
|
|
import { length } from 'stringz'
|
2019-08-09 17:06:27 +01:00
|
|
|
|
2020-04-23 07:13:29 +01:00
|
|
|
/**
|
|
|
|
* Renders a character counter
|
|
|
|
* @param {string} props.text - text to use to measure
|
|
|
|
* @param {number} props.max - max text allowed
|
|
|
|
*/
|
2020-08-18 01:57:35 +01:00
|
|
|
class CharacterCounter extends React.PureComponent {
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-04-24 04:17:27 +01:00
|
|
|
render() {
|
2020-04-16 07:00:43 +01:00
|
|
|
const { text, max } = this.props
|
2020-04-23 07:13:29 +01:00
|
|
|
|
2021-02-16 02:47:56 +00:00
|
|
|
const actualRadius = 10
|
|
|
|
const radius = 8
|
2020-02-14 00:40:04 +00:00
|
|
|
const circumference = 2 * Math.PI * radius
|
2020-04-16 07:00:43 +01:00
|
|
|
const diff = Math.min(length(text), max) / max
|
2020-02-14 00:40:04 +00:00
|
|
|
const dashoffset = circumference * (1 - diff)
|
2021-02-16 02:47:56 +00:00
|
|
|
const circleClass = length(text) > max ? _s.strokeError : _s.strokeBrand
|
2019-08-09 17:06:27 +01:00
|
|
|
|
2020-01-27 19:46:42 +00:00
|
|
|
return (
|
2020-08-18 21:49:11 +01:00
|
|
|
<div className={[_s.d, _s.mr10, _s.jcCenter, _s.aiCenter].join(' ')}>
|
2020-04-24 04:17:27 +01:00
|
|
|
<svg
|
|
|
|
width={actualRadius * 2}
|
|
|
|
height={actualRadius * 2}
|
|
|
|
viewBox={`0 0 ${actualRadius * 2} ${actualRadius * 2}`}
|
|
|
|
>
|
|
|
|
<circle
|
|
|
|
fill='none'
|
|
|
|
cx={actualRadius}
|
|
|
|
cy={actualRadius}
|
|
|
|
r={radius}
|
|
|
|
fill='none'
|
|
|
|
strokeWidth='2'
|
2021-02-16 02:47:56 +00:00
|
|
|
className={_s.strokeSecondary}
|
2020-04-24 04:17:27 +01:00
|
|
|
/>
|
|
|
|
<circle
|
|
|
|
style={{
|
2020-02-14 00:40:04 +00:00
|
|
|
strokeDashoffset: dashoffset,
|
|
|
|
strokeDasharray: circumference,
|
|
|
|
}}
|
|
|
|
fill='none'
|
2020-02-28 15:20:47 +00:00
|
|
|
cx={actualRadius}
|
|
|
|
cy={actualRadius}
|
|
|
|
r={radius}
|
2021-02-16 02:47:56 +00:00
|
|
|
strokeWidth='2.25'
|
2020-02-14 00:40:04 +00:00
|
|
|
strokeLinecap='round'
|
2021-02-16 02:47:56 +00:00
|
|
|
className={circleClass}
|
2020-02-14 00:40:04 +00:00
|
|
|
/>
|
|
|
|
</svg>
|
2020-01-27 19:46:42 +00:00
|
|
|
</div>
|
|
|
|
)
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-08-18 01:57:35 +01:00
|
|
|
|
|
|
|
CharacterCounter.propTypes = {
|
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
max: PropTypes.number.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CharacterCounter
|