Skip to content

Commit

Permalink
FIO-8912 update to model types (#5800)
Browse files Browse the repository at this point in the history
* update normalization to utilize component model types if applicable; fix tests

* update to core dependency

* add tests

* minor update to tests

* update deps, fix tests
  • Loading branch information
brendanbond authored and lane-formio committed Sep 10, 2024
1 parent a860fb5 commit bdd7fdc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/components/_classes/multivalue/Multivalue.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import Field from '../field/Field';
import _ from 'lodash';
import { Utils } from '@formio/core';

export default class Multivalue extends Field {
/**
* Normalize values coming into updateValue.
* @param {*} value - The value to normalize before setting.
* @param {Object} flags - Flags to use when normalizing the value.
* @param {object} flags - Flags to use when normalizing the value.
* @param {*} emptyValue - The empty value for the field.
* @returns {*} - The normalized value.
*/
normalizeValue(value, flags = {}, emptyValue = this.emptyValue) {
const underlyingValueShouldBeArray = Utils.getModelType(this.component) === 'array' || this.component.storeas === 'array' || Array.isArray(emptyValue);
if (this.component.multiple) {
if (Array.isArray(value)) {
if (underlyingValueShouldBeArray) {
if (value.length === 0 || !Array.isArray(value[0])) {
return [value];
}
}
if (value.length === 0) {
return [emptyValue];
}

if (this.component.storeas === 'array') {
return super.normalizeValue([value], flags);
}

return super.normalizeValue(value, flags);
} else {
return super.normalizeValue(value == null ? [emptyValue] : [value], flags);
}
} else {
if (Array.isArray(value) && !Array.isArray(emptyValue)) {
if (Array.isArray(value) && !underlyingValueShouldBeArray) {
if (this.component.storeas === 'string') {
return super.normalizeValue(value.join(this.delimiter || ''), flags);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/day/Day.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ export default class DayComponent extends Field {

isPartialDay(value) {
if (!value) {
return false;
return true;
}
const [DAY, MONTH, YEAR] = this.component.dayFirst ? [0, 1, 2] : [1, 0, 2];
const values = value.split('/');
Expand Down
36 changes: 36 additions & 0 deletions src/components/file/File.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,42 @@ describe('File Component', () => {
}).catch(done);
});

it('Should not incorrectly validate a non-multiple File component', () => {
comp1.multiple = false;
return Harness.testCreate(FileComponent, comp1).then((component) => {
assert(component.checkValidity(), 'Item should be valid');
component.setValue([
{
storage: 'base64',
name: 'IMG_5235-ce0abe18-5d3e-4ab4-84ca-b3e06684bc86.jpg',
url: 'data:image/jpg;base64,AAAAIGZ0eXBoZWljAAAAAG1pZjF',
size: 1159732,
type: 'image/jpeg',
originalName: 'IMG_5235.jpg',
}
]);
assert(component.checkValidity(), 'Item should be valid');
});
})

it('Should not incorrectly validate a multiple File Component', () => {
comp1.multiple = true;
return Harness.testCreate(FileComponent, comp1).then((component) => {
assert(component.checkValidity(), 'Item should be valid');
component.setValue([
{
storage: 'base64',
name: 'IMG_5235-ce0abe18-5d3e-4ab4-84ca-b3e06684bc86.jpg',
url: 'data:image/jpg;base64,AAAAIGZ0eXBoZWljAAAAAG1pZjF',
size: 1159732,
type: 'image/jpeg',
originalName: 'IMG_5235.jpg',
}
]);
assert(component.checkValidity(), 'Item should be valid');
});
});

it('Should abort the correct file when user clicks the file remove button', (done) => {
const cmp = _.cloneDeep(comp1);
const abortedFiles = [];
Expand Down
20 changes: 20 additions & 0 deletions src/components/hidden/Hidden.unit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@


import Harness from '../../../test/harness';
import HiddenComponent from './Hidden';
import assert from 'power-assert';

import {
comp1
Expand All @@ -9,4 +12,21 @@ describe('Hidden Component', () => {
it('Should build a hidden component', () => {
return Harness.testCreate(HiddenComponent, comp1);
});

it('Should not incorrectly validate multiple when hidden component has an array value', () => {
return Harness.testCreate(HiddenComponent, comp1).then((component) => {
assert(component.checkValidity(), 'Item should be valid');
component.setValue([
{
key: 'foo',
value: 'bar'
},
{
key: 'hello',
value: 'world'
}
]);
assert(component.checkValidity(), 'Item should be valid after setting value');
});
});
});
2 changes: 0 additions & 2 deletions src/utils/formUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
getModelType,
getComponentAbsolutePath,
getComponentPath,
isComponentModelType,
isComponentNestedDataType,
componentPath,
componentChildPath,
Expand Down Expand Up @@ -58,7 +57,6 @@ export {
getModelType,
getComponentAbsolutePath,
getComponentPath,
isComponentModelType,
isComponentNestedDataType,
componentPath,
componentChildPath,
Expand Down

0 comments on commit bdd7fdc

Please sign in to comment.