This commit is contained in:
Pieter Vander Vennet 2023-08-08 20:41:18 +02:00
parent 3c08c12974
commit 5d00f3cc6a

View file

@ -58,7 +58,7 @@ export default class TagRenderingConfig {
public readonly freeform?: { public readonly freeform?: {
readonly key: string readonly key: string
readonly type: string readonly type: ValidatorType
readonly placeholder: Translation readonly placeholder: Translation
readonly addExtraTags: UploadableTag[] readonly addExtraTags: UploadableTag[]
readonly inline: boolean readonly inline: boolean
@ -133,7 +133,17 @@ export default class TagRenderingConfig {
) { ) {
throw `Freeform.addExtraTags should be a list of strings - not a single string (at ${context})` throw `Freeform.addExtraTags should be a list of strings - not a single string (at ${context})`
} }
const type = json.freeform.type ?? "string" if (
json.freeform.type &&
Validators.availableTypes.indexOf(<any>json.freeform.type) < 0
) {
throw `At ${context}: invalid type, perhaps you meant ${Utils.sortedByLevenshteinDistance(
json.freeform.key,
<any>Validators.availableTypes,
(s) => <any>s
)}`
}
const type: ValidatorType = <any>json.freeform.type ?? "string"
let placeholder: Translation = Translations.T(json.freeform.placeholder) let placeholder: Translation = Translations.T(json.freeform.placeholder)
if (placeholder === undefined) { if (placeholder === undefined) {
@ -622,7 +632,7 @@ export default class TagRenderingConfig {
* *
* @param singleSelectedMapping (Only used if multiAnswer == false): the single mapping to apply. Use (mappings.length) for the freeform * @param singleSelectedMapping (Only used if multiAnswer == false): the single mapping to apply. Use (mappings.length) for the freeform
* @param multiSelectedMapping (Only used if multiAnswer == true): all the mappings that must be applied. Set multiSelectedMapping[mappings.length] to use the freeform as well * @param multiSelectedMapping (Only used if multiAnswer == true): all the mappings that must be applied. Set multiSelectedMapping[mappings.length] to use the freeform as well
* @param currentProperties: The current properties of the object for which the question should be answered * @param currentProperties The current properties of the object for which the question should be answered
*/ */
public constructChangeSpecification( public constructChangeSpecification(
freeformValue: string | undefined, freeformValue: string | undefined,
@ -685,7 +695,8 @@ export default class TagRenderingConfig {
return undefined return undefined
} }
return and return and
} else { }
// Is at least one mapping shown in the answer? // Is at least one mapping shown in the answer?
const someMappingIsShown = this.mappings.some((m) => { const someMappingIsShown = this.mappings.some((m) => {
if (typeof m.hideInAnswer === "boolean") { if (typeof m.hideInAnswer === "boolean") {
@ -697,7 +708,9 @@ export default class TagRenderingConfig {
// If all mappings are hidden for the current tags, we can safely assume that we should use the freeform key // If all mappings are hidden for the current tags, we can safely assume that we should use the freeform key
const useFreeform = const useFreeform =
freeformValue !== undefined && freeformValue !== undefined &&
(singleSelectedMapping === this.mappings.length || !someMappingIsShown) (singleSelectedMapping === this.mappings.length ||
!someMappingIsShown ||
singleSelectedMapping === undefined)
if (useFreeform) { if (useFreeform) {
return new And([ return new And([
new Tag(this.freeform.key, freeformValue), new Tag(this.freeform.key, freeformValue),
@ -709,16 +722,17 @@ export default class TagRenderingConfig {
...(this.mappings[singleSelectedMapping].addExtraTags ?? []), ...(this.mappings[singleSelectedMapping].addExtraTags ?? []),
]) ])
} else { } else {
console.warn("TagRenderingConfig.ConstructSpecification has a weird fallback for", { console.error("TagRenderingConfig.ConstructSpecification has a weird fallback for", {
freeformValue, freeformValue,
singleSelectedMapping, singleSelectedMapping,
multiSelectedMapping, multiSelectedMapping,
currentProperties, currentProperties,
useFreeform,
}) })
return undefined return undefined
} }
} }
}
GenerateDocumentation(): BaseUIElement { GenerateDocumentation(): BaseUIElement {
let withRender: (BaseUIElement | string)[] = [] let withRender: (BaseUIElement | string)[] = []