# Inline

The KendoReact TreeList enables you to create, update, and delete data records inline.

## Basic Concepts

The edit mode of the TreeList rows is based on the value of the [`editField`](https://www.telerik.com/kendo-react-ui/components/treelist/api/treelistprops#editField) property.

[Demo Source](https://demos.telerik.com/kendo-react-ui/treelist/editing-inline/func/source.txt)

## Setup

1. Set the field which will indicate the editable data items by using the [`editField`](https://www.telerik.com/kendo-react-ui/components/treelist/api/treelistprops#editfield) property. This field is part of the temporary data items which are used during editing.

   ```tsx
       <TreeList
           editField="inEdit"
   ```

2. Configure the command column by defining the command buttons inside the [TreeListCell](https://www.telerik.com/kendo-react-ui/components/treelist/api/treelistcell) component. To render the command cell, use a factory function which returns a functional component that has access to all handler functions and the value of the TreeList `editField` property.

   ```tsx
       const CommandCell = MyCommandCell(enterEdit, remove, save, cancel, addChild, editField);
   ```

   ```tsx
       const MyCommandCell = (
           enterEdit: (dataItem: any) => void,
           remove: (dataItem: any) => void,
           save: (dataItem: any) => void,
           cancel: (dataItem: any) => void,
           addChild: (dataItem: any) => void,
           editField: string
       ) => {
           return (props: TreeListCellProps) => {
               const { dataItem } = props;
               return dataItem[editField] ? (
                   <td>
                       <Button type="button" onClick={() => save(dataItem)}>
                           {dataItem.isNew ? 'Add' : 'Update'}
                       </Button>
                       <Button type="button" onClick={() => cancel(dataItem)}>
                           {dataItem.isNew ? 'Discard' : 'Cancel'}
                       </Button>
                   </td>
               ) : (
                   <td>
                       <Button type="button" onClick={() => addChild(dataItem)}>
                           Add Employee
                       </Button>
                       <Button type="button" onClick={() => enterEdit(dataItem)}>
                           Edit
                       </Button>
                       <Button type="button" onClick={() => remove(dataItem)}>
                           Remove
                       </Button>
                   </td>
               );
           };
       };
   ```

3. Set the [`editCell`](https://www.telerik.com/kendo-react-ui/components/treelist/api/treelistcolumnprops#editcell) and [`cell`](https://www.telerik.com/kendo-react-ui/components/treelist/api/treelistcolumnprops#cell) properties per column.

   ```tsx
       const columns: Array<TreeListColumnProps | {}> = [
           { editCell: TreeListTextEditor, ... },
           { editCell: TreeListTextEditor, ... },
           { editCell: TreeListBooleanEditor, ... },
           { cell: CommandCell, ... }
       ];
   ```

4. Define a function for the [`onItemChange`](https://www.telerik.com/kendo-react-ui/components/treelist/api/treelistprops#onitemchange) event which will handle any input changes during editing. Inside the event, all relevant data, such as the edited data item, the newly entered value, or the edited field will be available as [`onItemChange`](https://www.telerik.com/kendo-react-ui/components/treelist/api/treelistitemchangeevent) parameters.

   ```tsx
        <TreeList onItemChange={onItemChange}>
   ```

   ```tsx
       const onItemChange = (event: TreeListItemChangeEvent) => {
           const field: any = event.field;
           setState({
               ...state,
               data: mapTree(state.data, subItemsField, (item) =>
                   item.id === event.dataItem.id ? extendDataItem(item, subItemsField, { [field]: event.value }) : item
               )
           });
       };
   ```

5. Define the functions which will set the item in edit mode and create a new item in edit mode. You can call these functions from the command buttons in the command cell.

   ```tsx
       const enterEdit = (dataItem: any) => {
           setState({
               ...state,
               // Keep a copy of the original item so cancel can restore it.
               inEdit: [...state.inEdit, extendDataItem(dataItem, subItemsField)]
           });
       };
   ```

   ```tsx
       const addRecord = () => {
           const newRecord: { id: number; isNew: boolean } = createNewItem();
           setState({
               ...state,
               data: [newRecord, ...state.data],
               inEdit: [...state.inEdit, { ...newRecord }]
           });
       };
   ```

6. Define the functions which will handle the `save`, `cancel`, `remove`, and `addChild` actions. You can call these functions from the command buttons, the toolbar template, or a button which is outside of the TreeList.

   ```tsx
       const save = (dataItem: any) => {
           // Drop the temporary editing fields before saving.
           const { isNew, inEdit, ...itemToSave } = dataItem;
           setState({
               ...state,
               data: mapTree(state.data, subItemsField, (item) => (item.id === itemToSave.id ? itemToSave : item)),
               inEdit: state.inEdit.filter((i) => i.id !== itemToSave.id)
           });
       };
   ```

   ```tsx
       const cancel = (editedItem: any) => {
           const { inEdit, data } = state;
           if (editedItem.isNew) {
               // A new item has no original to restore, so remove it.
               return remove(editedItem);
           }

           setState({
               ...state,
               data: mapTree(data, subItemsField, (item) =>
                   item.id === editedItem.id ? inEdit.find((i) => i.id === item.id) : item
               ),
               inEdit: inEdit.filter((i) => i.id !== editedItem.id)
           });
       };
   ```

   ```tsx
       const remove = (dataItem: any) => {
           setState({
               ...state,
               data: removeItems(state.data, subItemsField, (i) => i.id === dataItem.id),
               inEdit: state.inEdit.filter((i) => i.id !== dataItem.id)
           });
       };
   ```

   ```tsx
       const addChild = (dataItem: any) => {
           const newRecord: { id: number; isNew: boolean } = createNewItem();

           setState({
               ...state,
               inEdit: [...state.inEdit, newRecord],
               // Expand the parent so the new child row is visible.
               expanded: [...state.expanded, dataItem.id],
               data: modifySubItems(
                   state.data,
                   subItemsField,
                   (item) => item.id === dataItem.id,
                   (subItems) => [newRecord, ...subItems]
               )
           });
       };
   ```

## Suggested Links

* [API Reference of the TreeList](https://www.telerik.com/kendo-react-ui/components/treelist/api/treelistprops)
* [API Reference of the TreeListToolbar](https://www.telerik.com/kendo-react-ui/components/treelist/api/treelisttoolbar)
* [API Index of the TreeList](https://www.telerik.com/kendo-react-ui/components/treelist/api)
