mirror of
https://github.com/discourse/discourse.git
synced 2024-12-18 21:05:58 +08:00
0431942f3d
This new iteration of select-kit focuses on following best principales and disallowing mutations inside select-kit components. A best effort has been made to avoid breaking changes, however if you content was a flat array, eg: ["foo", "bar"] You will need to set valueProperty=null and nameProperty=null on the component. Also almost every component should have an `onChange` handler now to decide what to do with the updated data. **select-kit will not mutate your data by itself anymore**
92 lines
2.1 KiB
JavaScript
92 lines
2.1 KiB
JavaScript
import Mixin from "@ember/object/mixin";
|
|
const { get } = Ember;
|
|
|
|
export default Mixin.create({
|
|
defaultItem(value, name) {
|
|
if (this.selectKit.valueProperty) {
|
|
const item = {};
|
|
item[this.selectKit.valueProperty] = value;
|
|
item[this.selectKit.nameProperty] = name;
|
|
return item;
|
|
} else {
|
|
return name || value;
|
|
}
|
|
},
|
|
|
|
itemForValue(value) {
|
|
if (this.selectKit.valueProperty) {
|
|
return this.mainCollection.findBy(this.selectKit.valueProperty, value);
|
|
} else {
|
|
return value;
|
|
}
|
|
},
|
|
|
|
getProperty(item, property, options = { definedOnly: true }) {
|
|
const { definedOnly } = options;
|
|
|
|
if (item && typeof property === "string") {
|
|
const attempt = get(item, property);
|
|
if (attempt) {
|
|
return attempt;
|
|
}
|
|
}
|
|
|
|
property = get(this.selectKit, property);
|
|
|
|
if (!item) {
|
|
return null;
|
|
}
|
|
|
|
if (!property && definedOnly) {
|
|
return null;
|
|
} else if (!property) {
|
|
return item;
|
|
} else if (typeof property === "string") {
|
|
return get(item, property);
|
|
} else {
|
|
return property(item);
|
|
}
|
|
},
|
|
|
|
getValue(item) {
|
|
return this.getProperty(item, "valueProperty", { definedOnly: false });
|
|
},
|
|
|
|
getName(item) {
|
|
return this.getProperty(item, "nameProperty", { definedOnly: false });
|
|
},
|
|
|
|
findValue(content, item) {
|
|
const property = Ember.get(this.selectKit, "valueProperty");
|
|
|
|
if (!property) {
|
|
if (content.indexOf(item) > -1) {
|
|
return item;
|
|
}
|
|
} else if (typeof property === "string") {
|
|
return content.findBy(property, this.getValue(item));
|
|
} else {
|
|
const value = this.getValue(item);
|
|
return content.find(contentItem => {
|
|
return this.getValue(contentItem) === value;
|
|
});
|
|
}
|
|
},
|
|
|
|
_isNumeric(input) {
|
|
return !isNaN(parseFloat(input)) && isFinite(input);
|
|
},
|
|
|
|
_normalize(input) {
|
|
if (input) {
|
|
input = input.toLowerCase();
|
|
|
|
if (typeof input.normalize === "function") {
|
|
input = input.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
}
|
|
}
|
|
|
|
return input;
|
|
}
|
|
});
|