Created
February 23, 2021 00:22
-
-
Save edupsousa/27e61d9e7a750713d6c3d2e7439faedc to your computer and use it in GitHub Desktop.
Lista de Tarefas - React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
<link | |
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" | |
rel="stylesheet" | |
integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" | |
crossorigin="anonymous" | |
/> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="row"> | |
<div id="app" class="col"></div> | |
</div> | |
</div> | |
<script | |
src="https://unpkg.com/react@17/umd/react.development.js" | |
crossorigin | |
></script> | |
<script | |
src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" | |
crossorigin | |
></script> | |
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> | |
<script src="tarefas.jsx" type="text/babel"></script> | |
<script type="text/babel"> | |
var rootElement = React.createElement(App); | |
var container = document.querySelector("#app"); | |
ReactDOM.render(rootElement, container); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function App() { | |
const [tarefas, setTarefas] = React.useState([]); | |
const adicionarTarefa = (nomeTarefa) => { | |
setTarefas([...tarefas, nomeTarefa]); | |
}; | |
const removerTarefa = (index) => { | |
const items = [...tarefas]; | |
items.splice(index, 1); | |
setTarefas(items); | |
} | |
return ( | |
<div> | |
<h3>Lista de Tarefas</h3> | |
<NovaTarefa adicionarTarefa={adicionarTarefa} /> | |
<ListaTarefas tarefas={tarefas} removerTarefa={removerTarefa} /> | |
</div> | |
); | |
} | |
function NovaTarefa(props) { | |
const adicionarTarefa = props.adicionarTarefa; | |
const [nomeTarefa, setNomeTarefa] = React.useState(''); | |
const handleSubmit = (ev) => { | |
ev.preventDefault(); | |
adicionarTarefa(nomeTarefa); | |
setNomeTarefa(''); | |
}; | |
return ( | |
<form onSubmit={handleSubmit}> | |
<div className="row"> | |
<div className="col-auto"> | |
<label htmlFor="" className="col-form-label"> | |
Nova Tarefa | |
</label> | |
</div> | |
<div className="col"> | |
<input type="text" className="form-control" value={nomeTarefa} onChange={ev => setNomeTarefa(ev.target.value)} /> | |
</div> | |
<div className="col-auto"> | |
<button type="submit" className="btn btn-primary">Adicionar</button> | |
</div> | |
</div> | |
</form> | |
); | |
} | |
function ListaTarefas(props) { | |
const tarefas = props.tarefas; | |
const removerTarefa = props.removerTarefa; | |
if (tarefas.length === 0) return null; | |
return ( | |
<ul className="list-group mt-3"> | |
{tarefas.map((tarefa, index) => ( | |
<li className="list-group-item" key={index}> | |
<div className="row"> | |
<div className="col">{tarefa}</div> | |
<div className="col-auto"> | |
<button type="button" className="btn-close" onClick={() => removerTarefa(index)}></button> | |
</div> | |
</div> | |
</li> | |
))} | |
</ul> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment