mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: allows to define label/title properties for display instead of name
Usage:
```
const content = [{foo: "FOO", bar: "BAR", value: 1, name: "foo-bar"}];
{{combo-box
content=content
value=value
labelProperty="foo"
titleProperty="bar"
}}
```
This commit is contained in:
@@ -517,6 +517,7 @@
|
|||||||
{{admin-group-selector
|
{{admin-group-selector
|
||||||
content=availableGroups
|
content=availableGroups
|
||||||
value=customGroupIdsBuffer
|
value=customGroupIdsBuffer
|
||||||
|
labelProperty="name"
|
||||||
onChange=(action (mut customGroupIdsBuffer))
|
onChange=(action (mut customGroupIdsBuffer))
|
||||||
}}
|
}}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ export default Component.extend(
|
|||||||
nameProperty: "name",
|
nameProperty: "name",
|
||||||
singleSelect: false,
|
singleSelect: false,
|
||||||
multiSelect: false,
|
multiSelect: false,
|
||||||
|
labelProperty: null,
|
||||||
|
titleProperty: null,
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
this._super(...arguments);
|
this._super(...arguments);
|
||||||
@@ -76,6 +78,8 @@ export default Component.extend(
|
|||||||
uniqueID: guidFor(this),
|
uniqueID: guidFor(this),
|
||||||
valueProperty: this.valueProperty,
|
valueProperty: this.valueProperty,
|
||||||
nameProperty: this.nameProperty,
|
nameProperty: this.nameProperty,
|
||||||
|
labelProperty: this.labelProperty,
|
||||||
|
titleProperty: this.titleProperty,
|
||||||
options: EmberObject.create(),
|
options: EmberObject.create(),
|
||||||
|
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
|
|||||||
@@ -38,13 +38,18 @@ export default Component.extend(UtilsMixin, {
|
|||||||
return this.getProperty(this.item, "ariaLabel") || this.title;
|
return this.getProperty(this.item, "ariaLabel") || this.title;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
title: computed("item.title", "rowName", function() {
|
title: computed("rowTitle", "item.title", "rowName", function() {
|
||||||
return this.getProperty(this.item, "title") || this.rowName;
|
return (
|
||||||
|
this.rowTitle || this.getProperty(this.item, "title") || this.rowName
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
label: computed("item.label", "title", "rowName", function() {
|
label: computed("rowLabel", "item.label", "title", "rowName", function() {
|
||||||
const label =
|
const label =
|
||||||
this.getProperty(this.item, "label") || this.title || this.rowName;
|
this.rowLabel ||
|
||||||
|
this.getProperty(this.item, "label") ||
|
||||||
|
this.title ||
|
||||||
|
this.rowName;
|
||||||
if (
|
if (
|
||||||
this.selectKit.options.allowAny &&
|
this.selectKit.options.allowAny &&
|
||||||
this.rowValue === this.selectKit.filter &&
|
this.rowValue === this.selectKit.filter &&
|
||||||
@@ -61,7 +66,9 @@ export default Component.extend(UtilsMixin, {
|
|||||||
|
|
||||||
this.setProperties({
|
this.setProperties({
|
||||||
rowName: this.getName(this.item),
|
rowName: this.getName(this.item),
|
||||||
rowValue: this.getValue(this.item)
|
rowValue: this.getValue(this.item),
|
||||||
|
rowLabel: this.getProperty(this.item, "labelProperty"),
|
||||||
|
rowTitle: this.getProperty(this.item, "titleProperty")
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ export default Component.extend(UtilsMixin, {
|
|||||||
|
|
||||||
// we can't listen on `item.nameProperty` given it's variable
|
// we can't listen on `item.nameProperty` given it's variable
|
||||||
this.setProperties({
|
this.setProperties({
|
||||||
|
headerLabel: this.getProperty(this.item, "labelProperty"),
|
||||||
|
headerTitle: this.getProperty(this.item, "titleProperty"),
|
||||||
name: this.getName(this.item),
|
name: this.getName(this.item),
|
||||||
value:
|
value:
|
||||||
this.item === this.selectKit.noneItem ? null : this.getValue(this.item)
|
this.item === this.selectKit.noneItem ? null : this.getValue(this.item)
|
||||||
@@ -38,12 +40,22 @@ export default Component.extend(UtilsMixin, {
|
|||||||
return String(this.title).replace("…", "");
|
return String(this.title).replace("…", "");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
title: computed("item", function() {
|
title: computed("headerTitle", "item", function() {
|
||||||
return this._safeProperty("title", this.item) || this.name || "";
|
return (
|
||||||
|
this.headerTitle ||
|
||||||
|
this._safeProperty("title", this.item) ||
|
||||||
|
this.name ||
|
||||||
|
""
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
label: computed("title", "name", function() {
|
label: computed("headerLabel", "title", "name", function() {
|
||||||
return this._safeProperty("label", this.item) || this.title || this.name;
|
return (
|
||||||
|
this.headerLabel ||
|
||||||
|
this._safeProperty("label", this.item) ||
|
||||||
|
this.title ||
|
||||||
|
this.name
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
icons: computed("item.{icon,icons}", function() {
|
icons: computed("item.{icon,icons}", function() {
|
||||||
|
|||||||
@@ -296,3 +296,45 @@ componentTest("focusAfterOnChange", {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
componentTest("labelProperty", {
|
||||||
|
template: '{{single-select labelProperty="foo" value=value content=content}}',
|
||||||
|
|
||||||
|
beforeEach() {
|
||||||
|
this.setProperties({
|
||||||
|
content: [{ id: 1, name: "john", foo: "JACKSON" }],
|
||||||
|
value: 1
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async test(assert) {
|
||||||
|
assert.equal(this.subject.header().label(), "JACKSON");
|
||||||
|
|
||||||
|
await this.subject.expand();
|
||||||
|
|
||||||
|
const row = this.subject.rowByValue(1);
|
||||||
|
|
||||||
|
assert.equal(row.label(), "JACKSON");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
componentTest("titleProperty", {
|
||||||
|
template: '{{single-select titleProperty="foo" value=value content=content}}',
|
||||||
|
|
||||||
|
beforeEach() {
|
||||||
|
this.setProperties({
|
||||||
|
content: [{ id: 1, name: "john", foo: "JACKSON" }],
|
||||||
|
value: 1
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async test(assert) {
|
||||||
|
assert.equal(this.subject.header().title(), "JACKSON");
|
||||||
|
|
||||||
|
await this.subject.expand();
|
||||||
|
|
||||||
|
const row = this.subject.rowByValue(1);
|
||||||
|
|
||||||
|
assert.equal(row.title(), "JACKSON");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -95,6 +95,12 @@ function rowHelper(row) {
|
|||||||
title() {
|
title() {
|
||||||
return row.attr("title");
|
return row.attr("title");
|
||||||
},
|
},
|
||||||
|
label() {
|
||||||
|
return row
|
||||||
|
.find(".name")
|
||||||
|
.text()
|
||||||
|
.trim();
|
||||||
|
},
|
||||||
value() {
|
value() {
|
||||||
const value = row.attr("data-value");
|
const value = row.attr("data-value");
|
||||||
return isEmpty(value) ? null : value;
|
return isEmpty(value) ? null : value;
|
||||||
@@ -124,7 +130,7 @@ function headerHelper(header) {
|
|||||||
return header.find(".d-icon");
|
return header.find(".d-icon");
|
||||||
},
|
},
|
||||||
title() {
|
title() {
|
||||||
return header.attr("title");
|
return header.find(".selected-name").attr("title");
|
||||||
},
|
},
|
||||||
el() {
|
el() {
|
||||||
return header;
|
return header;
|
||||||
|
|||||||
Reference in New Issue
Block a user