Skip to content

Commit c9431f3

Browse files
FIO-7588: fixed string value for Survey and Select (#5422)
* FIO-7588: fixed string value for Survey and Select * fixed render tests
1 parent 9975dae commit c9431f3

File tree

7 files changed

+36
-12
lines changed

7 files changed

+36
-12
lines changed

src/components/select/Select.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Formio } from '../../Formio';
33
import ListComponent from '../_classes/list/ListComponent';
44
import Input from '../_classes/input/Input';
55
import Form from '../../Form';
6-
import { getRandomComponentId, boolValue, isPromise, componentValueTypes, getComponentSavedTypes } from '../../utils/utils';
6+
import { getRandomComponentId, boolValue, isPromise, componentValueTypes, getComponentSavedTypes, unescapeHTML } from '../../utils/utils';
77
import Choices from '../../utils/ChoicesWrapper';
88

99
export default class SelectComponent extends ListComponent {
@@ -1221,10 +1221,10 @@ export default class SelectComponent extends ListComponent {
12211221
return added;
12221222
}
12231223

1224-
getValueAsString(data) {
1224+
getValueAsString(data, options) {
12251225
return (this.component.multiple && Array.isArray(data))
1226-
? data.map(this.asString.bind(this)).join(', ')
1227-
: this.asString(data);
1226+
? data.map((v) => this.asString(v, options)).join(', ')
1227+
: this.asString(data, options);
12281228
}
12291229

12301230
getValue() {
@@ -1636,7 +1636,7 @@ export default class SelectComponent extends ListComponent {
16361636
);
16371637
}
16381638

1639-
asString(value) {
1639+
asString(value, options = {}) {
16401640
value = value ?? this.getValue();
16411641
//need to convert values to strings to be able to compare values with available options that are strings
16421642
const convertToString = (data, valueProperty) => {
@@ -1697,13 +1697,20 @@ export default class SelectComponent extends ListComponent {
16971697
return value;
16981698
}
16991699

1700+
const getTemplateValue = (v) => {
1701+
const itemTemplate = this.itemTemplate(v);
1702+
return options.csv && itemTemplate
1703+
? unescapeHTML(itemTemplate)
1704+
: itemTemplate;
1705+
};
1706+
17001707
if (Array.isArray(value)) {
17011708
const items = [];
1702-
value.forEach(item => items.push(this.itemTemplate(item)));
1709+
value.forEach(item => items.push(getTemplateValue(item)));
17031710
if (this.component.dataSrc === 'resource' && items.length > 0 ) {
17041711
return items.join(', ');
17051712
}
1706-
else if ( items.length > 0) {
1713+
else if (items.length > 0) {
17071714
return items.join('<br />');
17081715
}
17091716
else {
@@ -1716,7 +1723,7 @@ export default class SelectComponent extends ListComponent {
17161723
}
17171724

17181725
return !_.isNil(value)
1719-
? this.itemTemplate(value)
1726+
? getTemplateValue(value)
17201727
: '-';
17211728
}
17221729

src/components/select/Select.unit.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ describe('Select Component', () => {
6060
});
6161
});
6262

63+
it('Should return plain text when csv option is provided', () => {
64+
return Harness.testCreate(SelectComponent, comp1).then((component) => {
65+
assert.equal(component.getView('red', { csv:true }), 'Red');
66+
});
67+
});
68+
6369
it('should correctly determine storage type when dataType is auto', function(done) {
6470
Harness.testCreate(SelectComponent, comp4).then((component) => {
6571
const value = component.normalizeSingleValue('true');

src/components/survey/Survey.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ export default class SurveyComponent extends Field {
179179
return result;
180180
}
181181

182+
if (_.isPlainObject(value)) {
183+
const { values = [], questions = [] } = this.component;
184+
return _.isEmpty(value)
185+
? ''
186+
: _.map(value,(v, q) => {
187+
const valueLabel = _.get(_.find(values, val => _.isEqual(val.value, v)), 'label', v);
188+
const questionLabel = _.get(_.find(questions, quest => _.isEqual(quest.value, q)), 'label', q);
189+
return `${questionLabel}: ${valueLabel}`;
190+
}).join('; ');
191+
}
192+
182193
return super.getValueAsString(value, options);
183194
}
184195
}

test/forms/helpers/testBasicComponentSettings/values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const stringValues = {
5757
day: '01/05/2005',
5858
time: '04:15',
5959
currency: '$30,000.00',
60-
survey: '{"question1":"yes","question2":"no"}',
60+
survey: 'Question 1: yes; Question 2: no',
6161
numberColumn: '1111',
6262
textFieldColumn: 'value',
6363
numberFieldset: '222222',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"one":"a","two":"b"}
1+
one: a; two: b
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"one":"b","two":"a"}
1+
one: b; two: a

test/updateRenders.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const forms = require('./formtest');
1414
Components.setComponents(AllComponents);
1515

1616
const dir = './test/renders';
17-
const componentDir = './lib/components';
17+
const componentDir = './lib/cjs/components';
1818
if (!fs.existsSync(dir)) {
1919
fs.mkdirSync(dir);
2020
}

0 commit comments

Comments
 (0)