(core) Preserve configured choices when converting between Choice and ChoiceList types.

Summary:
For conversions between Choice and ChoiceList, it makes more sense to preserve
the list of choices than to re-parse it from data.

Reported by Anais. Creating Choices from parsing ChoiceList cell values was
particularly poor, resulting in choices like "L,Foo,Bar".

Test Plan: Added a test case

Reviewers: paulfitz

Reviewed By: paulfitz

Differential Revision: https://phab.getgrist.com/D2819
This commit is contained in:
Dmitry S 2021-05-18 01:01:53 -04:00
parent 5f182841b9
commit 8c2f0307e5

View File

@ -80,8 +80,13 @@ export async function prepTransformColInfo(docModel: DocModel, origCol: ColumnRe
formula: "", // Will be filled in at the end. formula: "", // Will be filled in at the end.
}; };
const prevOptions = origCol.widgetOptionsJson.peek() || {};
switch (toType) { switch (toType) {
case 'Choice': { case 'Choice': {
if (Array.isArray(prevOptions.choices)) {
// Use previous choices if they are set, e.g. if converting from ChoiceList
widgetOptions = {choices: prevOptions.choices};
} else {
// Set suggested choices. Limit to 100, since too many choices is more likely to cause // Set suggested choices. Limit to 100, since too many choices is more likely to cause
// trouble than desired behavior. For many choices, recommend using a Ref to helper table. // trouble than desired behavior. For many choices, recommend using a Ref to helper table.
const columnData = tableData.getDistinctValues(origCol.colId(), 100); const columnData = tableData.getDistinctValues(origCol.colId(), 100);
@ -89,9 +94,14 @@ export async function prepTransformColInfo(docModel: DocModel, origCol: ColumnRe
columnData.delete(""); columnData.delete("");
widgetOptions = {choices: Array.from(columnData, String)}; widgetOptions = {choices: Array.from(columnData, String)};
} }
}
break; break;
} }
case 'ChoiceList': { case 'ChoiceList': {
if (Array.isArray(prevOptions.choices)) {
// Use previous choices if they are set, e.g. if converting from ChoiceList
widgetOptions = {choices: prevOptions.choices};
} else {
// Set suggested choices. This happens before the conversion to ChoiceList, so we do some // Set suggested choices. This happens before the conversion to ChoiceList, so we do some
// light guessing for likely choices to suggest. // light guessing for likely choices to suggest.
const choices = new Set<string>(); const choices = new Set<string>();
@ -105,6 +115,7 @@ export async function prepTransformColInfo(docModel: DocModel, origCol: ColumnRe
} }
choices.delete(""); choices.delete("");
widgetOptions = {choices: Array.from(choices)}; widgetOptions = {choices: Array.from(choices)};
}
break; break;
} }
case 'Ref': { case 'Ref': {
@ -170,7 +181,7 @@ export function getDefaultFormula(
`$${colId}`; `$${colId}`;
if (origCol.type.peek() === 'ChoiceList') { if (origCol.type.peek() === 'ChoiceList') {
origValFormula = `grist.ChoiceList.toString($${colId})` origValFormula = `grist.ChoiceList.toString($${colId})`;
} }
const toTypePure: string = gristTypes.extractTypeFromColType(newType); const toTypePure: string = gristTypes.extractTypeFromColType(newType);