Skip to content

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

DEV: simplifies form-kit fields args (#38346)

Joffrey JAFFEUX792a4a8ff59c
diff --git a/frontend/discourse/admin/components/admin-logo-form.gjs b/frontend/discourse/admin/components/admin-logo-form.gjs
index cb76c15e90d..b76e0faf9f6 100644
--- a/frontend/discourse/admin/components/admin-logo-form.gjs
+++ b/frontend/discourse/admin/components/admin-logo-form.gjs
@@ -167,20 +167,24 @@ export default class AdminLogoForm extends Component {
@description={{i18n "admin.config.logo.form.large_icon.description"}}
@helpText={{i18n "admin.config.logo.form.large_icon.help_text"}}
@onSet={{fn this.handleUpload "large_icon"}}
- @placeholderUrl={{this.placeholders.large_icon}}
as |field|
>
- <field.Image @type="branding" />
+ <field.Image
+ @type="branding"
+ @placeholderUrl={{this.placeholders.large_icon}}
+ />
</form.Field>
<form.Field
@name="favicon"
@title={{i18n "admin.config.logo.form.favicon.title"}}
@description={{i18n "admin.config.logo.form.favicon.description"}}
@onSet={{fn this.handleUpload "favicon"}}
- @placeholderUrl={{this.placeholders.favicon}}
as |field|
>
- <field.Image @type="branding" />
+ <field.Image
+ @type="branding"
+ @placeholderUrl={{this.placeholders.favicon}}
+ />
</form.Field>
<form.Field
@name="logo_small"
@@ -231,10 +235,12 @@ export default class AdminLogoForm extends Component {
}}
@helpText={{i18n "admin.config.logo.form.mobile_logo.help_text"}}
@onSet={{fn this.handleUpload "mobile_logo"}}
- @placeholderUrl={{this.placeholders.mobile_logo}}
as |field|
>
- <field.Image @type="branding" />
+ <field.Image
+ @type="branding"
+ @placeholderUrl={{this.placeholders.mobile_logo}}
+ />
</form.Field>
<form.Field
@name="mobile_logo_dark_required"
@@ -306,10 +312,12 @@ export default class AdminLogoForm extends Component {
"admin.config.logo.form.apple_touch_icon.help_text"
}}
@onSet={{fn this.handleUpload "apple_touch_icon"}}
- @placeholderUrl={{this.placeholders.apple_touch_icon}}
as |field|
>
- <field.Image @type="branding" />
+ <field.Image
+ @type="branding"
+ @placeholderUrl={{this.placeholders.apple_touch_icon}}
+ />
</form.Field>
</:content>
</AdminConfigAreaCardSection>
@@ -328,7 +336,6 @@ export default class AdminLogoForm extends Component {
}}
@helpText={{i18n "admin.config.logo.form.digest_logo.help_text"}}
@onSet={{fn this.handleUpload "digest_logo"}}
- @placeholderUrl={{this.placeholders.digest_logo}}
as |field|
>
<field.Image
@@ -352,10 +359,12 @@ export default class AdminLogoForm extends Component {
"admin.config.logo.form.opengraph_image.description"
}}
@onSet={{fn this.handleUpload "opengraph_image"}}
- @placeholderUrl={{this.placeholders.opengraph_image}}
as |field|
>
- <field.Image @type="branding" />
+ <field.Image
+ @type="branding"
+ @placeholderUrl={{this.placeholders.opengraph_image}}
+ />
</form.Field>
</:content>
</AdminConfigAreaCardSection>
diff --git a/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs b/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs
deleted file mode 100644
index 675ae375c43..00000000000
--- a/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs
+++ /dev/null
@@ -1,150 +0,0 @@
-import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
-import { concat, fn } from "@ember/helper";
-import didInsert from "@ember/render-modifiers/modifiers/did-insert";
-import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";
-import FKLabel from "discourse/form-kit/components/fk/label";
-import FKMeta from "discourse/form-kit/components/fk/meta";
-import FKOptional from "discourse/form-kit/components/fk/optional";
-import FKText from "discourse/form-kit/components/fk/text";
-import FKTooltip from "discourse/form-kit/components/fk/tooltip";
-import concatClass from "discourse/helpers/concat-class";
-import { eq } from "discourse/truth-helpers";
-
-export default class FKControlWrapper extends Component {
- @tracked controlWidth = "auto";
-
- constructor() {
- super(...arguments);
-
- this.args.field.type = this.args.component.controlType;
- }
-
- get controlType() {
- if (this.args.field.type === "input") {
- return this.args.field.type + "-" + (this.args.type || "text");
- }
-
- return this.args.field.type;
- }
-
- get error() {
- return (this.args.errors ?? {})[this.args.field.name];
- }
-
- get titleFormat() {
- return this.args.field.titleFormat || this.args.field.format;
- }
-
- get descriptionFormat() {
- return this.args.field.descriptionFormat || this.args.field.format;
- }
-
- normalizeName(name) {
- return name.replace(/\./g, "-");
- }
-
- <template>
- <div
- id={{concat "control-" (this.normalizeName @field.name)}}
- class={{concatClass
- "form-kit__container"
- "form-kit__field"
- (concat "form-kit__field-" this.controlType)
- (if this.error "has-error")
- (if @field.disabled "is-disabled")
- (if (eq @field.format "full") "--full")
- }}
- data-disabled={{@field.disabled}}
- data-name={{@field.name}}
- data-control-type={{this.controlType}}
- {{didInsert (fn @registerField @field.name @field)}}
- {{willDestroy (fn @unregisterField @field.name)}}
- >
- {{#unless (eq @field.type "checkbox")}}
- {{#if @field.showTitle}}
- <FKLabel
- class={{concatClass
- "form-kit__container-title"
- (if this.titleFormat (concat "--" this.titleFormat))
- }}
- @fieldId={{@field.id}}
- >
- <span>{{@field.title}}</span>
-
- <FKOptional @field={{@field}} />
- <FKTooltip @field={{@field}} />
- </FKLabel>
- {{/if}}
-
- {{#if @field.description}}
- <FKText
- class={{concatClass
- "form-kit__container-description"
- (if this.descriptionFormat (concat "--" this.descriptionFormat))
- }}
- >{{@field.description}}</FKText>
- {{/if}}
- {{/unless}}
-
- <div
- class={{concatClass
- "form-kit__container-content"
- (if @field.format (concat "--" @field.format))
- }}
- >
- <@component
- @field={{@field}}
- @value={{@value}}
- @type={{@type}}
- @yesLabel={{@yesLabel}}
- @noLabel={{@noLabel}}
- @lang={{@lang}}
- @before={{@before}}
- @after={{@after}}
- @height={{@height}}
- @preview={{@preview}}
- @includeTime={{@includeTime}}
- @expandedDatePickerOnDesktop={{@expandedDatePickerOnDesktop}}
- @selection={{@selection}}
- @includeNone={{@includeNone}}
- @colors={{@colors}}
- @usedColors={{@usedColors}}
- @allowNamedColors={{@allowNamedColors}}
- @collapseSwatches={{@collapseSwatches}}
- @collapseSwatchesLabel={{@collapseSwatchesLabel}}
- @fallbackValue={{@fallbackValue}}
- @showAllTags={{@showAllTags}}
- @excludeSynonyms={{@excludeSynonyms}}
- @excludeTagsWithSynonyms={{@excludeTagsWithSynonyms}}
- @unlimited={{@unlimited}}
- @categoryId={{@categoryId}}
- @allowCreate={{@allowCreate}}
- @placeholder={{@placeholder}}
- @hasBlock={{has-block}}
- @onControlWidthChange={{fn (mut this.controlWidth)}}
- id={{@field.id}}
- name={{@field.name}}
- aria-invalid={{if this.error "true"}}
- aria-describedby={{if this.error @field.errorId}}
- ...attributes
- as |components|
- >
- {{yield components}}
- </@component>
-
- {{#if @field.helpText}}
- <FKText
- class="form-kit__container-help-text"
- >{{@field.helpText}}</FKText>
- {{/if}}
-
- <FKMeta
- @field={{@field}}
- @error={{this.error}}
- @controlWidth={{this.controlWidth}}
- />
- </div>
- </div>
- </template>
-}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/base.gjs b/frontend/discourse/app/form-kit/components/fk/control/base.gjs
new file mode 100644
index 00000000000..eb4e751d4c1
--- /dev/null
+++ b/frontend/discourse/app/form-kit/components/fk/control/base.gjs
@@ -0,0 +1,9 @@
+import Component from "@glimmer/component";
+
+export default class FKBaseControl extends Component {
+ constructor(owner, args) {
+ super(owner, args);
+
+ args.field.type = this.constructor.controlType;
+ }
+}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/calendar.gjs b/frontend/discourse/app/form-kit/components/fk/control/calendar.gjs
index 3e71e9ce304..4d6f942afec 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/calendar.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/calendar.gjs
@@ -1,11 +1,11 @@
-import Component from "@glimmer/component";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { service } from "@ember/service";
import DatePicker from "discourse/components/date-picker";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import withEventValue from "discourse/helpers/with-event-value";
-export default class FKControlCalendar extends Component {
+export default class FKControlCalendar extends FKBaseControl {
static controlType = "calendar";
@service site;
@@ -78,6 +78,9 @@ export default class FKControlCalendar extends Component {
@minDate={{this.minDate}}
@maxDate={{this.maxDate}}
id={{@field.id}}
+ name={{@field.name}}
+ aria-invalid={{if @field.error "true"}}
+ aria-describedby={{if @field.error @field.errorId}}
class="form-kit__control-calendar"
/>
<div id={{this.containerId}} class="date-picker-container"></div>
@@ -89,6 +92,9 @@ export default class FKControlCalendar extends Component {
class="form-kit__control-input form-kit__control-date"
type="date"
value={{this.date}}
+ id={{@field.id}}
+ name={{@field.name}}
+ aria-describedby={{if @field.error @field.errorId}}
{{on "change" (withEventValue this.setDate)}}
/>
{{/if}}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/checkbox.gjs b/frontend/discourse/app/form-kit/components/fk/control/checkbox.gjs
index a9e0f99ee23..b37e8a265fc 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/checkbox.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/checkbox.gjs
@@ -1,12 +1,12 @@
-import Component from "@glimmer/component";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import FKLabel from "discourse/form-kit/components/fk/label";
import FKRequired from "discourse/form-kit/components/fk/required";
import FKTooltip from "discourse/form-kit/components/fk/tooltip";
import { eq, or } from "discourse/truth-helpers";
-export default class FKControlCheckbox extends Component {
+export default class FKControlCheckbox extends FKBaseControl {
static controlType = "checkbox";
@action
@@ -21,6 +21,10 @@ export default class FKControlCheckbox extends Component {
checked={{or (eq @field.value true) (eq @field.value "true")}}
class="form-kit__control-checkbox"
disabled={{@field.disabled}}
+ id={{@field.id}}
+ name={{@field.name}}
+ aria-invalid={{if @field.error "true"}}
+ aria-describedby={{if @field.error @field.errorId}}
...attributes
{{on "change" this.handleInput}}
/>
@@ -33,7 +37,7 @@ export default class FKControlCheckbox extends Component {
{{/if}}
<FKTooltip @field={{@field}} />
</span>
- {{#if @hasBlock}}
+ {{#if (has-block)}}
<span class="form-kit__control-checkbox-description">{{yield}}</span>
{{/if}}
</span>
diff --git a/frontend/discourse/app/form-kit/components/fk/control/code.gjs b/frontend/discourse/app/form-kit/components/fk/control/code.gjs
index bf4aea9f8dc..6eb26097d28 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/code.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/code.gjs
@@ -1,10 +1,10 @@
-import Component from "@glimmer/component";
import { action } from "@ember/object";
import { htmlSafe } from "@ember/template";
import AceEditor from "discourse/components/ace-editor";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import { escapeExpression } from "discourse/lib/utilities";
-export default class FKControlCode extends Component {
+export default class FKControlCode extends FKBaseControl {
static controlType = "code";
initialValue = this.args.field.value || "";
@@ -31,6 +31,10 @@ export default class FKControlCode extends Component {
@resizable={{true}}
class="form-kit__control-code"
style={{this.style}}
+ id={{@field.id}}
+ name={{@field.name}}
+ aria-invalid={{if @field.error "true"}}
+ aria-describedby={{if @field.error @field.errorId}}
...attributes
/>
</template>
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 50ec60585fa..26a06814167 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/color.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/color.gjs
@@ -1,10 +1,10 @@
-import Component from "@glimmer/component";
import { fn } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { service } from "@ember/service";
import { htmlSafe } from "@ember/template";
import DMenu from "discourse/float-kit/components/d-menu";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import concatClass from "discourse/helpers/concat-class";
import icon from "discourse/helpers/d-icon";
import {
@@ -60,7 +60,7 @@ function colorLuminanceClass(color) {
return calculateLuminance(color) > 0.5 ? "--is-light" : "--is-dark";
}
-export default class FKControlColor extends Component {
+export default class FKControlColor extends FKBaseControl {
static controlType = "color";
@service currentUser;
@@ -174,6 +174,10 @@ export default class FKControlColor extends Component {
maxlength={{this.maxLength}}
class="form-kit__control-color-input-hex"
disabled={{@field.disabled}}
+ id={{@field.id}}
+ name={{@field.name}}
+ aria-invalid={{if @field.error "true"}}
+ aria-describedby={{if @field.error @field.errorId}}
{{on "input" this.handleTextInput}}
{{on "blur" this.handleBlur}}
{{on "paste" this.handlePaste}}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/composer.gjs b/frontend/discourse/app/form-kit/components/fk/control/composer.gjs
index cb1a766e4ad..494d9145562 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/composer.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/composer.gjs
@@ -1,11 +1,11 @@
-import Component from "@glimmer/component";
import { action } from "@ember/object";
import { htmlSafe } from "@ember/template";
import DEditor from "discourse/components/d-editor";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import concatClass from "discourse/helpers/concat-class";
import { escapeExpression } from "discourse/lib/utilities";
-export default class FKControlComposer extends Component {
+export default class FKControlComposer extends FKBaseControl {
static controlType = "composer";
@action
diff --git a/frontend/discourse/app/form-kit/components/fk/control/custom.gjs b/frontend/discourse/app/form-kit/components/fk/control/custom.gjs
index 9b73cda758f..5c5ad9d4b92 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/custom.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/custom.gjs
@@ -1,6 +1,6 @@
-import Component from "@glimmer/component";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
-export default class FKControlCustom extends Component {
+export default class FKControlCustom extends FKBaseControl {
static controlType = "custom";
<template>
diff --git a/frontend/discourse/app/form-kit/components/fk/control/emoji.gjs b/frontend/discourse/app/form-kit/components/fk/control/emoji.gjs
index 723ec1329e3..d96cb291160 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/emoji.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/emoji.gjs
@@ -1,8 +1,8 @@
-import Component from "@glimmer/component";
import { action } from "@ember/object";
import EmojiPicker from "discourse/components/emoji-picker";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
-export default class FKControlEmoji extends Component {
+export default class FKControlEmoji extends FKBaseControl {
static controlType = "emoji";
@action
diff --git a/frontend/discourse/app/form-kit/components/fk/control/icon.gjs b/frontend/discourse/app/form-kit/components/fk/control/icon.gjs
index 37a1f8d2953..41cb7d8fa8a 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/icon.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/icon.gjs
@@ -1,9 +1,9 @@
-import Component from "@glimmer/component";
import { hash } from "@ember/helper";
import { action } from "@ember/object";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import IconPicker from "discourse/select-kit/components/icon-picker";
-export default class FKControlIcon extends Component {
+export default class FKControlIcon extends FKBaseControl {
static controlType = "icon";
@action
diff --git a/frontend/discourse/app/form-kit/components/fk/control/image.gjs b/frontend/discourse/app/form-kit/components/fk/control/image.gjs
index 4f378c731e8..86f5d7ad801 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/image.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/image.gjs
@@ -1,9 +1,9 @@
-import Component from "@glimmer/component";
import { action } from "@ember/object";
import { isBlank } from "@ember/utils";
import UppyImageUploader from "discourse/components/uppy-image-uploader";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
-export default class FKControlImage extends Component {
+export default class FKControlImage extends FKBaseControl {
static controlType = "image";
@action
@@ -28,7 +28,7 @@ export default class FKControlImage extends Component {
@onUploadDeleted={{this.removeImage}}
@type={{@type}}
@disabled={{@field.disabled}}
- @placeholderUrl={{@field.args.placeholderUrl}}
+ @placeholderUrl={{@placeholderUrl}}
class="form-kit__control-image no-repeat contain-image"
/>
</template>
diff --git a/frontend/discourse/app/form-kit/components/fk/control/input.gjs b/frontend/discourse/app/form-kit/components/fk/control/input.gjs
index 41f2364a8a6..d0e3a858e0f 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/input.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/input.gjs
@@ -1,6 +1,6 @@
-import Component from "@glimmer/component";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import concatClass from "discourse/helpers/concat-class";
const SUPPORTED_TYPES = [
@@ -21,11 +21,11 @@ const SUPPORTED_TYPES = [
"week",
];
-export default class FKControlInput extends Component {
+export default class FKControlInput extends FKBaseControl {
static controlType = "input";
constructor(owner, args) {
- super(...arguments);
+ super(owner, args);
if (["checkbox", "radio"].includes(args.type)) {
throw new Error(
@@ -40,6 +40,8 @@ export default class FKControlInput extends Component {
}", must be one of ${SUPPORTED_TYPES.join(", ")}!`
);
}
+
+ args.field.type = "input-" + (args.type ?? "text");
}
get type() {
@@ -99,6 +101,10 @@ export default class FKControlInput extends Component {
(if @after "has-suffix")
}}
disabled={{@field.disabled}}
+ id={{@field.id}}
+ name={{@field.name}}
+ aria-invalid={{if @field.error "true"}}
+ aria-describedby={{if @field.error @field.errorId}}
...attributes
{{on "focus" this.handleFocus}}
{{on "blur" this.handleBlur}}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/menu.gjs b/frontend/discourse/app/form-kit/components/fk/control/menu.gjs
index 804bc5ff65c..6bcbb12df5f 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/menu.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/menu.gjs
@@ -1,15 +1,15 @@
-import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { hash } from "@ember/helper";
import { action } from "@ember/object";
import DropdownMenu from "discourse/components/dropdown-menu";
import DMenu from "discourse/float-kit/components/d-menu";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import FKControlMenuContainer from "discourse/form-kit/components/fk/control/menu/container";
import FKControlMenuDivider from "discourse/form-kit/components/fk/control/menu/divider";
import FKControlMenuItem from "discourse/form-kit/components/fk/control/menu/item";
import icon from "discourse/helpers/d-icon";
-export default class FKControlMenu extends Component {
+export default class FKControlMenu extends FKBaseControl {
static controlType = "menu";
@tracked menuApi;
diff --git a/frontend/discourse/app/form-kit/components/fk/control/password.gjs b/frontend/discourse/app/form-kit/components/fk/control/password.gjs
index 981346c2a5c..0ee22f54c64 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/password.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/password.gjs
@@ -1,9 +1,9 @@
-import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { modifier as modifierFn } from "ember-modifier";
import DButton from "discourse/components/d-button";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import concatClass from "discourse/helpers/concat-class";
import { eq } from "discourse/truth-helpers";
@@ -12,7 +12,7 @@ const TYPES = {
password: "password",
};
-export default class FKControlInput extends Component {
+export default class FKControlPassword extends FKBaseControl {
static controlType = "password";
@tracked type = TYPES.password;
@@ -62,6 +62,10 @@ export default class FKControlInput extends Component {
value={{@field.value}}
class="form-kit__control-password"
disabled={{@field.disabled}}
+ id={{@field.id}}
+ name={{@field.name}}
+ aria-invalid={{if @field.error "true"}}
+ aria-describedby={{if @field.error @field.errorId}}
...attributes
{{on "input" this.handleInput}}
{{this.focusState}}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/question.gjs b/frontend/discourse/app/form-kit/components/fk/control/question.gjs
index bba0a6e805b..5e1db060879 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/question.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/question.gjs
@@ -1,12 +1,12 @@
-import Component from "@glimmer/component";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import FKLabel from "discourse/form-kit/components/fk/label";
import uniqueId from "discourse/helpers/unique-id";
import { eq } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
-export default class FKControlQuestion extends Component {
+export default class FKControlQuestion extends FKBaseControl {
static controlType = "question";
@action
@@ -25,6 +25,7 @@ export default class FKControlQuestion extends Component {
checked={{eq @field.value true}}
class="form-kit__control-radio"
disabled={{@field.disabled}}
+ aria-describedby={{if @field.error @field.errorId}}
...attributes
id={{uuid}}
{{on "change" this.handleInput}}
@@ -47,6 +48,7 @@ export default class FKControlQuestion extends Component {
checked={{eq @field.value false}}
class="form-kit__control-radio"
disabled={{@field.disabled}}
+ aria-describedby={{if @field.error @field.errorId}}
...attributes
id={{uuid}}
{{on "change" this.handleInput}}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/radio-group.gjs b/frontend/discourse/app/form-kit/components/fk/control/radio-group.gjs
index 4da05f273ab..11b287725a4 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/radio-group.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/radio-group.gjs
@@ -1,16 +1,20 @@
-import Component from "@glimmer/component";
import { hash } from "@ember/helper";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import FKFieldset from "discourse/form-kit/components/fk/fieldset";
import FKControlRadioGroupRadio from "./radio-group/radio";
-export default class FKControlRadioGroup extends Component {
+export default class FKControlRadioGroup extends FKBaseControl {
static controlType = "radio-group";
<template>
<FKFieldset
class="form-kit__control-radio-group"
@title={{@title}}
- @subtitle={{@subtitle}}
+ @description={{@description}}
+ id={{@field.id}}
+ name={{@field.name}}
+ aria-invalid={{if @field.error "true"}}
+ aria-describedby={{if @field.error @field.errorId}}
...attributes
>
{{yield
diff --git a/frontend/discourse/app/form-kit/components/fk/control/select.gjs b/frontend/discourse/app/form-kit/components/fk/control/select.gjs
index fc1292df744..3f044122268 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/select.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/select.gjs
@@ -1,7 +1,7 @@
-import Component from "@glimmer/component";
import { hash } from "@ember/helper";
import { isBlank } from "@ember/utils";
import DSelect, { DSelectOption } from "discourse/components/d-select";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
const SelectOption = <template>
<DSelectOption
@@ -13,7 +13,7 @@ const SelectOption = <template>
</DSelectOption>
</template>;
-export default class FKControlSelect extends Component {
+export default class FKControlSelect extends FKBaseControl {
static controlType = "select";
get includeNone() {
@@ -33,6 +33,10 @@ export default class FKControlSelect extends Component {
@value={{@field.value}}
@onChange={{@field.set}}
@includeNone={{this.includeNone}}
+ id={{@field.id}}
+ name={{@field.name}}
+ aria-invalid={{if @field.error "true"}}
+ aria-describedby={{if @field.error @field.errorId}}
...attributes
>
{{yield (hash Option=(component SelectOption [email protected]))}}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/tag-chooser.gjs b/frontend/discourse/app/form-kit/components/fk/control/tag-chooser.gjs
index 04f47993a97..c9315cc8dce 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/tag-chooser.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/tag-chooser.gjs
@@ -1,9 +1,9 @@
-import Component from "@glimmer/component";
import { hash } from "@ember/helper";
import { action } from "@ember/object";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import TagChooser from "discourse/select-kit/components/tag-chooser";
-export default class FKControlTagChooser extends Component {
+export default class FKControlTagChooser extends FKBaseControl {
static controlType = "tag-chooser";
@action
diff --git a/frontend/discourse/app/form-kit/components/fk/control/textarea.gjs b/frontend/discourse/app/form-kit/components/fk/control/textarea.gjs
index e4e8ea70ae4..880e1f416c3 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/textarea.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/textarea.gjs
@@ -1,16 +1,16 @@
-import Component from "@glimmer/component";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { htmlSafe } from "@ember/template";
import { modifier as modifierFn } from "ember-modifier";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
import { escapeExpression } from "discourse/lib/utilities";
-export default class FKControlTextarea extends Component {
+export default class FKControlTextarea extends FKBaseControl {
static controlType = "textarea";
resizeObserver = modifierFn((element) => {
const observer = new ResizeObserver(() => {
- this.args.onControlWidthChange?.(element.offsetWidth);
+ this.args.field.controlWidth = element.offsetWidth;
});
observer.observe(element);
@@ -59,6 +59,10 @@ export default class FKControlTextarea extends Component {
style={{this.style}}
disabled={{@field.disabled}}
value={{@field.value}}
+ id={{@field.id}}
+ name={{@field.name}}
+ aria-invalid={{if @field.error "true"}}
+ aria-describedby={{if @field.error @field.errorId}}
...attributes
{{this.resizeObserver}}
{{on "input" this.handleInput}}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/toggle.gjs b/frontend/discourse/app/form-kit/components/fk/control/toggle.gjs
index 7c84e3f5f82..74a1ca3914b 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/toggle.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/toggle.gjs
@@ -1,9 +1,9 @@
-import Component from "@glimmer/component";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import DToggleSwitch from "discourse/components/d-toggle-switch";
+import FKBaseControl from "discourse/form-kit/components/fk/control/base";
-export default class FKControlToggle extends Component {
+export default class FKControlToggle extends FKBaseControl {
static controlType = "toggle";
@action
diff --git a/frontend/discourse/app/form-kit/components/fk/field-data.gjs b/frontend/discourse/app/form-kit/components/fk/field-data.gjs
index e42c4dd7052..7a56c4d9ffc 100644
--- a/frontend/discourse/app/form-kit/components/fk/field-data.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/field-data.gjs
@@ -1,4 +1,5 @@
import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import ValidationParser from "discourse/form-kit/lib/validation-parser";
import Validator from "discourse/form-kit/lib/validator";
@@ -8,6 +9,9 @@ import uniqueId from "discourse/helpers/unique-id";
* Represents a field in a form with validation, registration, and field data management capabilities.
*/
export default class FKFieldData extends Component {
+ @tracked type;
+ @tracked controlWidth = "auto";
+
/**
* Unique identifier for the field.
* @type {string}
@@ -95,7 +99,7 @@ export default class FKFieldData extends Component {
* @type {string}
*/
get titleFormat() {
- return this.args.titleFormat;
+ return this.args.titleFormat || this.format;
}
/**
@@ -103,7 +107,7 @@ export default class FKFieldData extends Component {
* @type {string}
*/
get descriptionFormat() {
- return this.args.descriptionFormat;
+ return this.args.descriptionFormat || this.format;
}
/**
@@ -139,6 +143,10 @@ export default class FKFieldData extends Component {
return this.args.helpText;
}
+ get error() {
+ return (this.args.errors ?? {})[this.name];
+ }
+
/**
* Indicates whether to show the field's title.
* Defaults to `true`.
@@ -179,6 +187,10 @@ export default class FKFieldData extends Component {
return name;
}
+ get normalizedName() {
+ return this.name.replace(/\./g, "-");
+ }
+
/**
* Validation rules for the field.
* @type {string|Object}
diff --git a/frontend/discourse/app/form-kit/components/fk/field.gjs b/frontend/discourse/app/form-kit/components/fk/field.gjs
index da0c2ad2a07..5264928566a 100644
--- a/frontend/discourse/app/form-kit/components/fk/field.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/field.gjs
@@ -1,8 +1,11 @@
import Component from "@glimmer/component";
-import { hash } from "@ember/helper";
+import { concat, fn, hash } from "@ember/helper";
import { action } from "@ember/object";
import { getOwner } from "@ember/owner";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
+import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";
import curryComponent from "ember-curry-component";
+import { modifier as modifierFn } from "ember-modifier";
import FKControlCalendar from "discourse/form-kit/components/fk/control/calendar";
import FKControlCheckbox from "discourse/form-kit/components/fk/control/checkbox";
import FKControlCode from "discourse/form-kit/components/fk/control/code";
@@ -21,9 +24,15 @@ import FKControlSelect from "discourse/form-kit/components/fk/control/select";
import FKControlTagChooser from "discourse/form-kit/components/fk/control/tag-chooser";
import FKControlTextarea from "discourse/form-kit/components/fk/control/textarea";
import FKControlToggle from "discourse/form-kit/components/fk/control/toggle";
-import FKControlWrapper from "discourse/form-kit/components/fk/control-wrapper";
import FKFieldData from "discourse/form-kit/components/fk/field-data";
+import FKLabel from "discourse/form-kit/components/fk/label";
+import FKMeta from "discourse/form-kit/components/fk/meta";
+import FKOptional from "discourse/form-kit/components/fk/optional";
import FKRow from "discourse/form-kit/components/fk/row";
+import FKText from "discourse/form-kit/components/fk/text";
+import FKTooltip from "discourse/form-kit/components/fk/tooltip";
+import concatClass from "discourse/helpers/concat-class";
+import { eq } from "discourse/truth-helpers";
const RowColWrapper = <template>
<FKRow as |row|>
@@ -39,6 +48,15 @@ const EmptyWrapper = <template>
</template>;
export default class FKField extends Component {
+ applyControlType = modifierFn((element, [field]) => {
+ const type = field.type;
+
+ if (type) {
+ element.dataset.controlType = type;
+ element.classList.add(`form-kit__field-${type}`);
+ }
+ });
+
get wrapper() {
if (this.args.size) {
return RowColWrapper;
@@ -49,80 +67,114 @@ export default class FKField extends Component {
@action
componentFor(component, field) {
- const instance = this;
- const baseArguments = {
- get errors() {
- return instance.args.errors;
- },
- unregisterField: instance.args.unregisterField,
- registerField: instance.args.registerField,
- component,
- field,
- };
-
if (!component.controlType) {
throw new Error(
`Static property \`controlType\` is required on component:\n\n ${component}`
);
}
- return curryComponent(FKControlWrapper, baseArguments, getOwner(this));
+ return curryComponent(component, { field }, getOwner(this));
}
<template>
- <FKFieldData
- @name={{@name}}
- @data={{@data}}
- @triggerRevalidationFor={{@triggerRevalidationFor}}
- @title={{@title}}
- @tooltip={{@tooltip}}
- @description={{@description}}
- @helpText={{@helpText}}
- @showTitle={{@showTitle}}
- @collectionIndex={{@collectionIndex}}
- @set={{@set}}
- @addError={{@addError}}
- @validate={{@validate}}
- @validation={{@validation}}
- @onSet={{@onSet}}
- @registerField={{@registerField}}
- @format={{@format}}
- @titleFormat={{@titleFormat}}
- @descriptionFormat={{@descriptionFormat}}
- @disabled={{@disabled}}
- @parentName={{@parentName}}
- @placeholderUrl={{@placeholderUrl}}
- as |field|
- >
- <this.wrapper @size={{@size}}>
- {{yield
- (hash
- Custom=(this.componentFor FKControlCustom field)
- Code=(this.componentFor FKControlCode field)
- Question=(this.componentFor FKControlQuestion field)
- Textarea=(this.componentFor FKControlTextarea field)
- Checkbox=(this.componentFor FKControlCheckbox field)
- Color=(this.componentFor FKControlColor field)
- Image=(this.componentFor FKControlImage field)
- Password=(this.componentFor FKControlPassword field)
- Composer=(this.componentFor FKControlComposer field)
- Icon=(this.componentFor FKControlIcon field)
- Emoji=(this.componentFor FKControlEmoji field)
- Toggle=(this.componentFor FKControlToggle field)
- Menu=(this.componentFor FKControlMenu field)
- Select=(this.componentFor FKControlSelect field)
- TagChooser=(this.componentFor FKControlTagChooser field)
- Input=(this.componentFor FKControlInput field)
- RadioGroup=(this.componentFor FKControlRadioGroup field)
- Calendar=(this.componentFor FKControlCalendar field)
- errorId=field.errorId
- id=field.id
- name=field.name
- set=field.set
- value=field.value
- )
- }}
- </this.wrapper>
- </FKFieldData>
+ {{#let (curryComponent FKFieldData this.args) as |FieldData|}}
+ <FieldData as |field|>
+ <this.wrapper @size={{@size}}>
+ {{#if (has-block-params)}}
+ <div
+ id={{concat "control-" field.normalizedName}}
+ class={{concatClass
+ "form-kit__container"
+ "form-kit__field"
+ (if field.error "has-error")
+ (if field.disabled "is-disabled")
+ (if (eq field.format "full") "--full")
+ }}
+ data-disabled={{field.disabled}}
+ data-name={{field.name}}
+ {{this.applyControlType field}}
+ {{didInsert (fn @registerField field.name field)}}
+ {{willDestroy (fn @unregisterField field.name)}}
+ >
+ {{#if field.showTitle}}
+ <FKLabel
+ class={{concatClass
+ "form-kit__container-title"
+ (if field.titleFormat (concat "--" field.titleFormat))
+ }}
+ @fieldId={{field.id}}
+ >
+ <span>{{field.title}}</span>
+
+ <FKOptional @field={{field}} />
+ <FKTooltip @field={{field}} />
+ </FKLabel>
+ {{/if}}
+
+ {{#if field.description}}
+ <FKText
+ class={{concatClass
+ "form-kit__container-description"
+ (if
+ field.descriptionFormat
+ (concat "--" field.descriptionFormat)
+ )
+ }}
+ >{{field.description}}</FKText>
+ {{/if}}
+
+ <div
+ class={{concatClass
+ "form-kit__container-content"
+ (if field.format (concat "--" field.format))
+ }}
+ >
+ {{yield
+ (hash
+ Custom=(this.componentFor FKControlCustom field)
+ Code=(this.componentFor FKControlCode field)
+ Question=(this.componentFor FKControlQuestion field)
+ Textarea=(this.componentFor FKControlTextarea field)
+ Checkbox=(this.componentFor FKControlCheckbox field)
+ Color=(this.componentFor FKControlColor field)
+ Image=(this.componentFor FKControlImage field)
+ Password=(this.componentFor FKControlPassword field)
+ Composer=(this.componentFor FKControlComposer field)
+ Icon=(this.componentFor FKControlIcon field)
+ Emoji=(this.componentFor FKControlEmoji field)
+ Toggle=(this.componentFor FKControlToggle field)
+ Menu=(this.componentFor FKControlMenu field)
+ Select=(this.componentFor FKControlSelect field)
+ TagChooser=(this.componentFor FKControlTagChooser field)
+ Input=(this.componentFor FKControlInput field)
+ RadioGroup=(this.componentFor FKControlRadioGroup field)
+ Calendar=(this.componentFor FKControlCalendar field)
+ errorId=field.errorId
+ id=field.id
+ name=field.name
+ set=field.set
+ value=field.value
+ )
+ }}
+
+ {{#if field.helpText}}
+ <FKText
+ class="form-kit__container-help-text"
+ >{{field.helpText}}</FKText>
+ {{/if}}
+
+ <FKMeta
+ @field={{field}}
+ @error={{field.error}}
+ @controlWidth={{field.controlWidth}}
+ />
+ </div>
+ </div>
+ {{else}}
+ {{yield}}
+ {{/if}}
+ </this.wrapper>
+ </FieldData>
+ {{/let}}
</template>
}
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/code-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/code-test.gjs
index 4944d752257..6aa468475fb 100644
--- a/frontend/discourse/tests/integration/components/form-kit/controls/code-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/code-test.gjs
@@ -31,6 +31,43 @@ module("Integration | Component | FormKit | Controls | Code", function (hooks) {
assert.form().field("foo").hasValue("bar");
});
+ test("@height", async function (assert) {
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.Code @height={{100}} />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert.strictEqual(
+ document.querySelector(".form-kit__control-code").style.height,
+ "100px"
+ );
+ });
+
+ test("@lang", async function (assert) {
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.Code @lang="sql" />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert.strictEqual(
+ document
+ .querySelector(".form-kit__control-code")
+ .aceEditor.getSession()
+ .getMode().$id,
+ "ace/mode/sql"
+ );
+ });
+
test("when disabled", async function (assert) {
await render(
<template>
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 a2225f26727..600f59ed27b 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 { blur, fillIn, focus, render } from "@ember/test-helpers";
+import { blur, click, 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";
@@ -208,5 +208,36 @@ module(
assert.strictEqual(formKit().field("color").value(), "AABBCC");
});
+
+ test("@collapseSwatches and @collapseSwatchesLabel", async function (assert) {
+ const colors = ["FF0000", "00FF00", "0000FF"];
+ let data = { color: null };
+ 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
+ @colors={{colors}}
+ @collapseSwatches={{true}}
+ @collapseSwatchesLabel="Pick a preset"
+ />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert
+ .dom(".form-kit__control-color-swatches-btn")
+ .hasAttribute("title", "Pick a preset");
+ assert.dom(".form-kit__control-color-swatch").doesNotExist();
+
+ await click(".form-kit__control-color-swatches-btn");
+ await click('.form-kit__control-color-swatch[data-color="00FF00"]');
+ await formKit().submit();
+
+ assert.strictEqual(data.color, "00FF00");
+ });
}
);
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/emoji-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/emoji-test.gjs
new file mode 100644
index 00000000000..fe58c813d1a
--- /dev/null
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/emoji-test.gjs
@@ -0,0 +1,45 @@
+import { click, render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import Form from "discourse/components/form";
+import emojisFixtures from "discourse/tests/fixtures/emojis-fixtures";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
+import emojiPicker from "discourse/tests/helpers/emoji-picker-helper";
+import formKit from "discourse/tests/helpers/form-kit-helper";
+
+module(
+ "Integration | Component | FormKit | Controls | Emoji",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ pretender.get("/emojis.json", () =>
+ response(emojisFixtures["/emojis.json"])
+ );
+ pretender.get("/emojis/search-aliases.json", () => response([]));
+
+ this.emojiStore = this.container.lookup("service:emoji-store");
+ });
+
+ test("default", async function (assert) {
+ let data = { foo: null };
+ const mutateData = (x) => (data = x);
+
+ await render(
+ <template>
+ <Form @data={{data}} @onSubmit={{mutateData}} as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.Emoji />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ await click(".btn-emoji");
+ await emojiPicker(".emoji-picker").select("grinning");
+ await formKit().submit();
+
+ assert.strictEqual(data.foo, "grinning");
+ });
+ }
+);
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/image-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/image-test.gjs
index f4f20514a32..1a8717b7458 100644
--- a/frontend/discourse/tests/integration/components/form-kit/controls/image-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/image-test.gjs
@@ -97,5 +97,22 @@ module(
assert.dom(".file-uploader__controls input[type='file']").isDisabled();
assert.dom(".file-uploader__controls .btn-danger").isDisabled();
});
+
+ test("@placeholderUrl", async function (assert) {
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="image_url" @title="Foo" as |field|>
+ <field.Image
+ @type="site_setting"
+ @placeholderUrl="/images/avatar.png"
+ />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert.dom(".placeholder-overlay").exists();
+ });
}
);
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/input-text-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/input-text-test.gjs
index 3ae55144227..302eacc3ba0 100644
--- a/frontend/discourse/tests/integration/components/form-kit/controls/input-text-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/input-text-test.gjs
@@ -67,5 +67,22 @@ module(
assert.deepEqual(data.foo, null, "it nullifies the value");
});
+
+ test("@before and @after", async function (assert) {
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.Input @before="https://" @after=".com" />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert.dom(".form-kit__before-input").hasText("https://");
+ assert.dom(".form-kit__after-input").hasText(".com");
+ assert.dom(".form-kit__control-input").hasClass("has-prefix");
+ assert.dom(".form-kit__control-input").hasClass("has-suffix");
+ });
}
);
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/menu-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/menu-test.gjs
index 272d7472362..bf88e05b59c 100644
--- a/frontend/discourse/tests/integration/components/form-kit/controls/menu-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/menu-test.gjs
@@ -48,4 +48,22 @@ module("Integration | Component | FormKit | Controls | Menu", function (hooks) {
assert.dom(".form-kit__control-menu-trigger").hasAttribute("disabled");
});
+
+ test("@selection", async function (assert) {
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.Menu @selection="Current item" as |menu|>
+ <menu.Item @value="item-1">Item 1</menu.Item>
+ </field.Menu>
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert
+ .dom(".form-kit__control-menu-trigger .d-button-label")
+ .hasText("Current item");
+ });
});
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/radio-group-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/radio-group-test.gjs
index 697d6b3aa47..1b94324687d 100644
--- a/frontend/discourse/tests/integration/components/form-kit/controls/radio-group-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/radio-group-test.gjs
@@ -37,5 +37,26 @@ module(
assert.deepEqual(data.foo, "two");
});
+
+ test("@title and @description", async function (assert) {
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.RadioGroup
+ @title="Pick one"
+ @description="Choose carefully"
+ as |RadioGroup|
+ >
+ <RadioGroup.Radio @value="one">One</RadioGroup.Radio>
+ </field.RadioGroup>
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert.dom(".form-kit__fieldset-title").hasText("Pick one");
+ assert.dom(".form-kit__fieldset-description").hasText("Choose carefully");
+ });
}
);
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/tag-chooser-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/tag-chooser-test.gjs
index f024f988af6..fa5d7df0deb 100644
--- a/frontend/discourse/tests/integration/components/form-kit/controls/tag-chooser-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/tag-chooser-test.gjs
@@ -2,8 +2,10 @@ import { render } from "@ember/test-helpers";
import { module, test } from "qunit";
import Form from "discourse/components/form";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
import formKit from "discourse/tests/helpers/form-kit-helper";
import selectKit from "discourse/tests/helpers/select-kit-helper";
+import { i18n } from "discourse-i18n";
module(
"Integration | Component | FormKit | Controls | TagChooser",
@@ -48,5 +50,123 @@ module(
assert.dom(".form-kit__control-tag-chooser.is-disabled").exists();
});
+
+ test("passes search props through", async function (assert) {
+ let queryParams;
+
+ pretender.get("/tags/filter/search", (request) => {
+ queryParams = request.queryParams;
+
+ return response({
+ results: [{ id: 1, name: "monkey", slug: "monkey", count: 1 }],
+ });
+ });
+
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.TagChooser
+ @showAllTags={{true}}
+ @excludeSynonyms={{true}}
+ @excludeTagsWithSynonyms={{true}}
+ @categoryId={{123}}
+ />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ await selectKit(".form-kit__control-tag-chooser").expand();
+
+ assert.strictEqual(queryParams.categoryId, "123");
+ assert.strictEqual(queryParams.excludeSynonyms, "true");
+ assert.strictEqual(queryParams.excludeHasSynonyms, "true");
+ assert.strictEqual(queryParams.filterForInput, undefined);
+ });
+
+ test("@placeholder", async function (assert) {
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.TagChooser @placeholder="groups.selector_placeholder" />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ await selectKit(".form-kit__control-tag-chooser").expand();
+
+ assert
+ .dom(".form-kit__control-tag-chooser .filter-input")
+ .hasAttribute("placeholder", i18n("groups.selector_placeholder"));
+ });
+
+ test("@allowCreate", async function (assert) {
+ this.site.can_create_tag = true;
+ const chooser = selectKit(".form-kit__control-tag-chooser");
+
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.TagChooser />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ await chooser.expand();
+ await chooser.fillInFilter("brandnew");
+
+ assert
+ .dom(".select-kit-row[data-value='brandnew']")
+ .doesNotExist("it does not allow creating tags by default");
+
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.TagChooser @allowCreate={{true}} />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ await chooser.expand();
+ await chooser.fillInFilter("brandnew");
+
+ assert
+ .dom(".select-kit-row[data-value='brandnew']")
+ .exists("it allows creating tags when enabled");
+ });
+
+ test("@unlimited", async function (assert) {
+ this.siteSettings.max_tags_per_topic = 1;
+ let data = { foo: [{ id: 1, name: "monkey" }] };
+ const mutateData = (x) => (data = x);
+
+ await render(
+ <template>
+ <Form @data={{data}} @onSubmit={{mutateData}} as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.TagChooser @unlimited={{true}} />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ const chooser = selectKit(".form-kit__control-tag-chooser");
+ await chooser.expand();
+ await chooser.selectRowByName("gazelle");
+ await formKit().submit();
+
+ assert.deepEqual(
+ data.foo.map((tag) => tag.name),
+ ["monkey", "gazelle"]
+ );
+ assert.dom(".select-kit-error").doesNotExist();
+ });
}
);
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/textarea-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/textarea-test.gjs
index ae916f2fdf1..961036b6cc8 100644
--- a/frontend/discourse/tests/integration/components/form-kit/controls/textarea-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/textarea-test.gjs
@@ -116,5 +116,21 @@ module(
assert.deepEqual(data, { foo: "bar" });
});
+
+ test("@height", async function (assert) {
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field @name="foo" @title="Foo" as |field|>
+ <field.Textarea @height={{42}} />
+ </form.Field>
+ </Form>
+ </template>
+ );
+
+ assert
+ .dom(".form-kit__control-textarea")
+ .hasAttribute("style", "height: 42px");
+ });
}
);