Total Complexity | 3 |
Complexity/F | 0 |
Lines of Code | 48 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import React from "react"; |
||
2 | import { countNumberOfWords } from "./helpers"; |
||
3 | |||
4 | export interface SimpleWordCounterProps { |
||
5 | /** Maximum amount of words before passing the optimal range. The Progress Ring color correlates with this number. */ |
||
6 | wordLimit: number; |
||
7 | /** The text who's words are being counted. */ |
||
8 | value: string; |
||
9 | /** String before current number of words */ |
||
10 | beforeText?: string; |
||
11 | /** String after counter when user is under the max */ |
||
12 | underMaxMessage?: string; |
||
13 | /** String after counter when user is over the max */ |
||
14 | overMaxMessage?: string; |
||
15 | /** Keep absolute value of max words - number of words */ |
||
16 | absoluteValue?: boolean; |
||
17 | } |
||
18 | |||
19 | const SimpleWordCounter: React.FunctionComponent<SimpleWordCounterProps> = ({ |
||
20 | wordLimit, |
||
21 | value, |
||
22 | beforeText, |
||
23 | underMaxMessage, |
||
24 | overMaxMessage, |
||
25 | absoluteValue, |
||
26 | }): React.ReactElement => { |
||
27 | const minWords = 0; |
||
28 | const numOfWords = countNumberOfWords(value); |
||
29 | return ( |
||
30 | <span |
||
31 | role="progressbar" |
||
32 | aria-valuenow={numOfWords} |
||
33 | aria-valuemin={minWords} |
||
34 | aria-valuemax={wordLimit} |
||
35 | > |
||
36 | {beforeText && <span>{beforeText} </span>} |
||
37 | <span data-c-color={`${numOfWords <= wordLimit ? "go" : "stop"}`}> |
||
38 | {absoluteValue |
||
39 | ? Math.abs(wordLimit - numOfWords) |
||
40 | : wordLimit - numOfWords} |
||
41 | </span> |
||
42 | <span> {numOfWords <= wordLimit ? underMaxMessage : overMaxMessage}</span> |
||
43 | </span> |
||
44 | ); |
||
45 | }; |
||
46 | |||
47 | export default SimpleWordCounter; |
||
48 |