Skip to content
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

"Filter by" option to filter by the value of the cell #1251

Merged
merged 3 commits into from
Jun 2, 2023
Merged
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
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