JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
CSS
#root{
position: relative;
top:0;
right: 0;
left: 0;
bottom: 0;
}
.todoMain{
position: relative;
margin: 50px auto;
width: 1050px;
height: 500px;
background: #fafafa;
border-radius: 7px;
}
.todo{
box-sizing: border-box;
padding: 15px 30px;
color: aliceblue;
font-family: "Arial";
font-weight: bold;
display: inline-block;
height: 50px;
width: 100%;
background: #66d9ff;
border-radius: 7px 7px 0px 0px;
}
input.val{
margin-top: 10px;
width: calc(100% - 300px);
margin-left: 30px;
height: 35px;
border-radius: 3px;
padding-left: 10px;
}
input[type='button']{
height: 39px;
width: 105px;
border: none;
font-family: "Arial";
text-transform: uppercase;
color: snow;
font-weight: bold;
background: #55f655;
margin-left: 10px;
}
input[value='Clear']{
background: #f98686;
}
.listElem{
font-family: Arial;
color: slategray;
margin-top: 10px;
margin-left: 30px;
width: calc(100% - 100px);
height: 50px;
vertical-align: middle;
line-height: 50px;
padding-left: 30px;
border-radius: 5px;
border: 1px solid #66d9ff;
}
.listElem input{
float: right;
margin-right: 20px;
height: 20px;
width: 20px;
margin-top: 15px;
}
.listElem:hover{
transition: all 0.12s ease;
background-color: azure;
cursor: default;
}
JavaScript
function todoElems(data, parentState){
return (<div>{
data.map((n, i)=>{
return (<div className="listElem" key={i}>
{n}
<input type="checkbox" className="checkBoxes"/>
)
})}
</div>
)}
const initialState = ['Learn React', 'Learn Redux']
const currentState =[];
class App extends Component {
constructor(props){
super(props)
this.state = {
data: todoElems(initialState)
}
}
handleAdd(){
if(!this.inputAdd.value || this.inputAdd.value.match(/^[ ]+$/)){return;}
currentState.push(this.inputAdd.value)
this.setState((prevState) => ({
data: todoElems(currentState)
}))
}
componentDidUpdate(){
this.inputAdd.value ='';
}
handleRemove(){
let cb = document.getElementsByClassName('checkBoxes');
for(let i=0; i< cb.length; i++){
if(cb[i].checked == true){
currentState.splice(i,1);
i--;
}
}
this.setState((prevState) => ({
data: todoElems(currentState)
}))
}
render() {
return (
<div className="todoMain">
<span className="todo">TODO</span>
<input className='val' placeholder="Add a Task..." type="text" ref={(input) => { this.inputAdd = input }} />
<input type="button" value='Add' onClick={this.handleAdd.bind(this)} />
<input type="button" value='Clear' onClick={this.handleRemove.bind(this)} />
{this.state.data}
</div>)
} }