Skip to content

Commit

Permalink
[open-formulieren/open-forms#3607] Add address component
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Dec 6, 2023
1 parent b405923 commit 9a0a950
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 0 deletions.
157 changes: 157 additions & 0 deletions src/formio/components/AddressNL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**
* A form widget to select a location on a Leaflet map.
*/
import {Formik} from 'formik';
import React from 'react';
import {createRoot} from 'react-dom/client';
import {Formio} from 'react-formio';
import {FormattedMessage, IntlProvider} from 'react-intl';

import {ConfigContext} from 'Context';
import {TextField} from 'components/forms';

const Field = Formio.Components.components.field;

export default class AddressNL extends Field {
static schema(...extend) {
return Field.schema(
{
type: 'addressNL',
label: 'Address NL',
key: 'addressNL',
},
...extend
);
}

static get builderInfo() {
return {

Check warning on line 28 in src/formio/components/AddressNL.js

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.js#L27-L28

Added lines #L27 - L28 were not covered by tests
title: 'Address NL',
icon: 'home',
weight: 500,
schema: AddressNL.schema(),
};
}

get defaultSchema() {
return AddressNL.schema();
}

get emptyValue() {
return {
postcode: '',
houseNumber: '',
houseLetter: '',
houseNumberAddition: '',
};
}

validateMultiple() {
return false;
}

render() {
return super.render(
`<div ref="element">
${this.renderTemplate('addressNL')}
</div>`
);
}

/**
* Defer to React to actually render things - this keeps components DRY.
* @param {[type]} element [description]
* @return {[type]} [description]
*/
attach(element) {
this.loadRefs(element, {
addressNLContainer: 'single',
});
return super.attach(element).then(() => {
this.reactRoot = createRoot(this.refs.addressNLContainer);
this.renderReact();
});
}

destroy() {
const container = this.refs.addressNLContainer;
container && this.reactRoot.unmount();
super.destroy();
}

onMarkerSet(newLatLng) {
this.setValue(newLatLng, {modified: true});

Check warning on line 83 in src/formio/components/AddressNL.js

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.js#L82-L83

Added lines #L82 - L83 were not covered by tests
}

renderReact() {
const required = AddressNL.schema().validate.required;
this.reactRoot.render(
<IntlProvider {...this.options.intl}>
<ConfigContext.Provider value={{baseUrl: this.options.baseUrl}}>
<Formik initialValues={this.emptyValue}>
<>
<div className="openforms-columns">
<div className="column column--span-6 openforms-form-field-container">
<TextField
name="postcode"
label={
<FormattedMessage
description="Label for addressNL postcode input"
defaultMessage="Postcode"
/>
}
placeholder="1234AB"
isRequired={required}
/>
</div>
<div className="column column--span-6 openforms-form-field-container">
<TextField
name="houseNumber"
label={
<FormattedMessage
description="Label for addressNL houseNumber input"
defaultMessage="House number"
/>
}
isRequired={required}
/>
</div>
</div>
<div className="openforms-columns">
<div className="column column--span-6 openforms-form-field-container">
<TextField
name="houseLetter"
label={
<FormattedMessage
description="Label for addressNL houseLetter input"
defaultMessage="Houser letter"
/>
}
/>
</div>
<div className="column column--span-6 openforms-form-field-container">
<TextField
name="houseNumberAddition"
label={
<FormattedMessage
description="Label for addressNL houseNumberAddition input"
defaultMessage="House number addition"
/>
}
/>
</div>
</div>
</>
</Formik>
</ConfigContext.Provider>
</IntlProvider>
);
}

setValue(value, flags = {}) {
const changed = super.setValue(value, flags);
// re-render if the value is set, which may be because of existing submission data
changed && this.renderReact();
return changed;
}
}
27 changes: 27 additions & 0 deletions src/formio/components/AddressNL.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {withUtrechtDocument} from 'story-utils/decorators';
import {ConfigDecorator} from 'story-utils/decorators';

import {SingleFormioComponent} from './story-util';

export default {
title: 'Form.io components / Custom / Address NL',
decorators: [withUtrechtDocument, ConfigDecorator],
args: {
type: 'addressNL',
key: 'addressNL',
label: 'Address NL',
validate: {
required: false,
},
evalContext: {},
},
argTypes: {
key: {type: {required: true}},
label: {type: {required: true}},
type: {table: {disable: true}},
},
};

export const Default = {
render: SingleFormioComponent,
};
2 changes: 2 additions & 0 deletions src/formio/module.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AddressNL from './components/AddressNL';
import BsnField from './components/BsnField';
import Button from './components/Button';
import Checkbox from './components/Checkbox';
Expand Down Expand Up @@ -46,6 +47,7 @@ const FormIOModule = {
postcode: PostcodeField,
phoneNumber: PhoneNumberField,
bsn: BsnField,
addressNL: AddressNL,
file: FileField,
map: Map,
password: PasswordField,
Expand Down
1 change: 1 addition & 0 deletions src/formio/templates/addressNL.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div ref="addressNLContainer"></div>
2 changes: 2 additions & 0 deletions src/formio/templates/library.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {default as AddressNLTemplate} from './addressNL.ejs';
import {default as ButtonTemplate} from './button.ejs';
import {default as CheckboxTemplate} from './checkbox.ejs';
import {default as ColumnsTemplate} from './columns.ejs';
Expand Down Expand Up @@ -35,6 +36,7 @@ const OFLibrary = {
multiValueTable: {form: MultiValueTableTemplate},
editgrid: {form: EditGridTemplate},
editgridrow: {form: EditGridRowTemplate},
addressNL: {form: AddressNLTemplate},
};

export default OFLibrary;
24 changes: 24 additions & 0 deletions src/i18n/compiled/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,12 @@
"value": "isApplicable"
}
],
"LsgvKh": [
{
"type": 0,
"value": "Postcode"
}
],
"LwpSC/": [
{
"type": 0,
Expand Down Expand Up @@ -805,6 +811,12 @@
"value": "Payment is required for this product"
}
],
"PCv4sQ": [
{
"type": 0,
"value": "House number"
}
],
"PjYrw0": [
{
"type": 0,
Expand Down Expand Up @@ -1181,6 +1193,12 @@
"value": "Add another"
}
],
"cQeqG2": [
{
"type": 0,
"value": "Houser letter"
}
],
"cxDC/G": [
{
"type": 0,
Expand Down Expand Up @@ -1463,6 +1481,12 @@
"value": "Product"
}
],
"lVNV/d": [
{
"type": 0,
"value": "House number addition"
}
],
"lY+Mza": [
{
"type": 0,
Expand Down
24 changes: 24 additions & 0 deletions src/i18n/compiled/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,12 @@
"value": "isApplicable"
}
],
"LsgvKh": [
{
"type": 0,
"value": "Postcode"
}
],
"LwpSC/": [
{
"type": 0,
Expand Down Expand Up @@ -805,6 +811,12 @@
"value": "Voor dit product is betaling vereist"
}
],
"PCv4sQ": [
{
"type": 0,
"value": "Huis nummer"
}
],
"PjYrw0": [
{
"type": 0,
Expand Down Expand Up @@ -1185,6 +1197,12 @@
"value": "Nog één toevoegen"
}
],
"cQeqG2": [
{
"type": 0,
"value": "huis Letter"
}
],
"cxDC/G": [
{
"type": 0,
Expand Down Expand Up @@ -1467,6 +1485,12 @@
"value": "Product"
}
],
"lVNV/d": [
{
"type": 0,
"value": "Huis nummer toevoeging"
}
],
"lY+Mza": [
{
"type": 0,
Expand Down
20 changes: 20 additions & 0 deletions src/i18n/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@
"description": "Step label in progress indicator",
"originalDefault": "{isApplicable, select, false {{label} (n/a)} other {{label}} }"
},
"LsgvKh": {
"defaultMessage": "Postcode",
"description": "Label for addressNL postcode input",
"originalDefault": "Postcode"
},
"LwpSC/": {
"defaultMessage": "Total",
"description": "Label for the total price to pay",
Expand Down Expand Up @@ -374,6 +379,11 @@
"description": "Payment required info text",
"originalDefault": "Payment is required for this product"
},
"PCv4sQ": {
"defaultMessage": "House number",
"description": "Label for addressNL houseNumber input",
"originalDefault": "House number"
},
"PjYrw0": {
"defaultMessage": "Invalid input.",
"description": "ZOD 'too_small' error message, generic",
Expand Down Expand Up @@ -549,6 +559,11 @@
"description": "Edit grid add button, default label text",
"originalDefault": "Add another"
},
"cQeqG2": {
"defaultMessage": "Houser letter",
"description": "Label for addressNL houseLetter input",
"originalDefault": "Houser letter"
},
"cxDC/G": {
"defaultMessage": "The required field is not filled out.",
"description": "ZOD 'required' error message",
Expand Down Expand Up @@ -694,6 +709,11 @@
"description": "Appoinments: product select label",
"originalDefault": "Product"
},
"lVNV/d": {
"defaultMessage": "House number addition",
"description": "Label for addressNL houseNumberAddition input",
"originalDefault": "House number addition"
},
"lY+Mza": {
"defaultMessage": "Product",
"description": "Appointments products step page title",
Expand Down
Loading

0 comments on commit 9a0a950

Please sign in to comment.