Skip to content

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

FIX: Do not mutate color swatch array (#37433)

Martin Brennan84e625790967
diff --git a/frontend/discourse/app/form-kit/components/fk/control/color.gjs b/frontend/discourse/app/form-kit/components/fk/control/color.gjs
index 032bc8fa8b5..7881e1b4789 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/color.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/color.gjs
@@ -74,7 +74,7 @@ export default class FKControlColor extends Component {
return this.args.colors;
}
- return this.args.colors.sort((a) =>
+ return this.args.colors.toSorted((a) =>
isColorUsed(this.args.usedColors, a) ? 1 : -1
);
}
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/color-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/color-test.gjs
index 5d0faf0cdac..a2225f26727 100644
--- a/frontend/discourse/tests/integration/components/form-kit/controls/color-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/color-test.gjs
@@ -103,6 +103,27 @@ module(
assert.true(swatches[2].isUsed);
});
+ test("sorting used colors does not mutate the original array", async function (assert) {
+ const colors = ["FF0000", "00FF00", "0000FF"];
+ const usedColors = ["00FF00"];
+
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="color" @title="Color" as |field|>
+ <field.Color @colors={{colors}} @usedColors={{usedColors}} />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert.deepEqual(
+ colors,
+ ["FF0000", "00FF00", "0000FF"],
+ "original colors array should not be mutated"
+ );
+ });
+
test("when disabled", async function (assert) {
const colors = ["FF0000", "00FF00"];