Skip to content

Commit

Permalink
feat: added isActive to useFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar committed Oct 21, 2022
1 parent e189121 commit 20cd080
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/components/filterControl.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React, { createContext, useCallback, useState } from 'react';
import { createContext, useMemo, useState } from 'react';
import { useTheme } from '../hooks/useTheme';
import { useCssVariables } from '../theme/useCssVariables';
import { useColumnContext, useTableContext } from './table';

export const FilterControlContext = createContext((): void => void 0);
export const FilterControlContext = createContext({
isActive: false,
close: (): void => void 0,
});

export function FilterControl<T>(): JSX.Element | null {
const table = useTableContext<T>();
Expand Down Expand Up @@ -34,14 +37,18 @@ export function FilterControl<T>(): JSX.Element | null {
});
}

const close = useCallback(function () {
setAnchor(null);
}, []);
const context = useMemo(
() => ({
isActive: !!anchor,
close: () => setAnchor(null),
}),
[!!anchor],
);

if (!filter) return null;

return (
<FilterControlContext.Provider value={close}>
<FilterControlContext.Provider value={context}>
<div
onClick={(e) => setAnchor(e.currentTarget)}
onContextMenu={(e) => {
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ export function useFilter<T, V, F, S extends SerializableValue>(impl: FilterImpl

useEffect(() => delayedUpdate.flush(), [delayedUpdate]);

const context = useContext(FilterControlContext);

return {
value: dirtyValue ?? value,
onChange,
close: useContext(FilterControlContext),
filterBy: filterBy ?? ((x) => x as unknown as F | F[]),
...context,
};
}

0 comments on commit 20cd080

Please sign in to comment.