Skip to content

Commit

Permalink
Fix incorrect comment label when hasOther is true
Browse files Browse the repository at this point in the history
  • Loading branch information
dk981234 committed Jan 16, 2025
1 parent 025540b commit 84e9465
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/tables/columnbuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Question, QuestionCompositeModel, QuestionCustomModel, QuestionFileModel, QuestionMatrixDropdownModel, QuestionMatrixModel, QuestionSelectBase } from "survey-core";
import { BaseColumn, CommentColumn, CompositeQuestionColumn, CustomQuestionColumn, FileColumn, ImageColumn, MatrixColumn, MatrixDropdownColumn } from "./columns";
import { BaseColumn, CommentColumn, CompositeQuestionColumn, CustomQuestionColumn, FileColumn, ImageColumn, MatrixColumn, MatrixDropdownColumn, OtherColumn } from "./columns";
import { IColumn } from "./config";
import { Table } from "./table";

Expand All @@ -19,12 +19,12 @@ export class DefaultColumnsBuilder<T extends Question = Question> implements ICo

public buildColumns(question: T, table: Table): Array<IColumn> {
const columns = this.buildColumnsCore(question, table);
if (
question.hasComment ||
(question.hasOther && (<any>question as QuestionSelectBase)["getStoreOthersAsComment"]())
) {
if(question.hasComment) {
columns.push(new CommentColumn(question, table));
}
if(question.hasOther && (question as unknown as QuestionSelectBase)["getStoreOthersAsComment"]()) {
columns.push(new OtherColumn(question as unknown as QuestionSelectBase, table));
}
return columns;
}
}
Expand Down Expand Up @@ -96,4 +96,4 @@ export class CompositeColumnsBuilder extends DefaultColumnsBuilder<QuestionCompo
return new CompositeQuestionColumn(question, table);
}
}
ColumnsBuilderFactory.Instance.registerBuilderColumn("composite", new CompositeColumnsBuilder());
ColumnsBuilderFactory.Instance.registerBuilderColumn("composite", new CompositeColumnsBuilder());
15 changes: 9 additions & 6 deletions src/tables/columns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Base, ItemValue, MatrixRowModel, Question, QuestionCompositeModel, QuestionCustomModel, QuestionFileModel, QuestionMatrixDropdownModel, QuestionMatrixModel, settings } from "survey-core";
import { ItemValue, MatrixRowModel, Question, QuestionCompositeModel, QuestionCustomModel, QuestionFileModel, QuestionMatrixDropdownModel, QuestionMatrixModel, QuestionSelectBase, settings } from "survey-core";
import { createImagesContainer, createLinksContainer } from "../utils";
import { ICellData, IColumn, ColumnDataType, QuestionLocation, IColumnData } from "./config";
import { ITableOptions, Table } from "./table";
Expand Down Expand Up @@ -111,19 +111,22 @@ export class DefaultColumn extends BaseColumn {
}
}

export class CommentColumn extends BaseColumn {
export class CommentColumn<T extends Question = Question> extends BaseColumn<T> {
protected getName(): string {
return `${this.question.name}${settings.commentPrefix}`;
}
protected getDisplayName(): string {
return this.question.hasOther
? (<any>this.question).otherText
: this.question.commentText;
return this.question.commentText;
}
protected getDisplayValue(data: any, table: Table, options: ITableOptions) {
protected getDisplayValue(data: any, table: Table, options: ITableOptions): string {
return this.question.comment;
}
}
export class OtherColumn extends CommentColumn<QuestionSelectBase> {
protected getDisplayName(): string {
return this.question.otherText;
}
}

export class MatrixColumn extends BaseColumn<QuestionMatrixModel> {
private valueName: string;
Expand Down
26 changes: 24 additions & 2 deletions tests/tables/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,29 @@ test("check columns for question with other and storeOthersAsComment: false", ()
expect((<any>table).columns.length).toEqual(1);
});

test("check columns for question with other and comment", () => {
const json = {
questions: [
{
type: "radiogroup",
title: "Radio",
name: "radio",
choices: [{ value: "choiceValue", text: "choiceText" }],
otherText: "Other (Describe Radio)",
hasOther: true,
showCommentArea: true,
commentText: "Comment-Test"
},
],
};
const survey = new SurveyModel(json);
const table = new TableTest(survey, [], {}, []);
expect((<any>table).columns[0].name).toEqual("radio");
expect(<any>table.columns[1].name).toEqual("radio-Comment");
expect(<any>table.columns[1].displayName).toEqual("Comment-Test");
expect(<any>table.columns[1].dataType).toEqual(ColumnDataType.Text);
});

test("check data for question with comment", () => {
const json = {
questions: [
Expand Down Expand Up @@ -909,5 +932,4 @@ test("check set state with columns which not inside survey", () => {
expect(table.columns[0].getCellData(table, data).question).toBe(survey.getAllQuestions()[0]);
expect(table.columns[1].getCellData(table, data).displayValue).toEqual("text2");
expect(table.columns[1].getCellData(table, data).question).toBe(undefined);
});

});

0 comments on commit 84e9465

Please sign in to comment.