From d0e89da94c1709dfcd4bb884af499a331e945a6e Mon Sep 17 00:00:00 2001 From: soupette Date: Tue, 2 Feb 2021 15:39:28 +0100 Subject: [PATCH] Auto open the first section of conditions in RBAC Signed-off-by: soupette --- .../Roles/ConditionsSelect/MenuList/index.js | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/strapi-admin/ee/admin/components/Roles/ConditionsSelect/MenuList/index.js b/packages/strapi-admin/ee/admin/components/Roles/ConditionsSelect/MenuList/index.js index 86f4690db3..74cbe311c7 100644 --- a/packages/strapi-admin/ee/admin/components/Roles/ConditionsSelect/MenuList/index.js +++ b/packages/strapi-admin/ee/admin/components/Roles/ConditionsSelect/MenuList/index.js @@ -1,5 +1,5 @@ /* eslint-disable jsx-a11y/click-events-have-key-events */ -import React, { useState, useCallback } from 'react'; +import React, { useMemo, useState, useCallback } from 'react'; import PropTypes from 'prop-types'; import { components } from 'react-select'; import { groupBy, intersectionWith } from 'lodash'; @@ -13,12 +13,26 @@ import UpperFirst from '../../../../../../admin/src/components/Roles/ConditionsS import { usePermissionsContext } from '../../../../../../admin/src/hooks'; /* eslint-disable jsx-a11y/no-static-element-interactions */ +const createCollapsesObject = arrayOfCategories => + arrayOfCategories.reduce((acc, current, index) => { + acc[current[0]] = index === 0; + + return acc; + }, {}); const MenuList = ({ selectProps, ...rest }) => { const Component = components.MenuList; const { isSuperAdmin } = usePermissionsContext(); - const [collapses, setCollapses] = useState({}); - const optionsGroupByCategory = groupBy(selectProps.options, 'category'); + + const arrayOfOptionsGroupedByCategory = useMemo(() => { + return Object.entries(groupBy(selectProps.options, 'category')); + }, [selectProps.options]); + const initCollapses = useMemo(() => createCollapsesObject(arrayOfOptionsGroupedByCategory), [ + arrayOfOptionsGroupedByCategory, + ]); + + const [collapses, setCollapses] = useState(initCollapses); + const toggleCollapse = collapseName => { setCollapses(prevState => ({ ...prevState, [collapseName]: !collapses[collapseName] })); }; @@ -69,14 +83,16 @@ const MenuList = ({ selectProps, ...rest }) => { return (
    - {Object.entries(optionsGroupByCategory).map((category, index) => { + {arrayOfOptionsGroupedByCategory.map((category, index) => { + const [categoryName, conditions] = category; + return (
  • toggleCollapse(category[0])} + onClick={() => toggleCollapse(categoryName)} > { fontSize: '11px', color: '#919bae', }} - icon={collapses[category[0]] ? 'chevron-up' : 'chevron-down'} + icon={collapses[categoryName] ? 'chevron-up' : 'chevron-down'} />
    - - {category[1].map(action => { + + {conditions.map(action => { return (
  • @@ -130,7 +146,7 @@ const MenuList = ({ selectProps, ...rest }) => { ); })} - {index + 1 < Object.entries(optionsGroupByCategory).length && ( + {index + 1 < arrayOfOptionsGroupedByCategory.length && (
    )}