-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathwithCharLimit.js
42 lines (37 loc) · 968 Bytes
/
withCharLimit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import useCharLimit from './useCharLimit';
const withCharLimit = ( limit ) => ( Component ) => {
return ( props ) => {
const { value: content, onChange } = props;
const { charCount, errorMessage, handleContentChange } = useCharLimit(
content,
onChange,
limit
);
const colorState = charCount > limit ? 'red' : 'green';
const progressBarStyle = {
width: `${ ( charCount / limit ) * limit }%`,
height: '5px',
backgroundColor: colorState,
marginTop: '5px',
};
return (
<>
<Component
{ ...props }
onChange={ handleContentChange }
value={ content }
/>
{ errorMessage && (
<div className="error-message" style={ { color: 'red' } }>
{ errorMessage }
</div>
) }
<div className="progress-bar" style={ progressBarStyle }></div>
<div className="char-count" style={ { color: colorState } }>
{ charCount } / { limit }
</div>
</>
);
};
};
export default withCharLimit;