Merge pull request #400 from webzard-io/feat/merge-style

use concat method when merging customStyles
This commit is contained in:
yz-yu 2022-05-13 23:29:46 +08:00 committed by GitHub
commit b36dfddeb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -68,9 +68,11 @@ export const ImplWrapperMain = React.forwardRef<HTMLDivElement, ImplWrapperProps
return prevProps;
}
return mergeWith(prevProps, result.props, (obj, src) => {
if (isArray(obj)) {
return obj.concat(src);
return mergeWith({}, prevProps, result.props, (target, src, key) => {
if (isArray(target)) {
return target.concat(src);
} else if (key === 'customStyle') {
return mergeCustomStyle(target, src);
}
});
},
@ -175,3 +177,14 @@ const useDidUpdate = (fn: Function) => {
}
});
};
function mergeCustomStyle(s1?: Record<string, string>, s2?: Record<string, string>) {
if (s1 && s2) {
return mergeWith({}, s1, s2, (target: string, src: string) => {
if (target && src) {
return `${target};${src}`;
}
});
}
return s1 || s2;
}

View File

@ -25,8 +25,12 @@ export default implementRuntimeTrait({
})(() => {
return ({ styles }) => {
const customStyle: Record<string, string> = {};
styles.forEach(style => {
customStyle[style.styleSlot] = style.style;
styles.forEach(({ style, styleSlot }) => {
if (!customStyle[styleSlot]) {
customStyle[styleSlot] = '';
}
// add a ';' between css texts, in case user forgets to add ';' in the end
customStyle[styleSlot] = `${customStyle[styleSlot]};${style}`;
});
return {
props: {