Fix sort and filters behavior (#1211)

This commit is contained in:
Charles Bochet
2023-08-15 05:11:00 +02:00
committed by GitHub
parent e9619ec1ac
commit 9bbdf933e9
5 changed files with 47 additions and 25 deletions

View File

@ -13,6 +13,7 @@ type OwnProps = {
isActive: boolean; isActive: boolean;
children?: ReactNode; children?: ReactNode;
isUnfolded?: boolean; isUnfolded?: boolean;
icon?: ReactNode;
onIsUnfoldedChange?: (newIsUnfolded: boolean) => void; onIsUnfoldedChange?: (newIsUnfolded: boolean) => void;
resetState?: () => void; resetState?: () => void;
HotkeyScope: FiltersHotkeyScope; HotkeyScope: FiltersHotkeyScope;
@ -26,12 +27,19 @@ const StyledDropdownButtonContainer = styled.div`
z-index: 1; z-index: 1;
`; `;
const StyledDropdownButtonIcon = styled.div`
display: flex;
justify-content: center;
margin-right: ${({ theme }) => theme.spacing(1)};
`;
type StyledDropdownButtonProps = { type StyledDropdownButtonProps = {
isUnfolded: boolean; isUnfolded: boolean;
isActive: boolean; isActive: boolean;
}; };
const StyledDropdownButton = styled.div<StyledDropdownButtonProps>` const StyledDropdownButton = styled.div<StyledDropdownButtonProps>`
align-items: center;
background: ${({ theme }) => theme.background.primary}; background: ${({ theme }) => theme.background.primary};
border-radius: ${({ theme }) => theme.border.radius.sm}; border-radius: ${({ theme }) => theme.border.radius.sm};
color: ${({ isActive, theme, color }) => color: ${({ isActive, theme, color }) =>
@ -58,6 +66,7 @@ function DropdownButton({
isUnfolded = false, isUnfolded = false,
onIsUnfoldedChange, onIsUnfoldedChange,
HotkeyScope, HotkeyScope,
icon,
color, color,
}: OwnProps) { }: OwnProps) {
useScopedHotkeys( useScopedHotkeys(
@ -86,6 +95,7 @@ function DropdownButton({
aria-selected={isActive} aria-selected={isActive}
color={color} color={color}
> >
{icon && <StyledDropdownButtonIcon>{icon}</StyledDropdownButtonIcon>}
{label} {label}
</StyledDropdownButton> </StyledDropdownButton>
{isUnfolded && ( {isUnfolded && (

View File

@ -13,11 +13,13 @@ export function FilterDropdownButton({
HotkeyScope, HotkeyScope,
isPrimaryButton = false, isPrimaryButton = false,
color, color,
icon,
label, label,
}: { }: {
context: Context<string | null>; context: Context<string | null>;
HotkeyScope: FiltersHotkeyScope; HotkeyScope: FiltersHotkeyScope;
isPrimaryButton?: boolean; isPrimaryButton?: boolean;
icon?: React.ReactNode;
color?: string; color?: string;
label?: string; label?: string;
}) { }) {
@ -35,6 +37,7 @@ export function FilterDropdownButton({
<MultipleFiltersDropdownButton <MultipleFiltersDropdownButton
context={context} context={context}
HotkeyScope={HotkeyScope} HotkeyScope={HotkeyScope}
icon={icon}
isPrimaryButton={isPrimaryButton} isPrimaryButton={isPrimaryButton}
color={color} color={color}
label={label} label={label}

View File

@ -27,11 +27,13 @@ export function MultipleFiltersDropdownButton({
HotkeyScope, HotkeyScope,
isPrimaryButton = false, isPrimaryButton = false,
color, color,
icon,
label, label,
}: { }: {
context: Context<string | null>; context: Context<string | null>;
HotkeyScope: FiltersHotkeyScope; HotkeyScope: FiltersHotkeyScope;
isPrimaryButton?: boolean; isPrimaryButton?: boolean;
icon?: React.ReactNode;
color?: string; color?: string;
label?: string; label?: string;
}) { }) {
@ -77,20 +79,26 @@ export function MultipleFiltersDropdownButton({
const [isSortAndFilterBarOpen, setIsSortAndFilterBarOpen] = const [isSortAndFilterBarOpen, setIsSortAndFilterBarOpen] =
useRecoilScopedState(sortAndFilterBarScopedState, context); useRecoilScopedState(sortAndFilterBarScopedState, context);
function handleIsUnfoldedChange(newIsUnfolded: boolean) { function handleIsUnfoldedChange(unfolded: boolean) {
if (newIsUnfolded && (!isFilterSelected || !isPrimaryButton)) { if (unfolded && isPrimaryButton) {
setIsSortAndFilterBarOpen(!isSortAndFilterBarOpen);
}
if (
unfolded &&
((isPrimaryButton && !isFilterSelected) || !isPrimaryButton)
) {
setHotkeyScope(HotkeyScope); setHotkeyScope(HotkeyScope);
setIsUnfolded(true); setIsUnfolded(true);
setIsSortAndFilterBarOpen(true); return;
} else if (newIsUnfolded && isFilterSelected && isPrimaryButton) {
setIsSortAndFilterBarOpen(!isSortAndFilterBarOpen);
} else {
if (filterDefinitionUsedInDropdown?.type === 'entity') {
setHotkeyScope(HotkeyScope);
}
setIsUnfolded(false);
resetState();
} }
if (filterDefinitionUsedInDropdown?.type === 'entity') {
setHotkeyScope(HotkeyScope);
}
setIsUnfolded(false);
resetState();
} }
return ( return (
@ -98,6 +106,7 @@ export function MultipleFiltersDropdownButton({
label={label ?? 'Filter'} label={label ?? 'Filter'}
isActive={isFilterSelected} isActive={isFilterSelected}
isUnfolded={isUnfolded} isUnfolded={isUnfolded}
icon={icon}
onIsUnfoldedChange={handleIsUnfoldedChange} onIsUnfoldedChange={handleIsUnfoldedChange}
HotkeyScope={HotkeyScope} HotkeyScope={HotkeyScope}
color={color} color={color}

View File

@ -2,7 +2,11 @@ import { Context } from 'react';
import { useTheme } from '@emotion/react'; import { useTheme } from '@emotion/react';
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { IconArrowNarrowDown, IconArrowNarrowUp } from '@/ui/icon/index'; import {
IconArrowNarrowDown,
IconArrowNarrowUp,
IconPlus,
} from '@/ui/icon/index';
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState'; import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';
import { useRemoveFilter } from '../hooks/useRemoveFilter'; import { useRemoveFilter } from '../hooks/useRemoveFilter';
@ -48,7 +52,7 @@ const StyledChipcontainer = styled.div`
const StyledCancelButton = styled.button` const StyledCancelButton = styled.button`
background-color: inherit; background-color: inherit;
border: none; border: none;
color: ${({ theme }) => theme.font.color.secondary}; color: ${({ theme }) => theme.font.color.tertiary};
cursor: pointer; cursor: pointer;
font-weight: ${({ theme }) => theme.font.weight.medium}; font-weight: ${({ theme }) => theme.font.weight.medium};
margin-left: auto; margin-left: auto;
@ -162,8 +166,9 @@ function SortAndFilterBar<SortField>({
<FilterDropdownButton <FilterDropdownButton
context={context} context={context}
HotkeyScope={FiltersHotkeyScope.FilterDropdownButton} HotkeyScope={FiltersHotkeyScope.FilterDropdownButton}
color={theme.font.color.secondary} color={theme.font.color.tertiary}
label={`+ Add filter`} icon={<IconPlus size={theme.icon.size.md} />}
label={`Add filter`}
/> />
)} )}
</StyledFilterContainer> </StyledFilterContainer>

View File

@ -1,4 +1,4 @@
import { Context, useCallback, useEffect, useState } from 'react'; import { Context, useCallback, useState } from 'react';
import { useTheme } from '@emotion/react'; import { useTheme } from '@emotion/react';
import { IconChevronDown } from '@tabler/icons-react'; import { IconChevronDown } from '@tabler/icons-react';
@ -32,7 +32,6 @@ export function SortDropdownButton<SortField>({
onSortSelect, onSortSelect,
HotkeyScope, HotkeyScope,
context, context,
isPrimaryButton,
}: OwnProps<SortField>) { }: OwnProps<SortField>) {
const theme = useTheme(); const theme = useTheme();
@ -53,8 +52,10 @@ export function SortDropdownButton<SortField>({
setSelectedSortDirection('asc'); setSelectedSortDirection('asc');
}, []); }, []);
const [isSortAndFilterBarOpen, setIsSortAndFilterBarOpen] = const [, setIsSortAndFilterBarOpen] = useRecoilScopedState(
useRecoilScopedState(sortAndFilterBarScopedState, context); sortAndFilterBarScopedState,
context,
);
function handleIsUnfoldedChange(newIsUnfolded: boolean) { function handleIsUnfoldedChange(newIsUnfolded: boolean) {
if (newIsUnfolded) { if (newIsUnfolded) {
@ -65,12 +66,6 @@ export function SortDropdownButton<SortField>({
} }
} }
useEffect(() => {
if (isSortAndFilterBarOpen && isPrimaryButton && isSortSelected) {
setIsUnfolded(true);
}
}, [isPrimaryButton, isSortAndFilterBarOpen, isSortSelected]);
function handleAddSort(sort: SortType<SortField>) { function handleAddSort(sort: SortType<SortField>) {
setIsUnfolded(false); setIsUnfolded(false);
onSortItemSelect(sort); onSortItemSelect(sort);