From 4c8812737cd9e0bc8127ba33f760c4c724942e18 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 18 Jul 2024 14:33:08 +0200 Subject: [PATCH] DEV: supports setProperties (#27969) This is a convenience for when you have multiple properties to set in form kit. ``` // before set("foo", 1); set("bar", 2); //after setProperties({foo: 1, bar: 2}); ``` --- .../app/form-kit/components/fk/form.gjs | 9 +++++++++ .../components/form-kit/form-test.gjs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/app/assets/javascripts/discourse/app/form-kit/components/fk/form.gjs b/app/assets/javascripts/discourse/app/form-kit/components/fk/form.gjs index e50f27976df..f8ea5e86cac 100644 --- a/app/assets/javascripts/discourse/app/form-kit/components/fk/form.gjs +++ b/app/assets/javascripts/discourse/app/form-kit/components/fk/form.gjs @@ -36,6 +36,7 @@ class FKForm extends Component { this.args.onRegisterApi?.({ set: this.set, + setProperties: this.setProperties, submit: this.onSubmit, reset: this.onReset, }); @@ -121,6 +122,13 @@ class FKForm extends Component { } } + @action + async setProperties(object) { + for (const [name, value] of Object.entries(object)) { + await this.set(name, value); + } + } + @action registerField(name, field) { if (!name) { @@ -283,6 +291,7 @@ class FKForm extends Component { triggerRevalidationFor=this.triggerRevalidationFor ) set=this.set + setProperties=this.setProperties addItemToCollection=this.addItemToCollection ) this.formData.draftData diff --git a/app/assets/javascripts/discourse/tests/integration/components/form-kit/form-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/form-kit/form-test.gjs index 564319df550..7a82f94aaff 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/form-kit/form-test.gjs +++ b/app/assets/javascripts/discourse/tests/integration/components/form-kit/form-test.gjs @@ -213,4 +213,22 @@ module("Integration | Component | FormKit | Form", function (hooks) { assert.dom(".foo").hasText("2"); }); + + test("yielded setProperties", async function (assert) { + await render(); + + await click(".test"); + + assert.dom(".foo").hasText("2"); + assert.dom(".bar").hasText("2"); + }); });