Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sitecore-jss-forms] Fix for multi-valued fields with pre-filled form data #677

Merged
merged 2 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data/routes/release-notes/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ There are [migration instructions](/upgrade-guides/18.0) from JSS 16-based appli
* [React-Native sample] Styleguide-ComponentParams fix incorrect `params` prop types in connected mode.
* [React-Native sample] Fix connected tunnel mode for secure (https) Sitecore endpoints.
* [PR #638](https://github.com/Sitecore/jss/pull/638) [sitecore-jss-nextjs] [samples/nextjs] Fix issue with `getStaticPaths`only pre-rendering the first 10 pages.
* [PR #677](https://github.com/Sitecore/jss/pull/677) [sitecore-jss-forms] Fix issue where pre-filled (default) form data isn't removed for multi-valued fields when user de-selects values.

## Sitecore JSS 16.0 for Sitecore 10.1

Expand Down
57 changes: 56 additions & 1 deletion packages/sitecore-jss-forms/src/JssFormData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ describe('JssFormData', () => {
]);
});

it('should merge overwriting existing values', () => {
it('should remove key', () => {
const formData = new JssFormData();

formData.append('xxx', 'val-xxx');
formData.append('xxx', 'val-xxx');
formData.append('yyy', 'val-yyy');

formData.remove('xxx');

expect(formData.get()).to.deep.equal([{ key: 'yyy', value: 'val-yyy' }]);
});

it('should merge overwriting existing empty data', () => {
const x1formData = new JssFormData();
const x1 = {
a1: 'a1-val',
Expand Down Expand Up @@ -63,6 +75,49 @@ describe('JssFormData', () => {
]);
});

it('should merge overwriting existing pre-filled data', () => {
const x1formData = new JssFormData();
x1formData.append('a1', 'a1-val1');
x1formData.append('a2', 'a2-val1');
const x1 = {
a1: 'a1-val2',
a2: 'a2-val2',
};

x1formData.mergeOverwritingExisting(x1);

expect(x1formData.get()).to.deep.equal([
{ key: 'a1', value: 'a1-val2' },
{ key: 'a2', value: 'a2-val2' },
]);

const x2formData = new JssFormData();
x2formData.append('a1', 'a1-val1');
x2formData.append('a3', 'a3-val1');
x2formData.append('a5', 'a5-val1');
x2formData.append('a5', 'a5-val2');

const x2 = {
a1: 'a1-val2',
a2: 'a2-val1',
a3: ['a3-val2', 'a3-val3'],
a4: ['a4-val1', 'a4-val2', 'a4-val3'],
a5: [],
};

x2formData.mergeOverwritingExisting(x2);

expect(x2formData.get()).to.deep.equal([
{ key: 'a1', value: 'a1-val2' },
{ key: 'a2', value: 'a2-val1' },
{ key: 'a3', value: 'a3-val2' },
{ key: 'a3', value: 'a3-val3' },
{ key: 'a4', value: 'a4-val1' },
{ key: 'a4', value: 'a4-val2' },
{ key: 'a4', value: 'a4-val3' },
]);
});

it('should convert data to url encoded form data', () => {
const x1formData = new JssFormData();

Expand Down
29 changes: 21 additions & 8 deletions packages/sitecore-jss-forms/src/JssFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ export class JssFormData {
* @param {string | File} value
*/
public set(key: string, value: string | File) {
this.data = this.data.filter((entry) => entry.key !== key);
this.remove(key);
this.append(key, value);
}

/**
* Removes any values for a given key from the form data.
* @param {string} key
*/
public remove(key: string) {
this.data = this.data.filter((entry) => entry.key !== key);
}

/**
* Merges form data from a client-side state store (i.e. the user-specified values), overwriting any existing values for the keys
* @param {Object} values
Expand All @@ -38,13 +46,18 @@ export class JssFormData {
// we want to _set_ the first one to override anything existing,
// but _append_ anything after that to avoid overwriting our own values
if (Array.isArray(value)) {
value.forEach((v: string | File, index: number) => {
if (index === 0) {
this.set(key, v);
} else {
this.append(key, v);
}
});
if (value.length === 0) {
// if empty array, ensure any pre-filled values are cleared (i.e. user de-selected these)
this.remove(key);
} else {
value.forEach((v: string | File, index: number) => {
if (index === 0) {
this.set(key, v);
} else {
this.append(key, v);
}
});
}
} else {
this.set(key, value.toString());
}
Expand Down