Skip to content

Commit

Permalink
"Filter by" option to filter by the value of the cell (#1251)
Browse files Browse the repository at this point in the history
* add filter-by option in right-click menu

* enable filterby option on fields which are spread out versions of a JSON

* remove "Filter value" option
  • Loading branch information
mnmt7 authored Jun 2, 2023
1 parent 20a2100 commit 23e29ca
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/components/Table/ContextMenu/MenuContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
userRolesAtom,
altPressAtom,
confirmDialogAtom,
updateUserSettingsAtom,
} from "@src/atoms/projectScope";
import {
tableScope,
Expand All @@ -34,8 +35,10 @@ import {
updateFieldAtom,
tableFiltersPopoverAtom,
_updateRowDbAtom,
tableIdAtom,
} from "@src/atoms/tableScope";
import { FieldType } from "@src/constants/fields";
import { TableRow } from "@src/types/table";

interface IMenuContentsProps {
onClose: () => void;
Expand All @@ -58,6 +61,8 @@ export default function MenuContents({ onClose }: IMenuContentsProps) {
tableFiltersPopoverAtom,
tableScope
);
const [updateUserSettings] = useAtom(updateUserSettingsAtom, projectScope);
const [tableId] = useAtom(tableIdAtom, tableScope);

const addRowIdType = tableSchema.idType || "decrement";

Expand Down Expand Up @@ -241,22 +246,47 @@ export default function MenuContents({ onClose }: IMenuContentsProps) {

// Cell actions
// TODO: Add copy and paste here
const cellValue = row?.[selectedCell.columnKey];

const selectedColumnKey = selectedCell.columnKey;
const selectedColumnKeySplit = selectedColumnKey.split(".");

const getNestedFieldValue = (object: TableRow, keys: string[]) => {
let value = object;

for (let i = 0; i < keys.length; i++) {
const key = keys[i];

if (value && typeof value === "object" && key in value) {
value = value[key];
} else {
// Handle cases where the key does not exist in the nested structure
return undefined;
}
}

return value;
};

const cellValue = getNestedFieldValue(row, selectedColumnKeySplit);

const columnFilters = getFieldProp(
"filter",
selectedColumn?.type === FieldType.derivative
? selectedColumn.config?.renderFieldType
: selectedColumn?.type
);
const handleFilterValue = () => {
openTableFiltersPopover({
defaultQuery: {
const handleFilterBy = () => {
const filters = [
{
key: selectedColumn.fieldName,
operator: columnFilters!.operators[0]?.value || "==",
value: cellValue,
},
});
];

if (updateUserSettings) {
updateUserSettings({ tables: { [`${tableId}`]: { filters } } });
}
onClose();
};
const cellActions = [
Expand All @@ -272,10 +302,10 @@ export default function MenuContents({ onClose }: IMenuContentsProps) {
onClick: handleClearValue,
},
{
label: "Filter value",
label: "Filter by",
icon: <FilterIcon />,
disabled: !columnFilters || cellValue === undefined,
onClick: handleFilterValue,
onClick: handleFilterBy,
},
];
actionGroups.push(cellActions);
Expand Down

0 comments on commit 23e29ca

Please sign in to comment.