Skip to content

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

FIX: set category text color on field blur (#34289)

David Battersby202493cbeca1
diff --git a/app/assets/javascripts/admin/addon/components/color-input.gjs b/app/assets/javascripts/admin/addon/components/color-input.gjs
index b2d81fe706e..7db44e05862 100644
--- a/app/assets/javascripts/admin/addon/components/color-input.gjs
+++ b/app/assets/javascripts/admin/addon/components/color-input.gjs
@@ -34,9 +34,14 @@ export default class ColorInput extends Component {
if (!color.startsWith("#")) {
color = "#" + color;
}
- // auto fill common hex codes like #F8F -> #F8F8F8 and #DDD -> #DDDDDD
- if (color.length === 4 && color[1] === color[3]) {
- color = "#" + color.slice(1).slice(0, 2).repeat(3);
+ if (color.length === 4) {
+ color =
+ "#" +
+ color
+ .slice(1)
+ .split("")
+ .map((hex) => hex + hex)
+ .join("");
}
}
return color;
@@ -54,6 +59,11 @@ export default class ColorInput extends Component {
this.set("hexValue", event.target.value.replace("#", ""));
}
+ @action
+ handleBlur() {
+ this.onBlur?.(this.normalize(this.hexValue));
+ }
+
@observes("hexValue", "brightnessValue", "valid")
hexValueChanged() {
const hex = this.hexValue;
@@ -78,6 +88,7 @@ export default class ColorInput extends Component {
@input={{this.onHexInput}}
class="hex-input"
aria-labelledby={{this.ariaLabelledby}}
+ {{on "blur" this.handleBlur}}
/>
<input
class="picker"
diff --git a/app/assets/javascripts/discourse/app/components/edit-category-general.gjs b/app/assets/javascripts/discourse/app/components/edit-category-general.gjs
index f83194bbb73..5f086485a4e 100644
--- a/app/assets/javascripts/discourse/app/components/edit-category-general.gjs
+++ b/app/assets/javascripts/discourse/app/components/edit-category-general.gjs
@@ -352,7 +352,7 @@ export default class EditCategoryGeneral extends Component {
<ColorInput
@hexValue={{readonly field.value}}
@ariaLabelledby="foreground-color-label"
- @onChangeColor={{fn this.updateColor field}}
+ @onBlur={{fn this.updateColor field}}
/>
<ColorPicker
@colors={{CATEGORY_TEXT_COLORS}}
diff --git a/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js b/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js
index bf205b27c31..e271883b213 100644
--- a/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js
+++ b/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js
@@ -65,6 +65,10 @@ export default class EditCategoryTabsController extends Controller {
return false;
}
+ if (transientData.text_color.length < 6) {
+ return false;
+ }
+
if (this.saving || this.deleting) {
return true;
}
diff --git a/app/assets/javascripts/discourse/tests/integration/components/color-input-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/color-input-test.gjs
index 581459feaf2..ad5d824d111 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/color-input-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/color-input-test.gjs
@@ -1,4 +1,4 @@
-import { fillIn, render } from "@ember/test-helpers";
+import { fillIn, render, triggerEvent } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import ColorInput from "admin/components/color-input";
@@ -6,23 +6,26 @@ import ColorInput from "admin/components/color-input";
module("Integration | Component | ColorInput", function (hooks) {
setupRenderingTest(hooks);
- test("autocompletes hex codes", async function (assert) {
+ test("updates hex codes on blur", async function (assert) {
let result;
- const autocompleteHex = (color) => (result = color.replace("#", ""));
+ const expandHex = (color) => (result = color.replace("#", ""));
- await render(
- <template><ColorInput @onChangeColor={{autocompleteHex}} /></template>
- );
+ await render(<template><ColorInput @onBlur={{expandHex}} /></template>);
await fillIn(".hex-input", "000");
- assert.strictEqual(result, "000000", "black text");
+ await triggerEvent(".hex-input", "blur");
+ assert.strictEqual(result, "000000", "with black text");
+
await fillIn(".hex-input", "fff");
- assert.strictEqual(result, "ffffff", "white text");
+ await triggerEvent(".hex-input", "blur");
+ assert.strictEqual(result, "ffffff", "with white text");
+
await fillIn(".hex-input", "f2f");
- assert.strictEqual(result, "f2f2f2", "2 digit sequence");
- await fillIn(".hex-input", "DDD");
- assert.strictEqual(result, "DDDDDD", "3 digit sequence");
- await fillIn(".hex-input", "0f8");
- assert.strictEqual(result, "0f8", "with no sequence");
+ await triggerEvent(".hex-input", "blur");
+ assert.strictEqual(result, "ff22ff", "with 3 digit hex");
+
+ await fillIn(".hex-input", "052e3d");
+ await triggerEvent(".hex-input", "blur");
+ assert.strictEqual(result, "052e3d", "with 6 digit hex");
});
});