Skip to content

Commit

Permalink
FIO-7195: Fixes an issue where Select, Radio and SelectBoxes componen…
Browse files Browse the repository at this point in the history
…ts with URL DataSource show values instead of labels in modal preview
  • Loading branch information
alexandraRamanenka committed May 7, 2024
1 parent eeaff43 commit f89b57e
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 5 deletions.
44 changes: 43 additions & 1 deletion src/Webform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
formWithCollapsedPanel,
formWithCustomFormatDate,
tooltipActivateCheckbox,
formWithObjectValueSelect
formWithObjectValueSelect,
} from '../test/formtest';
import UpdateErrorClassesWidgets from '../test/forms/updateErrorClasses-widgets';
import nestedModalWizard from '../test/forms/nestedModalWizard';
Expand Down Expand Up @@ -78,7 +78,9 @@ import formWithDeeplyNestedConditionalComps from '../test/forms/formWithDeeplyNe
import formWithValidation from '../test/forms/formWithValidation';
import formWithNotAllowedTags from '../test/forms/formWithNotAllowedTags';
import formWithValidateWhenHidden from '../test/forms/formWithValidateWhenHidden';
import formWithSelectRadioUrlDataSource from '../test/forms/selectRadioUrlDataSource';
const SpySanitize = sinon.spy(FormioUtils, 'sanitize');

global.requestAnimationFrame = (cb) => cb();
global.cancelAnimationFrame = () => {};

Expand Down Expand Up @@ -4812,6 +4814,46 @@ describe('Webform tests', function() {
});
});

it('Should render labels for Select, Radio and selectBoxes components when Data Source is URL', (done) => {
const element = document.createElement('div');
const form = new Webform(element);
const originalMakeRequest = Formio.makeRequest;

Formio.makeRequest = function() {
return new Promise(resolve => {
const values = [
{ name : 'Alabama', abbreviation : 'AL' },
{ name : 'Alaska', abbreviation: 'AK' },
{ name: 'American Samoa', abbreviation: 'AS' }
];
resolve(values);
});
};

form.setForm(formWithSelectRadioUrlDataSource).then(() => {
const selectBoxes = form.getComponent('selectBoxes');
const select = form.getComponent('select');
const radio = form.getComponent('radio');

selectBoxes.setValue({ AL: false, AK: true, AS: true }, { modified: true });
select.setValue('AL', { modified: true });
radio.setValue('AL', { modified: true });

setTimeout(() => {
const previewSelectBoxes = selectBoxes.element.querySelector('[ref="openModal"]');
const previewSelect = select.element.querySelector('[ref="openModal"]');
const previewRadio = radio.element.querySelector('[ref="openModal"]');

Check warning on line 4845 in src/Webform.unit.js

View workflow job for this annotation

GitHub Actions / setup

'previewRadio' is assigned a value but never used
assert.equal(previewSelectBoxes.innerHTML, '\n <span>Alaska</span>, <span>American Samoa</span>\n');
assert.equal(previewSelect.innerHTML, '\n <span>Alabama</span>\n');
assert.equal(previewSelectBoxes.innerHTML, '\n <span>Alabama</span>\n');

Formio.makeRequest = originalMakeRequest;
done();
}, 300);
})
.catch((err) => done(err));
});

for (const formTest of FormTests) {
const useDoneInsteadOfPromise = formTest.useDone;

Expand Down
9 changes: 6 additions & 3 deletions src/components/radio/Radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,21 @@ export default class RadioComponent extends ListComponent {
return false;
}

getValueAsString(value) {
getValueAsString(value, options = {}) {
if (_.isObject(value)) {
value = JSON.stringify(value);
}
else if (!_.isString(value)) {
value = _.toString(value);
}
if (this.component.dataSrc !== 'values') {

const isModalPreviewWithUrlDataSource = options.modalPreview && this.component.dataSrc === 'url';
if (this.component.dataSrc !== 'values' && !isModalPreviewWithUrlDataSource) {
return value;
}

const option = _.find(this.component.values, (v) => v.value === value);
const values = isModalPreviewWithUrlDataSource ? this.loadedOptions : this.component.values;
const option = _.find(values, (v) => v.value === value);

if (!value) {
return _.get(option, 'label', '');
Expand Down
5 changes: 5 additions & 0 deletions src/components/select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,11 @@ export default class SelectComponent extends ListComponent {

asString(value, options = {}) {
value = value ?? this.getValue();

if (options.modalPreview && this.selectData) {
const { label } = this.selectValueAndLabel(value);
return label;
}
//need to convert values to strings to be able to compare values with available options that are strings
const convertToString = (data, valueProperty) => {
if (valueProperty) {
Expand Down
5 changes: 4 additions & 1 deletion src/components/selectboxes/SelectBoxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,15 @@ export default class SelectBoxesComponent extends RadioComponent {
return changed;
}

getValueAsString(value) {
getValueAsString(value, options = {}) {
if (!value) {
return '';
}

if (this.isSelectURL) {
if (options.modalPreview && this.loadedOptions) {
return this.loadedOptions.filter((option) => value[option.value]).map((option) => option.label).join(', ');
}
return _(value).pickBy((val) => val).keys().join(', ');
}
return _(this.component.values || [])
Expand Down
95 changes: 95 additions & 0 deletions test/forms/selectRadioUrlDataSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
export default {
type: 'form',
display: 'form',
components: [
{
label: 'Select Boxes - URL',
optionsLabelPosition: 'right',
tableView: true,
modalEdit: true,
dataSrc: 'url',
values: [
{
label: '',
value: '',
shortcut: '',
},
],
valueProperty: 'abbreviation',
key: 'selectBoxes',
type: 'selectboxes',
data: {
url: 'https://gists.rawgit.com/mshafrir/2646763/raw/states_titlecase.json',
headers: [
{
key: '',
value: '',
},
],
},
template: '<span>{{ item.name }}</span>',
input: true,
inputType: 'checkbox',
},
{
label: 'Radio',
optionsLabelPosition: 'right',
inline: false,
tableView: true,
modalEdit: true,
dataSrc: 'url',
values: [
{
label: '',
value: '',
shortcut: '',
},
],
valueProperty: 'abbreviation',
key: 'radio',
type: 'radio',
data: {
url: 'https://gists.rawgit.com/mshafrir/2646763/raw/states_titlecase.json',
headers: [
{
key: '',
value: '',
},
],
},
template: '<span>{{ item.name }}</span>',
input: true,
},
{
label: 'Select',
widget: 'choicesjs',
tableView: true,
modalEdit: true,
dataSrc: 'url',
data: {
url: 'https://gists.rawgit.com/mshafrir/2646763/raw/states_titlecase.json',
headers: [
{
key: '',
value: '',
},
],
},
valueProperty: 'abbreviation',
template: '<span>{{ item.name }}</span>',
key: 'select',
type: 'select',
disableLimit: false,
noRefreshOnScroll: false,
input: true,
},
{
type: 'button',
label: 'Submit',
key: 'submit',
disableOnInvalid: true,
input: true,
tableView: false,
},
],
};

0 comments on commit f89b57e

Please sign in to comment.