我在我的表单中使用formik。我想用formik实现react-draft-wysiwyg编辑器。然而,在我的例子中,我发现了以下警告。
Warning: Formik called `handleBlur`, but you forgot to pass an `id` or `name` attribute to your input:正因为如此,我认为,如果它有验证问题,我不能显示错误,而且如果我移动到下一个表单并返回到我已经将内容放在编辑器上的表单,状态也不会保留。
是警告导致了这些问题,还是我遗漏了一些重要的东西?
下面是我尝试将formik与react-draft-wysiwyg绑定的方法
import React from "react";
import styled from "styled-components";
import { Editor } from "react-draft-wysiwyg";
import htmlToDraft from "html-to-draftjs";
import draftToHtml from "draftjs-to-html";
import { EditorState, convertToRaw, ContentState } from "draft-js";
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
const EditorField = ({
input,
meta,
field,
form,
label,
placeholder,
labelCss
}) => {
const [active, setActive] = React.useState();
const [editorState, setEditorState] = React.useState(
EditorState.createEmpty()
);
React.useEffect(() => {
if (form.dirty) {
return;
}
if (!field.value) {
return;
}
const contentBlock = htmlToDraft(field.value);
if (contentBlock) {
const contentState = ContentState.createFromBlockArray(
contentBlock.contentBlocks
);
const editorState = EditorState.createWithContent(contentState);
setEditorState(editorState);
}
}, [field.value, form.dirty]);
const onEditorStateChange = editorState => {
changeValue(editorState);
};
const changeValue = editorState => {
setEditorState(editorState);
form.setFieldValue(
field.name,
draftToHtml(convertToRaw(editorState.getCurrentContent()))
);
};
const hasError = form.touched[field.name] && form.errors[field.name];
return (
<>
<Wrapper>
{label && (
<Label isActive={active} css={labelCss}>
{label}
</Label>
)}
<Editor
editorState={editorState}
wrapperClassName="wrapper-class"
editorClassName="editor-class"
toolbarClassName="toolbar-class"
onEditorStateChange={editorState => onEditorStateChange(editorState)}
placeholder={placeholder}
toolbar={{
options: [
"inline",
"blockType",
"fontSize",
"fontFamily",
"list",
"textAlign",
"link",
"embedded",
"remove",
"history"
]
}}
name={field.name}
id={field.name}
onFocus={() => setActive(true)}
onBlur={e => {
setActive(false);
field.onBlur(e);
}}
/>
{!!hasError && <Error>{hasError}</Error>}
</Wrapper>
</>
);
};
export default EditorField;
const Wrapper = styled.div``;在呈现表单时,我执行以下操作
<Field
component={EditorField}
name="article"
label="Write an article"
placeholder="content here"
/>不过,我从服务器获得的数据显示在编辑器中。
发布于 2021-05-28 20:06:13
在组件Editor.js中
import { useField } from "formik";
const [field, meta] = useField(props.name);
const error = meta.touched && meta.error;我添加了bellow <编辑器... />
{meta.touched && meta.error && (
<FormHelperText>{error}</FormHelperText>
)}在我在编辑器中单击时使用编辑器的页面中,该值将默认为<p></p>和长度8,因此测试脏ll为真
<EditorDraft
name="value"
style={{ maxHeight: "200px" }}
// when click in the editor , the value will be by default <p></p> and length 8
handleChange={(v) => {
if (v.length > 8) {
setFieldValue("value", v);
} else {
setFieldValue("value", "");
}
}}
defaultValue={values.value}
toolbar={{
options: [
"inline",
"blockType",
"fontSize",
"fontFamily",
"list",
"textAlign",
"colorPicker",
"remove",
"history",
],
}}
/>;https://stackoverflow.com/questions/60106496
复制相似问题