React Data GridCell Editor Component - Imperative
This page describes the old imperative way of declaring custom cell editor components when the grid option reactiveCustomComponents is not set. It is strongly recommended to instead use the new behaviour described on the Custom Cell Editor page.
An example cell editor component looks like this:
export default forwardRef((props, ref) => {
const [value, setValue] = useState(props.value);
/* Component Cell Editor Lifecycle methods */
useImperativeHandle(ref, () => {
return {
getValue() {
return value;
},
};
});
return (
<input
type="text"
value={value || ''}
onChange={({ target: { value: newValue }}) => setValue(newValue))}
/>
);
});The example below shows a few cell editors in action.
- The
DoublingCell Editor will double a given input and reject values over a 1000 - The
MoodCell Editor illustrates a slightly more complicated editor with values changed depending on the smiley chosen - The
NumericCell Editor illustrates a slightly more complicated numeric editor to theDoublingeditor, with increased input validation
Custom Cell Editor Interface
The interface for a custom cell editor component is as follows:
interface ICellEditorReactComp {
// Mandatory - Return the final value - called by the grid once after editing is complete
getValue(): any;
// Gets called once after initialised. If you return true, the editor will not be
// used and the grid will continue editing. Use this to make a decision on editing
// inside the init() function, eg maybe you want to only start editing if the user
// hits a numeric key, but not a letter, if the editor is for numbers.
isCancelBeforeStart?(): boolean;
// Gets called once after editing is complete. If your return true, then the new
// value will not be used. The editing will have no impact on the record. Use this
// if you do not want a new value from your gui, i.e. you want to cancel the editing.
isCancelAfterEnd?(): boolean;
// If doing full line edit, then gets called when focus should be put into the editor
focusIn?(): boolean;
// If doing full line edit, then gets called when focus is leaving the editor
focusOut?(): boolean;
}Note that you will need to expose the lifecycle/callback methods (for example, the getValue callback) with
forwardRef and useImperativeHandle.
Custom Cell Editor Parameters
When a React component is instantiated the grid will make the grid APIs, a number of utility methods as well as the cell and
row values available to you via props - the interface for what is provided is documented below.
If custom params are provided via the colDef.cellEditorParams property, these
will be additionally added to the params object, overriding items of the same name if a name clash exists.
Properties available on the ICellEditorParams<TData = any, TValue = any, TContext = any> interface.
valueTypeTValue | null | undefined | Current value of the cell |
event | Key value of key that started the edit, eg 'Enter' or 'F2' - non-printable characters appear here |
columnTypeColumn | Grid column |
col | Column definition |
nodeTypeIRowNode | Row node for the cell |
dataTypeTData | Row data |
row | Editing row index |
cell | If doing full row edit, this is true if the cell is the one that started the edit (eg it is the cell the use double clicked on, or pressed a key on etc). |
on | callback to tell grid a key was pressed - useful to pass control key events (tab, arrows etc) back to grid - however you do |
stop | Callback to tell grid to stop editing the current cell. Call with input parameter true to prevent focus from moving to the next cell after editing stops in case the grid property enterNavigatesVerticallyAfterEdit=true |
e | A reference to the DOM element representing the grid cell that your component will live inside. Useful if you want to add event listeners or classes at this level. This is the DOM element that gets browser focus when selecting cells. |
parse | Utility function to parse a value using the column's colDef.valueParser |
format | Utility function to format a value using the column's colDef.valueFormatter |
apiTypeGridApi | The grid api. |
contextTypeTContext | Application context as set on gridOptions.context. |
Accessing Cell Editor Instances
After the grid has created an instance of a cell editor for a cell it is possible to access that instance. This is useful if you want to call a method that you provide on the cell editor that has nothing to do with the operation of the grid. Accessing cell editors is done using the grid API getCellEditorInstances(params).
If you are doing normal editing, then only one cell is editable at any given time. For this reason if you call getCellEditorInstances() with no params, it will return back the editing cell's editor if a cell is editing, or an empty list if no cell is editing.
An example of calling getCellEditorInstances() is as follows:
const instances = api.getCellEditorInstances(params);
if (instances.length > 0) {
const instance = instances[0];
}