chore: 组件销毁时清空自定义样式 (#8016)

* chore: 组件销毁时清空自定义样式

* 优化

* 还原page高度

---------

Co-authored-by: qinhaoyan <30946345+qinhaoyan@users.noreply.github.com>
This commit is contained in:
qkiroc 2023-09-06 18:52:58 +08:00 committed by GitHub
parent 60e2743cf6
commit 12f8d0dc7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 19 deletions

View File

@ -1,35 +1,41 @@
import {useEffect} from 'react'; import {useEffect, useRef} from 'react';
import type {RendererEnv} from '../env'; import type {RendererEnv} from '../env';
import type {CustomStyleClassName} from '../utils/style-helper'; import type {InsertCustomStyle} from '../utils/style-helper';
import {insertCustomStyle, insertEditCustomStyle} from '../utils/style-helper'; import {StyleDom} from '../utils/style-helper';
interface CustomStyleProps { interface CustomStyleProps {
config: { config: {
themeCss?: any;
classNames?: CustomStyleClassName[];
id?: string;
defaultData?: any;
wrapperCustomStyle?: any; wrapperCustomStyle?: any;
componentId?: string; componentId?: string;
}; } & InsertCustomStyle;
env: RendererEnv; env: RendererEnv;
} }
export default function (props: CustomStyleProps) { export default function (props: CustomStyleProps) {
const {themeCss, classNames, id, defaultData, wrapperCustomStyle} = const {themeCss, classNames, id, defaultData, wrapperCustomStyle} =
props.config; props.config;
if (!themeCss && !wrapperCustomStyle) {
return null;
}
const styleDom = useRef(new StyleDom(id || '')).current;
useEffect(() => { useEffect(() => {
insertCustomStyle( styleDom.insertCustomStyle({
themeCss, themeCss,
classNames, classNames,
id,
defaultData, defaultData,
props.env?.customStyleClassPrefix customStyleClassPrefix: props.env?.customStyleClassPrefix
); });
return () => {
styleDom.removeCustomStyle();
};
}, [props.config.themeCss]); }, [props.config.themeCss]);
useEffect(() => { useEffect(() => {
insertEditCustomStyle(wrapperCustomStyle, id); styleDom.insertEditCustomStyle(wrapperCustomStyle);
return () => {
styleDom.removeCustomStyle('wrapperCustomStyle');
};
}, [props.config.wrapperCustomStyle]); }, [props.config.wrapperCustomStyle]);
return null; return null;

View File

@ -1545,7 +1545,7 @@ export class FormItemWrap extends React.Component<FormItemProps> {
} }
], ],
wrapperCustomStyle, wrapperCustomStyle,
id: id + '-item' id: id && id + '-item'
}} }}
env={env} env={env}
/> />

View File

@ -43,7 +43,7 @@ interface extra {
suf?: string; suf?: string;
} }
export function findOrCreactStyle(id: string) { export function findOrCreateStyle(id: string) {
let varStyleTag = document.getElementById(id); let varStyleTag = document.getElementById(id);
if (!varStyleTag) { if (!varStyleTag) {
varStyleTag = document.createElement('style'); varStyleTag = document.createElement('style');
@ -54,7 +54,7 @@ export function findOrCreactStyle(id: string) {
} }
export function insertStyle(style: string, id: string) { export function insertStyle(style: string, id: string) {
const varStyleTag = findOrCreactStyle(id); const varStyleTag = findOrCreateStyle(id);
// bca-disable-line // bca-disable-line
varStyleTag.innerHTML = style; varStyleTag.innerHTML = style;
@ -65,7 +65,7 @@ export function insertStyle(style: string, id: string) {
} }
export function addStyle(style: string, id: string) { export function addStyle(style: string, id: string) {
const varStyleTag = findOrCreactStyle(id); const varStyleTag = findOrCreateStyle(id);
// bca-disable-line // bca-disable-line
varStyleTag.innerHTML += style; varStyleTag.innerHTML += style;
} }
@ -361,3 +361,61 @@ export function insertEditCustomStyle(customStyle: any, id?: string) {
'wrapperCustomStyle-' + (id?.replace('u:', '') || uuid()) 'wrapperCustomStyle-' + (id?.replace('u:', '') || uuid())
); );
} }
export interface InsertCustomStyle {
themeCss?: any;
classNames?: CustomStyleClassName[];
id?: string;
defaultData?: any;
customStyleClassPrefix?: string;
}
export class StyleDom {
id: string;
constructor(id: string) {
this.id = id;
}
/**
*
*
* @param {InsertCustomStyle} params -
* @param {string} params.themeCss -
* @param {string} params.classNames -
* @param {string} params.defaultData -
* @param {string} params.customStyleClassPrefix -
*/
insertCustomStyle({
themeCss,
classNames,
defaultData,
customStyleClassPrefix
}: InsertCustomStyle) {
insertCustomStyle(
themeCss,
classNames,
this.id,
defaultData,
customStyleClassPrefix
);
}
/**
*
*
* @param wrapperCustomStyle
*/
insertEditCustomStyle(wrapperCustomStyle: any) {
insertEditCustomStyle(wrapperCustomStyle, this.id);
}
/**
*
*/
removeCustomStyle(type?: string) {
const style = document.getElementById(
(type ? type + '-' : '') + this.id.replace('u:', '')
);
if (style) {
style.remove();
}
}
}

View File

@ -1080,6 +1080,9 @@ export default class TextControl extends React.PureComponent<
@autobind @autobind
formatInputThemeCss() { formatInputThemeCss() {
const {themeCss, css} = this.props; const {themeCss, css} = this.props;
if (!themeCss && !css) {
return;
}
const inputFontThemeCss: any = {inputControlClassName: {}}; const inputFontThemeCss: any = {inputControlClassName: {}};
const inputControlClassNameObject = const inputControlClassNameObject =
(themeCss || css)?.inputControlClassName || {}; (themeCss || css)?.inputControlClassName || {};
@ -1153,7 +1156,7 @@ export default class TextControl extends React.PureComponent<
} }
} }
], ],
id: id + '-inner' id: id && id + '-inner'
}} }}
env={env} env={env}
/> />
@ -1167,7 +1170,7 @@ export default class TextControl extends React.PureComponent<
value: addOnClassName value: addOnClassName
} }
], ],
id: id + '-addOn' id: id && id + '-addOn'
}} }}
env={env} env={env}
/> />