Skip to content

Commit

Permalink
Refactor #5144 - Add mergeProps method to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Jan 17, 2024
1 parent f27f8fe commit 65f2e37
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
34 changes: 34 additions & 0 deletions components/lib/utils/MergeProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,37 @@ export function _mergeProps(props, options = {}) {

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;
}
}

return merged;
}, {});
}

return undefined;
}
5 changes: 3 additions & 2 deletions components/lib/utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import DomHandler from './DomHandler';
import EventBus from './EventBus';
import IconUtils from './IconUtils';
import { mask } from './Mask';
import { _mergeProps } from './MergeProps';
import { _mergeProps, mergeProps } from './MergeProps';
import ObjectUtils from './ObjectUtils';
import UniqueComponentId from './UniqueComponentId';
import { ZIndexUtils } from './ZIndexUtils';

export { DomHandler, EventBus, IconUtils, ObjectUtils, UniqueComponentId, ZIndexUtils, _mergeProps, classNames, mask };
export { DomHandler, EventBus, IconUtils, ObjectUtils, UniqueComponentId, ZIndexUtils, _mergeProps, classNames, mask, mergeProps };

1 change: 1 addition & 0 deletions components/lib/utils/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as React from 'react';

export declare function classNames(...args: any[]): string | undefined;

// @todo - replace it with mergeProps
export declare function _mergeProps(args: object[], options?: any): object | undefined;

export declare class DomHandler {
Expand Down

0 comments on commit 65f2e37

Please sign in to comment.