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

Fix support for address autofill in editgrid #574

Merged
merged 2 commits into from
Oct 23, 2023
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
2 changes: 1 addition & 1 deletion src/formio/components/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TextField extends Formio.Components.components.textfield {

fieldLogic(data, row) {
const changed = super.fieldLogic(data, row);
this.handleSettingLocationData(data);
this.handleSettingLocationData(row);
return changed;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/formio/components/TextField.mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {rest} from 'msw';

import {BASE_URL} from 'api-mocks';

export const mockAddressAutoCompleteGet = (street = 'Keizersgracht', city = 'Amsterdam') =>
rest.get(`${BASE_URL}location/get-street-name-and-city`, (req, res, ctx) => {
return res(ctx.json({streetName: street, city}));

Check warning on line 7 in src/formio/components/TextField.mocks.js

View check run for this annotation

Codecov / codecov/patch

src/formio/components/TextField.mocks.js#L6-L7

Added lines #L6 - L7 were not covered by tests
});
136 changes: 133 additions & 3 deletions src/formio/components/TextField.stories.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {withUtrechtDocument} from 'story-utils/decorators';
import {expect} from '@storybook/jest';
import {userEvent, waitFor, within} from '@storybook/testing-library';

import {SingleFormioComponent} from './story-util';
import {ConfigDecorator, withUtrechtDocument} from 'story-utils/decorators';
import {sleep} from 'utils';

import {mockAddressAutoCompleteGet} from './TextField.mocks';
import {MultipleFormioComponents, SingleFormioComponent} from './story-util';

export default {
title: 'Form.io components / Vanilla / TextField',
decorators: [withUtrechtDocument],
decorators: [withUtrechtDocument, ConfigDecorator],
args: {
type: 'textfield',
extraComponentProperties: {},
Expand All @@ -22,6 +27,7 @@ export default {
},
parameters: {
controls: {sort: 'requiredFirst'},
msw: {handlers: [mockAddressAutoCompleteGet('Keizersgracht', 'Amsterdam')]},
},
};

Expand All @@ -32,3 +38,127 @@ export const TextField = {
label: 'Onderwerp',
},
};

export const TextFieldsWithLocation = {
render: MultipleFormioComponents,
args: {
components: [
{
type: 'textfield',
key: 'postcode',
label: 'Postcode',
inputMask: '9999 AA',
},
{
type: 'textfield',
key: 'houseNumber',
label: 'Number',
},
{
type: 'textfield',
key: 'streetname',
label: 'Street',
deriveStreetName: true,
derivePostcode: 'postcode',
deriveHouseNumber: 'houseNumber',
},
{
type: 'textfield',
key: 'streetname',
label: 'City',
deriveCity: true,
derivePostcode: 'postcode',
deriveHouseNumber: 'houseNumber',
},
],
evalContext: {},
},

play: async ({canvasElement, step}) => {
const canvas = within(canvasElement);
// formio... :thisisfine:
await sleep(100);

await step('Fill out postcode and number', async () => {
await userEvent.type(canvas.getByLabelText('Postcode'), '1015 CJ');
await userEvent.type(canvas.getByLabelText('Number'), '117');
});

await step('Check that street and city are autofilled', async () => {
await waitFor(async () => {
expect(canvas.getByLabelText('Street')).toHaveDisplayValue('Keizersgracht');
});
expect(canvas.getByLabelText('City')).toHaveDisplayValue('Amsterdam');
});
},
};

export const TextFieldsWithLocationInEditGrid = {
render: SingleFormioComponent,
args: {
type: 'editgrid',
key: 'addresses',
label: 'Addresses',
groupLabel: '',
maxLength: null,
extraComponentProperties: {
hideLabel: false,
components: [
{
type: 'textfield',
key: 'postcode',
label: 'Postcode',
inputMask: '9999 AA',
},
{
type: 'textfield',
key: 'houseNumber',
label: 'Number',
},
{
type: 'textfield',
key: 'streetname',
label: 'Street',
deriveStreetName: true,
derivePostcode: 'postcode',
deriveHouseNumber: 'houseNumber',
},
{
type: 'textfield',
key: 'streetname',
label: 'City',
deriveCity: true,
derivePostcode: 'postcode',
deriveHouseNumber: 'houseNumber',
},
],
inlineEdit: false,
description: '',
disableAddingRemovingRows: false,
addAnother: 'Add another',
saveRow: 'Confirm',
removeRow: 'Delete',
},
evalContext: {},
},

play: async ({canvasElement, step}) => {
const canvas = within(canvasElement);
// formio... :thisisfine:
await sleep(100);

await userEvent.click(canvas.getByRole('button', {name: 'Add another'}));

await step('Fill out postcode and number', async () => {
await userEvent.type(canvas.getByLabelText('Postcode'), '1015 CJ');
await userEvent.type(canvas.getByLabelText('Number'), '117');
});

await step('Check that street and city are autofilled', async () => {
await waitFor(async () => {
expect(canvas.getByLabelText('Street')).toHaveDisplayValue('Keizersgracht');
});
expect(canvas.getByLabelText('City')).toHaveDisplayValue('Amsterdam');
});
},
};