I manage to set up CKEditor and it works great. My problem is I'm refactoring the code and I need the editor component to be called by several other components.
So, my handleChange is not really working anymore. How can I pass only data to parent's handleChange function?
parent:
function IncluirArtigo() {
const campos = { autor: '', titulo: '', texto: '' };
const [valores, setValores] = useState(campos);
const [erro, setErro] = useState(false);
const history = useHistory();
const handleChange = e => {
setValores({
...valores,
[e.target.name]: e.target.value
});
console.log(valores)
}
const incluir = async (e) => {
e.preventDefault();
console.log(valores);
try {
//await PostArtigo(valores);
/* Poderia redirecionar para o artigo criado, se o post retornasse uma id */
goHome();
}
catch (e) {
console.error('erro', e);
setErro(true);
}
}
const goHome = () => history.push('/');
return (
<div className="container">
{
erro ?
<div>Houve erro na inclusão do artigo, tente novamente</div>
:
<>
<h3>Inclusão de Artigos</h3>
<form onSubmit={incluir} className="form-incluir">
<label htmlFor="autor">Autor:</label>
<input
type="text"
name="autor"
value={valores.autor}
onChange={handleChange}
/>
<label htmlFor="titulo">Título:</label>
<input
type="text"
name="titulo"
value={valores.titulo}
onChange={handleChange}
/>
<label>Conteúdo:</label>
<div className="editor">
<Editor />
</div>
<input className="btn-enviar" type="submit" value="Enviar" />
</form>
</>
}
</div >
)
}
editor.js
function Editor() {
return (
<div className="editor">
<CKEditor
editor={ClassicEditor}
data={valores.texto}
onChange={(event, editor) => {
const texto = editor.getData();
console.log({ event, editor, texto });
handleChange() /* I need this function to pass only valores data */
}}
/>
</div>
)
}