Skip to content

Commit de1077f

Browse files
Theme: apply theme styles in textComp
1 parent 47e4b7a commit de1077f

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,8 +18,12 @@ 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";
23+
import { ThemeContext } from "../utils/themeContext";
24+
import { CompTypeContext } from "../utils/compTypeContext";
25+
import { changeChildAction } from "lowcoder-core";
26+
import { setInitialCompStyles } from "../utils/themeUtil";
2327

2428
const getStyle = (style: TextStyleType) => {
2529
return css`
@@ -112,6 +116,7 @@ const typeOptions = [
112116
value: "text",
113117
},
114118
] as const;
119+
115120
const VerticalAlignmentOptions = [
116121
{ label: <AlignTop />, value: "flex-start" },
117122
{ label: <AlignVerticalCenter />, value: "center" },
@@ -128,13 +133,29 @@ let TextTmpComp = (function () {
128133
type: dropdownControl(typeOptions, "markdown"),
129134
horizontalAlignment: alignWithJustifyControl(),
130135
verticalAlignment: dropdownControl(VerticalAlignmentOptions, "center"),
131-
style: styleControl(TextStyle),
136+
style: styleControl(TextStyle, 'style'),
132137
animationStyle: styleControl(AnimationStyle),
133138
margin: MarginControl,
134139
padding: PaddingControl,
135140
};
136-
return new UICompBuilder(childrenMap, (props) => {
141+
return new UICompBuilder(childrenMap, (props, dispatch) => {
137142
const value = props.text.value;
143+
const theme = useContext(ThemeContext);
144+
const compType = useContext(CompTypeContext);
145+
const compTheme = theme?.theme?.components?.[compType];
146+
const styleProps: Record<string, any> = {};
147+
['style'].forEach((key: string) => {
148+
styleProps[key] = (props as any)[key];
149+
});
150+
151+
useEffect(() => {
152+
setInitialCompStyles({
153+
dispatch,
154+
compTheme,
155+
styleProps,
156+
});
157+
}, []);
158+
138159
return (
139160
<TextContainer
140161
$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)