75 lines
3.1 KiB
JavaScript
75 lines
3.1 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = mergeSlotProps;
|
|
var _isEventHandler = _interopRequireDefault(require("@mui/utils/isEventHandler"));
|
|
var _clsx = _interopRequireDefault(require("clsx"));
|
|
function mergeSlotProps(externalSlotProps, defaultSlotProps) {
|
|
if (!externalSlotProps) {
|
|
return defaultSlotProps;
|
|
}
|
|
function extractHandlers(externalSlotPropsValue, defaultSlotPropsValue) {
|
|
const handlers = {};
|
|
Object.keys(defaultSlotPropsValue).forEach(key => {
|
|
if ((0, _isEventHandler.default)(key, defaultSlotPropsValue[key]) && typeof externalSlotPropsValue[key] === 'function') {
|
|
// only compose the handlers if both default and external slot props match the event handler
|
|
handlers[key] = (...args) => {
|
|
externalSlotPropsValue[key](...args);
|
|
defaultSlotPropsValue[key](...args);
|
|
};
|
|
}
|
|
});
|
|
return handlers;
|
|
}
|
|
if (typeof externalSlotProps === 'function' || typeof defaultSlotProps === 'function') {
|
|
return ownerState => {
|
|
const defaultSlotPropsValue = typeof defaultSlotProps === 'function' ? defaultSlotProps(ownerState) : defaultSlotProps;
|
|
const externalSlotPropsValue = typeof externalSlotProps === 'function' ? externalSlotProps({
|
|
...ownerState,
|
|
...defaultSlotPropsValue
|
|
}) : externalSlotProps;
|
|
const className = (0, _clsx.default)(ownerState?.className, defaultSlotPropsValue?.className, externalSlotPropsValue?.className);
|
|
const handlers = extractHandlers(externalSlotPropsValue, defaultSlotPropsValue);
|
|
return {
|
|
...defaultSlotPropsValue,
|
|
...externalSlotPropsValue,
|
|
...handlers,
|
|
...(!!className && {
|
|
className
|
|
}),
|
|
...(defaultSlotPropsValue?.style && externalSlotPropsValue?.style && {
|
|
style: {
|
|
...defaultSlotPropsValue.style,
|
|
...externalSlotPropsValue.style
|
|
}
|
|
}),
|
|
...(defaultSlotPropsValue?.sx && externalSlotPropsValue?.sx && {
|
|
sx: [...(Array.isArray(defaultSlotPropsValue.sx) ? defaultSlotPropsValue.sx : [defaultSlotPropsValue.sx]), ...(Array.isArray(externalSlotPropsValue.sx) ? externalSlotPropsValue.sx : [externalSlotPropsValue.sx])]
|
|
})
|
|
};
|
|
};
|
|
}
|
|
const typedDefaultSlotProps = defaultSlotProps;
|
|
const handlers = extractHandlers(externalSlotProps, typedDefaultSlotProps);
|
|
const className = (0, _clsx.default)(typedDefaultSlotProps?.className, externalSlotProps?.className);
|
|
return {
|
|
...defaultSlotProps,
|
|
...externalSlotProps,
|
|
...handlers,
|
|
...(!!className && {
|
|
className
|
|
}),
|
|
...(typedDefaultSlotProps?.style && externalSlotProps?.style && {
|
|
style: {
|
|
...typedDefaultSlotProps.style,
|
|
...externalSlotProps.style
|
|
}
|
|
}),
|
|
...(typedDefaultSlotProps?.sx && externalSlotProps?.sx && {
|
|
sx: [...(Array.isArray(typedDefaultSlotProps.sx) ? typedDefaultSlotProps.sx : [typedDefaultSlotProps.sx]), ...(Array.isArray(externalSlotProps.sx) ? externalSlotProps.sx : [externalSlotProps.sx])]
|
|
})
|
|
};
|
|
} |