Skip to content

Commit

Permalink
♻️ [#5104] Setting textfield defaultValue properly
Browse files Browse the repository at this point in the history
The previous solution, derived from #4659, is no-longer needed.
- With the previous changes to the `WebformBuilder` (d64fd73) the correct defaultValue is used when editing a component.
- To ensure that the textfield components start with an empty string as `defaultValue`, we can just define it in the schema and point the `get defaultSchema()` to our own schema.

This also makes the textfield components more predictable. If for some reason you set the defaultValue to `null`, then this will be respected.
  • Loading branch information
robinmolen committed Feb 26, 2025
1 parent 69fec08 commit 339c6c9
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 42 deletions.
9 changes: 4 additions & 5 deletions src/openforms/js/components/form/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class EmailField extends FormioEmail {
static schema(...extend) {
const schema = FormioEmail.schema(
{
defaultValue: '',
validateOn: 'blur',
},
...extend
Expand All @@ -31,12 +32,10 @@ class EmailField extends FormioEmail {
super(...args);

patchValidateDefaults(this);
}

// somewhere the default emptyValue/defaultValue does not seem to be used and it forces
// component.defaultValue to be null, which causes issues with multiples #4659
if (this.component.defaultValue === null) {
this.component.defaultValue = '';
}
get defaultSchema() {
return EmailField.schema();
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/openforms/js/components/form/iban.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class IbanField extends TextField {
static schema(...extend) {
return TextField.schema(
{
defaultValue: '',
type: 'iban',
label: 'IBAN',
key: 'iban',
Expand All @@ -31,12 +32,6 @@ class IbanField extends TextField {
super(...args);

patchValidateDefaults(this);

// somewhere the default emptyValue/defaultValue does not seem to be used and it forces
// component.defaultValue to be null, which causes issues with multiples #4659
if (this.component.defaultValue === null) {
this.component.defaultValue = '';
}
}

get defaultSchema() {
Expand Down
7 changes: 1 addition & 6 deletions src/openforms/js/components/form/licenseplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class LicensePlate extends TextField {
static schema(...extend) {
const schema = TextField.schema(
{
defaultValue: '',
type: 'licenseplate',
label: 'License plate',
key: 'licenseplate',
Expand Down Expand Up @@ -39,12 +40,6 @@ class LicensePlate extends TextField {
super(...args);

patchValidateDefaults(this);

// somewhere the default emptyValue/defaultValue does not seem to be used and it forces
// component.defaultValue to be null, which causes issues with multiples #4659
if (this.component.defaultValue === null) {
this.component.defaultValue = '';
}
}

get defaultSchema() {
Expand Down
7 changes: 1 addition & 6 deletions src/openforms/js/components/form/phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class PhoneNumberField extends PhoneNumber {
static schema(...extend) {
const schema = PhoneNumber.schema(
{
defaultValue: '',
inputMask: null,
},
...extend
Expand All @@ -30,12 +31,6 @@ class PhoneNumberField extends PhoneNumber {
super(...args);

patchValidateDefaults(this);

// somewhere the default emptyValue/defaultValue does not seem to be used and it forces
// component.defaultValue to be null, which causes issues with multiples #4659
if (this.component.defaultValue === null) {
this.component.defaultValue = '';
}
}

get defaultSchema() {
Expand Down
4 changes: 4 additions & 0 deletions src/openforms/js/components/form/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class RadioField extends RadioFormio {

return super.setSelectedClasses();
}

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

export default RadioField;
10 changes: 3 additions & 7 deletions src/openforms/js/components/form/textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const FormioTextarea = Formio.Components.components.textarea;

class TextArea extends FormioTextarea {
static schema(...extend) {
return localiseSchema(FormioTextarea.schema({validate: {maxLength: 10000}}, ...extend));
return localiseSchema(
FormioTextarea.schema({defaultValue: '', validate: {maxLength: 10000}}, ...extend)
);
}

static get builderInfo() {
Expand All @@ -23,12 +25,6 @@ class TextArea extends FormioTextarea {
super(...args);

patchValidateDefaults(this);

// somewhere the default emptyValue/defaultValue does not seem to be used and it forces
// component.defaultValue to be null, which causes issues with multiples #4659
if (this.component.defaultValue === null) {
this.component.defaultValue = '';
}
}

get defaultSchema() {
Expand Down
10 changes: 4 additions & 6 deletions src/openforms/js/components/form/textfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const patchValidateDefaults = instance => {

class TextField extends FormioTextField {
static schema(...extend) {
return localiseSchema(FormioTextField.schema(...extend));
return localiseSchema(FormioTextField.schema({defaultValue: ''}, ...extend));
}

static get builderInfo() {
Expand All @@ -44,12 +44,10 @@ class TextField extends FormioTextField {
super(...args);

patchValidateDefaults(this);
}

// somewhere the default emptyValue/defaultValue does not seem to be used and it forces
// component.defaultValue to be null, which causes issues with multiples #4659
if (this.component.defaultValue === null) {
this.component.defaultValue = '';
}
get defaultSchema() {
return TextField.schema();
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/openforms/js/components/form/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ const Time = Formio.Components.components.time;
class TimeField extends Time {
constructor(...args) {
super(...args);

// somewhere the default emptyValue/defaultValue does not seem to be used and it forces
// component.defaultValue to be null, which causes issues with multiples #4659
if (this.component.defaultValue === null) {
this.component.defaultValue = '';
}
}

static schema(...extend) {
Expand Down

0 comments on commit 339c6c9

Please sign in to comment.