Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add `commitField` to FormKit (#38944)

Sérgio Saquetim726dad407c2e
diff --git a/frontend/discourse/app/form-kit/components/fk/form.gjs b/frontend/discourse/app/form-kit/components/fk/form.gjs
index 0cce24f7cc2..62ae22ffaad 100644
--- a/frontend/discourse/app/form-kit/components/fk/form.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/form.gjs
@@ -48,6 +48,7 @@ class FKForm extends Component {
set: this.set,
setProperties: this.setProperties,
get: this.get,
+ commitField: this.commitField,
submit: this.onSubmit,
reset: this.onReset,
addError: this.addError,
@@ -188,6 +189,11 @@ class FKForm extends Component {
}
}
+ @action
+ commitField(name) {
+ this.formData.commitField(name);
+ }
+
@action
async setProperties(object) {
for (const [name, value] of Object.entries(object)) {
@@ -350,6 +356,7 @@ class FKForm extends Component {
CheckboxGroup=(this.componentFor FKCheckboxGroup)
set=this.set
setProperties=this.setProperties
+ commitField=this.commitField
addItemToCollection=this.addItemToCollection
)
this.formData.draftData
diff --git a/frontend/discourse/app/form-kit/lib/fk-form-data.js b/frontend/discourse/app/form-kit/lib/fk-form-data.js
index 09020d009da..0dd0f498e4a 100644
--- a/frontend/discourse/app/form-kit/lib/fk-form-data.js
+++ b/frontend/discourse/app/form-kit/lib/fk-form-data.js
@@ -197,6 +197,21 @@ export default class FKFormData {
);
}
+ /**
+ * Commits the current draft value of a specific field as the new baseline
+ * and removes only its patches from the history. Other fields' dirty state
+ * is unaffected.
+ * @param {string} name - The top-level property name to commit.
+ */
+ commitField(name) {
+ this.data = produce(this.data, (target) => {
+ target[name] = this.draftData[name];
+ });
+
+ this.patches = this.patches.filter((p) => p.path[0] !== name);
+ this.inversePatches = this.inversePatches.filter((p) => p.path[0] !== name);
+ }
+
/**
* Resets the patches and inverse patches.
*/
diff --git a/frontend/discourse/tests/integration/components/form-kit/form-test.gjs b/frontend/discourse/tests/integration/components/form-kit/form-test.gjs
index 92af2e0da94..164031d9320 100644
--- a/frontend/discourse/tests/integration/components/form-kit/form-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/form-test.gjs
@@ -181,6 +181,34 @@ module("Integration | Component | FormKit | Form", function (hooks) {
}, 0);
});
+ test("@onRegisterApi - commitField", async function (assert) {
+ let formApi;
+ const model = { foo: "a", bar: "b" };
+ const registerApi = (api) => (formApi = api);
+
+ await render(
+ <template>
+ <Form @data={{model}} @onRegisterApi={{registerApi}} as |form data|>
+ <div class="foo">{{data.foo}}</div>
+ <div class="bar">{{data.bar}}</div>
+ </Form>
+ </template>
+ );
+
+ await formApi.set("foo", "a2");
+ await formApi.set("bar", "b2");
+ formApi.commitField("foo");
+
+ assert.true(formApi.isDirty, "still dirty because 'bar' is uncommitted");
+
+ await formApi.reset();
+
+ assert
+ .dom(".foo")
+ .hasText("a2", "'foo' keeps its committed value after reset");
+ assert.dom(".bar").hasText("b", "'bar' reverts to original after reset");
+ });
+
test("@onRegisterApi - isDirty", async function (assert) {
let formApi;
const model = { foo: 1 };
@@ -296,6 +324,39 @@ module("Integration | Component | FormKit | Form", function (hooks) {
assert.dom(".foo").hasText("2");
});
+ test("yielded commitField", async function (assert) {
+ let formApi;
+ const registerApi = (api) => (formApi = api);
+
+ await render(
+ <template>
+ <Form
+ @data={{hash foo=1 bar=2}}
+ @onRegisterApi={{registerApi}}
+ as |form data|
+ >
+ <div class="foo">{{data.foo}}</div>
+ <div class="bar">{{data.bar}}</div>
+ <form.Button class="set-foo" @action={{fn form.set "foo" 10}} />
+ <form.Button
+ class="commit-foo"
+ @action={{fn form.commitField "foo"}}
+ />
+ </Form>
+ </template>
+ );
+
+ await click(".set-foo");
+ assert.dom(".foo").hasText("10");
+
+ await click(".commit-foo");
+
+ await formApi.reset();
+
+ assert.dom(".foo").hasText("10", "committed field survives reset");
+ assert.dom(".bar").hasText("2", "uncommitted field keeps original value");
+ });
+
test("yielded setProperties", async function (assert) {
await render(
<template>
diff --git a/frontend/discourse/tests/unit/lib/form-kit/fk-form-data-test.js b/frontend/discourse/tests/unit/lib/form-kit/fk-form-data-test.js
new file mode 100644
index 00000000000..124dc308ff4
--- /dev/null
+++ b/frontend/discourse/tests/unit/lib/form-kit/fk-form-data-test.js
@@ -0,0 +1,93 @@
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import FKFormData from "discourse/form-kit/lib/fk-form-data";
+
+module("Unit | Lib | FormKit | FKFormData", function (hooks) {
+ setupTest(hooks);
+
+ test("commitField - commits the draft value as the new baseline", function (assert) {
+ const formData = new FKFormData({ foo: "original", bar: 1 });
+
+ formData.set("foo", "changed");
+ formData.commitField("foo");
+
+ assert.strictEqual(
+ formData.data.foo,
+ "changed",
+ "baseline data is updated to the draft value"
+ );
+ assert.strictEqual(
+ formData.draftData.foo,
+ "changed",
+ "draft data still holds the committed value"
+ );
+ });
+
+ test("commitField - removes only patches for the committed field", function (assert) {
+ const formData = new FKFormData({ foo: "a", bar: "b" });
+
+ formData.set("foo", "a2");
+ formData.set("bar", "b2");
+
+ assert.true(formData.patches.length >= 2, "patches exist for both fields");
+
+ formData.commitField("foo");
+
+ const remainingPaths = formData.patches.map((p) => p.path[0]);
+ assert.false(
+ remainingPaths.includes("foo"),
+ "patches for 'foo' are removed"
+ );
+ assert.true(
+ remainingPaths.includes("bar"),
+ "patches for 'bar' are preserved"
+ );
+ });
+
+ test("commitField - field is no longer dirty after commit", function (assert) {
+ const formData = new FKFormData({ foo: "a" });
+
+ formData.set("foo", "a2");
+ assert.true(formData.isDirty, "dirty after change");
+
+ formData.commitField("foo");
+ assert.false(
+ formData.isDirty,
+ "not dirty after committing the only changed field"
+ );
+ });
+
+ test("commitField - other fields remain dirty", function (assert) {
+ const formData = new FKFormData({ foo: "a", bar: "b" });
+
+ formData.set("foo", "a2");
+ formData.set("bar", "b2");
+ formData.commitField("foo");
+
+ assert.true(
+ formData.isDirty,
+ "still dirty because 'bar' has uncommitted changes"
+ );
+ });
+
+ test("commitField - committed field survives reset", async function (assert) {
+ const formData = new FKFormData({ foo: "a", bar: "b" });
+
+ formData.set("foo", "a2");
+ formData.set("bar", "b2");
+ formData.commitField("foo");
+
+ await formData.rollback();
+
+ assert.strictEqual(
+ formData.draftData.foo,
+ "a2",
+ "'foo' keeps its committed value after rollback"
+ );
+ assert.strictEqual(
+ formData.draftData.bar,
+ "b",
+ "'bar' reverts to original value after rollback"
+ );
+ });
+});