react div content editable render value with span











up vote
0
down vote

favorite












I've used div with contentEditable=true in my react component like this:



interface ContentEditableProps {
onChange: (evevt) => void;
onRef: (ref) => void;
html: any;
}

class ContentEditable extends React.Component<ContentEditableProps, {}>{
myRef: HTMLDivElement
lastHtml: string
constructor(props) {
super(props);
this.emitChange = this.emitChange.bind(this);
this.onRef = this.onRef.bind(this)
}

emitChange(event) {
this.props.onChange(event.target.innterText);
}

onRef(ref) {
this.myRef = ref;
this.props.onRef(ref);
}

render() {
return <div
onInput={this.emitChange}
contentEditable={true}
ref={this.onRef}
dangerouslySetInnerHTML={{ __html: this.props.html }}
>
</div >;
}

}


on emitChange I've used the innterText to show in my Parent Component.



interface IndicatorInputState {
html: any
}
class ParentComponent extends React.Component<{}, IndicatorInputState>{

constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);

this.onRef = this.onRef.bind(this);

this.state = {
html:''
}
}

onChange(value) {

this.setState({html: `<span>${value}</span>`});

}

render() {
return (
<React.Fragment>

<div className="indicator-input">
<ContentEditable onChange={this.onChange} html={this.state.html} onRef={this.onRef} >

</ContentEditable>


</div>
</React.Fragment>
)
}
}
export default ParentComponent


in My parent Component I have a sate(html) and set the html by this value
in this situation I don't have problem but if I use span instead of just using value then my cursor have a freaky behavior and it goes to left of my div
does anybody know how can i set the cursor to right? Provided that whenever the user write any things or delete text with backspace then the cursor must be at right(by the way:i used css for example direction but it isn't use full because backspace doesn't work in right position)
the sample of my code is here:



https://codepen.io/saeedafroozi/pen/WYweeO?editors=1010



please help me










share|improve this question




























    up vote
    0
    down vote

    favorite












    I've used div with contentEditable=true in my react component like this:



    interface ContentEditableProps {
    onChange: (evevt) => void;
    onRef: (ref) => void;
    html: any;
    }

    class ContentEditable extends React.Component<ContentEditableProps, {}>{
    myRef: HTMLDivElement
    lastHtml: string
    constructor(props) {
    super(props);
    this.emitChange = this.emitChange.bind(this);
    this.onRef = this.onRef.bind(this)
    }

    emitChange(event) {
    this.props.onChange(event.target.innterText);
    }

    onRef(ref) {
    this.myRef = ref;
    this.props.onRef(ref);
    }

    render() {
    return <div
    onInput={this.emitChange}
    contentEditable={true}
    ref={this.onRef}
    dangerouslySetInnerHTML={{ __html: this.props.html }}
    >
    </div >;
    }

    }


    on emitChange I've used the innterText to show in my Parent Component.



    interface IndicatorInputState {
    html: any
    }
    class ParentComponent extends React.Component<{}, IndicatorInputState>{

    constructor(props) {
    super(props);
    this.onChange = this.onChange.bind(this);

    this.onRef = this.onRef.bind(this);

    this.state = {
    html:''
    }
    }

    onChange(value) {

    this.setState({html: `<span>${value}</span>`});

    }

    render() {
    return (
    <React.Fragment>

    <div className="indicator-input">
    <ContentEditable onChange={this.onChange} html={this.state.html} onRef={this.onRef} >

    </ContentEditable>


    </div>
    </React.Fragment>
    )
    }
    }
    export default ParentComponent


    in My parent Component I have a sate(html) and set the html by this value
    in this situation I don't have problem but if I use span instead of just using value then my cursor have a freaky behavior and it goes to left of my div
    does anybody know how can i set the cursor to right? Provided that whenever the user write any things or delete text with backspace then the cursor must be at right(by the way:i used css for example direction but it isn't use full because backspace doesn't work in right position)
    the sample of my code is here:



    https://codepen.io/saeedafroozi/pen/WYweeO?editors=1010



    please help me










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I've used div with contentEditable=true in my react component like this:



      interface ContentEditableProps {
      onChange: (evevt) => void;
      onRef: (ref) => void;
      html: any;
      }

      class ContentEditable extends React.Component<ContentEditableProps, {}>{
      myRef: HTMLDivElement
      lastHtml: string
      constructor(props) {
      super(props);
      this.emitChange = this.emitChange.bind(this);
      this.onRef = this.onRef.bind(this)
      }

      emitChange(event) {
      this.props.onChange(event.target.innterText);
      }

      onRef(ref) {
      this.myRef = ref;
      this.props.onRef(ref);
      }

      render() {
      return <div
      onInput={this.emitChange}
      contentEditable={true}
      ref={this.onRef}
      dangerouslySetInnerHTML={{ __html: this.props.html }}
      >
      </div >;
      }

      }


      on emitChange I've used the innterText to show in my Parent Component.



      interface IndicatorInputState {
      html: any
      }
      class ParentComponent extends React.Component<{}, IndicatorInputState>{

      constructor(props) {
      super(props);
      this.onChange = this.onChange.bind(this);

      this.onRef = this.onRef.bind(this);

      this.state = {
      html:''
      }
      }

      onChange(value) {

      this.setState({html: `<span>${value}</span>`});

      }

      render() {
      return (
      <React.Fragment>

      <div className="indicator-input">
      <ContentEditable onChange={this.onChange} html={this.state.html} onRef={this.onRef} >

      </ContentEditable>


      </div>
      </React.Fragment>
      )
      }
      }
      export default ParentComponent


      in My parent Component I have a sate(html) and set the html by this value
      in this situation I don't have problem but if I use span instead of just using value then my cursor have a freaky behavior and it goes to left of my div
      does anybody know how can i set the cursor to right? Provided that whenever the user write any things or delete text with backspace then the cursor must be at right(by the way:i used css for example direction but it isn't use full because backspace doesn't work in right position)
      the sample of my code is here:



      https://codepen.io/saeedafroozi/pen/WYweeO?editors=1010



      please help me










      share|improve this question















      I've used div with contentEditable=true in my react component like this:



      interface ContentEditableProps {
      onChange: (evevt) => void;
      onRef: (ref) => void;
      html: any;
      }

      class ContentEditable extends React.Component<ContentEditableProps, {}>{
      myRef: HTMLDivElement
      lastHtml: string
      constructor(props) {
      super(props);
      this.emitChange = this.emitChange.bind(this);
      this.onRef = this.onRef.bind(this)
      }

      emitChange(event) {
      this.props.onChange(event.target.innterText);
      }

      onRef(ref) {
      this.myRef = ref;
      this.props.onRef(ref);
      }

      render() {
      return <div
      onInput={this.emitChange}
      contentEditable={true}
      ref={this.onRef}
      dangerouslySetInnerHTML={{ __html: this.props.html }}
      >
      </div >;
      }

      }


      on emitChange I've used the innterText to show in my Parent Component.



      interface IndicatorInputState {
      html: any
      }
      class ParentComponent extends React.Component<{}, IndicatorInputState>{

      constructor(props) {
      super(props);
      this.onChange = this.onChange.bind(this);

      this.onRef = this.onRef.bind(this);

      this.state = {
      html:''
      }
      }

      onChange(value) {

      this.setState({html: `<span>${value}</span>`});

      }

      render() {
      return (
      <React.Fragment>

      <div className="indicator-input">
      <ContentEditable onChange={this.onChange} html={this.state.html} onRef={this.onRef} >

      </ContentEditable>


      </div>
      </React.Fragment>
      )
      }
      }
      export default ParentComponent


      in My parent Component I have a sate(html) and set the html by this value
      in this situation I don't have problem but if I use span instead of just using value then my cursor have a freaky behavior and it goes to left of my div
      does anybody know how can i set the cursor to right? Provided that whenever the user write any things or delete text with backspace then the cursor must be at right(by the way:i used css for example direction but it isn't use full because backspace doesn't work in right position)
      the sample of my code is here:



      https://codepen.io/saeedafroozi/pen/WYweeO?editors=1010



      please help me







      html css reactjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 12:43









      zubair khanzada

      5522513




      5522513










      asked Nov 8 at 11:09









      saeed

      45




      45





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206544%2freact-div-content-editable-render-value-with-span%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206544%2freact-div-content-editable-render-value-with-span%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Landwehr

          Reims

          Schenkenzell