UX: Improve category selector in theme objects editor (#26311)

Why this change?

Prior to this change, the category selector was not clearable and did
not allow a none value. This is incorrect as the category selector
should be clearable and should allow a none value when the property is
not required.
This commit is contained in:
Alan Guo Xiang Tan 2024-03-22 11:36:53 +08:00 committed by GitHub
parent 404583298f
commit 3685eafb7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 16 deletions

View File

@ -1,12 +1,12 @@
import Component from "@glimmer/component"; import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking"; import { tracked } from "@glimmer/tracking";
import { hash } from "@ember/helper";
import { action } from "@ember/object"; import { action } from "@ember/object";
import FieldInputDescription from "admin/components/schema-theme-setting/field-input-description"; import FieldInputDescription from "admin/components/schema-theme-setting/field-input-description";
import CategoryChooser from "select-kit/components/category-chooser"; import CategoryChooser from "select-kit/components/category-chooser";
export default class SchemaThemeSettingTypeCategory extends Component { export default class SchemaThemeSettingTypeCategory extends Component {
@tracked value = this.args.value; @tracked value = this.args.value;
required = this.args.spec.required;
@action @action
onInput(newVal) { onInput(newVal) {
@ -14,12 +14,21 @@ export default class SchemaThemeSettingTypeCategory extends Component {
this.args.onChange(newVal); this.args.onChange(newVal);
} }
get categoryChooserOptions() {
return {
allowUncategorized: false,
none: !this.required,
clearable: !this.required,
};
}
<template> <template>
<CategoryChooser <CategoryChooser
@value={{this.value}} @value={{this.value}}
@onChange={{this.onInput}} @onChange={{this.onInput}}
@options={{hash allowUncategorized=false}} @options={{this.categoryChooserOptions}}
/> />
<FieldInputDescription @description={{@description}} /> <FieldInputDescription @description={{@description}} />
</template> </template>
} }

View File

@ -668,15 +668,54 @@ module(
}); });
test("input fields of type category", async function (assert) { test("input fields of type category", async function (assert) {
const setting = schemaAndData(3); const setting = ThemeSettings.create({
setting: "objects_setting",
objects_schema: {
name: "something",
identifier: "id",
properties: {
required_category: {
type: "category",
required: true,
},
not_required_category: {
type: "category",
},
},
},
value: [
{
required_category: 6,
},
],
});
await render(<template> await render(<template>
<AdminSchemaThemeSettingEditor @themeId="1" @setting={{setting}} /> <AdminSchemaThemeSettingEditor @themeId="1" @setting={{setting}} />
</template>); </template>);
const inputFields = new InputFieldsFromDOM(); const inputFields = new InputFieldsFromDOM();
const categorySelector = selectKit(
`${inputFields.fields.category_field.selector} .select-kit` assert
.dom(inputFields.fields.required_category.labelElement)
.hasText("required_category*");
let categorySelector = selectKit(
`${inputFields.fields.required_category.selector} .select-kit`
);
assert.strictEqual(categorySelector.header().value(), "6");
assert
.dom(categorySelector.clearButton())
.doesNotExist("is not clearable");
assert
.dom(inputFields.fields.not_required_category.labelElement)
.hasText("not_required_category");
categorySelector = selectKit(
`${inputFields.fields.not_required_category.selector} .select-kit`
); );
assert.strictEqual(categorySelector.header().value(), null); assert.strictEqual(categorySelector.header().value(), null);
@ -684,17 +723,7 @@ module(
await categorySelector.expand(); await categorySelector.expand();
await categorySelector.selectRowByIndex(1); await categorySelector.selectRowByIndex(1);
const selectedCategoryId = categorySelector.header().value(); assert.dom(categorySelector.clearButton()).exists("is clearable");
assert.ok(selectedCategoryId);
const tree = new TreeFromDOM();
await click(tree.nodes[1].element);
assert.strictEqual(categorySelector.header().value(), null);
tree.refresh();
await click(tree.nodes[0].element);
assert.strictEqual(categorySelector.header().value(), selectedCategoryId);
}); });
test("input fields of type tag", async function (assert) { test("input fields of type tag", async function (assert) {