A platform for community discussion. Free, open, simple.
UX: errors on submit and fallback color for simplified category creation (#37330)
Kris27353d405753
diff --git a/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss b/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scssindex 1a1fbff0ef3..1406166c62d 100644--- a/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss+++ b/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss@@ -95,4 +95,19 @@.category-permissions-table {width: 100%;}++ .form-kit__errors-summary {+ margin-top: var(--space-4);++ + .form-kit__section.edit-category-content {+ margin-top: 0;+ }+ }++ .form-kit__conditional-display {+ .badge-category__wrapper {+ min-height: 2.6em;+ top: var(--space-1);+ }+ }}diff --git a/app/assets/stylesheets/common/form-kit/_control-color.scss b/app/assets/stylesheets/common/form-kit/_control-color.scssindex 95f937c8b85..3d67f7343aa 100644--- a/app/assets/stylesheets/common/form-kit/_control-color.scss+++ b/app/assets/stylesheets/common/form-kit/_control-color.scss@@ -74,12 +74,12 @@}}-.form-kit__control-color-input-picker {+.form-kit__control-color-input-picker[type="color"] {width: var(--color-button-width);height: var(--color-button-height);padding: 0;border: 1px solid var(--primary-low-mid);- border-radius: var(--d-input-border-radius);+ border-radius: var(--d-button-border-radius);cursor: pointer;&::-webkit-color-swatch-wrapper {diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex a7a93c4e068..f84702b40ea 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -4566,6 +4566,8 @@ en:save_error: There was an error saving the category.errors:self_lockout: "You are about to change category permissions in a way that will remove your own access. Are you sure you want to proceed?"+ validations:+ emoji_required: "Select an emoji to continue."name: "Category name"untitled: "Untitled category"description: "Description"diff --git a/frontend/discourse/admin/components/upsert-category/general.gjs b/frontend/discourse/admin/components/upsert-category/general.gjsindex d53fad7c296..27ed7b11429 100644--- a/frontend/discourse/admin/components/upsert-category/general.gjs+++ b/frontend/discourse/admin/components/upsert-category/general.gjs@@ -342,16 +342,18 @@ export default class UpsertCategoryGeneral extends Component {await set("color", color);- const whiteDiff = this.colorDifference(color, CATEGORY_TEXT_COLORS[0]);- const blackDiff = this.colorDifference(color, CATEGORY_TEXT_COLORS[1]);- const colorIndex = whiteDiff > blackDiff ? 0 : 1;+ if (color) {+ const whiteDiff = this.colorDifference(color, CATEGORY_TEXT_COLORS[0]);+ const blackDiff = this.colorDifference(color, CATEGORY_TEXT_COLORS[1]);+ const colorIndex = whiteDiff > blackDiff ? 0 : 1;- this.args.form.set("text_color", CATEGORY_TEXT_COLORS[colorIndex]);+ this.args.form.set("text_color", CATEGORY_TEXT_COLORS[colorIndex]);- this.args.updatePreview?.({- color,- text_color: CATEGORY_TEXT_COLORS[colorIndex],- });+ this.args.updatePreview?.({+ color,+ text_color: CATEGORY_TEXT_COLORS[colorIndex],+ });+ }}@action@@ -448,6 +450,16 @@ export default class UpsertCategoryGeneral extends Component {}}+ @action+ validateEmoji(name, value, { addError, data }) {+ if (data.style_type === "emoji" && !value) {+ addError(name, {+ title: i18n("category.emoji"),+ message: i18n("category.validations.emoji_required"),+ });+ }+ }+get categoryDescription() {if (this.args.category.description) {return htmlSafe(this.args.category.description);@@ -516,12 +528,17 @@ export default class UpsertCategoryGeneral extends Component {@usedColors={{this.usedBackgroundColors}}@collapseSwatches={{true}}@collapseSwatchesLabel={{i18n "category.color_palette"}}+ @fallbackValue={{@category.color}}/></@form.Field><@form.Container @title={{i18n "category.style"}}><@form.ConditionalContent- @activeName={{or @category.styleType "square"}}+ @activeName={{or+ @transientData.style_type+ @category.styleType+ "square"+ }}@onChange={{this.onStyleTypeChange}}as |cc|>@@ -575,6 +592,7 @@ export default class UpsertCategoryGeneral extends Component {@showTitle={{false}}@format="large"@onSet={{this.onEmojiSet}}+ @validate={{this.validateEmoji}}as |field|><field.Custom>diff --git a/frontend/discourse/admin/controllers/edit-category/tabs.js b/frontend/discourse/admin/controllers/edit-category/tabs.jsindex 84e36908565..df6ca6754f4 100644--- a/frontend/discourse/admin/controllers/edit-category/tabs.js+++ b/frontend/discourse/admin/controllers/edit-category/tabs.js@@ -106,23 +106,6 @@ export default class EditCategoryTabsController extends Controller {return data;}- @action- canSaveForm(transientData) {- if (!transientData.name) {- return false;- }-- if (transientData.style_type === "emoji" && !transientData.emoji) {- return false;- }-- if (this.saving || this.deleting) {- return true;- }-- return true;- }-@discourseComputed("saving", "deleting")deleteDisabled(saving, deleting) {return deleting || saving || false;@@ -174,6 +157,31 @@ export default class EditCategoryTabsController extends Controller {this.showAdvancedTabs = this.showAdvancedTabs || tab !== "general";}+ @action+ validateForm(data, { addError }) {+ let hasGeneralTabErrors = false;++ if (!data.name) {+ addError("name", {+ title: i18n("category.name"),+ message: i18n("form_kit.errors.required"),+ });+ hasGeneralTabErrors = true;+ }++ if (data.style_type === "emoji" && !data.emoji) {+ addError("emoji", {+ title: i18n("category.emoji"),+ message: i18n("category.validations.emoji_required"),+ });+ hasGeneralTabErrors = true;+ }++ if (hasGeneralTabErrors && this.selectedTab !== "general") {+ this.selectedTab = "general";+ }+ }+@actionregisterValidator(validator) {this.validators.push(validator);diff --git a/frontend/discourse/admin/templates/edit-category/tabs.gjs b/frontend/discourse/admin/templates/edit-category/tabs.gjsindex 2d1bf20fa98..7d8ef32eef6 100644--- a/frontend/discourse/admin/templates/edit-category/tabs.gjs+++ b/frontend/discourse/admin/templates/edit-category/tabs.gjs@@ -15,7 +15,6 @@ import UpsertCategoryTags from "discourse/admin/components/upsert-category/tags"import EditCategoryTabsHorizontal from "discourse/admin/templates/edit-category/tabs-horizontal";import EditCategoryTabsVertical from "discourse/admin/templates/edit-category/tabs-vertical";import Form from "discourse/components/form";-import { not } from "discourse/truth-helpers";const TAB_COMPONENTS = {general: EditCategoryGeneral,@@ -60,6 +59,7 @@ export default <template>@data={{@controller.formData}}@onDirtyCheck={{@controller.isLeavingForm}}@onSubmit={{@controller.saveCategory}}+ @validate={{@controller.validateForm}}as |form transientData|><form.Section@@ -112,11 +112,7 @@ export default <template>{{/if}}<form.Actions class="edit-category-footer">- <form.Submit- @disabled={{not (@controller.canSaveForm transientData)}}- @label={{@controller.saveLabel}}- id="save-category"- />+ <form.Submit @label={{@controller.saveLabel}} id="save-category" />{{#if @controller.model.can_delete}}<form.Buttondiff --git a/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs b/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjsindex a6013f03e94..43366eb3798 100644--- a/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs+++ b/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs@@ -113,6 +113,7 @@ export default class FKControlWrapper extends Component {@allowNamedColors={{@allowNamedColors}}@collapseSwatches={{@collapseSwatches}}@collapseSwatchesLabel={{@collapseSwatchesLabel}}+ @fallbackValue={{@fallbackValue}}@onControlWidthChange={{fn (mut this.controlWidth)}}id={{@field.id}}name={{@field.name}}diff --git a/frontend/discourse/app/form-kit/components/fk/control/color.gjs b/frontend/discourse/app/form-kit/components/fk/control/color.gjsindex 82fc9af5730..032bc8fa8b5 100644--- a/frontend/discourse/app/form-kit/components/fk/control/color.gjs+++ b/frontend/discourse/app/form-kit/components/fk/control/color.gjs@@ -111,6 +111,13 @@ export default class FKControlColor extends Component {this.args.field.set(event.target.value);}+ @action+ handleBlur() {+ if (!this.args.field.value && this.args.fallbackValue) {+ this.args.field.set(this.args.fallbackValue);+ }+ }+@actionhandlePickerInput(event) {this.args.field.set(event.target.value.replace(/^#/, ""));@@ -144,6 +151,7 @@ export default class FKControlColor extends Component {class="form-kit__control-color-input-hex"disabled={{@field.disabled}}{{on "input" this.handleTextInput}}+ {{on "blur" this.handleBlur}}{{on "paste" this.handlePaste}}...attributes/>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 c164679045f..5d0faf0cdac 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@@ -1,4 +1,4 @@-import { render } from "@ember/test-helpers";+import { blur, fillIn, focus, render } from "@ember/test-helpers";import { module, test } from "qunit";import Form from "discourse/components/form";import { setupRenderingTest } from "discourse/tests/helpers/component-test";@@ -25,7 +25,7 @@ module(assert.strictEqual(formKit().field("color").value(), "FF0000");- await formKit().field("color").fillIn("00FF00");+ await fillIn(".form-kit__control-color-input-hex", "00FF00");await formKit().submit();assert.strictEqual(data.color, "00FF00");@@ -166,5 +166,26 @@ module(const picker = formKit().field("color").pickerElement;assert.strictEqual(picker.value, "#800080");});++ test("@fallbackValue restores value when field is cleared", async function (assert) {+ let data = { color: "" };+ const mutateData = (x) => (data = x);++ await render(+ <template>+ <Form @onSubmit={{mutateData}} @data={{data}} as |form|>+ <form.Field @name="color" @title="Color" as |field|>+ <field.Color @fallbackValue="AABBCC" />+ </form.Field>+ </Form>+ </template>+ );++ await focus(".form-kit__control-color-input-hex");+ await fillIn(".form-kit__control-color-input-hex", "");+ await blur(".form-kit__control-color-input-hex");++ assert.strictEqual(formKit().field("color").value(), "AABBCC");+ });});