您所在街区的问题是由于selector 您的emailPlaceholder 属性-将值设置为span.kathyContactFormEmail, 但实际上,在您的save() 函数(或函数返回的元素的标记/HTML)。
您所拥有的是kathyContactFormEmail 作为类,因此您需要更改emailPlaceholder\'sselector 到input.kathyContactFormEmail.
除此之外,您还应该更改source 从…起html 到attribute 和设置attribute 到placeholder 像这样:
emailPlaceholder: {
type: \'string\',
source: \'attribute\', // change it from \'html\' to \'attribute\'
attribute: \'placeholder\', // add add this
selector: \'input.kathyContactFormEmail\'
}
首先,因为您将属性与输入的占位符一起使用,即。
<input placeholder="{ emailPlaceholder }" />.
其次,input 元件是自动关闭的,即。<input /> 而不是<input></input>, 因此innerHTML 值为空。
所以html 源代码无法处理输入,因为块编辑器将从内部HTML读取属性值,但使用<input /> (甚至<input>foo</input> 无效..),编辑器将获得一个空字符串。
其他问题/注意事项,而不是使用RichText 用于编辑的元素emailPlaceholder 属性,我会使用一个简单的文本字段,例如使用TextControl 像这样:
<TextControl
label="Placeholder text"
value={ emailPlaceholder }
onChange={ ( val ) => updateEl( \'emailPlaceholder\', val ) }
/>
PS:不要忘记导入或加载组件,例如。
const { TextControl } = wp.components;.
如果您使用该组件,那么您需要使用div, 碎片(<> 和</>) 或者别的什么label 包装RichText 和TextControl 元素,例如。<div><RichText .../><TextControl .../></div>.
在RichText 参考says:
RichText.Content 应在中使用save 块的功能,以正确保存富文本内容。
因此<span className={"kathyContactFormEmailLabelText flexCenterCenter"}>{emailLabel}</span>, 您应该执行以下操作:
<RichText.Content
tagName="span"
className="kathyContactFormEmailLabelText flexCenterCenter"
value={ emailLabel }
/>
对于字符串文字或非动态属性值,应该只使用
<attribute>="<value>" 语法,例如
className={\'kathyContactForm\'}, 使用
className="kathyContactForm". 这样,您的代码将变得更简单。
在您的edit() 函数,我不知道为什么要返回数组,即。return ([<form>...</form>]), 但你应该设置一个key 每个最外层父元素的属性(例如,如果<p><b>foo <i>bar</i></b> baz</p>, 最外层的父级是p). 例如,你会return ([<form key="your-key">...</form>]).
并确保使用唯一的键-两个RichText 您的edit() 函数使用了相同的键(“可编辑”)。