(core) Use visible column formatting when converting RefList to Text

Summary: Tweaked ReferenceListFormatter and ValueConverter to sensibly convert to Text. Fixes an embarrassing bug exposed during checkin 😱

Test Plan: Add test for converting from RefList displaying formatted dates to Text

Reviewers: paulfitz

Reviewed By: paulfitz

Differential Revision: https://phab.getgrist.com/D3254
This commit is contained in:
Alex Hall 2022-02-08 20:25:50 +02:00
parent 0de0cb0f4a
commit f877f3859d
2 changed files with 9 additions and 6 deletions

View File

@ -21,6 +21,8 @@ import {CellValue, GristObjCode} from 'app/plugin/GristData';
* is a list it only converts nicely if the list contains exactly one element. * is a list it only converts nicely if the list contains exactly one element.
*/ */
export class ValueConverter { export class ValueConverter {
private _isTargetText: boolean = ["Text", "Choice"].includes(this.parser.type);
constructor(public formatter: BaseFormatter, public parser: ValueParser) { constructor(public formatter: BaseFormatter, public parser: ValueParser) {
} }
@ -29,14 +31,15 @@ export class ValueConverter {
if (value.length === 1) { if (value.length === 1) {
// Empty list: ['L'] // Empty list: ['L']
return null; return null;
} else if (value.length === 2) { } else if (value.length > 2 || this._isTargetText) {
// List with multiple values, or the target type is text.
// Since we're converting to just one value,
// format the whole thing as text, which is an error for most types.
return this.formatter.formatAny(value);
} else {
// Singleton list: ['L', value] // Singleton list: ['L', value]
// Convert just that one value. // Convert just that one value.
value = value[1]; value = value[1];
} else {
// List with multiple values. Since we're converting to just one value,
// format the whole thing as text, which is an error for most types.
return this.formatter.formatAny(value);
} }
} }
return this.convertInner(value); return this.convertInner(value);

View File

@ -250,7 +250,7 @@ class ReferenceListFormatter extends ReferenceFormatter {
// Part of this repeats the logic in BaseFormatter.formatAny which is overridden in ReferenceFormatter // Part of this repeats the logic in BaseFormatter.formatAny which is overridden in ReferenceFormatter
// It also ensures that complex lists (e.g. if this RefList is displaying a ChoiceList) // It also ensures that complex lists (e.g. if this RefList is displaying a ChoiceList)
// are formatted as JSON instead of CSV. // are formatted as JSON instead of CSV.
if (!isList(value) || hasNestedObjects(value)) { if (!isList(value) || hasNestedObjects(decodeObject(value) as CellValue[])) {
return formatUnknown(value); return formatUnknown(value);
} }
// In the most common case, lists of simple objects like strings or dates // In the most common case, lists of simple objects like strings or dates