DrawerContainer displaying/hiding items

1 Answer 88 Views
Drawer
Rebeca
Top achievements
Rank 1
Iron
Rebeca asked on 06 Dec 2021, 01:24 PM

Hi,

I am using a menu similar to the one used here https://stackblitz.com/edit/react-raladh?file=app%2FDrawerContainer.jsx

However, I would like to hide and display the child items given a condition. Do you have any recommendation for achieveing that?

Best regards,

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 06 Dec 2021, 02:51 PM

Hello,

This is already done conditionally in the demo using the visible custom prop:

const CustomItem = (props) => {
  const { visible, ...others } = props;
  const arrowDir = props['data-expanded']
    ? 'k-i-arrow-chevron-down'
    : 'k-i-arrow-chevron-right';
  return (
    <React.Fragment>
      {props.visible === false ? null : (
        <DrawerItem {...others}>
          <span className={'k-icon ' + props.icon} />
          <span className={'k-item-text'}>{props.text}</span>
          {props['data-expanded'] !== undefined && (
            <span
              className={'k-icon ' + arrowDir}
              style={{
                position: 'absolute',
                right: 10,
              }}
            />
          )}
        </DrawerItem>
      )}
    </React.Fragment>
  );
};
You can add any logic to the custom item and display it based on some other condition in the application.

Regards,
Stefan
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.

Rebeca
Top achievements
Rank 1
Iron
commented on 06 Dec 2021, 03:15 PM

Thank you Stefan. However, as the way it is, you still have those elements (even though they are hidden). I would like to have them "available" given a condition, if the condition is not true, for example, the item should remain hidden and unavailable. Following the demo example, I would be able to see "Italian food" only if this was available.

Thank you for your assistance in this matter.

 

 

Stefan
Telerik team
commented on 07 Dec 2021, 05:51 AM

Hello,

In the example, the items that have visible false are not rendered in the DOM. Could you please clarify what you mean by `you still have those elements (even though they are hidden)`.

For example, I added a custom available property for Italian food and it is not in the DOM:

https://stackblitz.com/edit/react-raladh-jqathw?file=app%2FDrawerContainer.jsx

Rebeca
Top achievements
Rank 1
Iron
commented on 09 Dec 2021, 01:52 PM

Hi Stefan,

Thanks! Now I can see the available property, however, I would like to find a way that this is set up inside a condition. As it is, I am not able to include a conditional statement inside React.useState.

Perhaps, you can give me an example where I can access the available property and change its value regarding a condition, so "Italian Food", for instance, is only available given a condition.

Best,

Stefan
Telerik team
commented on 13 Dec 2021, 05:49 AM

Hello, 

To ensure that we can provide an example that covers this, please share an example with the current issue that needs to be addressed. Then based on that we will update it if possible to cover the requirement.

You can use the provided example as a starting point at tells us where the condition has to be added and what should happen based on that.

Rebeca
Top achievements
Rank 1
Iron
commented on 20 Dec 2021, 01:05 PM | edited

Hi Stefan,

Thank you for your assistance. I have the following questions:

  1. When the available property is added, the menu is always visible instead. Can we pass more than one condition, for instance, available and visible?
  2. In the example below, I created a condition given the "fetchItalianFood" response, however, I am afraid that in my case since I clone props inside the condition, the state is not updated. Do you have any suggestion?https://stackblitz.com/edit/react-raladh?file=app%2FDrawerContainer.jsx

Best,

Konstantin Dikov
Telerik team
commented on 22 Dec 2021, 08:51 AM

If you need to make a request to determine whether or not a item should be displayed or not, you can initially hide it and if it should be displayed after the response, you can show it by using a state variable. Here is the modified example:

const CustomItem = (props) => {
  const [fetchItalianFood, setFetchItalianFood] = React.useState(0);
  React.useEffect(() => {
    setFetchItalianFood(1); //this simulated async response
  });

Tags
Drawer
Asked by
Rebeca
Top achievements
Rank 1
Iron
Answers by
Stefan
Telerik team
Share this question
or