Skip to content

Commit

Permalink
fix: 🐛 fix grouping selector not closing on outside click
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustav-Eikaas committed Aug 18, 2023
1 parent 67d83a0 commit 1297869
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/workspace-fusion/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [4.0.3]

fix: outside click on garden popover grouping selector

## [1.0.13]

fix: double fetch on init
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace-fusion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@equinor/workspace-fusion",
"version": "4.0.2",
"version": "4.0.3",
"type": "module",
"sideEffects": false,
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { MutableRefObject, useEffect } from 'react';

export const useOutsideClick = (
handler: (e: MouseEvent, el: HTMLElement) => void,
...refs: MutableRefObject<HTMLElement | null>[]
) => {
const onOutsideClick = (e: MouseEvent) => {
/** Eds input LI is removed from dom before callback runs */
if (!document.contains(e.target as Node)) {
return;
}

// updated this to use the passed componentRef
const inMain = refs.some((s) => s.current?.contains(e.target as HTMLElement));
const isOutside = !inMain;
if (isOutside) {
handler(e, e.target as HTMLElement);
}
};
useEffect(() => {
document.addEventListener('click', onOutsideClick);
return () => {
document.removeEventListener('click', onOutsideClick);
};
}, [refs]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Autocomplete, Radio } from '@equinor/eds-core-react';
import { FilterState } from '@equinor/workspace-filter';
import { GardenDataSource, GroupingOption } from '@equinor/workspace-garden';
import { useQuery } from '@tanstack/react-query';
import { useRef } from 'react';
import styled from 'styled-components';
import { useOutsideClick } from '../../../lib/hooks/useGardenPopoverOutsideClick';

type GroupingSelectorProps = {
dataSource: GardenDataSource<FilterState>;
Expand All @@ -13,6 +15,9 @@ type GroupingSelectorProps = {
type: string | null;
onChangeDimension: (dimension: string | null) => void;
onChangeMode: (type: string | null) => void;
popoverRef: React.MutableRefObject<HTMLDivElement | null>;
iconRef: React.MutableRefObject<HTMLDivElement | null>;
close: VoidFunction;
};

export function GroupingSelector({
Expand All @@ -24,6 +29,9 @@ export function GroupingSelector({
onChangeDimension,
onChangeMode,
groupingKeys,
iconRef,
popoverRef,
close,
}: GroupingSelectorProps): JSX.Element | null {
const { data } = useQuery(['garden', ...groupingKeys, dimension, type, context], {
refetchOnWindowFocus: false,
Expand All @@ -34,6 +42,17 @@ export function GroupingSelector({
dataSource.getGardenMeta({ groupingKeys, dimension, type }, context, signal ?? new AbortSignal()),
});

const selectorRef = useRef(null);

useOutsideClick(
(e, a) => {
console.log('outside click', a);
close();
},
popoverRef,
iconRef
);

const setGardenKey = (key: string) => {
const foundGroupingOption = data?.allGroupingOptions.find((option) => option.groupingKey === key);
if (!foundGroupingOption) {
Expand Down Expand Up @@ -72,6 +91,7 @@ export function GroupingSelector({
return (
<StyledAutoCompleteWrapper>
<Autocomplete
ref={selectorRef}
key={groupingKeys[0]}
options={data.allGroupingOptions.map((option: GroupingOption) => option.groupingKey)}
label={'Column headers'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const GardenPopoverItem = ({
const [isOpen, setIsOpen] = useState(false);
const pRef = useRef(null);
const [groupState, setGroupState] = useState<GroupState>(groupingKeys$.value);
const popoverRef = useRef<HTMLDivElement | null>(null);

useEffect(() => {
const sub = groupingKeys$.pipe(distinctUntilChanged()).subscribe((r) => {
Expand All @@ -53,7 +54,7 @@ export const GardenPopoverItem = ({
onClick={() => setIsOpen((s) => !s)}
/>
{createPortal(
<Popover style={{ height: '450px', width: '300px' }} open={isOpen} anchorEl={pRef.current}>
<Popover ref={popoverRef} style={{ height: '450px', width: '300px' }} open={isOpen} anchorEl={pRef.current}>
<Popover.Header>
<StyledPopoverHeaderLine>
<Popover.Title>Garden settings</Popover.Title>
Expand All @@ -67,6 +68,9 @@ export const GardenPopoverItem = ({
<Popover.Content style={{ overflow: 'hidden' }}>
<Suspense fallback={<GroupingSelectorLoading />}>
<GroupingSelector
iconRef={pRef}
close={() => setIsOpen(false)}
popoverRef={popoverRef}
groupingKeys={groupState.groupingKeys}
setGroupingKeys={setGroupingKeys}
dimension={groupState.dimension}
Expand Down

0 comments on commit 1297869

Please sign in to comment.