2020-02-03 21:22:14 +08:00
|
|
|
import { computed } from "@ember/object";
|
|
|
|
import { isEmpty } from "@ember/utils";
|
2024-08-22 16:39:03 +08:00
|
|
|
import { classNames } from "@ember-decorators/component";
|
|
|
|
import SelectKitComponent, {
|
|
|
|
pluginApiIdentifiers,
|
|
|
|
selectKitOptions,
|
|
|
|
} from "select-kit/components/select-kit";
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2024-08-22 16:39:03 +08:00
|
|
|
@classNames("single-select")
|
|
|
|
@selectKitOptions({
|
|
|
|
headerComponent: "select-kit/single-select-header",
|
|
|
|
})
|
|
|
|
@pluginApiIdentifiers(["single-select"])
|
|
|
|
export default class SingleSelect extends SelectKitComponent {
|
|
|
|
singleSelect = true;
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2024-08-22 16:39:03 +08:00
|
|
|
@computed("value", "content.[]", "selectKit.noneItem")
|
|
|
|
get selectedContent() {
|
|
|
|
if (!isEmpty(this.value)) {
|
|
|
|
let content;
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2024-08-22 16:39:03 +08:00
|
|
|
const value =
|
|
|
|
this.selectKit.options.castInteger && this._isNumeric(this.value)
|
|
|
|
? Number(this.value)
|
|
|
|
: this.value;
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2024-08-22 16:39:03 +08:00
|
|
|
if (this.selectKit.valueProperty) {
|
|
|
|
content = (this.content || []).findBy(
|
|
|
|
this.selectKit.valueProperty,
|
|
|
|
value
|
|
|
|
);
|
2020-02-14 17:00:40 +08:00
|
|
|
|
2024-08-22 16:39:03 +08:00
|
|
|
return this.selectKit.modifySelection(
|
|
|
|
content || this.defaultItem(value, value)
|
|
|
|
);
|
2020-02-03 21:22:14 +08:00
|
|
|
} else {
|
2024-08-22 16:39:03 +08:00
|
|
|
return this.selectKit.modifySelection(
|
|
|
|
(this.content || []).filter((c) => c === value)
|
|
|
|
);
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
2024-08-22 16:39:03 +08:00
|
|
|
} else {
|
|
|
|
return this.selectKit.noneItem;
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
2024-08-22 16:39:03 +08:00
|
|
|
}
|
|
|
|
}
|