score:1
here is a nice way to make a contenteditable
div with html
and onchange
props. i used that with emojione
's shortnametoimage
function.
the styles can be added to contenteditable
props if you want them to come from the state
the only thing that needs to be fixed is the caret position after an emoji is added.
class application extends react.component {
state = {
value: "start writing here"
};
render() {
return (
<contenteditable
html={emojione.shortnametoimage(this.state.value)}
onchange={this._onchange}
/>
);
}
_onchange = e => {
this.setstate({ value: e.target.value });
};
}
var contenteditable = react.createclass({
render: function() {
return (
<div
contenteditable
classname="message-input"
oninput={this.emitchange}
onblur={this.emitchange}
dangerouslysetinnerhtml={{ __html: this.props.html }}
/>
);
},
shouldcomponentupdate: function(nextprops) {
return nextprops.html !== this.getdomnode().innerhtml;
},
componentdidupdate: function() {
if (this.props.html !== this.getdomnode().innerhtml) {
this.getdomnode().innerhtml = this.props.html;
}
},
emitchange: function() {
var html = this.getdomnode().innerhtml;
if (this.props.onchange && html !== this.lasthtml) {
this.props.onchange({ target: { value: html } });
}
this.lasthtml = html;
}
});
react.render(<application />, document.getelementbyid("app"));
html, body {
height: 100%
}
.message-input {
width: 100%;
height: 100%;
font-size: 30px;
}
.emojione {
height: 30px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.min.js"></script>
<div id="app"></div>
score:1
i have used emojione and this code snippet to basically come up with a very non-react solution as shown below. hope it helps.
import react from 'react';
import {render} from 'react-dom';
import emojione from 'emojione';
class app extends react.component {
constructor(props) {
super(props);
}
updatetext = () => {
const html = emojione.shortnametoimage(this.refs.inputdiv.innerhtml);
let sel, range;
if (window.getselection) {
// ie9 and non-ie
sel = window.getselection();
if (sel.getrangeat && sel.rangecount) {
range = sel.getrangeat(0);
range.deletecontents();
// range.createcontextualfragment() would be useful here but is
// non-standard and not supported in all browsers (ie9, for one)
let el = this.refs.inputdiv;
el.innerhtml = html;
let frag = document.createdocumentfragment(), node, lastnode;
while ((node = el.firstchild)) {
lastnode = frag.appendchild(node);
}
range.insertnode(frag);
// preserve the selection
if (lastnode) {
range = range.clonerange();
range.setstartafter(lastnode);
range.collapse(true);
sel.removeallranges();
sel.addrange(range);
}
}
} else if (document.selection && document.selection.type !== "control") {
// ie < 9
document.selection.createrange().pastehtml(html);
}
};
render() {
return (
<div ref="inputdiv" contenteditable oninput={this.updatetext}/>
);
}
}
render(<app/>, document.getelementbyid('root'));
here is the working example https://codesandbox.io/s/ol2lmkqqlq
Source: stackoverflow.com
Related Query
- How to implement react with on type (realtime) emojis?
- How to implement Error Boundary with React Hooks Component
- How to implement skipWaiting with Create React App?
- How to implement Stripe with React Native?
- How to implement componentDidMount with hooks in React to be in line with the EsLint rule "react-hooks/exhaustive-deps": "warn"?
- How to implement the OAuth2 Authorization code grant with PKCE for a React single page application?
- How to implement a scrollspy with React
- How to limit the text filed length with input type number in React JS and prevent entry of E,e, -, + etc
- How do you correctly use React.lazy() in Typescript to import a react component with a generic type parameter?
- How to implement a gameloop with requestAnimationFrame across multiple React Redux components?
- How can I implement SSO Authentication with Active Directory in React App?
- ReactJS - How can I implement pagination for react with keep state when route change?
- How to implement a scrollspy in react native with ScrollView?
- How to implement share to twitter with react js
- How do you implement "check all" in React with child components?
- How to type a Typescript React Component that accepts a Context with a super-set of properties
- How to send image to server with Blob type using Axios in React Native?
- How to use radium in React typescript (Error: has no properties in common with type 'CSSProperties'. TS2559)?
- How can we implement an Open with Postman button to open a postman collection link sent from a third party react application?
- How to implement Firebase authentication with React Redux?
- How to implement pagination with filter in React
- How to correctly serve my React production build through Django. Currently having MIME type issues with current configuration
- How to implement react routing with bootstrap navbar component?
- How to properly implement "subscription"-like fetches with React useEffect
- How to type any param with React Typescript in Functional Component
- React warning: contains an input of type hidden with value and default value - how to fix?
- How to define an interface for React function component with defaultProps without throwing missing type error?
- How to specify return type as a list of React components with Flow
- How to implement a React Component with sub-components and call them?
- How do I fix a React TypeScript error with the return value of useHotkeys hook not matching the type of the div element ref prop
More Query from same tag
- Stripe parameter_invalid_integer
- React How to add new dynamic key to existing state?
- Put Border Color React Material Theme
- Element type error in Query component only when using a HOC
- How To Redirect To Another page in React js
- Updating a variable after ajax call in ReactJS?
- Webpack : Cannot read property 'readFile' of undefined, No output files
- How to stop already logged in user to go back to the sign-in page in React.js
- React Native generate thumbnail for video url
- React: RangeError: Invalid time value - DatePicker error after Saving to LocalStorage
- why useEffect run twice in react, how to prevent this issue?
- React function not passing down from functional to functional component
- props not available in component
- I need to reset the state after filtering
- React - update remote data, update the view
- useEffect only on pageload AND page refresh
- ReactJS with Django Elements Displayed As Text
- Is there a standard way to route a setState redux action?
- How do I get the url of my post in react and node application as a string?
- Rendering new component on click in react
- Unable to change border color using Material UI palette color in React.js
- Mixing tags and map function inside react conditional
- How to render the infoPage.js page in a seperate window when the user click on handleClick function
- React. Transferring data from textarea to array JSON
- Destructing Props Multiple Times in React Component?
- React router <Redirect> is not redirecting on state
- Using webpack magic comments in an typescript (sharepoint modern ui) project
- How to handle loader state for the initial render as initial state of loader is false?
- Why am I not able to set my state using useState and send the state to firebase realtime database at the same time?
- this.props equal to nextProps componentShouldUpdate