Skip to content

Commit a0949f1

Browse files
authored
support custom widget in label of Button widget (#1114)
1 parent 1d4dcdb commit a0949f1

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

.changeset/calm-lamps-cough.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ensembleui/react-kitchen-sink": patch
3+
"@ensembleui/react-runtime": patch
4+
---
5+
6+
support custom widget in label of Button widget

apps/kitchen-sink/src/ensemble/screens/actions.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ View:
3434
names: heading-3
3535
text: Execute Conditional Action
3636
- Button:
37-
label: Tap to pick an image
37+
body:
38+
Text:
39+
text: Tap to pick an image
40+
styles:
41+
fontFamily: fantasy
42+
color: green
3843
onTap:
3944
pickFiles:
4045
allowedExtensions: ["jpg", "png", "pdf", "docs"]

packages/runtime/src/widgets/Button.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { EnsembleAction, Expression } from "@ensembleui/react-framework";
2-
import { useRegisterBindings } from "@ensembleui/react-framework";
2+
import { unwrapWidget, useRegisterBindings } from "@ensembleui/react-framework";
33
import { Button as AntButton, Form as AntForm } from "antd";
44
import {
55
type MouseEvent,
@@ -12,11 +12,13 @@ import { WidgetRegistry } from "../registry";
1212
import type { EnsembleWidgetProps, IconProps } from "../shared/types";
1313
import { useEnsembleAction } from "../runtime/hooks/useEnsembleAction";
1414
import { Icon } from "./Icon";
15+
import { EnsembleRuntime } from "../runtime";
16+
import { isString } from "lodash-es";
1517

1618
const widgetName = "Button";
1719

1820
export type ButtonProps = {
19-
label: Expression<string>;
21+
label: Expression<string> | { [key: string]: unknown };
2022
onTap?: EnsembleAction;
2123
submitForm?: boolean;
2224
startingIcon?: IconProps;
@@ -58,10 +60,18 @@ export const Button: React.FC<ButtonProps> = ({ id, onTap, ...rest }) => {
5860
}
5961
}, [values?.loading]);
6062

63+
const label = useMemo(() => {
64+
const rawLabel = values?.label;
65+
if (!rawLabel) return null;
66+
if (isString(rawLabel)) return rawLabel;
67+
return EnsembleRuntime.render([unwrapWidget(rawLabel)]);
68+
}, [values?.label]);
69+
6170
const ButtonComponent = useMemo(() => {
6271
return (
6372
<AntButton
6473
disabled={values?.disabled ?? false}
74+
className={values?.styles?.names}
6575
htmlType={values?.submitForm === true ? "submit" : "button"}
6676
loading={Boolean(loading)}
6777
onClick={onClickCallback}
@@ -82,7 +92,7 @@ export const Button: React.FC<ButtonProps> = ({ id, onTap, ...rest }) => {
8292
<Icon {...values.startingIcon} />
8393
) : null}
8494

85-
{!loading && <>{values?.label}</>}
95+
{!loading && <>{label}</>}
8696

8797
{!loading && values?.endingIcon ? (
8898
<Icon {...values.endingIcon} />

0 commit comments

Comments
 (0)