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 Sep 10, 2024
1 parent da23223 commit a8ed692
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"dependencies": {
"@formio/bootstrap": "3.0.0-dev.98.17ba6ea",
"@formio/choices.js": "^10.2.1",
"@formio/core": "2.1.0-dev.145.4491833",
"@formio/core": "2.1.0-dev.146.e57530c",
"@formio/text-mask-addons": "^3.8.0-formio.2",
"@formio/vanilla-text-mask": "^5.1.1-formio.1",
"abortcontroller-polyfill": "^1.7.5",
Expand Down
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 @@ -630,7 +630,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
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@
fuse.js "^6.6.2"
redux "^4.2.0"

"@formio/[email protected].145.4491833":
version "2.1.0-dev.145.4491833"
resolved "https://registry.yarnpkg.com/@formio/core/-/core-2.1.0-dev.145.4491833.tgz#f8bb24949e71d865a2ec354a3f58dc5bf56dcde6"
integrity sha512-mN9tymogT+6qWbwlVTNB3xGI+sk9sz4WYxe7BW66fuPpsUsYwxuUtSrnGbM8WInkqmj7aNZrXd5WQ2SAV93wjQ==
"@formio/[email protected].146.e57530c":
version "2.1.0-dev.146.e57530c"
resolved "https://registry.yarnpkg.com/@formio/core/-/core-2.1.0-dev.146.e57530c.tgz#def115ff5d61e4d6833c4c807c246a3ca29afe92"
integrity sha512-oRaTKy1eAFVjZclH/Alx6TrdRFNTwlnGgDt5wQ9Nz9L2lGXxuoRv+KPtpBzst9hFZat9JnkZToDOXAQGj5hkHQ==
dependencies:
"@types/json-logic-js" "^2.0.7"
browser-cookies "^1.2.0"
Expand Down

0 comments on commit a8ed692

Please sign in to comment.