A platform for community discussion. Free, open, simple.
FIX: Add `@prefixHex` option to FormKit color control (#38920)
Régis Hanol42c355621705
diff --git a/frontend/discourse/admin/components/admin-welcome-banner-form.gjs b/frontend/discourse/admin/components/admin-welcome-banner-form.gjsindex 5890d3f9afb..5982dd8bc14 100644--- a/frontend/discourse/admin/components/admin-welcome-banner-form.gjs+++ b/frontend/discourse/admin/components/admin-welcome-banner-form.gjs@@ -422,7 +422,7 @@ export default class AdminWelcomeBannerForm extends Component {@type="color"as |field|>- <field.Control @allowNamedColors={{true}} />+ <field.Control @allowNamedColors={{true}} @prefixHex={{true}} /></form.Field><form.Fielddiff --git a/frontend/discourse/app/form-kit/components/fk/control/color.gjs b/frontend/discourse/app/form-kit/components/fk/control/color.gjsindex 70ac19d6b53..162e7c57cef 100644--- a/frontend/discourse/app/form-kit/components/fk/control/color.gjs+++ b/frontend/discourse/app/form-kit/components/fk/control/color.gjs@@ -66,7 +66,7 @@ export default class FKControlColor extends FKBaseControl {@service currentUser;get showPrefix() {- return !this.args.allowNamedColors;+ return this.args.prefixHex || !this.args.allowNamedColors;}get maxLength() {@@ -103,8 +103,13 @@ export default class FKControlColor extends FKBaseControl {);}- get normalizedValueForPicker() {+ get bareValue() {const value = this.args.field.value;+ return value?.replace(/^#/, "") ?? "";+ }++ get normalizedValueForPicker() {+ const value = this.bareValue;if (!value) {return "#000000";}@@ -121,7 +126,7 @@ export default class FKControlColor extends FKBaseControl {}get pickerIconClass() {- const value = this.args.field.value;+ const value = this.bareValue;if (!value || !isValidHex(value)) {return "--is-light";}@@ -130,33 +135,41 @@ export default class FKControlColor extends FKBaseControl {return calculateLuminance(hex) > 0.5 ? "--is-light" : "--is-dark";}+ setValue(value) {+ if (this.args.prefixHex && value && isValidHex(value.replace(/^#/, ""))) {+ value = value.startsWith("#") ? value : `#${value}`;+ }+ this.args.field.set(value);+ }+@actionhandleTextInput(event) {- this.args.field.set(event.target.value);+ this.setValue(event.target.value);}@actionhandleBlur() {if (!this.args.field.value && this.args.fallbackValue) {- this.args.field.set(this.args.fallbackValue);+ this.setValue(this.args.fallbackValue);}}@actionhandlePickerInput(event) {- this.args.field.set(event.target.value.replace(/^#/, ""));+ const value = event.target.value.replace(/^#/, "");+ this.setValue(value);}@actionhandlePaste(event) {event.preventDefault();const colorCode = event.clipboardData.getData("text/plain") ?? "";- this.args.field.set(colorCode.replace(/^#/, ""));+ this.setValue(colorCode.replace(/^#/, ""));}@actionselectColor(color, closeMenu) {- this.args.field.set(color);+ this.setValue(color);if (typeof closeMenu === "function") {closeMenu();}@@ -170,7 +183,7 @@ export default class FKControlColor extends FKBaseControl {{{/if}}<inputtype="text"- value={{@field.value}}+ value={{this.bareValue}}maxlength={{this.maxLength}}class="form-kit__control-color-input-hex"disabled={{@field.disabled}}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.gjsindex e59fadd08a1..7ea9e2496fb 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@@ -210,6 +210,53 @@ module(assert.strictEqual(formKit().field("color").value(), "AABBCC");});+ test("@prefixHex stores value with # prefix", async function (assert) {+ let data = { color: "" };+ const mutateData = (x) => (data = x);++ await render(+ <template>+ <Form @onSubmit={{mutateData}} @data={{data}} as |form|>+ <form.Field @type="color" @name="color" @title="Color" as |field|>+ <field.Control @prefixHex={{true}} />+ </form.Field>+ </Form>+ </template>+ );++ assert.true(formKit().field("color").hasPrefix());++ await fillIn(".form-kit__control-color-input-hex", "FF0000");+ await formKit().submit();++ assert.strictEqual(data.color, "#FF0000");+ });++ test("@prefixHex with native color picker", async function (assert) {+ let data = { color: "#FF0000" };+ const mutateData = (x) => (data = x);++ await render(+ <template>+ <Form @onSubmit={{mutateData}} @data={{data}} as |form|>+ <form.Field @type="color" @name="color" @title="Color" as |field|>+ <field.Control @prefixHex={{true}} />+ </form.Field>+ </Form>+ </template>+ );++ assert.dom(".form-kit__control-color-input-hex").hasValue("FF0000");++ const picker = formKit().field("color").pickerElement;+ picker.value = "#00FF00";+ picker.dispatchEvent(new Event("input", { bubbles: true }));++ await formKit().submit();++ assert.strictEqual(data.color, "#00ff00");+ });+test("@collapseSwatches and @collapseSwatchesLabel", async function (assert) {const colors = ["FF0000", "00FF00", "0000FF"];let data = { color: null };diff --git a/plugins/styleguide/assets/javascripts/discourse/components/sections/atoms/05-forms.gjs b/plugins/styleguide/assets/javascripts/discourse/components/sections/atoms/05-forms.gjsindex d9e118c0f27..eb86c208913 100644--- a/plugins/styleguide/assets/javascripts/discourse/components/sections/atoms/05-forms.gjs+++ b/plugins/styleguide/assets/javascripts/discourse/components/sections/atoms/05-forms.gjs@@ -256,6 +256,18 @@ const USED_COLORS = ["00FF00"];</template>`;}+ get colorPrefixHexCode() {+ return `import Form from "discourse/components/form";++<template>+ <Form as |form|>+ <form.Field @title="Color" @name="color" @type="color" as |field|>+ <field.Control @prefixHex={{true}} placeholder="RRGGBB" />+ </form.Field>+ </Form>+</template>`;+ }+get sectionCode() {return `import Form from "discourse/components/form";@@ -635,6 +647,17 @@ import Form from "discourse/components/form";</Form></StyleguideExample>+ <StyleguideExample+ @title="Color with # prefix in value"+ @code={{this.colorPrefixHexCode}}+ >+ <Form as |form|>+ <form.Field @title="Color" @name="color" @type="color" as |field|>+ <field.Control @prefixHex={{true}} placeholder="RRGGBB" />+ </form.Field>+ </Form>+ </StyleguideExample>+<h2>Layout</h2><StyleguideExample @title="Section" @code={{this.sectionCode}}>