User interaction in React Signature component

30 Jan 202316 minutes to read

The below interactions were available in Signature, and we can walk through one by one.

  • Undo and Redo
  • Clear
  • Disabled
  • ReadOnly

Undo and Redo

In the Signature, every action can be maintained as a snap for undo and redo operations. And maintained SnapIndex for indexing the snap collection.

The undo method reverts the last action of signature by decreasing SnapIndex value to index previous snap. Here, canUndo method is used to ensure whether undo can be performed or not.

The redo method reverts the last undo action of the signature by increasing the SnapIndex to get the next snap. Here, canRedo method is used to ensure whether redo can be performed or not.

Clear

The clear method is used to clears the signature and makes the canvas empty. This is also considered in Undo/ Redo. Here, isEmpty method is used to ensure whether the signature is empty or not.

Disabled

The disabled property is used to enables/disables the signature component. In the disabled state, the user is not allowed to draw signature. And it can’t be focused until the user enabled the signature.

ReadOnly

The isReadOnly property is used to enables/disables the ReadOnly Signature. It can be focused but it prevents drawing in Signature.

The following sample explains about user interactions available in signature.

import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { SignatureComponent } from '@syncfusion/ej2-react-inputs';
import { ButtonComponent, CheckBoxComponent } from '@syncfusion/ej2-react-buttons';
import { getComponent } from '@syncfusion/ej2-base';
function App() {
    function undoBtnCreated() {
        document.getElementById('undo').addEventListener('click', undoBtnClick);
    }
    function undoBtnClick() {
        let signature = getComponent(document.getElementById('signature'), 'signature');
        if (!signature.disabled && !signature.isReadOnly) {
            signature.undo();
        }
    }
    function redoBtnCreated() {
        document.getElementById('redo').addEventListener('click', redoBtnClick);
    }
    function redoBtnClick() {
        let signature = getComponent(document.getElementById('signature'), 'signature');
        if (!signature.disabled && !signature.isReadOnly) {
            signature.redo();
        }
    }
    function clrBtnCreated() {
        document.getElementById('clear').addEventListener('click', clrBtnClick);
    }
    function clrBtnClick() {
        let signature = getComponent(document.getElementById('signature'), 'signature');
        if (!signature.disabled && !signature.isReadOnly) {
            signature.clear();
        }
    }
    function disableChange(args) {
        let signature = getComponent(document.getElementById('signature'), 'signature');
        signature.disabled = args.checked;
    }
    function readOnlyChange(args) {
        let signature = getComponent(document.getElementById('signature'), 'signature');
        signature.isReadOnly = args.checked;
    }
    function change(args) {
        let signature = getComponent(document.getElementById('signature'), 'signature');
        let undoButton = getComponent(document.getElementById('undo'), 'btn');
        let redoButton = getComponent(document.getElementById('redo'), 'btn');
        let clearButton = getComponent(document.getElementById('clear'), 'btn');
        if (!signature.disabled && !signature.isReadOnly) {
            if (signature.canUndo()) {
                undoButton.disabled = false;
            }
            else {
                undoButton.disabled = true;
            }
            if (signature.canRedo()) {
                redoButton.disabled = false;
            }
            else {
                redoButton.disabled = true;
            }
            if (!signature.isEmpty()) {
                clearButton.disabled = false;
            }
            else {
                clearButton.disabled = true;
            }
        }
    }
    return (<div id='container'>
      <div id="option">
          <table>
              <tr>
                  <td>
                    <ButtonComponent id='undo' cssClass='e-primary' content='UNDO' disabled={true} created={undoBtnCreated.bind(this)}/>
                  </td><td>
                    <ButtonComponent id='redo' cssClass='e-primary' content='REDO' disabled={true} created={redoBtnCreated.bind(this)}/>
                  </td><td>
                    <ButtonComponent id='clear' cssClass='e-primary' content='CLEAR' disabled={true} created={clrBtnCreated.bind(this)}/>
                  </td><td>
                      <div id="checkbox1">
                        <CheckBoxComponent label="Disabled" change={disableChange}/>
                      </div>
                      <div id="checkbox2"><CheckBoxComponent label="ReadOnly" change={readOnlyChange}/></div>
                  </td>
              </tr>
          </table>
      </div>
      <div id="signature-control">
          <SignatureComponent id='signature' change={change}/>
      </div>
  </div>);
}
;
export default App;
ReactDOM.render(<App />, document.getElementById('element'));
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { SignatureFileType, Signature, SignatureComponent, SignatureChangeEventArgs } from '@syncfusion/ej2-react-inputs';
import { ButtonComponent, CheckBoxComponent } from '@syncfusion/ej2-react-buttons';
import { getComponent, closest } from '@syncfusion/ej2-base';
import { ChangeEventArgs, CheckBox } from '@syncfusion/ej2-buttons';

function App() {
  function undoBtnCreated() {
    document.getElementById('undo').addEventListener('click', undoBtnClick);
  }
  function undoBtnClick() {
    let signature: Signature = getComponent(document.getElementById('signature'), 'signature');
    if (!signature.disabled && !signature.isReadOnly) {
      signature.undo();
    }
  }
  function redoBtnCreated() {
    document.getElementById('redo').addEventListener('click', redoBtnClick);
  }
  function redoBtnClick() {
    let signature: Signature = getComponent(document.getElementById('signature'), 'signature');
    if (!signature.disabled && !signature.isReadOnly) {
      signature.redo();
    }
  }
  function clrBtnCreated() {
    document.getElementById('clear').addEventListener('click', clrBtnClick);
  }
  function clrBtnClick() {
    let signature: Signature = getComponent(document.getElementById('signature'), 'signature');
    if (!signature.disabled && !signature.isReadOnly) {
      signature.clear();
    }
  }
  function disableChange(args: ChangeEventArgs) {
    let signature: Signature = getComponent(document.getElementById('signature'), 'signature');
    signature.disabled = args.checked;
  }
  function readOnlyChange(args: ChangeEventArgs) {
    let signature: Signature = getComponent(document.getElementById('signature'), 'signature');
    signature.isReadOnly = args.checked;
  }
  function change(args: SignatureChangeEventArgs) {
    let signature: Signature = getComponent(document.getElementById('signature'), 'signature');
    let undoButton: Button = getComponent(document.getElementById('undo'), 'btn');
    let redoButton: Button = getComponent(document.getElementById('redo'), 'btn');
    let clearButton: Button = getComponent(document.getElementById('clear'), 'btn');
    if (!signature.disabled && !signature.isReadOnly) {
        if (signature.canUndo()) {
            undoButton.disabled = false;
        } else {
            undoButton.disabled = true;
        }
        if (signature.canRedo()) {
            redoButton.disabled = false;
        } else {
            redoButton.disabled = true;
        }
        if (!signature.isEmpty()) {
            clearButton.disabled = false;
        } else {
            clearButton.disabled = true;
        }
    }
  }
  return (<div id='container'>
      <div id="option">
          <table>
              <tr>
                  <td>
                    <ButtonComponent id='undo' cssClass='e-primary' content='UNDO' disabled={true} created={undoBtnCreated.bind(this)}/>
                  </td><td>
                    <ButtonComponent id='redo' cssClass='e-primary' content='REDO' disabled={true} created={redoBtnCreated.bind(this)}/>
                  </td><td>
                    <ButtonComponent  id='clear' cssClass='e-primary' content='CLEAR' disabled={true} created={clrBtnCreated.bind(this)}/>
                  </td><td>
                      <div id="checkbox1">
                        <CheckBoxComponent label="Disabled" change={disableChange}/>
                      </div>
                      <div id="checkbox2"><CheckBoxComponent label="ReadOnly" change={readOnlyChange}/></div>
                  </td>
              </tr>
          </table>
      </div>
      <div id ="signature-control">
          <SignatureComponent id='signature' change={change}/>
      </div>
  </div>
  )
};
export default App;
ReactDOM.render(<App />, document.getElementById('element'));