Ag-grid: Example how to use onKeyDown with React cell editing

Created on 3 Dec 2016  路  3Comments  路  Source: ag-grid/ag-grid

Hello, I'm trying to pass the 'onKeyDown' function to my React component. Followed the docs and looked for any example on the net but I'm not able to do it. Can you please share an example on how to do it?:

My column definition

{
 headerName: 'Email', field: 'email', editable:true,
 suppressFilter: true, suppressSorting: true, suppressToolPanel: true,
 cellEditorFramework: PATableCellEditor,      <== How can I pass 'my' onKeyDown here?
},

Cell editor

export default class PATableCellEditor extends Component {
    constructor(props) {
        super(props);

        // What I understood from the docs is that props.onKeyDown should be already in place

    }
    :

Thanks in advance,

Most helpful comment

Answering myself, found a workaround (or maybe this is the way to do it):

Cell editor

export default class PATableCellEditor extends Component {
    constructor(props) {
        super(props);
        :
    }
    :
    afterGuiAttached() {
        // get ref from React component
        let eInput = this.refs.textField;
        :
        // Add a listener to 'keydown'
        let self = this;
        eInput.addEventListener('keydown', function (event) {
            self.myOnKeyDown(event)
        });
        :
    }
    :
    // Stop propagating 'left'/'right' keys
    myOnKeyDown(event) {
        let key = event.which || event.keyCode;
        if (key === 37 ||  // left
            key === 39) {  // right
            event.stopPropagation();
        }
    }
    :

All 3 comments

Answering myself, found a workaround (or maybe this is the way to do it):

Cell editor

export default class PATableCellEditor extends Component {
    constructor(props) {
        super(props);
        :
    }
    :
    afterGuiAttached() {
        // get ref from React component
        let eInput = this.refs.textField;
        :
        // Add a listener to 'keydown'
        let self = this;
        eInput.addEventListener('keydown', function (event) {
            self.myOnKeyDown(event)
        });
        :
    }
    :
    // Stop propagating 'left'/'right' keys
    myOnKeyDown(event) {
        let key = event.which || event.keyCode;
        if (key === 37 ||  // left
            key === 39) {  // right
            event.stopPropagation();
        }
    }
    :

thank you for feeding back and closing 馃憤

@ceolter I found a way to add my onKeyDown like this:
rowItem.cellEditorParams={
onKeyDown: myOnKeyDown,
}
but It works only when cellEditor is popup type.

As @LuisPalacios 's feedback

let eInput = this.refs.textField;

this is not a universal way for me because I get the render object dynamically and can't handle editor's refs. what should I do?
Thank you very much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ballwood picture ballwood  路  3Comments

basvandenberg picture basvandenberg  路  3Comments

saurabhraja picture saurabhraja  路  3Comments

mikeerickson picture mikeerickson  路  3Comments

WxWatch picture WxWatch  路  3Comments