A platform for community discussion. Free, open, simple.
DEV: Convert `SimpleList` to glimmer (#34490)
Jarek Radosz4e417223e5d5
diff --git a/app/assets/javascripts/admin/addon/components/admin-logo-form.gjs b/app/assets/javascripts/admin/addon/components/admin-logo-form.gjsindex ee0274b6fd6..10c2893b44b 100644--- a/app/assets/javascripts/admin/addon/components/admin-logo-form.gjs+++ b/app/assets/javascripts/admin/addon/components/admin-logo-form.gjs@@ -287,11 +287,11 @@ export default class AdminLogoForm extends Component {><field.Custom><SimpleList- @id={{field.id}}@onChange={{fn this.updateManifestScreenshots field}}@inputDelimiter="|"@values={{field.value}}@allowAny={{true}}+ id={{field.id}}/></field.Custom></form.Field>diff --git a/app/assets/javascripts/admin/addon/components/simple-list.gjs b/app/assets/javascripts/admin/addon/components/simple-list.gjsindex d9bdfd2a44c..e0a889c181d 100644--- a/app/assets/javascripts/admin/addon/components/simple-list.gjs+++ b/app/assets/javascripts/admin/addon/components/simple-list.gjs@@ -1,69 +1,69 @@-/* eslint-disable ember/no-classic-components */-import { arrayContentDidChange } from "@ember/-internals/metal";-import Component, { Input } from "@ember/component";+import Component from "@glimmer/component";+import { cached, tracked } from "@glimmer/tracking";import { fn, hash } from "@ember/helper";import { on } from "@ember/modifier";import { action } from "@ember/object";-import { empty } from "@ember/object/computed";-import { isEmpty } from "@ember/utils";-import { classNameBindings } from "@ember-decorators/component";-import { gt } from "truth-helpers";+import { TrackedArray } from "@ember-compat/tracked-built-ins";+import { gt, not } from "truth-helpers";import DButton from "discourse/components/d-button";-import discourseComputed from "discourse/lib/decorators";+import withEventValue from "discourse/helpers/with-event-value";import { i18n } from "discourse-i18n";import ComboBox from "select-kit/components/combo-box";-@classNameBindings(":simple-list", ":value-list")+// args: onChange, inputDelimiter, values, allowAny, choicesexport default class SimpleList extends Component {- @empty("newValue") inputEmpty;-- inputDelimiter = null;- newValue = "";- collection = null;- values = null;- choices = null;- allowAny = false;-- didReceiveAttrs() {- super.didReceiveAttrs(...arguments);- this.set("collection", this._splitValues(this.values, this.inputDelimiter));- this.set("isPredefinedList", !this.allowAny && !isEmpty(this.choices));+ @tracked newValue = "";++ @cached+ get collection() {+ return new TrackedArray(+ this.args.values+ ?.split(this.args.inputDelimiter || "\n")+ .filter(Boolean) || []+ );}+ get isPredefinedList() {+ return !this.args.allowAny && this.args.choices?.length > 0;+ }++ get validValues() {+ return this.args.choices?.filter((name) => !this.collection.includes(name));+ }++ @actionkeyDown(event) {- if (event.which === 13) {+ if (event.key === "Enter") {this.addValue(this.newValue);- return;}}@actionchangeValue(index, event) {- this.collection.replace(index, 1, [event.target.value]);- arrayContentDidChange(this.collection, index);- this._onChange();+ this.collection[index] = event.target.value;+ this.args.onChange?.(this.collection);}@action- addValue(newValue) {- if (!newValue) {+ addValue(value) {+ if (!value) {return;}- this.set("newValue", null);- this.collection.addObject(newValue);- this._onChange();+ this.newValue = null;+ this.collection.push(value);+ this.args.onChange?.(this.collection);}@action- removeValue(value) {- this.collection.removeObject(value);- this._onChange();+ removeAt(index) {+ this.collection.splice(index, 1);+ this.args.onChange?.(this.collection);}@action- shift(operation, index) {- let futureIndex = index + operation;+ shift(index, offset) {+ let futureIndex = index + offset;if (futureIndex > this.collection.length - 1) {futureIndex = 0;@@ -72,97 +72,83 @@ export default class SimpleList extends Component {}const shiftedValue = this.collection[index];- this.collection.removeAt(index);- this.collection.insertAt(futureIndex, shiftedValue);-- this._onChange();- }-- _onChange() {- this.onChange?.(this.collection);- }-- @discourseComputed("choices", "values")- validValues(choices, values) {- return choices.filter((name) => !values.includes(name));- }-- @discourseComputed("collection")- showUpDownButtons(collection) {- return collection.length - 1 ? true : false;- }+ this.collection.splice(index, 1);+ this.collection.splice(futureIndex, 0, shiftedValue);- _splitValues(values, delimiter) {- return values && values.length- ? values.split(delimiter || "\n").filter(Boolean)- : [];+ this.args.onChange?.(this.collection);}<template>- {{#if this.collection}}- <div class="values">- {{#each this.collection as |value index|}}- <div data-index={{index}} class="value">- <DButton- @action={{fn this.removeValue value}}- @icon="xmark"- class="btn-default remove-value-btn btn-small"- />-- <Input- title={{value}}- @value={{value}}- class="value-input"- {{on "focusout" (fn this.changeValue index)}}- />-- {{#if this.showUpDownButtons}}+ <div class="simple-list value-list" ...attributes>+ {{#if this.collection}}+ <div class="values">+ {{this.collection.length}}+ {{#each this.collection as |value index|}}+ <div data-index={{index}} class="value"><DButton- @action={{fn this.shift -1 index}}- @icon="arrow-up"- class="btn-default shift-up-value-btn btn-small"+ @action={{fn this.removeAt index}}+ @icon="xmark"+ class="btn-default remove-value-btn btn-small"/>- <DButton- @action={{fn this.shift 1 index}}- @icon="arrow-down"- class="btn-default shift-down-value-btn btn-small"++ <input+ {{on "focusout" (fn this.changeValue index)}}+ value={{value}}+ title={{value}}+ type="text"+ class="value-input"/>- {{/if}}- </div>- {{/each}}- </div>- {{/if}}-- <div class="simple-list-input">- {{#if this.isPredefinedList}}- {{#if (gt this.validValues.length 0)}}- <ComboBox- @content={{this.validValues}}- @value={{this.newValue}}- @onChange={{this.addValue}}- @valueProperty={{this.setting.computedValueProperty}}- @nameProperty={{this.setting.computedNameProperty}}- @options={{hash castInteger=true allowAny=false}}++ {{#if (gt this.collection.length 1)}}+ <DButton+ @action={{fn this.shift index -1}}+ @icon="arrow-up"+ class="btn-default shift-up-value-btn btn-small"+ />+ <DButton+ @action={{fn this.shift index 1}}+ @icon="arrow-down"+ class="btn-default shift-down-value-btn btn-small"+ />+ {{/if}}+ </div>+ {{/each}}+ </div>+ {{/if}}++ <div class="simple-list-input">+ {{#if this.isPredefinedList}}+ {{#if this.validValues}}+ <ComboBox+ @content={{this.validValues}}+ @value={{this.newValue}}+ @onChange={{this.addValue}}+ @valueProperty={{@setting.computedValueProperty}}+ @nameProperty={{@setting.computedNameProperty}}+ @options={{hash castInteger=true allowAny=false}}+ class="add-value-input"+ />+ {{/if}}+ {{else}}+ <input+ {{on "input" (withEventValue (fn (mut this.newValue)))}}+ {{on "keydown" this.keyDown}}+ value={{this.newValue}}+ type="text"+ placeholder={{i18n "admin.site_settings.simple_list.add_item"}}+ autocomplete="off"+ autocorrect="off"+ autocapitalize="off"class="add-value-input"/>+ <DButton+ @action={{fn this.addValue this.newValue}}+ @disabled={{not this.newValue}}+ @icon="plus"+ class="add-value-btn btn-small"+ />{{/if}}- {{else}}- <Input- @type="text"- @value={{this.newValue}}- placeholder={{i18n "admin.site_settings.simple_list.add_item"}}- class="add-value-input"- autocomplete="off"- autocorrect="off"- autocapitalize="off"- />- <DButton- @action={{fn this.addValue this.newValue}}- @disabled={{this.inputEmpty}}- @icon="plus"- class="add-value-btn btn-small"- />- {{/if}}+ </div></div></template>}diff --git a/app/assets/javascripts/discourse/tests/integration/components/simple-list-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/simple-list-test.gjsindex 6da38cc65ac..59bd471ff85 100644--- a/app/assets/javascripts/discourse/tests/integration/components/simple-list-test.gjs+++ b/app/assets/javascripts/discourse/tests/integration/components/simple-list-test.gjs@@ -1,8 +1,10 @@+import { tracked } from "@glimmer/tracking";import {blur,click,fillIn,render,+ settled,triggerKeyEvent,} from "@ember/test-helpers";import { module, test } from "qunit";@@ -13,15 +15,11 @@ module("Integration | Component | simple-list", function (hooks) {setupRenderingTest(hooks);test("adding a value", async function (assert) {- const self = this;+ const values = "vinkas\nosama";+ await render(<template><SimpleList @values={{values}} /></template>);- this.set("values", "vinkas\nosama");-- await render(<template><SimpleList @values={{self.values}} /></template>);-- assert- .dom(".add-value-btn[disabled]")- .exists("while loading the + button is disabled");+ assert.dom(".values .value").exists({ count: 2 });+ assert.dom(".add-value-btn").isDisabled("disabled when input is empty");await fillIn(".add-value-input", "penar");await click(".add-value-btn");@@ -39,27 +37,25 @@ module("Integration | Component | simple-list", function (hooks) {assert.dom(".values .value")- .exists({ count: 4 }, "adds the value when keying Enter");+ .exists({ count: 4 }, "adds the value when pressing Enter");});test("adding a value when list is predefined", async function (assert) {- const self = this;-- this.set("values", "vinkas\nosama");- this.set("choices", ["vinkas", "osama", "kris"]);+ const values = "vinkas\nosama";+ const choices = ["vinkas", "osama", "kris", "jeff"];await render(<template><SimpleList- @values={{self.values}}+ @values={{values}}@allowAny={{false}}- @choices={{self.choices}}+ @choices={{choices}}/></template>);await click(".add-value-input summary");- assert.dom(".select-kit-row").exists({ count: 1 });+ assert.dom(".select-kit-row").exists({ count: 2 });await click(".select-kit-row");assert@@ -68,19 +64,17 @@ module("Integration | Component | simple-list", function (hooks) {});test("changing a value", async function (assert) {- const self = this;-const done = assert.async();- this.set("values", "vinkas\nosama");- this.set("onChange", function (collection) {+ const values = "vinkas\nosama";+ const onChange = (collection) => {assert.deepEqual(collection, ["vinkas", "jarek"]);done();- });+ };await render(<template>- <SimpleList @values={{self.values}} @onChange={{self.onChange}} />+ <SimpleList @values={{values}} @onChange={{onChange}} /></template>);@@ -91,11 +85,8 @@ module("Integration | Component | simple-list", function (hooks) {});test("removing a value", async function (assert) {- const self = this;-- this.set("values", "vinkas\nosama");-- await render(<template><SimpleList @values={{self.values}} /></template>);+ const values = "vinkas\nosama";+ await render(<template><SimpleList @values={{values}} /></template>);await click(".values .value[data-index='0'] .remove-value-btn");@@ -109,13 +100,9 @@ module("Integration | Component | simple-list", function (hooks) {});test("delimiter support", async function (assert) {- const self = this;-- this.set("values", "vinkas|osama");-await render(<template>- <SimpleList @values={{self.values}} @inputDelimiter="|" />+ <SimpleList @values="vinkas|osama" @inputDelimiter="|" /></template>);@@ -130,4 +117,25 @@ module("Integration | Component | simple-list", function (hooks) {.dom(".values .value[data-index='2'] .value-input").hasValue("eviltrout", "adds the correct value");});++ test("updates when values change", async function (assert) {+ const state = new (class {+ @tracked values = "vinkas|osama";+ })();++ await render(+ <template>+ <SimpleList @values={{state.values}} @inputDelimiter="|" />+ </template>+ );++ assert+ .dom(".values .value[data-index='0'] .value-input")+ .hasValue("vinkas");++ state.values = "kris|jeff";+ await settled();++ assert.dom(".values .value[data-index='0'] .value-input").hasValue("kris");+ });});