Skip to content

Commit

Permalink
Refactor #4257 - Add usePassThrough method to customize default PT ob…
Browse files Browse the repository at this point in the history
…jects
  • Loading branch information
mertsincan committed Aug 10, 2023
1 parent 51955d7 commit 2bd85b0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
59 changes: 43 additions & 16 deletions components/lib/passthrough/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
import { ObjectUtils } from 'primevue/utils';
import { mergeProps } from 'vue';

// @todo: Improve this method
export const usePassThrough = (theme, pt = {}, { merge = false }) => {
const newTheme = { ...theme };
const componentNames = Object.keys(pt);

return componentNames.reduce((t, n) => {
if (!merge) {
t[n] = pt[n];
} else {
t[n] = Object.entries(pt[n]).reduce((_t, [sk, sv]) => {
return mergeProps(t[n][sk], sv);
}, {});
}

return t;
}, newTheme);
export const usePassThrough = (pt1 = {}, pt2 = {}, { merge = true, useMergeProps = false, ignoredKeysOnMerge = [], customizer = undefined } = {}) => {
const newPT = { ...pt1 };

if (merge) {
const getPTClassValue = (value) => (ObjectUtils.isString(value) || ObjectUtils.isArray(value) ? { class: value } : value);

const setOptionValue = (to, from, nestedKey, ignoredKey) => {
const keys = nestedKey.split('.');
const key = keys.shift();
const getValue = (value) => (ObjectUtils.isFunction(customizer) ? customizer({ key, to, from, value }) : value);

if (!!ignoredKey && ignoredKey.endsWith(key)) {
!!key && (to[key] = getValue(from[key]));
} else {
const matched = [Object.keys(to[key] || {}), Object.keys(from[key] || {})].flat().some((k) => k.match(/^(class|style|on(.+))/));

if (matched) {
to[key] = getValue(useMergeProps ? mergeProps(getPTClassValue(to[key]), getPTClassValue(from[key])) : { ...getPTClassValue(to[key]), ...getPTClassValue(from[key]) });
} else if (ObjectUtils.isNotEmpty(from[key])) {
to[key] = { ...to[key] };
setOptionValue(to[key], from[key], keys.join('.'), ignoredKey);
} else if (!!key) {
to[key] = getValue(from[key]);
}
}
};

const nestedKeys = ObjectUtils.nestedKeys(pt2);

nestedKeys.forEach((nestedKey) => {
setOptionValue(
newPT,
pt2,
nestedKey,
ignoredKeysOnMerge.find((k) => (k.indexOf('.') > 0 ? nestedKey.startsWith(k) : nestedKey.split('.')[0] === k))
);
});
} else {
Object.keys(pt2).forEach((key) => (newPT[key] = pt2[key]));
}

return newPT;
};
10 changes: 10 additions & 0 deletions components/lib/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,15 @@ export default {
}

return index;
},

nestedKeys(obj = {}, parentKey = '') {
return Object.entries(obj).reduce((o, [key, value]) => {
const currentKey = parentKey ? `${parentKey}.${key}` : key;

this.isObject(value) ? (o = o.concat(this.nestedKeys(value, currentKey))) : o.push(currentKey);

return o;
}, []);
}
};

0 comments on commit 2bd85b0

Please sign in to comment.