Refactor on default pt option

This commit is contained in:
mertsincan 2023-05-10 12:49:54 +01:00
parent ff4d5786d6
commit 133f9e2e5a
3 changed files with 105 additions and 88 deletions

View file

@ -1,5 +1,6 @@
<script>
import { ObjectUtils } from 'primevue/utils';
import { mergeProps } from 'vue';
export default {
name: 'BaseComponent',
@ -10,17 +11,22 @@ export default {
}
},
methods: {
getPTItem(obj = {}, key = '') {
getOption(obj = {}, key = '') {
const fKey = ObjectUtils.convertToFlatCase(key);
return obj[Object.keys(obj).find((k) => ObjectUtils.convertToFlatCase(k) === fKey) || ''];
},
getPTValue(obj = {}, key = '', params = {}) {
const self = ObjectUtils.getItemValue(this.getPTItem(obj, key), params);
const globalComponentPT = this.getPTItem(this.$primevue.config.pt, this.$.type.name);
const global = ObjectUtils.getItemValue(this.getPTItem(globalComponentPT, key), params);
const self = ObjectUtils.getItemValue(this.getOption(obj, key), params);
const globalPT = ObjectUtils.getItemValue(this.getOption(this.defaultPT, key), params);
const merged = mergeProps(self, globalPT);
return { ...global, ...self };
return merged;
/*
* @todo: The 'class' option in self can always be more powerful to style the component easily.
*
* return self && self['class'] ? { ...merged, ...{ class: self['class'] } } : merged;
*/
},
ptm(key = '', params = {}) {
return this.getPTValue(this.pt, key, { props: this.$props, state: this.$data, ...params });
@ -28,6 +34,14 @@ export default {
ptmo(obj = {}, key = '', params = {}) {
return this.getPTValue(obj, key, params);
}
},
computed: {
defaultPT() {
return ObjectUtils.getItemValue(this.getOption(this.$primevue.config.pt, this.$.type.name), this.defaultsParams);
},
defaultsParams() {
return { instance: this.$ };
}
}
};
</script>