Skip to content

Commit

Permalink
[open-formulieren/open-forms#5002] Added showStreetCity to the formbu…
Browse files Browse the repository at this point in the history
…ilder
  • Loading branch information
vaszig committed Jan 24, 2025
1 parent 10c1985 commit 87feb2e
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 5 deletions.
10 changes: 10 additions & 0 deletions i18n/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,11 @@
"description": "Tooltip for 'maxNumberOfFiles' builder field",
"originalDefault": "The maximum number of files that can be uploaded."
},
"Kv4hZl": {
"defaultMessage": "When enabled, the street name and city are shown in the form. By default they are shown.",
"description": "Tooltip for 'ShowStreetCity' builder field",
"originalDefault": "When enabled, the street name and city are shown in the form. By default they are shown."
},
"KwatvH": {
"defaultMessage": "The number of rows for this text area.",
"description": "Tooltip for 'NumberOfRows' builder field",
Expand Down Expand Up @@ -1598,5 +1603,10 @@
"defaultMessage": "{hasDescription, select, true {Edit description} other {Add description}}",
"description": "Link to expand/show the option description textarea.",
"originalDefault": "{hasDescription, select, true {Edit description} other {Add description}}"
},
"zbPFLe": {
"defaultMessage": "Show street name and city",
"description": "Label for 'ShowStreetCity' builder field",
"originalDefault": "Show street name and city"
}
}
10 changes: 10 additions & 0 deletions i18n/messages/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@
"description": "Tooltip for 'maxNumberOfFiles' builder field",
"originalDefault": "The maximum number of files that can be uploaded."
},
"Kv4hZl": {
"defaultMessage": "When enabled, the street name and city are shown in the form. By default they are shown.",
"description": "Tooltip for 'ShowStreetCity' builder field",
"originalDefault": "When enabled, the street name and city are shown in the form. By default they are shown."
},
"KwatvH": {
"defaultMessage": "Het aantal regels voor dit tekstvlak.",
"description": "Tooltip for 'NumberOfRows' builder field",
Expand Down Expand Up @@ -1618,5 +1623,10 @@
"defaultMessage": "{hasDescription, select, true {Bewerk omschrijving} other {Voeg omschrijving toe}}",
"description": "Link to expand/show the option description textarea.",
"originalDefault": "{hasDescription, select, true {Edit description} other {Add description}}"
},
"zbPFLe": {
"defaultMessage": "Show street name and city",
"description": "Label for 'ShowStreetCity' builder field",
"originalDefault": "Show street name and city"
}
}
1 change: 1 addition & 0 deletions src/components/ComponentConfiguration.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2361,6 +2361,7 @@ export const AddressNL: Story = {
},
deriveAddress: false,
layout: 'singleColumn',
showStreetCity: true,
openForms: {
components: {
postcode: {
Expand Down
1 change: 1 addition & 0 deletions src/components/ComponentPreview.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ export const AddressNL: Story = {
},
deriveAddress: false,
layout: 'singleColumn',
showStreetCity: true,
},
},
};
Expand Down
28 changes: 28 additions & 0 deletions src/registry/addressNL/edit.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,31 @@ export const CityValidationTabWithConfiguration: Story = {
});
},
};

export const ShowStreetCityWithDeriveAddress: Story = {
name: 'AddressNL with show and derive address',
args: {
component: {
id: 'wekruya',
type: 'addressNL',
key: 'address',
label: 'An address field',
showStreetCity: false,
},
},
play: async ({canvasElement}) => {
const canvas = within(canvasElement);

const showStreetCityCheckbox = await canvas.findByLabelText('Show street name and city');
expect(showStreetCityCheckbox).not.toBeChecked();

const deriveAddressCheckbox = await canvas.findByLabelText('Derive address');
await userEvent.click(deriveAddressCheckbox);
expect(showStreetCityCheckbox).toBeChecked();
expect(showStreetCityCheckbox).toBeDisabled();

await userEvent.click(deriveAddressCheckbox);
expect(showStreetCityCheckbox).toBeChecked();
expect(showStreetCityCheckbox).not.toBeDisabled();
},
};
42 changes: 41 additions & 1 deletion src/registry/addressNL/edit.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {AddressNLComponentSchema} from '@open-formulieren/types';
import {TextField} from 'components/formio';
import {useFormikContext} from 'formik';
import {ChangeEvent} from 'react';
import {useContext} from 'react';
import {FormattedMessage, defineMessage, useIntl} from 'react-intl';

Expand Down Expand Up @@ -103,6 +105,8 @@ export const SubcomponentValidation: React.FC<SubcomponentValidationProps> = ({

const DeriveAddress = () => {
const intl = useIntl();
const {getFieldProps, setFieldValue} = useFormikContext();
const {onChange: formikOnChange} = getFieldProps<boolean>('deriveAddress');
const tooltip = intl.formatMessage({
description: "Tooltip for 'DeriveAddress' builder field",
defaultMessage:
Expand All @@ -118,6 +122,40 @@ const DeriveAddress = () => {
/>
}
tooltip={tooltip}
onChange={(event: ChangeEvent<HTMLInputElement>) => {
const {value} = event.target;
formikOnChange(event);
if (value) {
setFieldValue('showStreetCity', value);
}
}}
/>
);
};

const ShowStreetCity = () => {
const intl = useIntl();
const {
values: {deriveAddress},
} = useFormikContext<AddressNLComponentSchema>();

const tooltip = intl.formatMessage({
description: "Tooltip for 'ShowStreetCity' builder field",
defaultMessage:
'When enabled, the street name and city are shown in the form. By default they are shown.',
});

return (
<Checkbox
name="showStreetCity"
label={
<FormattedMessage
description="Label for 'ShowStreetCity' builder field"
defaultMessage="Show street name and city"
/>
}
tooltip={tooltip}
disabled={deriveAddress}
/>
);
};
Expand Down Expand Up @@ -195,6 +233,7 @@ const EditForm: EditFormDefinition<AddressNLComponentSchema> = () => {
<Tooltip />
<PresentationConfig />
<ColumnsLayout />
<ShowStreetCity />
<DeriveAddress />
<Hidden />
<ClearOnHide />
Expand Down Expand Up @@ -309,7 +348,7 @@ const EditForm: EditFormDefinition<AddressNLComponentSchema> = () => {
React.Children and related API's legacy API - this may get removed in future
versions.
Explicitly specifying the schema and default values is therefore probbaly best, at
Explicitly specifying the schema and default values is therefore probably best, at
the cost of some repetition.
*/
EditForm.defaultValues = {
Expand All @@ -326,6 +365,7 @@ EditForm.defaultValues = {
isSensitiveData: true,
deriveAddress: false,
layout: 'doubleColumn',
showStreetCity: true,

Check failure on line 368 in src/registry/addressNL/edit.tsx

View workflow job for this annotation

GitHub Actions / Create 'production' build

Type '{ label: string; key: string; description: string; tooltip: string; showInSummary: true; showInEmail: false; showInPDF: true; hidden: false; clearOnHide: true; isSensitiveData: true; deriveAddress: false; layout: "doubleColumn"; showStreetCity: boolean; defaultValue: { postcode: string; houseNumber: string; houseLetter: string; houseNumberAddition: string; }; conditional: { show: undefined; when: string; eq: string; }; validate: { required: false; plugins: never[]; }; translatedErrors: {}; registration: { attribute: string; }; openForms: { translations: {}; components: { postcode: { validate: { pattern: string; }; translatedErrors: {}; }; city: { validate: { pattern: string; }; translatedErrors: {}; }; }; }; }' is not assignable to type 'Omit<AddressNLComponentSchema, "type" | "id">'.

Check failure on line 368 in src/registry/addressNL/edit.tsx

View workflow job for this annotation

GitHub Actions / Create 'production' build

Type '{ label: string; key: string; description: string; tooltip: string; showInSummary: true; showInEmail: false; showInPDF: true; hidden: false; clearOnHide: true; isSensitiveData: true; deriveAddress: false; layout: "doubleColumn"; showStreetCity: boolean; defaultValue: { postcode: string; houseNumber: string; houseLetter: string; houseNumberAddition: string; }; conditional: { show: undefined; when: string; eq: string; }; validate: { required: false; plugins: never[]; }; translatedErrors: {}; registration: { attribute: string; }; openForms: { translations: {}; components: { postcode: { validate: { pattern: string; }; translatedErrors: {}; }; city: { validate: { pattern: string; }; translatedErrors: {}; }; }; }; }' is not assignable to type 'Omit<AddressNLComponentSchema, "type" | "id">'.
defaultValue: {
postcode: '',
houseNumber: '',
Expand Down
19 changes: 15 additions & 4 deletions src/registry/addressNL/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ import './preview.scss';
* @open-formulieren/formio-renderer instead for a more accurate preview.
*/
const Preview: React.FC<ComponentPreviewProps<AddressNLComponentSchema>> = ({component}) => {
const {key, label, description, tooltip, validate = {}, deriveAddress, layout} = component;
const {
key,
label,
description,
tooltip,
validate = {},
deriveAddress,
layout,
showStreetCity,

Check failure on line 26 in src/registry/addressNL/preview.tsx

View workflow job for this annotation

GitHub Actions / Create 'production' build

Property 'showStreetCity' does not exist on type 'AddressNLComponentSchema'.

Check failure on line 26 in src/registry/addressNL/preview.tsx

View workflow job for this annotation

GitHub Actions / Create 'production' build

Property 'showStreetCity' does not exist on type 'AddressNLComponentSchema'.
} = component;

const {required = false} = validate;
return (
Expand Down Expand Up @@ -59,7 +68,7 @@ const Preview: React.FC<ComponentPreviewProps<AddressNLComponentSchema>> = ({com
/>
}
/>
{deriveAddress && (
{(showStreetCity || deriveAddress) && (
<>
<TextField
name={`${key}.city`}
Expand All @@ -69,7 +78,8 @@ const Preview: React.FC<ComponentPreviewProps<AddressNLComponentSchema>> = ({com
defaultMessage="City"
/>
}
disabled
disabled={deriveAddress}
required={required}
/>
<TextField
name={`${key}.streetNumber`}
Expand All @@ -79,7 +89,8 @@ const Preview: React.FC<ComponentPreviewProps<AddressNLComponentSchema>> = ({com
defaultMessage="Street name"
/>
}
disabled
disabled={deriveAddress}
required={required}
/>
</>
)}
Expand Down

0 comments on commit 87feb2e

Please sign in to comment.