Skip to content

Commit 8c17c04

Browse files
Theme: apply theme styles in textComp
1 parent e819e35 commit 8c17c04

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

client/packages/lowcoder/src/comps/comps/textComp.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import styled, { css } from "styled-components";
66
import { AlignCenter } from "lowcoder-design";
77
import { AlignLeft } from "lowcoder-design";
88
import { AlignRight } from "lowcoder-design";
9-
import { UICompBuilder } from "../generators";
9+
import { UICompBuilder, withDefault } from "../generators";
1010
import { NameConfig, NameConfigHidden, withExposingConfigs } from "../generators/withExposing";
1111
import { markdownCompCss, TacoMarkDown } from "lowcoder-design";
1212
import { styleControl } from "comps/controls/styleControl";
@@ -18,11 +18,15 @@ import { alignWithJustifyControl } from "comps/controls/alignControl";
1818
import { MarginControl } from "../controls/marginControl";
1919
import { PaddingControl } from "../controls/paddingControl";
2020

21-
import React, { useContext } from "react";
21+
import React, { useContext, useEffect } from "react";
2222
import { EditorContext } from "comps/editorState";
2323
import { clickEvent, eventHandlerControl } from "../controls/eventHandlerControl";
2424

2525
const EventOptions = [clickEvent] as const;
26+
import { ThemeContext } from "../utils/themeContext";
27+
import { CompTypeContext } from "../utils/compTypeContext";
28+
import { changeChildAction } from "lowcoder-core";
29+
import { setInitialCompStyles } from "../utils/themeUtil";
2630

2731
const getStyle = (style: TextStyleType) => {
2832
return css`
@@ -115,6 +119,7 @@ const typeOptions = [
115119
value: "text",
116120
},
117121
] as const;
122+
118123
const VerticalAlignmentOptions = [
119124
{ label: <AlignTop />, value: "flex-start" },
120125
{ label: <AlignVerticalCenter />, value: "center" },
@@ -132,13 +137,29 @@ let TextTmpComp = (function () {
132137
type: dropdownControl(typeOptions, "markdown"),
133138
horizontalAlignment: alignWithJustifyControl(),
134139
verticalAlignment: dropdownControl(VerticalAlignmentOptions, "center"),
135-
style: styleControl(TextStyle),
140+
style: styleControl(TextStyle, 'style'),
136141
animationStyle: styleControl(AnimationStyle),
137142
margin: MarginControl,
138143
padding: PaddingControl,
139144
};
140-
return new UICompBuilder(childrenMap, (props) => {
145+
return new UICompBuilder(childrenMap, (props, dispatch) => {
141146
const value = props.text.value;
147+
const theme = useContext(ThemeContext);
148+
const compType = useContext(CompTypeContext);
149+
const compTheme = theme?.theme?.components?.[compType];
150+
const styleProps: Record<string, any> = {};
151+
['style'].forEach((key: string) => {
152+
styleProps[key] = (props as any)[key];
153+
});
154+
155+
useEffect(() => {
156+
setInitialCompStyles({
157+
dispatch,
158+
compTheme,
159+
styleProps,
160+
});
161+
}, []);
162+
142163
return (
143164
<TextContainer
144165
$animationStyle={props.animationStyle}

client/packages/lowcoder/src/comps/utils/themeUtil.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { ThemeType } from "api/commonSettingApi";
22
import { getLocalThemeId } from "util/localStorageUtil";
33
import { getGlobalSettings } from "./globalSettings";
4+
import { CompAction, multiChangeAction, changeValueAction } from "lowcoder-core";
5+
import { JSONObject, JSONValue } from "@lowcoder-ee/util/jsonTypes";
46

57
export const DEFAULT_THEMEID = "default";
68

@@ -20,3 +22,26 @@ export function getCurrentTheme(themeList: ThemeType[], appThemeId: string) {
2022
: appThemeId
2123
);
2224
}
25+
26+
export function setInitialCompStyles({
27+
dispatch,
28+
compTheme,
29+
styleProps,
30+
}: {
31+
dispatch: (action: CompAction) => void,
32+
compTheme?: JSONObject,
33+
styleProps: Record<string, any>,
34+
}) {
35+
const styleKeys = Object.keys(styleProps);
36+
const actions: Record<string, any> = {};
37+
styleKeys.forEach(styleKey => {
38+
actions[styleKey] = changeValueAction({
39+
...(compTheme?.[styleKey] as object || {}),
40+
...styleProps[styleKey],
41+
}, false);
42+
})
43+
44+
dispatch(
45+
multiChangeAction(actions),
46+
);
47+
}

0 commit comments

Comments
 (0)