我正在尝试使用contentEditable div构建评论框,并具有 以下问题:
- 无法获取text属性的初始值。
- 未从contentEditable获取setState值。
- 表情符号存在问题,使用输入元素时可以正常工作,但不能与contentEditable一起使用。
使用下面提供的代码,当console.log出现时,我变得不确定, 不知道我在想什么。
预期结果。
- 从contentEditable中获取键入的值,包括表情符号。
// Content Input Model
import React, { Fragment } from 'react'
import '../../assets/css/comment.css'
const ContentInput = ({valueChange,contentInputId,ref,onClick,onPhClick,placeholder, ...props }) => {
return (
<Fragment>
<div
spellCheck="false"
role="textbox"
id={contentInputId}
ref={ref}
contentEditable="true"
onChange={valueChange}
aria-multiline="true"
data-placeholder={placeholder}
{...props}
/>
<div className="comment_submit_button" onClick={onClick}>
<span className='submit_arrow_light'> </span>
</div>
</Fragment>
)
};
export default ContentInput
// Comment Modal///
import React , {Component} from 'react';
import EmojiPicker from 'emoji-picker-react'
import '../../../../assets/css/comment.css'
import JSEMOJI from 'emoji-js'
import ContentInput from "../../../../UIElements/Inputs/ContentInput";
//emoji set up
let jsemoji = new JSEMOJI();
// set the style to emojione (default - apple)
jsemoji.img_set = 'emojione';
// set the storage location for all emojis
jsemoji.img_sets.emojione.path = 'https://cdn.jsdelivr.net/emojione/assets/3.0/png/32/';
class CommentModal extends Component {
constructor(props) {
super(props);
this.state = {
text : ' ' ,
emojiShown : false ,
};
this.desChange = this.desChange.bind(this);
}
desChange = (e) => {
// let text = this.state.text;
this.setState({text : e.target.value});
};
comment = (e) => {
e.preventDefault();
let {text} = this.state;
let {back , incrementComments , ...rest} = this.props;
const updatedText = this.setState({text : e.target.value});
console.log(updatedText);
};
//displays emoji inside the input window
handleEmojiClick = (n , e) => {
let emoji = jsemoji.replace_colons(`:${e.name}:`);
this.setState({
text : e.target.value + emoji ,
emojiShown : !this.state.emojiShown
});
console.log(this.emojiShown)
};
toggleEmojiState = () => {
this.setState({
emojiShown : !this.state.emojiShown
});
};
render() {
return (
<div className='comment_model_container display_none'>
<div className="comment_content_container global_bt">
<div className="comment_text_area_container ">
<ContentInput
valueChange={this.desChange}
contentInputId='comment_box'
onClick={this.comment}
spellcheck="true"
className='comment_text_area global_bt'
placeholder='Leave a comment...'
/>
</div>
<div>
</div>
<div className='emoji_container'>
<span id="show-emoji-yes" onClick={!this.toggleEmojiState}><span
className='grey_smiley_icon'> </span></span>
<div className="emoji-table">
<EmojiPicker onEmojiClick={this.handleEmojiClick} className='emoji_popup'/>
</div>
</div>
</div>
</div>
);
}
}
export default CommentModal;
```
答案 0 :(得分:0)
在onInput
上使用ContentInput
而不是onChange
然后在e.target.innerHTML
上使用desChange
而不是e.target.value
contentEditable事件应为“ onInput” Content Editable change events