Debian Codegraph @

node/@patternfly/react-table/src/components/Table/base/merge-props.ts

raw
1 /**
2 * merge-props.js
3 *
4 * Forked from reactabular-table version 8.14.0
5 * https://github.com/reactabular/reactabular/tree/v8.14.0/packages/reactabular-table/src
6 */
7 import { cloneElement } from 'react';
8 import mergeWith from 'lodash/mergeWith';
9 import { css } from '@patternfly/react-styles';
10
11 /**
12 * @param {any} props - Props
13 */
14 export function mergeProps(...props: any) {
15 const firstProps = props[0];
16 const restProps = props.slice(1);
17
18 if (!restProps.length) {
19 return mergeWith({}, firstProps);
20 }
21
22 // Avoid mutating the first prop collection
23 return mergeWith(mergeWith({}, firstProps), ...restProps, (a: any, b: any, key: any) => {
24 if (key === 'children') {
25 if (a && b) {
26 // compose the two
27 return cloneElement(a, {
28 children: b
29 });
30 }
31
32 // Children have to be merged in reverse order for Reactabular
33 // logic to work.
34 return { ...b, ...a };
35 }
36
37 if (key === 'className') {
38 // Process class names through classNames to merge properly
39 // as a string.
40 return css(a, b);
41 }
42
43 return undefined;
44 });
45 }