Skip to content

Commit

Permalink
feat(FilterGroup): disabled options
Browse files Browse the repository at this point in the history
  • Loading branch information
MEsteves22 authored and plagoa committed Feb 21, 2024
1 parent ffdc673 commit 6bd17f1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 36 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/FilterGroup/Counter/Counter.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useContext } from "react";

import { ExtractNames } from "../../utils/classes";

import { useDefaultProps } from "../../hooks/useDefaultProps";

import { HvFilterGroupContext } from "../FilterGroupContext";
import { staticClasses, useClasses } from "./Counter.styles";
import { HvFilterGroupFilters, HvFilterGroupValue } from "../types";
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/FilterGroup/FilterContent/FilterContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { forwardRef, useContext, useMemo, useRef, useState } from "react";

import { Filters } from "@hitachivantara/uikit-react-icons";

import { useTheme } from "../../hooks/useTheme";
Expand All @@ -10,9 +9,7 @@ import { HvFormStatus } from "../../Forms";
import { HvTypography } from "../../Typography";
import { ExtractNames } from "../../utils/classes";
import { setId } from "../../utils/setId";

import { useDefaultProps } from "../../hooks/useDefaultProps";

import { HvFilterGroupValue, HvFilterGroupHorizontalPlacement } from "../types";
import { staticClasses, useClasses } from "./FilterContent.styles";
import { HvFilterGroupContext } from "../FilterGroupContext";
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/FilterGroup/FilterGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { useControlled } from "../hooks/useControlled";
import { ExtractNames } from "../utils/classes";
import { setId } from "../utils/setId";
import { useDefaultProps } from "../hooks/useDefaultProps";

import {
HvFilterGroupContent,
HvFilterGroupContentProps,
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/FilterGroup/FilterGroupContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import {
useMemo,
useState,
} from "react";

import isEqual from "lodash/isEqual";

import { useDefaultProps } from "../hooks/useDefaultProps";

import { useSavedState } from "../utils/useSavedState";

import { HvFilterGroupFilters, HvFilterGroupValue } from "./types";

interface HvFilterGroupContextValue {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/FilterGroup/LeftPanel/LeftPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { HvPanel } from "../../Panel";
import { ExtractNames } from "../../utils/classes";
import { setId } from "../../utils/setId";
import { wrapperTooltip } from "../../utils/wrapperTooltip";

import { HvFilterGroupCounter } from "../Counter";
import { HvFilterGroupContext } from "../FilterGroupContext";
import { staticClasses, useClasses } from "./LeftPanel.styles";
Expand Down
50 changes: 24 additions & 26 deletions packages/core/src/FilterGroup/RightPanel/RightPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useCallback, useContext, useEffect, useMemo, useState } from "react";

import cloneDeep from "lodash/cloneDeep";

import { HvCheckBox } from "../../CheckBox";
Expand All @@ -9,7 +8,6 @@ import { HvPanel } from "../../Panel";
import { HvTypography } from "../../Typography";
import { ExtractNames } from "../../utils/classes";
import { setId } from "../../utils/setId";

import { HvFilterGroupContext } from "../FilterGroupContext";
import { staticClasses, useClasses } from "./RightPanel.styles";

Expand Down Expand Up @@ -48,22 +46,27 @@ export const HvFilterGroupRightPanel = ({
activeGroup,
} = useContext(HvFilterGroupContext);

const activeGroupOptions = useMemo(
() =>
filterOptions[activeGroup]?.data
.filter((option) =>
option.name.toLowerCase().includes(searchStr.toLowerCase())
)
.map((option) => option.id) || [],
[filterOptions, activeGroup, searchStr]
);
const { all: allActiveGroupOptions, enabled: enabledActiveGroupOptions } =
useMemo(() => {
const filteredOptions = filterOptions[activeGroup]?.data.filter(
(option) => option.name.toLowerCase().includes(searchStr.toLowerCase())
);

return {
all: filteredOptions.map((option) => option.id) || [],
enabled:
filteredOptions
.filter((option) => !option.disabled)
.map((option) => option.id) || [],
};
}, [filterOptions, activeGroup, searchStr]);

const activeFilterValues = useMemo(
() =>
filterValues[activeGroup]?.filter((value) =>
activeGroupOptions.includes(value)
allActiveGroupOptions.includes(value)
) || [],
[filterValues, activeGroupOptions, activeGroup]
[filterValues, allActiveGroupOptions, activeGroup]
);

const listValues = useMemo(
Expand All @@ -81,11 +84,11 @@ export const HvFilterGroupRightPanel = ({
const updateSelectAll = useCallback(() => {
const nbrSelected = activeFilterValues?.length;
const hasSelection = nbrSelected > 0;
const allSelect = nbrSelected === activeGroupOptions.length;
const allSelect = nbrSelected === allActiveGroupOptions.length;

setAnySelected(hasSelection);
setAllSelected(hasSelection && allSelect);
}, [activeFilterValues, activeGroupOptions]);
}, [activeFilterValues, allActiveGroupOptions]);

useEffect(() => {
updateSelectAll();
Expand All @@ -108,30 +111,25 @@ export const HvFilterGroupRightPanel = ({
if (anySelected) {
if (searchStr !== "") {
newFilterValues[activeGroup] = filterValues[activeGroup]?.filter(
(value) => !activeGroupOptions.includes(value)
(value) => !enabledActiveGroupOptions.includes(value)
);
} else {
newFilterValues[activeGroup] = [];
}
} else {
newFilterValues[activeGroup] = [...activeGroupOptions];
newFilterValues[activeGroup] = [...enabledActiveGroupOptions];
}

setFilterValues(newFilterValues);
}, [
activeGroup,
activeGroupOptions,
enabledActiveGroupOptions,
anySelected,
filterValues,
setFilterValues,
searchStr,
]);

/**
* Create selectAll component.
*
* @returns {*}
*/
const SelectAll = useCallback(() => {
const nbrSelected = activeFilterValues?.length;

Expand All @@ -140,12 +138,12 @@ export const HvFilterGroupRightPanel = ({
{nbrSelected > 0 ? (
<>
<b>{nbrSelected}</b>
{` ${labels?.multiSelectionConjunction} ${activeGroupOptions.length}`}
{` ${labels?.multiSelectionConjunction} ${allActiveGroupOptions.length}`}
</>
) : (
<>
<b>{labels?.selectAll}</b>
{` (${activeGroupOptions.length})`}
{` (${allActiveGroupOptions.length})`}
</>
)}
</HvTypography>
Expand All @@ -165,7 +163,7 @@ export const HvFilterGroupRightPanel = ({
);
}, [
activeFilterValues?.length,
activeGroupOptions.length,
allActiveGroupOptions.length,
allSelected,
anySelected,
handleSelectAll,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/FilterGroup/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type HvFilterGroupFilters = {
data: {
id: string | number;
name: string;
disabled?: boolean;
}[];
}[];

Expand Down

0 comments on commit 6bd17f1

Please sign in to comment.