lo he hecho con decoradores, si no ingresé mientras escribía (creando un nuevo bloque), funciona bien, ingrese la descripción de la imagen aquí
pero cuando enviamos un mensaje de texto con el decorador de nueva línea, llama tantas veces como se creó una nueva línea y no funcionó, por favor, si alguien tiene una solución, por favor ayuda, gracias de antemano
ingrese la descripción de la imagen aquí
mi código de componente de reacción a continuación:
import React, { useState, useRef } from "react"; importar {Editor,
EditorState, ContentState, CompositeDecorator, convertToRaw, } from "draft-js";const Decorado = ({hijos}) => { return {hijos}; };
function findWithRegex(contentBlock, callback) { const text = contentBlock.getText(); callback(1000, texto.longitud > 1000 ? texto.longitud : 1001); }
const estilos = { editor: { // borde: "1px gris sólido", // minHeight: "6em", }, };
función handleStrategy(contentBlock, devolución de llamada) {
findWithRegex(contentBlock, devolución de llamada); }const createDecorator = () => new CompositeDecorator([ { estrategia: handleStrategy, componente: Decorado, }, ]);
function TextArea(props) { const [editorState, setEditorState] = useState( EditorState.createWithContent( ContentState.createFromText(props.value), createDecorator() ) );
const editor = useRef(null);
función focusEditor() { editor.current.focus(); }
const onChanage = (editState) => { const blocks = convertToRaw(editState.getCurrentContent()).blocks; let value = blocks .map((block) => (!block.text.trim() && "\n") || block.text) .join("\n"); setEditorState(editarEstado); props.formik.setFieldValue(props.name, value !== "\n" ? value : "") };
React.useEffect(() => { focusEditor(); }, []);
devolver ( ); }
exportar TextArea por defecto;