Changed subItemsField for the DropDownTree does not work

1 Answer 93 Views
DropDownList TreeList
Dide
Top achievements
Rank 2
Iron
Iron
Dide asked on 21 Oct 2022, 09:02 AM

I'm trying to change the field name of the `subItemsField` of the `DropDownTree` component and following the example of the docs here: https://www.telerik.com/kendo-react-ui/components/dropdowns/dropdowntree/

It only works when the field is called `items`, and doesn't use the `subItemsField` string I add (which is `relatedProducts`). This is a problem because I don't want to call this field `items` in my data

Data example:

  const data = [
    {
      name: 'Furniture',
      relatedProducts: [
        { name: 'Tables & Chairs' },
        { name: 'Sofas' },
        { name: 'Occasional Furniture' },
      ],
    },
    {
      name: 'Decor',
      relatedProducts: [{ name: 'Tables & Chairs' }, { name: 'Sofas' }, { name: 'Occasional Furniture' }],
    },
  ];

 

Component with changed `subItemsField`

import { extendDataItem, mapTree } from '@progress/kendo-react-common';
import { filterBy } from '@progress/kendo-react-data-tools';
import {
  DropDownTree,
  DropDownTreeChangeEvent,
  DropDownTreeExpandEvent,
} from '@progress/kendo-react-dropdowns';
import React from 'react';

export const processTreeData = (data, state, fields) => {
  const { selectField, expandField, dataItemKey, subItemsField } = fields;

  const { expanded, value, filter } = state;
  const filtering = Boolean(filter && filter.value);

  return mapTree(
    filtering ? filterBy(data, [filter], subItemsField) : data,
    subItemsField,
    (item) => {
      const props = {
        [expandField]: expanded.includes(item[dataItemKey]),
        [selectField]: value && item[dataItemKey] === value[dataItemKey],
      };

      return filtering ? extendDataItem(item, subItemsField, props) : { ...item, ...props };
    },
  );
};

export const expandedState = (item, dataItemKey, expanded) => {
  const nextExpanded = expanded.slice();
  const itemKey = item[dataItemKey];
  const index = expanded.indexOf(itemKey);
  index === -1 ? nextExpanded.push(itemKey) : nextExpanded.splice(index, 1);

  return nextExpanded;
};

const selectField = 'selected';
const expandField = 'expanded';
const dataItemKey = 'name';
const textField = 'name';
const subItemsField = 'relatedProducts';

const fields = { selectField, expandField, dataItemKey, subItemsField };

const InputWithTreeList = ({ data }: { data: Record<string, unknown>[] }) => {
  const [value, setValue] = React.useState(null);
  const [expanded, setExpanded] = React.useState([data[0][dataItemKey]]);

  const onChange = (event: DropDownTreeChangeEvent) => setValue(event.value);
  const onExpandChange = React.useCallback(
    (event: DropDownTreeExpandEvent) =>
      setExpanded(expandedState(event.item, dataItemKey, expanded)),
    [expanded],
  );

  const treeData = React.useMemo(() => {
    return processTreeData(data, { expanded, value }, fields);
  }, [expanded, value]);

  return (
    <DropDownTree
      style={{ width: '300px' }}
      data={treeData}
      value={value}
      onChange={onChange}
      placeholder="Start typing..."
      textField={textField}
      dataItemKey={dataItemKey}
      selectField={selectField}
      expandField={expandField}
      onExpandChange={onExpandChange}
    />
  );
};

export default InputWithTreeList;

1 Answer, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 24 Oct 2022, 06:55 PM

Hi Dide,

In order to change the subItemsField in the DropDownTree you need to set the "subItemsField" property of the component:

const subItemsField = 'subItems';
...
    <DropDownTree
      subItemsField={subItemsField}
     ....

Here is an example with the above change:

Hope this helps.

 

Regards,
Konstantin Dikov
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
DropDownList TreeList
Asked by
Dide
Top achievements
Rank 2
Iron
Iron
Answers by
Konstantin Dikov
Telerik team
Share this question
or