Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource for tree-menu-item #437

Merged
merged 27 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@

- Add styles for primary dropdown-toggle (INDIGO Sprint 230623, [!428](https://github.com/TeskaLabs/asab-webui/pull/428))

- Add a condition that will allow users with the right resource to see hidden tree-menu-items (INDIGO Sprint 230623, [!437](https://github.com/TeskaLabs/asab-webui/pull/437))

### Bugfix

- Bug fix for HelpComponent, not display a Helpcomoponent when the user changing the page (INDIGO Sprint 230317, [!401](https://github.com/TeskaLabs/asab-webui/pull/401))
Expand Down
45 changes: 45 additions & 0 deletions doc/tree-menu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# ASAB WebUI TreeMenu component

This component is based on the library [react-simple-tree-menu](https://github.com/iannbing/react-simple-tree-menu).

Displays menu items in a clear structure.

## Example code

```javascript
import { TreeMenu } from 'asab-webui';
...

<TreeMenu
resource={resource}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aringocode If there is a resource in the example, there should be also resources, innit? :)

data={treeData}
hasSearch={true}
hasNodes={true} // default: false
searchOptions={{
placeholder: t("Search"),
dropdown: {
title: t("Actions"),
children: ChildrenCustomDropdownComponent
}
}}
/>

```


## Props:

#### Required:

- `data`: object or array, data in a special structure. The detailed data structure is described in the library documentation react-simple-tree-menu.
Pe5h4 marked this conversation as resolved.
Show resolved Hide resolved

- `hasSearch`: boolean, changes the styles for the search input and affects the display of placeholder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aringocode Its not a placeholer, it should render the Search for the TreeMenu


- `hasNodes`: boolean, if a `TreeNode` is the last node of its branch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aringocode I dont understand this :(


- `resource`: string, resource granting display disabled tree-menu-item elements
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aringocode Add also info about that resources are optional argument



#### Optional:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are more optional props, I would just add reference to this: https://github.com/iannbing/react-simple-tree-menu#api

- `searchOptions`: object, conducts placeholder and the dropdown component

49 changes: 27 additions & 22 deletions src/components/TreeMenu/TreeMenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const TreeMenuItem = ({
level = 0, hasNodes, isOpen,
label, searchTerm, openNodes,
toggleNode, matchSearch, focused,
type, isDisabled, ...props
type, isDisabled, resource, ...props
}) => {
const sessionExpired = useSelector(state => state.auth?.sessionExpired);
const resources = useSelector(state => state.auth?.resources);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aringocode I think resources should be also passed to <TreeMenu /> as a prop, like we said - resource/resources should be a optional argument. Take a look to the ButtonWithAuthz, it has a similar approach - and why I think its better to do it this way?Because we will loose a dependency to some parts of the application when we will be decoupling the UI's to microfrontend apps

const paddingLeft = 1.25 * level + 0.5;
const selected = focused ? " selected" : "";
const disabled = (isDisabled || sessionExpired) ? " disabled" : "";
Expand All @@ -24,28 +25,32 @@ const TreeMenuItem = ({
props.onClick && props.onClick();
}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aringocode Remove this empty line please

return (
<li
{...props}
active="false"
className={`tree-menu-item${selected}${disabled}`}
style={{ paddingLeft: `${paddingLeft}rem` }}
onClick={e => {handleClick(e)}}
>
{(type == "folder") && (
<div
style={{ display: 'inline-block' }}
// Clickable toggle icon, not restricted as handleClick method
onClick={e => {
e.stopPropagation();
hasNodes && toggleNode && toggleNode();
}}
>
<ToggleIcon on={isOpen} selected={selected} />
</div>
)}
{label}
</li>
isDisabled && (resources.indexOf(resource) == -1) && (resources.indexOf("authz:superuser") == -1) ?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aringocode Are you aware of that this feature will hide all the content in the Treemenu when e.g. session will expire? I think this has to be handled a bit different way. Also resource should not be a required parameter (we are using that also in Configuration UI).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pe5h4 I tested, if the session ends, the items don't disappear anywhere

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aringocode Are you sure, even tho when the testing user does not have superuser rights or correct resource?

Copy link
Collaborator Author

@aringocode aringocode Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pe5h4 Yeah, I'm sure. These screenshots show resources for a user without access to disabled items and superuser. As you can see in these screenshots, the items did not disappear after the session ended.

Without edit library resource:
image
image

Superuser:
image

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, good. Test that also in Configuration part of the UI please :)

null
:
<li
{...props}
active="false"
className={`tree-menu-item${selected}${disabled}`}
style={{ paddingLeft: `${paddingLeft}rem` }}
onClick={e => {handleClick(e)}}
>
{(type == "folder") && (
<div
style={{ display: 'inline-block' }}
// Clickable toggle icon, not restricted as handleClick method
onClick={e => {
e.stopPropagation();
hasNodes && toggleNode && toggleNode();
}}
>
<ToggleIcon on={isOpen} selected={selected} />
</div>
)}
{label}
</li>
)
};

Expand Down
10 changes: 5 additions & 5 deletions src/components/TreeMenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SimpleTreeMenu from 'react-simple-tree-menu';
import TreeMenuItem from "./TreeMenuItem";

const TreeMenu = ({
data, searchOptions, ...props
data, searchOptions, hasSearch, ...props
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took hasSearch parameter out of the props because it's only used in this file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aringocode Not sure about this action, hasSearch is a part of the TreeMenuItem and it can be eventually present in the Treemenu. Have you tested it with the hasSearch={true} option?

}) => {
const [isDropdownMenuOpen, setDropdownMenu] = useState(false);

Expand All @@ -22,20 +22,20 @@ const TreeMenu = ({
{({ search, items }) => (
<>
<InputGroup>
<InputGroupText className={props.hasSearch ? "p-0 border-0" : "p-0 border-0 w-100"}>
<InputGroupText className={hasSearch ? "p-0 border-0" : "p-0 border-0 w-100"}>
{searchOptions?.dropdown && (
<ButtonDropdown
className={props.hasSearch ? "" : "w-100"}
className={hasSearch ? "" : "w-100"}
size="sm"
isOpen={isDropdownMenuOpen}
toggle={() => setDropdownMenu(prev => !prev)}
>
<DropdownToggle className={props.hasSearch ? "" : "w-100"} caret>{searchOptions.dropdown.title}</DropdownToggle>
<DropdownToggle className={hasSearch ? "" : "w-100"} caret>{searchOptions.dropdown.title}</DropdownToggle>
{searchOptions.dropdown.children}
</ButtonDropdown>
)}
</InputGroupText>
{props.hasSearch &&
{hasSearch &&
<Input
onChange={e => search(e.target.value)}
placeholder={searchOptions?.placeholder}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import { useTranslation } from 'react-i18next';

import { TreeMenu } from 'asab-webui';
import { types } from './actions/actions';
import {
Input,
InputGroup, InputGroupText,
ButtonDropdown, DropdownToggle,
DropdownMenu, DropdownItem
} from "reactstrap";
import { DropdownMenu, DropdownItem } from "reactstrap";


export function TreeViewComponent(props) {
Expand Down