-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor #4257 - Add usePassThrough method to customize default PT ob…
…jects
- Loading branch information
1 parent
51955d7
commit 2bd85b0
Showing
2 changed files
with
53 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters