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

[OF#5002] Update addressNL component with showStreetCity field #207

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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"
}
}
4 changes: 4 additions & 0 deletions src/components/ComponentConfiguration.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,11 @@ export const AddressNL: Story = {
required: false,
},
deriveAddress: false,
defaultValue: {
autoPopulated: false,
},
layout: 'singleColumn',
showStreetCity: true,
openForms: {
components: {
postcode: {
Expand Down
4 changes: 4 additions & 0 deletions src/components/ComponentPreview.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,10 @@ export const AddressNL: Story = {
},
deriveAddress: false,
layout: 'singleColumn',
showStreetCity: true,
defaultValue: {
autoPopulated: false,
},
},
},
};
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 @@

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 @@
/>
}
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 @@
<Tooltip />
<PresentationConfig />
<ColumnsLayout />
<ShowStreetCity />
<DeriveAddress />
<Hidden />
<ClearOnHide />
Expand Down Expand Up @@ -309,7 +348,7 @@
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 @@
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">'.
defaultValue: {
postcode: '',
houseNumber: '',
Expand Down
1 change: 1 addition & 0 deletions src/registry/addressNL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export default {
city: '',
streetName: '',
secretStreetCity: '',
autoPopulated: false,
},
} satisfies RegistryEntry<AddressNLComponentSchema>;
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 @@
* @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'.
} = component;

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