Skip to content

Commit

Permalink
Merge pull request #3 from imersao-alura/aula03
Browse files Browse the repository at this point in the history
Aula 03 no ar!
  • Loading branch information
omariosouto authored Jul 30, 2020
2 parents 89bab8c + 005895c commit ffa57a5
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 9 deletions.
19 changes: 19 additions & 0 deletions src/components/FormField/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";

function FormField({ label, type, name, value, onChange }) {
return (
<div>
<label>
{label}:
<input
type={type}
value={value}
name={name}
onChange={onChange}
/>
</label>
</div>
)
}

export default FormField;
100 changes: 91 additions & 9 deletions src/pages/cadastro/Categoria/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,108 @@
import React from 'react';
import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import PageDefault from '../../../components/PageDefault';
import FormField from '../../../components/FormField';

function CadastroCategoria() {
const valoresIniciais = {
nome: '',
descricao: '',
cor: '',
}
const [categorias, setCategorias] = useState([]);
const [values, setValues] = useState(valoresIniciais);


function setValue(chave, valor) {
// chave: nome, descricao, bla, bli
setValues({
...values,
[chave]: valor, // nome: 'valor'
})
}

function handleChange(infosDoEvento) {
setValue(
infosDoEvento.target.getAttribute('name'),
infosDoEvento.target.value
);
}

return (
<PageDefault>
<h1>Cadastro de Categoria</h1>
<h1>Cadastro de Categoria: {values.nome}</h1>

<form onSubmit={function handleSubmit(infosDoEvento) {
infosDoEvento.preventDefault();
setCategorias([
...categorias,
values
]);

setValues(valoresIniciais)
}}>

<FormField
label="Nome da Categoria"
type="text"
name="nome"
value={values.nome}
onChange={handleChange}
/>

<form>
<FormField
label="Descrição:"
type="????"
name="descricao"
value={values.descricao}
onChange={handleChange}
/>
{/* <div>
<label>
Descrição:
<textarea
type="text"
value={values.descricao}
name="descricao"
onChange={handleChange}
/>
</label>
</div> */}

<label>
Nome da Categoria:
<input
type="text"
/>
</label>
<FormField
label="Cor"
type="color"
name="cor"
value={values.cor}
onChange={handleChange}
/>
{/* <div>
<label>
Cor:
<input
type="color"
value={values.cor}
name="cor"
onChange={handleChange}
/>
</label>
</div> */}

<button>
Cadastrar
</button>
</form>


<ul>
{categorias.map((categoria, indice) => {
return (
<li key={`${categoria}${indice}`}>
{categoria.nome}
</li>
)
})}
</ul>

<Link to="/">
Ir para home
Expand Down

1 comment on commit ffa57a5

@vercel
Copy link

@vercel vercel bot commented on ffa57a5 Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.