(core) Use individual choices for filtering choice lists

Test Plan: Wrote unit and browser tests that verify new behavior.

Reviewers: paulfitz, dsagal

Reviewed By: dsagal

Subscribers: alexmojaki

Differential Revision: https://phab.getgrist.com/D2855
This commit is contained in:
George Gevoian
2021-06-11 07:32:05 -07:00
parent 5d3a4b5b5b
commit b94eb107d4
6 changed files with 71 additions and 20 deletions

View File

@@ -1,18 +1,29 @@
import { CellValue } from "app/common/DocActions";
import { FilterState, makeFilterState } from "app/common/FilterState";
import { decodeObject } from "app/plugin/objtypes";
import { isList } from "./gristTypes";
export type ColumnFilterFunc = (value: CellValue) => boolean;
// Returns a filter function for a particular column: the function takes a cell value and returns
// whether it's accepted according to the given FilterState.
export function makeFilterFunc({ include, values }: FilterState): ColumnFilterFunc {
export function makeFilterFunc({ include, values }: FilterState,
columnType?: string): ColumnFilterFunc {
// NOTE: This logic results in complex values and their stringified JSON representations as equivalent.
// For example, a TypeError in the formula column and the string '["E","TypeError"]' would be seen as the same.
// TODO: This narrow corner case seems acceptable for now, but may be worth revisiting.
return (val: CellValue) => (values.has(Array.isArray(val) ? JSON.stringify(val) : val) === include);
return (val: CellValue) => {
if (isList(val) && columnType === 'ChoiceList') {
const list = decodeObject(val) as unknown[];
return list.some(item => values.has(item as any) === include);
}
return (values.has(Array.isArray(val) ? JSON.stringify(val) : val) === include);
};
}
// Given a JSON string, returns a ColumnFilterFunc
export function buildColFilter(filterJson: string | undefined): ColumnFilterFunc | null {
return filterJson ? makeFilterFunc(makeFilterState(filterJson)) : null;
export function buildColFilter(filterJson: string | undefined,
columnType?: string): ColumnFilterFunc | null {
return filterJson ? makeFilterFunc(makeFilterState(filterJson), columnType) : null;
}

View File

@@ -136,12 +136,19 @@ export function isCensored(value: CellValue): value is [GristObjCode.Censored] {
return getObjCode(value) === GristObjCode.Censored;
}
/**
* Returns whether a value (as received in a DocAction) represents a list.
*/
export function isList(value: CellValue): value is [GristObjCode.List, ...unknown[]] {
return Array.isArray(value) && value[0] === GristObjCode.List;
}
/**
* Returns whether a value (as received in a DocAction) represents a list or is null,
* which is a valid value for list types in grist.
*/
export function isListOrNull(value: CellValue): boolean {
return value === null || (Array.isArray(value) && value[0] === GristObjCode.List);
return value === null || isList(value);
}
/**