How do I create a multiline text editor in KendoReact Grid?

2 Answers 77 Views
Grid TextArea
Ryan
Top achievements
Rank 1
Iron
Ryan asked on 08 Oct 2024, 10:21 PM | edited on 08 Oct 2024, 10:22 PM

I am allowing users to edit in my KendoReact grid. By default the editor renders <input type=text elements. Is it possible to change that so it would render a <textarea element instead?

I see there are a few solutions with non-KendoReact: https://www.telerik.com/forums/how-to-change-input-to-textarea-in-popup-editor

I can't figure out how to make this work with KendoReact, other than creating a custom editor, which seems like a pain for a request that must be pretty common.

2 Answers, 1 is accepted

Sort by
0
Accepted
Ryan
Top achievements
Rank 1
Iron
answered on 09 Oct 2024, 05:10 PM
This is so useful! My table is using the "all cells are in edit mode at all times" where I was hoping to have text areas available. I don't know if that's possible too?
Ryan
Top achievements
Rank 1
Iron
commented on 09 Oct 2024, 05:46 PM | edited

I made this work. I don't know if this is "correct", but it certainly works for me. I added a TextAreaCell:

/**
 * This component is used to render a text area cell where users can edit multi-line text in the Telerik Kendo React grid.
 * @param props grid cell props
 * @constructor
 */
export function TextAreaCell(props) {
  const {dataItem} = props;
  return (
    <td {...props.tdProps}>
      <textarea name={props.field}
                value={dataItem[props.field]}
                onChange={(event: any) => {
                  event.field = props.field;
                  event.dataItem = dataItem;
                  event.value = event.target.value;
                  props.onChange(event);
                }}
                className={styles["textarea-column"]}
      />
    </td>
  );
}

Then in my Grid I use it like so:


<Grid data={myData}
      editField="inEdit"
      onItemChange={handleUpdate}
>
<GridColumn field="shouldSave" title="Save?" width="60px" editor="boolean" />
<GridColumn field="name" title="Name" width="150px" />
<GridColumn field="page" title="Page" width="50px" editable={false} />
<GridColumn field="description" title="Description" cell={TextAreaCell}/>
</Grid>
The "description" column is now a text area and my `handleUpdate` handler is called with the correct data item, etc.
Hetali
Telerik team
commented on 10 Oct 2024, 03:12 PM

Hi Ryan,

Indeed, using the cell property of the GridColumnProps is the right way to define a custom component in the column. You can also use the KendoReact TextArea component in the column as seen in this StackBlitz example.

Please let me know if I can further assist you.

Regards,
Hetali
Progress Telerik
Ryan
Top achievements
Rank 1
Iron
commented on 10 Oct 2024, 03:45 PM | edited

Thanks Hetali,

Unfortunately nothing is editable in that StackBlitz example. I had to change it to this to make it work:

export function TextAreaCell(props) {
  const {dataItem} = props;
  return (
    <td {...props.tdProps} className={styles["textarea-cell"]}>
      <TextArea name={props.field}
                value={dataItem[props.field]}
                onChange={(event: any) => {
                  event.field = props.field;
                  event.dataItem = dataItem;
                  props.onChange(event);
                }}
                className={styles["textarea-column"]}
      />
    </td>
  );
}
Hetali
Telerik team
commented on 10 Oct 2024, 04:22 PM

Hi Ryan,

I'm glad you were able to modify the code and make the column editable.

I will proceed to close the ticket. In case you have more questions, simply reply in this thread to reopen the ticket.

Regards,
Hetali
Progress Telerik

1
Hetali
Telerik team
answered on 09 Oct 2024, 03:21 PM

Hi Ryan,

Thank you for reaching out.

In order to change the input element to a textarea element in the KendoReact Grid Editor, modify the component of FieldProps to a textarea component. For example:

const TextAreaField = fieldRenderProps => {
  return <>
    <Label editorId={id} className={"k-form-label"}> {label} </Label>
    <div className={"k-form-field-wrap"}>
      <TextArea {...others} />
    </div>
  </>;
};

const EditForm = props => {
  return <Form>
    <FormElement>
      <FieldWrapper>
        <Field name={"ProductName"} component={TextAreaField} label={"Product Name"} />
      </FieldWrapper>
    </FormElement>
  </Dialog>} {...other} />;
};

Output:

TextArea with External Form

TextArea in Inline Editing

I have create two StackBlitz examples where I have modified the input with textarea in the KendoReact Grid Editor:

  1. KendoReact Grid with External Form
  2. KendoReact Grid with Inline Editing

Please give the above mentioned approach a try and let me know if it helps modify the input with textarea or if you have any further questions.

Regards,
Hetali
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid TextArea
Asked by
Ryan
Top achievements
Rank 1
Iron
Answers by
Ryan
Top achievements
Rank 1
Iron
Hetali
Telerik team
Share this question
or