(core) Fix filtering of empty reflists

Summary:
A formula returning an empty RecordSet in a RefList columns results in storing [] instead of null.
This caused a bug where the empty list was 'flattened' and the cell not appearing in filters at all.
This diff fixes the bug by filtering for the default value `null` instead for RefLists and the empty string for ChoiceLists.
I didn't manage to actually reproduce the bug for ChoiceLists, but this seemed the most sensible thing to do.

Test Plan: New nbrowser test.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D3478
This commit is contained in:
Alex Hall 2022-06-10 23:34:12 +02:00
parent 64d9ecacdb
commit 0350e2df58
3 changed files with 19 additions and 2 deletions

View File

@ -319,8 +319,13 @@ BaseView.prototype.filterByThisCellValue = function() {
// ChoiceList and Reflist values get 'flattened' out so we filter by each element within. // ChoiceList and Reflist values get 'flattened' out so we filter by each element within.
// In any other column type, complex values (even lists) get converted to JSON. // In any other column type, complex values (even lists) get converted to JSON.
let filterValues; let filterValues;
if (gristTypes.isList(value) && gristTypes.isListType(col.type.peek())) { const colType = col.type.peek();
if (gristTypes.isList(value) && gristTypes.isListType(colType)) {
filterValues = value.slice(1); filterValues = value.slice(1);
if (!filterValues.length) {
// If the list is empty, filter instead by an empty value for the whole list
filterValues = [colType === "ChoiceList" ? "" : null];
}
} else { } else {
if (Array.isArray(value)) { if (Array.isArray(value)) {
value = JSON.stringify(value); value = JSON.stringify(value);

View File

@ -496,6 +496,10 @@ function addCountsToMap(valueMap: Map<CellValue, IFilterCount>, rowIds: RowId[],
// If row contains a list and the column is a Choice List, treat each choice as a separate key // If row contains a list and the column is a Choice List, treat each choice as a separate key
if (isList(key) && (columnType === 'ChoiceList')) { if (isList(key) && (columnType === 'ChoiceList')) {
const list = decodeObject(key) as unknown[]; const list = decodeObject(key) as unknown[];
if (!list.length) {
// If the list is empty, add an item for the whole list, otherwise the row will be missing from filters.
addSingleCountToMap(valueMap, '', () => '', () => '', areHiddenRows);
}
for (const item of list) { for (const item of list) {
addSingleCountToMap(valueMap, item, () => item, () => item, areHiddenRows); addSingleCountToMap(valueMap, item, () => item, () => item, areHiddenRows);
} }
@ -505,6 +509,10 @@ function addCountsToMap(valueMap: Map<CellValue, IFilterCount>, rowIds: RowId[],
// If row contains a Reference List, treat each reference as a separate key // If row contains a Reference List, treat each reference as a separate key
if (isList(key) && isRefListType(columnType)) { if (isList(key) && isRefListType(columnType)) {
const refIds = decodeObject(key) as unknown[]; const refIds = decodeObject(key) as unknown[];
if (!refIds.length) {
// If the list is empty, add an item for the whole list, otherwise the row will be missing from filters.
addSingleCountToMap(valueMap, null, () => null, () => null, areHiddenRows);
}
const refLabels = labelMapFunc(rowId); const refLabels = labelMapFunc(rowId);
const displayValues = valueMapFunc(rowId); const displayValues = valueMapFunc(rowId);
refIds.forEach((id, i) => { refIds.forEach((id, i) => {

View File

@ -35,7 +35,11 @@ export function makeFilterFunc(state: FilterState,
return (val: CellValue) => { return (val: CellValue) => {
if (isList(val) && columnType && isListType(columnType)) { if (isList(val) && columnType && isListType(columnType)) {
const list = decodeObject(val) as unknown[]; const list = decodeObject(val) as unknown[];
return list.some(item => values.has(item as any) === include); if (list.length) {
return list.some(item => values.has(item as any) === include);
}
// If the list is empty, filter instead by an empty value for the whole list
val = columnType === "ChoiceList" ? "" : null;
} }
return (values.has(Array.isArray(val) ? JSON.stringify(val) : val) === include); return (values.has(Array.isArray(val) ? JSON.stringify(val) : val) === include);
}; };