Skip to content

Set an individual class for components #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 72 additions & 9 deletions client/packages/lowcoder/src/comps/generators/uiCompBuilder.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BoolCodeControl } from "comps/controls/codeControl";
import { BoolCodeControl, StringControl } from "comps/controls/codeControl";
import React, { ReactNode, useContext, useRef } from "react";
import { ExternalEditorContext } from "util/context/ExternalEditorContext";
import { Comp, CompParams, MultiBaseComp } from "lowcoder-core";
Expand All @@ -22,10 +22,14 @@ import {
MethodConfigsType,
withMethodExposing,
} from "./withMethodExposing";
import { Section } from "lowcoder-design";
import { trans } from "i18n";

export type NewChildren<ChildrenCompMap extends Record<string, Comp<unknown>>> =
ChildrenCompMap & {
hidden: InstanceType<typeof BoolCodeControl>;
className: InstanceType<typeof StringControl>;
dataTestId: InstanceType<typeof StringControl>;
};

export function HidableView(props: {
Expand All @@ -50,12 +54,51 @@ export function HidableView(props: {
}
}

export function ExtendedComponentView(props: {
children: JSX.Element | React.ReactNode;
className: string;
dataTestId: string;
}) {
if (!props.className && !props.dataTestId) {
return <>{props.children}</>;
}

return (
<div className={props.className} data-testid={props.dataTestId}>
{props.children}
</div>
);
}

export function ExtendedPropertyView<
ChildrenCompMap extends Record<string, Comp<unknown>>,
>(props: {
children: JSX.Element | React.ReactNode,
childrenMap: NewChildren<ChildrenCompMap>
}
) {
return (
<>
{props.children}
<Section name={trans("prop.component")}>
{props.childrenMap.className?.propertyView({ label: trans("prop.className") })}
{props.childrenMap.dataTestId?.propertyView({ label: trans("prop.dataTestId") })}
</Section>
</>
);
}

export function uiChildren<
ChildrenCompMap extends Record<string, Comp<unknown>>,
>(
childrenMap: ToConstructor<ChildrenCompMap>
): ToConstructor<NewChildren<ChildrenCompMap>> {
return { ...childrenMap, hidden: BoolCodeControl } as any;
return {
...childrenMap,
hidden: BoolCodeControl,
className: StringControl,
dataTestId: StringControl
} as any;
}

type ViewReturn = ReactNode;
Expand Down Expand Up @@ -89,10 +132,22 @@ export class UICompBuilder<
setPropertyViewFn(
propertyViewFn: PropertyViewFnTypeForComp<NewChildren<ChildrenCompMap>>
) {
this.propertyViewFn = propertyViewFn;
this.propertyViewFn = this.decoratePropertyViewFn(propertyViewFn);
return this;
}

decoratePropertyViewFn(
propertyViewFn: PropertyViewFnTypeForComp<NewChildren<ChildrenCompMap>>
): PropertyViewFnTypeForComp<NewChildren<ChildrenCompMap>> {
return (childrenMap, dispatch) => {
return (
<ExtendedPropertyView childrenMap={childrenMap}>
{propertyViewFn(childrenMap, dispatch)}
</ExtendedPropertyView>
);
};
}

setExposeStateConfigs(
configs: ExposingConfig<ChildrenToComp<ChildrenCompMap>>[]
) {
Expand All @@ -110,8 +165,11 @@ export class UICompBuilder<
}

build() {
if (this.childrenMap.hasOwnProperty("hidden")) {
throw new Error("already has hidden");
const reservedProps = ["hidden", "className", "dataTestId"];
for (const reservedProp of reservedProps) {
if (this.childrenMap.hasOwnProperty(reservedProp)) {
throw new Error(`Property »${reservedProp}« is reserved and must not be implemented in components!`);
}
}
const newChildrenMap = uiChildren(this.childrenMap);
const builder = this;
Expand All @@ -122,7 +180,7 @@ export class UICompBuilder<
ToNodeType<NewChildren<ChildrenCompMap>>
> {
ref: React.RefObject<HTMLDivElement> = React.createRef();

override parseChildrenFromValue(
params: CompParams<ToDataType<NewChildren<ChildrenCompMap>>>
): NewChildren<ChildrenCompMap> {
Expand Down Expand Up @@ -185,8 +243,13 @@ function UIView(props: { comp: any; viewFn: any }) {
//END ADD BY FRED

return (
<HidableView hidden={childrenProps.hidden as boolean}>
{props.viewFn(childrenProps, comp.dispatch)}
</HidableView>
<ExtendedComponentView
className={childrenProps.className as string}
dataTestId={childrenProps.dataTestId as string}
>
<HidableView hidden={childrenProps.hidden as boolean}>
{props.viewFn(childrenProps, comp.dispatch)}
</HidableView>
</ExtendedComponentView>
);
}
5 changes: 4 additions & 1 deletion client/packages/lowcoder/src/i18n/locales/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ export const de: typeof en = {
"showBody": "Hauptbereich zeigen",
"showFooter": "Fußbereich anzeigen",
"maskClosable": "Zum Schließen auf \"Schließen\" klicken",
"showMask": "Maske darstellen"
"showMask": "Maske darstellen",
"component": "Komponente",
"className": "Klasse",
"dataTestId": "Test ID",
},
"autoHeightProp": {
...en.autoHeightProp,
Expand Down
3 changes: 3 additions & 0 deletions client/packages/lowcoder/src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ export const en = {
"baseURL": "Lowcoder API Base URL",
"horizontal": "Horizontal",
"minHorizontalWidth": "Minimum Horizontal Width",
"component": "Component",
"className": "Class",
"dataTestId": "Test ID",
},
"autoHeightProp": {
"auto": "Auto",
Expand Down
Loading