您可以在不使用自定义类将文本包装到元素的情况下完成此操作。可以使用方法设置选定文本的样式
RichUtils.toggleInlineStyle
. 更多详细信息
here
看看这个工作示例-
https://jsfiddle.net/x2gsp6ju/2/
customStyleMap
对象该对象的键应该是自定义样式和值的唯一名称-具有适当样式的对象。
const customStyleMap = {
redBackground: {
backgroundColor: 'red'
},
underlined: {
textDecoration: 'underline',
fontSize: 26
},
};
将此对象传递给
的属性
Editor
<Editor
placeholder="Type away :)"
editorState={this.state.editorState}
onChange={this._handleChange}
customStyleMap={customStyleMap}
/>
在本例中,我在单击适当的按钮后为所选文本应用样式,我调用
this.applyCustomSTyles
方法和传递样式名称作为第一个参数。在这种方法中,我生成了新的
editorState
具有
RichUtils.toggleInlineStyles
:
applyCustomStyles = (nameOfCustomStyle) => {
this._handleChange(
RichUtils.toggleInlineStyle(
this.state.editorState,
nameOfCustomStyle
)
);
}