Skip to content

fix(FilterBar): add event properties to onFiltersDialogSave + fix manually controlled visibleInFilterBar prop #682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/main/src/components/FilterBar/FilterDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ export const FilterDialog = (props) => {
}, [children, searchString, filterBarRefs]);

const handleCheckBoxChange = useCallback(
(element, toggledFilters) => (e) => {
(element) => (e) => {
if (handleSelectionChange) {
handleSelectionChange(enrichEventWithDetails(e, { element, checked: e.target.checked }));
}
setToggledFilters({ ...toggledFilters, [element.key]: e.target.checked });
setToggledFilters((old) => ({ ...old, [element.key]: e.target.checked }));
},
[setToggledFilters, handleSelectionChange]
);
Expand All @@ -251,7 +251,7 @@ export const FilterDialog = (props) => {
{el}
<CheckBox
checked={el.props.visibleInFilterBar || el.props.required || el.type.displayName !== 'FilterGroupItem'}
onChange={handleCheckBoxChange(el, toggledFilters)}
onChange={handleCheckBoxChange(el)}
disabled={el.props.required || el.type.displayName !== 'FilterGroupItem'}
/>
</div>
Expand Down Expand Up @@ -282,7 +282,7 @@ export const FilterDialog = (props) => {
)}
{renderGroups()}
</div>
</Dialog>,
</Dialog>,
document.body
);
};
32 changes: 18 additions & 14 deletions packages/main/src/components/FilterBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,18 @@ const FilterBar: FC<FilterBarPropTypes> = forwardRef((props: FilterBarPropTypes,
);

useEffect(() => {
if (showFilterConfiguration) {
Children.toArray(children).forEach((item: ReactElement<any>) => {
if (
prevVisibleInFilterBarProps.current?.[item.key] !== undefined &&
prevVisibleInFilterBarProps.current?.[item.key] !== item.props.visibleInFilterBar
) {
const updatedToggledFilters = toggledFilters;
delete updatedToggledFilters[item.key];
setToggledFilters(updatedToggledFilters);
Children.toArray(children).forEach((item: ReactElement<any>) => {
setToggledFilters((prev) => {
if (!item.props.hasOwnProperty('visibleInFilterBar') && prev?.[item.key] === undefined) {
return { ...prev, [item.key]: true };
}
if (item.props.hasOwnProperty('visibleInFilterBar')) {
return { ...prev, [item.key]: item.props.visibleInFilterBar };
}
return prev;
});
}
}, [children, prevVisibleInFilterBarProps, setToggledFilters, toggledFilters, showFilterConfiguration]);
});
}, [children, setToggledFilters]);

useEffect(() => {
setShowFilters(useToolbar ? filterBarExpanded : true);
Expand Down Expand Up @@ -171,9 +170,14 @@ const FilterBar: FC<FilterBarPropTypes> = forwardRef((props: FilterBarPropTypes,
const handleDialogSave = useCallback(
(e, newRefs, updatedToggledFilters) => {
setDialogRefs(newRefs);
setToggledFilters({ ...toggledFilters, ...updatedToggledFilters });
setToggledFilters((old) => ({ ...old, ...updatedToggledFilters }));
if (onFiltersDialogSave) {
onFiltersDialogSave(enrichEventWithDetails(e));
onFiltersDialogSave(
enrichEventWithDetails(e, {
elements: newRefs,
toggledElements: { ...toggledFilters, ...updatedToggledFilters }
})
);
}
handleDialogClose(e);
},
Expand Down Expand Up @@ -215,7 +219,7 @@ const FilterBar: FC<FilterBarPropTypes> = forwardRef((props: FilterBarPropTypes,
]);

const safeChildren = useCallback(() => {
if (showFilterConfiguration && Object.keys(toggledFilters).length > 0) {
if (Object.keys(toggledFilters).length > 0) {
return Children.toArray(children).map((child: ReactElement) => {
if (toggledFilters?.[child.key] !== undefined) {
return cloneElement(child, {
Expand Down
1 change: 0 additions & 1 deletion packages/main/src/components/FilterGroupItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ FilterGroupItem.displayName = 'FilterGroupItem';
FilterGroupItem.defaultProps = {
groupName: 'default',
visible: true,
visibleInFilterBar: true,
required: false,
label: ''
};