-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathTextForm.js
65 lines (57 loc) · 2.78 KB
/
TextForm.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, {useState} from 'react'
export default function TextForm(props) {
const handleUpClick = ()=>{
let newText = text.toUpperCase();
setText(newText)
props.showAlert("Converted to uppercase!", "success");
}
const handleLoClick = ()=>{
let newText = text.toLowerCase();
setText(newText)
props.showAlert("Converted to lowercase!", "success");
}
const handleClearClick = ()=>{
let newText = '';
setText(newText);
props.showAlert("Text Cleared!", "success");
}
const handleOnChange = (event)=>{
setText(event.target.value)
}
// Credits: A
const handleCopy = () => {
navigator.clipboard.writeText(text);
props.showAlert("Copied to Clipboard!", "success");
}
// Credits: Coding Wala
const handleExtraSpaces = () => {
let newText = text.split(/[ ]+/);
setText(newText.join(" "));
props.showAlert("Extra spaces removed!", "success");
}
const [text, setText] = useState('');
// text = "new text"; // Wrong way to change the state
// setText("new text"); // Correct way to change the state
return (
<>
<div className="container" style={{color: props.mode==='dark'?'white':'#042743'}}>
<h1 className='mb-4'>{props.heading}</h1>
<div className="mb-3">
<textarea className="form-control" value={text} onChange={handleOnChange} style={{backgroundColor: props.mode==='dark'?'#13466e':'white', color: props.mode==='dark'?'white':'#042743'}} id="myBox" rows="8"></textarea>
</div>
<button disabled={text.length===0} className="btn btn-primary mx-1 my-1" onClick={handleUpClick}>Convert to Uppercase</button>
<button disabled={text.length===0} className="btn btn-primary mx-1 my-1" onClick={handleLoClick}>Convert to Lowercase</button>
<button disabled={text.length===0} className="btn btn-primary mx-1 my-1" onClick={handleClearClick}>Clear Text</button>
<button disabled={text.length===0} className="btn btn-primary mx-1 my-1" onClick={handleCopy}>Copy Text</button>
<button disabled={text.length===0} className="btn btn-primary mx-1 my-1" onClick={handleExtraSpaces}>Remove Extra Spaces</button>
</div>
<div className="container my-3" style={{color: props.mode==='dark'?'white':'#042743'}}>
<h2>Your text summary</h2>
<p>{text.split(/\s+/).filter((element)=>{return element.length!==0}).length-1} words and {text.length} characters</p>
<p>{0.008 * text.split(/\s+/).filter((element)=>{return element.length!==0}).length} Minutes read</p>
<h2>Preview</h2>
<p>{text.length>0?text:"Nothing to preview!"}</p>
</div>
</>
)
}