-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
12 changed files
with
95 additions
and
102 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
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
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
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
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
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
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
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
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
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,77 +1,51 @@ | ||
export function _mergeProps(props, options = {}) { | ||
if (props) { | ||
const { classNameMergeFunction } = options; | ||
const isFn = (o) => !!(o && o.constructor && o.call && o.apply); | ||
|
||
return props.reduce((merged, ps) => { | ||
for (const key in ps) { | ||
const value = ps[key]; | ||
|
||
if (key === 'style') { | ||
merged['style'] = { ...merged['style'], ...ps['style'] }; | ||
} else if (key === 'className') { | ||
let newClassname = ''; | ||
|
||
if (classNameMergeFunction && classNameMergeFunction instanceof Function) { | ||
newClassname = classNameMergeFunction(merged['className'], ps['className']); | ||
} else { | ||
newClassname = [merged['className'], ps['className']].join(' ').trim(); | ||
} | ||
|
||
const isEmpty = newClassname === null || newClassname === undefined || newClassname === ''; | ||
|
||
merged['className'] = isEmpty ? undefined : newClassname; | ||
} else if (isFn(value)) { | ||
const fn = merged[key]; | ||
|
||
merged[key] = fn | ||
? (...args) => { | ||
fn(...args); | ||
value(...args); | ||
} | ||
: value; | ||
/** | ||
* Merges properties together taking an Array of props and merging into one single set of | ||
* properties. The options can contain a "classNameMergeFunction" which can be something | ||
* like Tailwind Merge for properly merging Tailwind classes. | ||
* | ||
* @param {object[]} props the array of object properties to merge | ||
* @param {*} options either empty or could contain a custom merge function like TailwindMerge | ||
* @returns the single properties value after merging | ||
*/ | ||
export function mergeProps(props, options = {}) { | ||
if (!props) return undefined; | ||
|
||
const isFunction = (obj) => typeof obj === 'function'; | ||
const { classNameMergeFunction } = options; | ||
const hasMergeFunction = isFunction(classNameMergeFunction); | ||
|
||
return props.reduce((merged, ps) => { | ||
if (!ps) return merged; | ||
|
||
for (const key in ps) { | ||
const value = ps[key]; | ||
|
||
if (key === 'style') { | ||
merged['style'] = { ...merged['style'], ...ps['style'] }; | ||
} else if (key === 'className') { | ||
let newClassName = ''; | ||
|
||
if (hasMergeFunction) { | ||
newClassName = classNameMergeFunction(merged['className'], ps['className']); | ||
} else { | ||
merged[key] = value; | ||
newClassName = [merged['className'], ps['className']].join(' ').trim(); | ||
} | ||
} | ||
|
||
return merged; | ||
}, {}); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
// @todo - Update this function and remove _mergeProps | ||
export function mergeProps(...props) { | ||
if (props) { | ||
const isFn = (o) => !!(o && o.constructor && o.call && o.apply); | ||
|
||
return props.reduce((merged, ps) => { | ||
for (const key in ps) { | ||
const value = ps[key]; | ||
|
||
if (key === 'style') { | ||
merged['style'] = { ...merged['style'], ...ps['style'] }; | ||
} else if (key === 'className') { | ||
merged['className'] = [merged['className'], ps['className']].join(' ').trim(); | ||
} else if (isFn(value)) { | ||
const fn = merged[key]; | ||
|
||
merged[key] = fn | ||
? (...args) => { | ||
fn(...args); | ||
value(...args); | ||
} | ||
: value; | ||
} else { | ||
merged[key] = value; | ||
} | ||
merged['className'] = newClassName || undefined; | ||
} else if (isFunction(value)) { | ||
const existingFn = merged[key]; | ||
|
||
merged[key] = existingFn | ||
? (...args) => { | ||
existingFn(...args); | ||
value(...args); | ||
} | ||
: value; | ||
} else { | ||
merged[key] = value; | ||
} | ||
} | ||
|
||
return merged; | ||
}, {}); | ||
} | ||
|
||
return undefined; | ||
return merged; | ||
}, {}); | ||
} |
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
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